From 0fab1e3c04ad38ba460b03db6924872122cc1a73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cchrisshank=E2=80=9D?= Date: Tue, 10 Dec 2024 14:36:33 -0800 Subject: [PATCH] style rope in CSS --- demo/sticky-html-rope.html | 7 ++++- src/folk-rope.ts | 59 ++++++++++++++------------------------ src/folk-xanadu.ts | 3 ++ 3 files changed, 30 insertions(+), 39 deletions(-) diff --git a/demo/sticky-html-rope.html b/demo/sticky-html-rope.html index bb06190..aa3993b 100644 --- a/demo/sticky-html-rope.html +++ b/demo/sticky-html-rope.html @@ -23,8 +23,12 @@ folk-rope { display: block; position: absolute; - inset: 0 0 0 0; + inset: 0; pointer-events: none; + + &[target='#box2'] { + --folk-rope-color: rebeccapurple; + } } button { @@ -40,6 +44,7 @@ + diff --git a/src/folk-rope.ts b/src/folk-rope.ts index 3f0eb3c..9fc8048 100644 --- a/src/folk-rope.ts +++ b/src/folk-rope.ts @@ -4,8 +4,9 @@ import { Vector } from './common/Vector.ts'; import type { Point } from './common/types.ts'; import { DOMRectTransform } from './common/DOMRectTransform.ts'; import { FolkBaseConnection } from './folk-base-connection.ts'; -import { PropertyValues } from '@lit/reactive-element'; +import { css, PropertyValues } from '@lit/reactive-element'; import { AnimationFrameController, AnimationFrameControllerHost } from './common/animation-frame-controller.ts'; +import { property } from '@lit/reactive-element/decorators.js'; const lerp = (first: number, second: number, percentage: number) => first + (second - first) * percentage; @@ -31,65 +32,47 @@ declare global { export class FolkRope extends FolkBaseConnection implements AnimationFrameControllerHost { static override tagName = 'folk-rope'; + static styles = css` + svg { + height: 100%; + pointer-events: none; + width: 100%; + } + + path { + fill: none; + pointer-events: auto; + stroke: var(--folk-rope-color, black); + stroke-width: var(--folk-rope-width, 3); + stroke-linecap: var(--folk-rope-linecap, round); + } + `; + #rAF = new AnimationFrameController(this); #svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg'); #path = document.createElementNS('http://www.w3.org/2000/svg', 'path'); #path2 = document.createElementNS('http://www.w3.org/2000/svg', 'path'); - #gravity = { x: 0, y: 3000 }; #points: RopePoint[] = []; get points() { return this.#points; } - get gravity() { - return this.#gravity; - } - - set gravity(gravity) { - this.#gravity = gravity; - } - - #stroke = ''; - get stroke() { - return this.#stroke; - } - set stroke(stroke) { - this.#stroke = stroke; - this.#path.setAttribute('stroke', this.#stroke); - this.#path2.setAttribute('stroke', this.#stroke); - } + @property({ type: Object }) gravity = { x: 0, y: 3000 }; override firstUpdated(changedProperties: PropertyValues): void { super.firstUpdated(changedProperties); - this.#path.setAttribute('stroke-width', '3'); - this.#path.setAttribute('stroke-linecap', 'round'); - - this.#path.setAttribute('fill', 'none'); - this.#path.style.pointerEvents = 'auto'; - - this.#path2.setAttribute('stroke-width', '3'); - this.#path2.setAttribute('stroke-linecap', 'round'); - this.#path2.setAttribute('fill', 'none'); - this.#path2.style.pointerEvents = 'auto'; - - this.#svg.style.height = '100%'; - this.#svg.style.width = '100%'; - this.#svg.style.pointerEvents = 'none'; - this.#svg.appendChild(this.#path); - this.#svg.appendChild(this.#path2); + this.#svg.append(this.#path, this.#path2); this.renderRoot.appendChild(this.#svg); - - this.stroke = this.getAttribute('stroke') || 'black'; } tick() { for (const point of this.#points) { - this.#integratePoint(point, this.#gravity); + this.#integratePoint(point, this.gravity); } // 3 constraint iterations is enough for fixed timestep diff --git a/src/folk-xanadu.ts b/src/folk-xanadu.ts index 4ba4d59..9ed4af6 100644 --- a/src/folk-xanadu.ts +++ b/src/folk-xanadu.ts @@ -12,9 +12,12 @@ export class FolkXanadu extends FolkBaseConnection { if (sourceRect === null || targetRect === null) { this.style.clipPath = ''; + this.style.display = 'none'; return; } + this.style.display = 'block'; + // If the right side of the target is to the left of the right side of the source then swap them if (sourceRect.x + sourceRect.width > targetRect.x + targetRect.width) { const temp = sourceRect;