raf refactors
This commit is contained in:
parent
619fae1ee2
commit
7e2acb820e
|
|
@ -21,7 +21,7 @@ export class ClientRectObserver {
|
||||||
#rootRect = this.#root.getBoundingClientRect();
|
#rootRect = this.#root.getBoundingClientRect();
|
||||||
|
|
||||||
#entries: ClientRectObserverEntry[] = [];
|
#entries: ClientRectObserverEntry[] = [];
|
||||||
#rafId = 0;
|
#rafId = -1;
|
||||||
|
|
||||||
#callback: ClientRectObserverCallback;
|
#callback: ClientRectObserverCallback;
|
||||||
|
|
||||||
|
|
@ -48,19 +48,18 @@ export class ClientRectObserver {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
#appendEntry(entry: ClientRectObserverEntry) {
|
async #appendEntry(entry: ClientRectObserverEntry) {
|
||||||
|
if (this.#entries.length === 0) {
|
||||||
|
Promise.resolve().then(this.#flush);
|
||||||
|
}
|
||||||
|
|
||||||
// deduplicate the same target
|
// deduplicate the same target
|
||||||
this.#entries.push(entry);
|
this.#entries.push(entry);
|
||||||
|
|
||||||
if (this.#rafId === 0) {
|
|
||||||
this.#rafId = requestAnimationFrame(this.#flush);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#flush = () => {
|
#flush = () => {
|
||||||
const entries = this.#entries;
|
const entries = this.#entries;
|
||||||
this.#entries = [];
|
this.#entries = [];
|
||||||
this.#rafId = 0;
|
|
||||||
this.#callback(entries, this);
|
this.#callback(entries, this);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ import {
|
||||||
vertexShader,
|
vertexShader,
|
||||||
} from './folk-sand.glsl.ts';
|
} from './folk-sand.glsl.ts';
|
||||||
import { FolkShape } from './folk-shape.ts';
|
import { FolkShape } from './folk-shape.ts';
|
||||||
|
import { requestAnimationFrame } from './common/rAF.ts';
|
||||||
|
|
||||||
export class FolkSand extends HTMLElement {
|
export class FolkSand extends HTMLElement {
|
||||||
static tagName = 'folk-sand';
|
static tagName = 'folk-sand';
|
||||||
|
|
@ -88,7 +89,7 @@ export class FolkSand extends HTMLElement {
|
||||||
this.updateCollisionTexture();
|
this.updateCollisionTexture();
|
||||||
|
|
||||||
this.attachEventListeners();
|
this.attachEventListeners();
|
||||||
requestAnimationFrame(this.render.bind(this));
|
this.render();
|
||||||
}
|
}
|
||||||
|
|
||||||
disconnectedCallback() {
|
disconnectedCallback() {
|
||||||
|
|
@ -365,7 +366,7 @@ export class FolkSand extends HTMLElement {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private render(time: number) {
|
private render = (time: number = performance.now()) => {
|
||||||
if (this.resizeCanvas()) {
|
if (this.resizeCanvas()) {
|
||||||
this.processResize();
|
this.processResize();
|
||||||
}
|
}
|
||||||
|
|
@ -378,8 +379,8 @@ export class FolkSand extends HTMLElement {
|
||||||
this.pointer.prevX = this.pointer.x;
|
this.pointer.prevX = this.pointer.x;
|
||||||
this.pointer.prevY = this.pointer.y;
|
this.pointer.prevY = this.pointer.y;
|
||||||
|
|
||||||
requestAnimationFrame(this.render.bind(this));
|
requestAnimationFrame(this.render);
|
||||||
}
|
};
|
||||||
|
|
||||||
private renderPass(time: number) {
|
private renderPass(time: number) {
|
||||||
const gl = this.gl;
|
const gl = this.gl;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue