Mirror: A frag-canvas custom element to apply Shadertoy fragment shaders to a canvas or image/video element

feat: Skip texture copying for image elements that haven't changed (#6)

* Skip image texture copying for <img> elements

* Add changeset

Changed files
+27 -1
.changeset
src
+5
.changeset/lazy-onions-tan.md
···
+
---
+
'frag-canvas': patch
+
---
+
+
Skip loading `<img>` as texture on every frame and skip on repeats
+22 -1
src/frag-canvas-element.ts
···
return [year, month, day, time] as const;
};
+
const isImageElement = (tex: TexImageSource): tex is HTMLImageElement =>
+
(tex as Element).tagName === 'IMG';
+
const preprocessShader = (source: string) => {
let header = '';
let output = source.trim();
···
let frameCount = 0;
let prevTimestamp: DOMHighResTimeStamp;
+
let prevSource: string | null;
const state = {
draw(source: TexImageSource, timestamp: DOMHighResTimeStamp) {
···
gl.useProgram(program);
-
if (source) {
+
if (isImageElement(source)) {
+
if (source.complete) {
+
const { currentSrc } = source;
+
if (prevSource !== currentSrc) {
+
prevSource = currentSrc;
+
gl.texImage2D(
+
gl.TEXTURE_2D,
+
0,
+
gl.RGBA,
+
gl.RGBA,
+
gl.UNSIGNED_BYTE,
+
source
+
);
+
}
+
}
+
} else if (source) {
+
prevSource = null;
gl.texImage2D(
gl.TEXTURE_2D,
0,
···
if (iChannelResolution)
gl.uniform3fv(iChannelResolution, [width, height, 0]);
} else {
+
prevSource = null;
if (iChannelResolution) gl.uniform3fv(iChannelResolution, [0, 0, 0]);
}