make rope canvas responsive
This commit is contained in:
parent
61883bc805
commit
b25c795e7c
|
|
@ -1,5 +1,6 @@
|
||||||
// This is a rewrite of https://github.com/guerrillacontra/html5-es6-physics-rope
|
// This is a rewrite of https://github.com/guerrillacontra/html5-es6-physics-rope
|
||||||
|
|
||||||
|
import { ResizeObserverManager } from '../resize-observer.ts';
|
||||||
import { AbstractArrow } from './abstract-arrow.ts';
|
import { AbstractArrow } from './abstract-arrow.ts';
|
||||||
import { Vertex } from './utils.ts';
|
import { Vertex } from './utils.ts';
|
||||||
|
|
||||||
|
|
@ -31,6 +32,8 @@ interface RopePoint {
|
||||||
next: RopePoint | null;
|
next: RopePoint | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const resizeObserver = new ResizeObserverManager();
|
||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
interface HTMLElementTagNameMap {
|
interface HTMLElementTagNameMap {
|
||||||
'fc-rope': FolkRope;
|
'fc-rope': FolkRope;
|
||||||
|
|
@ -44,6 +47,7 @@ export class FolkRope extends AbstractArrow {
|
||||||
#context = this.#canvas.getContext('2d')!;
|
#context = this.#canvas.getContext('2d')!;
|
||||||
#shadow = this.attachShadow({ mode: 'open' });
|
#shadow = this.attachShadow({ mode: 'open' });
|
||||||
|
|
||||||
|
#rAFId = -1;
|
||||||
#lastTime = 0;
|
#lastTime = 0;
|
||||||
#currentTime = 0;
|
#currentTime = 0;
|
||||||
#deltaTime = 0;
|
#deltaTime = 0;
|
||||||
|
|
@ -55,15 +59,29 @@ export class FolkRope extends AbstractArrow {
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
this.#canvas.width = this.clientWidth;
|
|
||||||
this.#canvas.height = this.clientHeight;
|
|
||||||
|
|
||||||
this.#shadow.appendChild(this.#canvas);
|
this.#shadow.appendChild(this.#canvas);
|
||||||
this.tick = this.tick.bind(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tick(timestamp: number) {
|
connectedCallback(): void {
|
||||||
requestAnimationFrame(this.tick);
|
super.connectedCallback();
|
||||||
|
|
||||||
|
resizeObserver.observe(this, this.#onResize);
|
||||||
|
}
|
||||||
|
|
||||||
|
disconnectedCallback(): void {
|
||||||
|
super.disconnectedCallback();
|
||||||
|
|
||||||
|
resizeObserver.unobserve(this, this.#onResize);
|
||||||
|
}
|
||||||
|
|
||||||
|
#onResize = (entry) => {
|
||||||
|
this.#canvas.width = entry.contentRect.width;
|
||||||
|
this.#canvas.height = entry.contentRect.height;
|
||||||
|
this.#drawRopePoints();
|
||||||
|
};
|
||||||
|
|
||||||
|
#tick = (timestamp: number = performance.now()) => {
|
||||||
|
this.#rAFId = requestAnimationFrame(this.#tick);
|
||||||
|
|
||||||
this.#currentTime = timestamp;
|
this.#currentTime = timestamp;
|
||||||
|
|
||||||
|
|
@ -84,15 +102,15 @@ export class FolkRope extends AbstractArrow {
|
||||||
|
|
||||||
this.#previousDelta = dts;
|
this.#previousDelta = dts;
|
||||||
|
|
||||||
this.drawRopePoints();
|
this.#drawRopePoints();
|
||||||
|
|
||||||
this.#lastTime = this.#currentTime - (this.#deltaTime % this.#interval);
|
this.#lastTime = this.#currentTime - (this.#deltaTime % this.#interval);
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
render(sourceRect: DOMRectReadOnly, targetRect: DOMRectReadOnly) {
|
render(sourceRect: DOMRectReadOnly, targetRect: DOMRectReadOnly) {
|
||||||
if (this.#points.length === 0) {
|
if (this.#points.length === 0) {
|
||||||
this.#points = this.generatePoints(
|
this.#points = this.#generatePoints(
|
||||||
{ x: sourceRect.x, y: sourceRect.y },
|
{ x: sourceRect.x, y: sourceRect.y },
|
||||||
{ x: targetRect.right, y: targetRect.bottom }
|
{ x: targetRect.right, y: targetRect.bottom }
|
||||||
);
|
);
|
||||||
|
|
@ -101,7 +119,7 @@ export class FolkRope extends AbstractArrow {
|
||||||
this.#currentTime = 0;
|
this.#currentTime = 0;
|
||||||
this.#deltaTime = 0;
|
this.#deltaTime = 0;
|
||||||
|
|
||||||
this.tick(performance.now());
|
this.#tick();
|
||||||
}
|
}
|
||||||
|
|
||||||
const startingPoint = this.#points.at(0);
|
const startingPoint = this.#points.at(0);
|
||||||
|
|
@ -116,7 +134,7 @@ export class FolkRope extends AbstractArrow {
|
||||||
endingPoint.pos.y = targetRect.bottom;
|
endingPoint.pos.y = targetRect.bottom;
|
||||||
}
|
}
|
||||||
|
|
||||||
drawRopePoints() {
|
#drawRopePoints() {
|
||||||
this.#context.clearRect(0, 0, this.#canvas.width, this.#canvas.height);
|
this.#context.clearRect(0, 0, this.#canvas.width, this.#canvas.height);
|
||||||
|
|
||||||
for (let i = 0; i < this.#points.length; i++) {
|
for (let i = 0; i < this.#points.length; i++) {
|
||||||
|
|
@ -135,7 +153,7 @@ export class FolkRope extends AbstractArrow {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
generatePoints(start: Vertex, end: Vertex) {
|
#generatePoints(start: Vertex, end: Vertex) {
|
||||||
const delta = Vector.sub(end, start);
|
const delta = Vector.sub(end, start);
|
||||||
const len = Vector.mag(delta);
|
const len = Vector.mag(delta);
|
||||||
const resolution = 5;
|
const resolution = 5;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue