@vfx-js/effects 1.4.3 → 1.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (135) hide show
  1. package/lib/cjs/ascii.js +63 -73
  2. package/lib/cjs/ascii.js.map +1 -1
  3. package/lib/cjs/bad-jpeg.js +7 -5
  4. package/lib/cjs/bad-jpeg.js.map +1 -1
  5. package/lib/cjs/bloom.js +68 -78
  6. package/lib/cjs/bloom.js.map +1 -1
  7. package/lib/cjs/chromatic.js +1 -0
  8. package/lib/cjs/chromatic.js.map +1 -1
  9. package/lib/cjs/colored-edges.js +1 -0
  10. package/lib/cjs/colored-edges.js.map +1 -1
  11. package/lib/cjs/datamosh.js +173 -183
  12. package/lib/cjs/datamosh.js.map +1 -1
  13. package/lib/cjs/dither.js +6 -3
  14. package/lib/cjs/dither.js.map +1 -1
  15. package/lib/cjs/duotone.js +4 -2
  16. package/lib/cjs/duotone.js.map +1 -1
  17. package/lib/cjs/fluid.js +62 -74
  18. package/lib/cjs/fluid.js.map +1 -1
  19. package/lib/cjs/glitch.js +1 -0
  20. package/lib/cjs/glitch.js.map +1 -1
  21. package/lib/cjs/gradient-map.js +4 -1
  22. package/lib/cjs/gradient-map.js.map +1 -1
  23. package/lib/cjs/halftone.js +3 -0
  24. package/lib/cjs/halftone.js.map +1 -1
  25. package/lib/cjs/hue-shift.js +1 -0
  26. package/lib/cjs/hue-shift.js.map +1 -1
  27. package/lib/cjs/jpeg-glitch.js +273 -277
  28. package/lib/cjs/jpeg-glitch.js.map +1 -1
  29. package/lib/cjs/light-streak.js +34 -47
  30. package/lib/cjs/light-streak.js.map +1 -1
  31. package/lib/cjs/matrix.js +48 -59
  32. package/lib/cjs/matrix.js.map +1 -1
  33. package/lib/cjs/package.json +2 -1
  34. package/lib/cjs/particle-explode.js +122 -128
  35. package/lib/cjs/particle-explode.js.map +1 -1
  36. package/lib/cjs/particle.js +253 -258
  37. package/lib/cjs/particle.js.map +1 -1
  38. package/lib/cjs/pattern-refraction.js +1 -0
  39. package/lib/cjs/pattern-refraction.js.map +1 -1
  40. package/lib/cjs/pixel-stretch.js +1 -0
  41. package/lib/cjs/pixel-stretch.js.map +1 -1
  42. package/lib/cjs/pixelate.js +1 -0
  43. package/lib/cjs/pixelate.js.map +1 -1
  44. package/lib/cjs/pixelsort.js +102 -121
  45. package/lib/cjs/pixelsort.js.map +1 -1
  46. package/lib/cjs/rainbow.js +1 -0
  47. package/lib/cjs/rainbow.js.map +1 -1
  48. package/lib/cjs/rgb-glitch.js +1 -0
  49. package/lib/cjs/rgb-glitch.js.map +1 -1
  50. package/lib/cjs/rgb-shift.js +1 -0
  51. package/lib/cjs/rgb-shift.js.map +1 -1
  52. package/lib/cjs/scanline.js +1 -0
  53. package/lib/cjs/scanline.js.map +1 -1
  54. package/lib/cjs/sinewave.js +1 -0
  55. package/lib/cjs/sinewave.js.map +1 -1
  56. package/lib/cjs/slice-shift.js +1 -0
  57. package/lib/cjs/slice-shift.js.map +1 -1
  58. package/lib/cjs/tile-pixelate.js +4 -1
  59. package/lib/cjs/tile-pixelate.js.map +1 -1
  60. package/lib/cjs/tritone.js +5 -2
  61. package/lib/cjs/tritone.js.map +1 -1
  62. package/lib/cjs/vignette.js +1 -0
  63. package/lib/cjs/vignette.js.map +1 -1
  64. package/lib/cjs/voronoi.js +4 -1
  65. package/lib/cjs/voronoi.js.map +1 -1
  66. package/lib/cjs/warp.js +1 -0
  67. package/lib/cjs/warp.js.map +1 -1
  68. package/lib/esm/ascii.js +63 -73
  69. package/lib/esm/ascii.js.map +1 -1
  70. package/lib/esm/bad-jpeg.js +7 -5
  71. package/lib/esm/bad-jpeg.js.map +1 -1
  72. package/lib/esm/bloom.js +67 -77
  73. package/lib/esm/bloom.js.map +1 -1
  74. package/lib/esm/chromatic.js +1 -0
  75. package/lib/esm/chromatic.js.map +1 -1
  76. package/lib/esm/colored-edges.js +1 -0
  77. package/lib/esm/colored-edges.js.map +1 -1
  78. package/lib/esm/datamosh.js +172 -182
  79. package/lib/esm/datamosh.js.map +1 -1
  80. package/lib/esm/dither.js +6 -3
  81. package/lib/esm/dither.js.map +1 -1
  82. package/lib/esm/duotone.js +4 -2
  83. package/lib/esm/duotone.js.map +1 -1
  84. package/lib/esm/fluid.js +62 -74
  85. package/lib/esm/fluid.js.map +1 -1
  86. package/lib/esm/glitch.js +1 -0
  87. package/lib/esm/glitch.js.map +1 -1
  88. package/lib/esm/gradient-map.js +4 -1
  89. package/lib/esm/gradient-map.js.map +1 -1
  90. package/lib/esm/halftone.js +3 -0
  91. package/lib/esm/halftone.js.map +1 -1
  92. package/lib/esm/hue-shift.js +1 -0
  93. package/lib/esm/hue-shift.js.map +1 -1
  94. package/lib/esm/jpeg-glitch.js +272 -276
  95. package/lib/esm/jpeg-glitch.js.map +1 -1
  96. package/lib/esm/light-streak.js +34 -47
  97. package/lib/esm/light-streak.js.map +1 -1
  98. package/lib/esm/matrix.js +48 -59
  99. package/lib/esm/matrix.js.map +1 -1
  100. package/lib/esm/package.json +2 -1
  101. package/lib/esm/particle-explode.js +122 -128
  102. package/lib/esm/particle-explode.js.map +1 -1
  103. package/lib/esm/particle.js +253 -258
  104. package/lib/esm/particle.js.map +1 -1
  105. package/lib/esm/pattern-refraction.js +1 -0
  106. package/lib/esm/pattern-refraction.js.map +1 -1
  107. package/lib/esm/pixel-stretch.js +1 -0
  108. package/lib/esm/pixel-stretch.js.map +1 -1
  109. package/lib/esm/pixelate.js +1 -0
  110. package/lib/esm/pixelate.js.map +1 -1
  111. package/lib/esm/pixelsort.js +102 -121
  112. package/lib/esm/pixelsort.js.map +1 -1
  113. package/lib/esm/rainbow.js +1 -0
  114. package/lib/esm/rainbow.js.map +1 -1
  115. package/lib/esm/rgb-glitch.js +1 -0
  116. package/lib/esm/rgb-glitch.js.map +1 -1
  117. package/lib/esm/rgb-shift.js +1 -0
  118. package/lib/esm/rgb-shift.js.map +1 -1
  119. package/lib/esm/scanline.js +1 -0
  120. package/lib/esm/scanline.js.map +1 -1
  121. package/lib/esm/sinewave.js +1 -0
  122. package/lib/esm/sinewave.js.map +1 -1
  123. package/lib/esm/slice-shift.js +1 -0
  124. package/lib/esm/slice-shift.js.map +1 -1
  125. package/lib/esm/tile-pixelate.js +4 -1
  126. package/lib/esm/tile-pixelate.js.map +1 -1
  127. package/lib/esm/tritone.js +5 -2
  128. package/lib/esm/tritone.js.map +1 -1
  129. package/lib/esm/vignette.js +1 -0
  130. package/lib/esm/vignette.js.map +1 -1
  131. package/lib/esm/voronoi.js +4 -1
  132. package/lib/esm/voronoi.js.map +1 -1
  133. package/lib/esm/warp.js +1 -0
  134. package/lib/esm/warp.js.map +1 -1
  135. package/package.json +4 -3
@@ -1,18 +1,6 @@
1
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
2
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
3
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
4
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
5
- };
6
- var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
7
- if (kind === "m") throw new TypeError("Private method is not writable");
8
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
9
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
10
- return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
11
- };
12
- var _JPEGGlitchEffect_instances, _JPEGGlitchEffect_supported, _JPEGGlitchEffect_readRT, _JPEGGlitchEffect_outTex, _JPEGGlitchEffect_gl, _JPEGGlitchEffect_outGLTex, _JPEGGlitchEffect_uploadPending, _JPEGGlitchEffect_restoreUnsub, _JPEGGlitchEffect_srcCanvas, _JPEGGlitchEffect_srcCtx, _JPEGGlitchEffect_encCanvas, _JPEGGlitchEffect_encCtx, _JPEGGlitchEffect_outCanvas, _JPEGGlitchEffect_outCtx, _JPEGGlitchEffect_raw, _JPEGGlitchEffect_imageData, _JPEGGlitchEffect_w, _JPEGGlitchEffect_h, _JPEGGlitchEffect_srcW, _JPEGGlitchEffect_srcH, _JPEGGlitchEffect_dirty, _JPEGGlitchEffect_busy, _JPEGGlitchEffect_lastGlitchTime, _JPEGGlitchEffect_generation, _JPEGGlitchEffect_glitchCount, _JPEGGlitchEffect_hasResult, _JPEGGlitchEffect_producedFrames, _JPEGGlitchEffect_createOutTexture, _JPEGGlitchEffect_passthrough, _JPEGGlitchEffect_shouldGlitch, _JPEGGlitchEffect_resize, _JPEGGlitchEffect_startGlitch, _JPEGGlitchEffect_uploadOutCanvas, _JPEGGlitchEffect_runGlitch;
13
1
  import { captureContent } from "./_capture.js";
14
2
  // Passthrough of the live source to the canvas (used until the first
15
- // glitch result is ready). Premultiplied for the canvas blend.
3
+ // glitch result is ready). `src` is already premultiplied.
16
4
  const FRAG_PASSTHROUGH = `#version 300 es
17
5
  precision highp float;
18
6
  in vec2 uvSrc;
@@ -25,8 +13,7 @@ void main() {
25
13
  outColor = vec4(0.0);
26
14
  return;
27
15
  }
28
- vec4 c = texture(src, uvSrc);
29
- outColor = vec4(c.rgb * c.a, c.a);
16
+ outColor = texture(src, uvSrc);
30
17
  }
31
18
  `;
32
19
  // Draw the decoded glitch canvas (element-sized, upright) to the canvas.
@@ -173,109 +160,133 @@ function glitchBytes(bytes, headerLength, iterations, rng) {
173
160
  * render loop. The source passes through until the first result lands.
174
161
  */
175
162
  export class JPEGGlitchEffect {
163
+ params;
164
+ enabled = true;
165
+ #supported = false;
166
+ #readRT = null;
167
+ #outTex = null;
168
+ // Raw GL handles for the on-demand display-texture upload.
169
+ #gl = null;
170
+ // Display texture we upload `outCanvas` into only when a new glitch
171
+ // lands, instead of re-uploading the canvas every frame.
172
+ #outGLTex = null;
173
+ #uploadPending = false;
174
+ #restoreUnsub = null;
175
+ #srcCanvas = null;
176
+ #srcCtx = null;
177
+ // Holds the (optionally rotated) image that actually gets encoded; its
178
+ // size is set per-glitch since a quarter turn swaps width and height.
179
+ #encCanvas = null;
180
+ #encCtx = null;
181
+ #outCanvas = null;
182
+ #outCtx = null;
183
+ #raw = new Uint8Array(0);
184
+ #imageData = null;
185
+ #w = 0;
186
+ #h = 0;
187
+ // Source native size last seen — a change (e.g. an image finishing
188
+ // loading) re-arms a static glitch so it doesn't latch a blank frame.
189
+ #srcW = 0;
190
+ #srcH = 0;
191
+ // Re-glitch when a static frame's inputs change (params / resize).
192
+ #dirty = true;
193
+ // An encode/decode round trip is in flight; gate new glitches on it.
194
+ #busy = false;
195
+ // Last glitch start time (seconds), for the `speed` throttle.
196
+ #lastGlitchTime = -1e9;
197
+ // Bumped on resize so a stale in-flight result is discarded.
198
+ #generation = 0;
199
+ // Drives the animated seed walk when `speed > 0`.
200
+ #glitchCount = 0;
201
+ #hasResult = false;
202
+ // Number of decoded glitch results produced. Increments once per
203
+ // completed encode/decode round trip — sample it to measure the real
204
+ // output rate, which is capped by codec latency below high `speed`.
205
+ #producedFrames = 0;
176
206
  /** Count of decoded glitch results produced so far. */
177
207
  get producedFrames() {
178
- return __classPrivateFieldGet(this, _JPEGGlitchEffect_producedFrames, "f");
208
+ return this.#producedFrames;
179
209
  }
180
210
  constructor(initial = {}) {
181
- _JPEGGlitchEffect_instances.add(this);
182
- this.enabled = true;
183
- _JPEGGlitchEffect_supported.set(this, false);
184
- _JPEGGlitchEffect_readRT.set(this, null);
185
- _JPEGGlitchEffect_outTex.set(this, null);
186
- // Raw GL handles for the on-demand display-texture upload.
187
- _JPEGGlitchEffect_gl.set(this, null);
188
- // Display texture we upload `outCanvas` into only when a new glitch
189
- // lands, instead of re-uploading the canvas every frame.
190
- _JPEGGlitchEffect_outGLTex.set(this, null);
191
- _JPEGGlitchEffect_uploadPending.set(this, false);
192
- _JPEGGlitchEffect_restoreUnsub.set(this, null);
193
- _JPEGGlitchEffect_srcCanvas.set(this, null);
194
- _JPEGGlitchEffect_srcCtx.set(this, null);
195
- // Holds the (optionally rotated) image that actually gets encoded; its
196
- // size is set per-glitch since a quarter turn swaps width and height.
197
- _JPEGGlitchEffect_encCanvas.set(this, null);
198
- _JPEGGlitchEffect_encCtx.set(this, null);
199
- _JPEGGlitchEffect_outCanvas.set(this, null);
200
- _JPEGGlitchEffect_outCtx.set(this, null);
201
- _JPEGGlitchEffect_raw.set(this, new Uint8Array(0));
202
- _JPEGGlitchEffect_imageData.set(this, null);
203
- _JPEGGlitchEffect_w.set(this, 0);
204
- _JPEGGlitchEffect_h.set(this, 0);
205
- // Source native size last seen — a change (e.g. an image finishing
206
- // loading) re-arms a static glitch so it doesn't latch a blank frame.
207
- _JPEGGlitchEffect_srcW.set(this, 0);
208
- _JPEGGlitchEffect_srcH.set(this, 0);
209
- // Re-glitch when a static frame's inputs change (params / resize).
210
- _JPEGGlitchEffect_dirty.set(this, true);
211
- // An encode/decode round trip is in flight; gate new glitches on it.
212
- _JPEGGlitchEffect_busy.set(this, false);
213
- // Last glitch start time (seconds), for the `speed` throttle.
214
- _JPEGGlitchEffect_lastGlitchTime.set(this, -1e9);
215
- // Bumped on resize so a stale in-flight result is discarded.
216
- _JPEGGlitchEffect_generation.set(this, 0);
217
- // Drives the animated seed walk when `speed > 0`.
218
- _JPEGGlitchEffect_glitchCount.set(this, 0);
219
- _JPEGGlitchEffect_hasResult.set(this, false);
220
- // Number of decoded glitch results produced. Increments once per
221
- // completed encode/decode round trip — sample it to measure the real
222
- // output rate, which is capped by codec latency below high `speed`.
223
- _JPEGGlitchEffect_producedFrames.set(this, 0);
224
211
  this.params = { ...DEFAULT_PARAMS, ...initial };
225
212
  }
226
213
  setParams(updates) {
227
214
  Object.assign(this.params, updates);
228
- __classPrivateFieldSet(this, _JPEGGlitchEffect_dirty, true, "f");
215
+ this.#dirty = true;
229
216
  }
230
217
  init(ctx) {
231
- __classPrivateFieldSet(this, _JPEGGlitchEffect_supported, typeof createImageBitmap === "function" &&
232
- (typeof OffscreenCanvas !== "undefined" ||
233
- typeof document !== "undefined"), "f");
234
- if (!__classPrivateFieldGet(this, _JPEGGlitchEffect_supported, "f")) {
218
+ this.#supported =
219
+ typeof createImageBitmap === "function" &&
220
+ (typeof OffscreenCanvas !== "undefined" ||
221
+ typeof document !== "undefined");
222
+ if (!this.#supported) {
235
223
  return;
236
224
  }
237
225
  // The element's time base restarts at 0 on each (re-)attach.
238
- __classPrivateFieldSet(this, _JPEGGlitchEffect_lastGlitchTime, -1e9, "f");
239
- __classPrivateFieldSet(this, _JPEGGlitchEffect_dirty, true, "f");
240
- __classPrivateFieldSet(this, _JPEGGlitchEffect_srcCanvas, createCanvas(1, 1), "f");
241
- __classPrivateFieldSet(this, _JPEGGlitchEffect_srcCtx, get2d(__classPrivateFieldGet(this, _JPEGGlitchEffect_srcCanvas, "f")), "f");
242
- __classPrivateFieldSet(this, _JPEGGlitchEffect_encCanvas, createCanvas(1, 1), "f");
243
- __classPrivateFieldSet(this, _JPEGGlitchEffect_encCtx, get2d(__classPrivateFieldGet(this, _JPEGGlitchEffect_encCanvas, "f")), "f");
244
- __classPrivateFieldSet(this, _JPEGGlitchEffect_outCanvas, createCanvas(1, 1), "f");
245
- __classPrivateFieldSet(this, _JPEGGlitchEffect_outCtx, get2d(__classPrivateFieldGet(this, _JPEGGlitchEffect_outCanvas, "f")), "f");
226
+ this.#lastGlitchTime = -1e9;
227
+ this.#dirty = true;
228
+ this.#srcCanvas = createCanvas(1, 1);
229
+ this.#srcCtx = get2d(this.#srcCanvas);
230
+ this.#encCanvas = createCanvas(1, 1);
231
+ this.#encCtx = get2d(this.#encCanvas);
232
+ this.#outCanvas = createCanvas(1, 1);
233
+ this.#outCtx = get2d(this.#outCanvas);
246
234
  // Sized to the processing resolution in #resize; start at 1×1.
247
- __classPrivateFieldSet(this, _JPEGGlitchEffect_readRT, ctx.createRenderTarget({ size: [1, 1] }), "f");
248
- __classPrivateFieldSet(this, _JPEGGlitchEffect_gl, ctx.gl, "f");
249
- __classPrivateFieldGet(this, _JPEGGlitchEffect_instances, "m", _JPEGGlitchEffect_createOutTexture).call(this, ctx);
235
+ this.#readRT = ctx.createRenderTarget({ size: [1, 1] });
236
+ this.#gl = ctx.gl;
237
+ this.#createOutTexture(ctx);
250
238
  // The display texture dies on context loss; rebuild it.
251
- __classPrivateFieldSet(this, _JPEGGlitchEffect_restoreUnsub, ctx.onContextRestored(() => {
252
- __classPrivateFieldGet(this, _JPEGGlitchEffect_instances, "m", _JPEGGlitchEffect_createOutTexture).call(this, ctx);
239
+ this.#restoreUnsub = ctx.onContextRestored(() => {
240
+ this.#createOutTexture(ctx);
253
241
  // Re-push the last decoded frame, if any.
254
- __classPrivateFieldSet(this, _JPEGGlitchEffect_uploadPending, __classPrivateFieldGet(this, _JPEGGlitchEffect_hasResult, "f"), "f");
255
- }), "f");
242
+ this.#uploadPending = this.#hasResult;
243
+ });
244
+ }
245
+ // Create (or recreate) the display texture and wrap it for sampling.
246
+ // We own the GL handle and upload into it manually, so context-loss
247
+ // rebuilds are our responsibility.
248
+ #createOutTexture(ctx) {
249
+ const gl = ctx.gl;
250
+ const tex = gl.createTexture();
251
+ if (!tex) {
252
+ return;
253
+ }
254
+ gl.bindTexture(gl.TEXTURE_2D, tex);
255
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
256
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
257
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
258
+ // Nearest mag so low resolutionScale upscales crisp, not blurry.
259
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
260
+ // 1×1 placeholder so the sampler is valid before the first upload.
261
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, new Uint8Array([0, 0, 0, 0]));
262
+ gl.bindTexture(gl.TEXTURE_2D, null);
263
+ this.#outGLTex = tex;
264
+ this.#outTex = ctx.wrapTexture(tex, {
265
+ size: [this.#w || 1, this.#h || 1],
266
+ });
256
267
  }
257
268
  // update() runs even while disabled (the chain only skips render); drop
258
269
  // the cached glitch so re-enabling starts clean instead of flashing it.
259
270
  update() {
260
271
  if (this.enabled === false) {
261
- __classPrivateFieldSet(this, _JPEGGlitchEffect_hasResult, false, "f");
272
+ this.#hasResult = false;
262
273
  }
263
274
  }
264
275
  render(ctx) {
265
- if (this.params.bypass || !__classPrivateFieldGet(this, _JPEGGlitchEffect_supported, "f") || !__classPrivateFieldGet(this, _JPEGGlitchEffect_readRT, "f")) {
276
+ if (this.params.bypass || !this.#supported || !this.#readRT) {
266
277
  // Drop any stale decoded frame so re-enabling starts clean rather
267
278
  // than flashing the last glitch.
268
- __classPrivateFieldSet(this, _JPEGGlitchEffect_hasResult, false, "f");
269
- __classPrivateFieldGet(this, _JPEGGlitchEffect_instances, "m", _JPEGGlitchEffect_passthrough).call(this, ctx);
279
+ this.#hasResult = false;
280
+ this.#passthrough(ctx);
270
281
  return;
271
282
  }
272
283
  // Re-arm a static glitch when the source first loads / changes size.
273
284
  const srcW = ctx.src.width;
274
285
  const srcH = ctx.src.height;
275
- if (srcW !== __classPrivateFieldGet(this, _JPEGGlitchEffect_srcW, "f") || srcH !== __classPrivateFieldGet(this, _JPEGGlitchEffect_srcH, "f")) {
276
- __classPrivateFieldSet(this, _JPEGGlitchEffect_srcW, srcW, "f");
277
- __classPrivateFieldSet(this, _JPEGGlitchEffect_srcH, srcH, "f");
278
- __classPrivateFieldSet(this, _JPEGGlitchEffect_dirty, true, "f");
286
+ if (srcW !== this.#srcW || srcH !== this.#srcH) {
287
+ this.#srcW = srcW;
288
+ this.#srcH = srcH;
289
+ this.#dirty = true;
279
290
  }
280
291
  // Process at a fraction of the element resolution. The decoded
281
292
  // output is sampled by uvContent so it scales back up to fit.
@@ -284,215 +295,200 @@ export class JPEGGlitchEffect {
284
295
  const w = Math.max(1, Math.round(ew * scale));
285
296
  const h = Math.max(1, Math.round(eh * scale));
286
297
  if (ew >= 2 && eh >= 2 && srcW > 0 && srcH > 0) {
287
- if (w !== __classPrivateFieldGet(this, _JPEGGlitchEffect_w, "f") || h !== __classPrivateFieldGet(this, _JPEGGlitchEffect_h, "f")) {
288
- __classPrivateFieldGet(this, _JPEGGlitchEffect_instances, "m", _JPEGGlitchEffect_resize).call(this, ctx, w, h);
298
+ if (w !== this.#w || h !== this.#h) {
299
+ this.#resize(ctx, w, h);
289
300
  }
290
- if (__classPrivateFieldGet(this, _JPEGGlitchEffect_instances, "m", _JPEGGlitchEffect_shouldGlitch).call(this, ctx)) {
291
- __classPrivateFieldGet(this, _JPEGGlitchEffect_instances, "m", _JPEGGlitchEffect_startGlitch).call(this, ctx);
301
+ if (this.#shouldGlitch(ctx)) {
302
+ this.#startGlitch(ctx);
292
303
  }
293
304
  }
294
- if (__classPrivateFieldGet(this, _JPEGGlitchEffect_hasResult, "f") && __classPrivateFieldGet(this, _JPEGGlitchEffect_outTex, "f")) {
305
+ if (this.#hasResult && this.#outTex) {
295
306
  // Upload the decoded canvas to the GPU only when it changed.
296
- if (__classPrivateFieldGet(this, _JPEGGlitchEffect_uploadPending, "f")) {
297
- __classPrivateFieldGet(this, _JPEGGlitchEffect_instances, "m", _JPEGGlitchEffect_uploadOutCanvas).call(this, ctx);
307
+ if (this.#uploadPending) {
308
+ this.#uploadOutCanvas(ctx);
298
309
  }
299
310
  ctx.draw({
300
311
  frag: FRAG_OUTPUT,
301
- uniforms: { tex: __classPrivateFieldGet(this, _JPEGGlitchEffect_outTex, "f") },
312
+ uniforms: { tex: this.#outTex },
302
313
  target: ctx.target,
303
314
  });
304
315
  }
305
316
  else {
306
- __classPrivateFieldGet(this, _JPEGGlitchEffect_instances, "m", _JPEGGlitchEffect_passthrough).call(this, ctx);
317
+ this.#passthrough(ctx);
307
318
  }
308
319
  }
309
- dispose() {
310
- var _a;
311
- __classPrivateFieldGet(this, _JPEGGlitchEffect_restoreUnsub, "f")?.call(this);
312
- __classPrivateFieldSet(this, _JPEGGlitchEffect_restoreUnsub, null, "f");
313
- if (__classPrivateFieldGet(this, _JPEGGlitchEffect_gl, "f") && __classPrivateFieldGet(this, _JPEGGlitchEffect_outGLTex, "f")) {
314
- __classPrivateFieldGet(this, _JPEGGlitchEffect_gl, "f").deleteTexture(__classPrivateFieldGet(this, _JPEGGlitchEffect_outGLTex, "f"));
315
- }
316
- __classPrivateFieldSet(this, _JPEGGlitchEffect_outGLTex, null, "f");
317
- __classPrivateFieldSet(this, _JPEGGlitchEffect_gl, null, "f");
318
- __classPrivateFieldSet(this, _JPEGGlitchEffect_uploadPending, false, "f");
319
- __classPrivateFieldGet(this, _JPEGGlitchEffect_readRT, "f")?.dispose();
320
- __classPrivateFieldSet(this, _JPEGGlitchEffect_readRT, null, "f");
321
- __classPrivateFieldSet(this, _JPEGGlitchEffect_outTex, null, "f");
322
- __classPrivateFieldSet(this, _JPEGGlitchEffect_srcCanvas, null, "f");
323
- __classPrivateFieldSet(this, _JPEGGlitchEffect_srcCtx, null, "f");
324
- __classPrivateFieldSet(this, _JPEGGlitchEffect_encCanvas, null, "f");
325
- __classPrivateFieldSet(this, _JPEGGlitchEffect_encCtx, null, "f");
326
- __classPrivateFieldSet(this, _JPEGGlitchEffect_outCanvas, null, "f");
327
- __classPrivateFieldSet(this, _JPEGGlitchEffect_outCtx, null, "f");
328
- __classPrivateFieldSet(this, _JPEGGlitchEffect_imageData, null, "f");
329
- __classPrivateFieldSet(this, _JPEGGlitchEffect_raw, new Uint8Array(0), "f");
330
- __classPrivateFieldSet(this, _JPEGGlitchEffect_hasResult, false, "f");
331
- __classPrivateFieldSet(this, _JPEGGlitchEffect_busy, false, "f");
332
- // Forget the processed size so a re-init reallocates the readback RT
333
- // (which dispose just freed) instead of mistaking it for current.
334
- __classPrivateFieldSet(this, _JPEGGlitchEffect_w, 0, "f");
335
- __classPrivateFieldSet(this, _JPEGGlitchEffect_h, 0, "f");
336
- // Invalidate any in-flight round trip.
337
- __classPrivateFieldSet(this, _JPEGGlitchEffect_generation, (_a = __classPrivateFieldGet(this, _JPEGGlitchEffect_generation, "f"), _a++, _a), "f");
338
- }
339
- }
340
- _JPEGGlitchEffect_supported = new WeakMap(), _JPEGGlitchEffect_readRT = new WeakMap(), _JPEGGlitchEffect_outTex = new WeakMap(), _JPEGGlitchEffect_gl = new WeakMap(), _JPEGGlitchEffect_outGLTex = new WeakMap(), _JPEGGlitchEffect_uploadPending = new WeakMap(), _JPEGGlitchEffect_restoreUnsub = new WeakMap(), _JPEGGlitchEffect_srcCanvas = new WeakMap(), _JPEGGlitchEffect_srcCtx = new WeakMap(), _JPEGGlitchEffect_encCanvas = new WeakMap(), _JPEGGlitchEffect_encCtx = new WeakMap(), _JPEGGlitchEffect_outCanvas = new WeakMap(), _JPEGGlitchEffect_outCtx = new WeakMap(), _JPEGGlitchEffect_raw = new WeakMap(), _JPEGGlitchEffect_imageData = new WeakMap(), _JPEGGlitchEffect_w = new WeakMap(), _JPEGGlitchEffect_h = new WeakMap(), _JPEGGlitchEffect_srcW = new WeakMap(), _JPEGGlitchEffect_srcH = new WeakMap(), _JPEGGlitchEffect_dirty = new WeakMap(), _JPEGGlitchEffect_busy = new WeakMap(), _JPEGGlitchEffect_lastGlitchTime = new WeakMap(), _JPEGGlitchEffect_generation = new WeakMap(), _JPEGGlitchEffect_glitchCount = new WeakMap(), _JPEGGlitchEffect_hasResult = new WeakMap(), _JPEGGlitchEffect_producedFrames = new WeakMap(), _JPEGGlitchEffect_instances = new WeakSet(), _JPEGGlitchEffect_createOutTexture = function _JPEGGlitchEffect_createOutTexture(ctx) {
341
- const gl = ctx.gl;
342
- const tex = gl.createTexture();
343
- if (!tex) {
344
- return;
345
- }
346
- gl.bindTexture(gl.TEXTURE_2D, tex);
347
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
348
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
349
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
350
- // Nearest mag so low resolutionScale upscales crisp, not blurry.
351
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
352
- // 1×1 placeholder so the sampler is valid before the first upload.
353
- gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, new Uint8Array([0, 0, 0, 0]));
354
- gl.bindTexture(gl.TEXTURE_2D, null);
355
- __classPrivateFieldSet(this, _JPEGGlitchEffect_outGLTex, tex, "f");
356
- __classPrivateFieldSet(this, _JPEGGlitchEffect_outTex, ctx.wrapTexture(tex, {
357
- size: [__classPrivateFieldGet(this, _JPEGGlitchEffect_w, "f") || 1, __classPrivateFieldGet(this, _JPEGGlitchEffect_h, "f") || 1],
358
- }), "f");
359
- }, _JPEGGlitchEffect_passthrough = function _JPEGGlitchEffect_passthrough(ctx) {
360
- ctx.draw({
361
- frag: FRAG_PASSTHROUGH,
362
- uniforms: { src: ctx.src },
363
- target: ctx.target,
364
- });
365
- }, _JPEGGlitchEffect_shouldGlitch = function _JPEGGlitchEffect_shouldGlitch(ctx) {
366
- if (__classPrivateFieldGet(this, _JPEGGlitchEffect_busy, "f")) {
367
- return false;
368
- }
369
- const speed = this.params.speed;
370
- if (speed > 0) {
371
- return ctx.time - __classPrivateFieldGet(this, _JPEGGlitchEffect_lastGlitchTime, "f") >= 1 / speed;
372
- }
373
- return __classPrivateFieldGet(this, _JPEGGlitchEffect_dirty, "f");
374
- }, _JPEGGlitchEffect_resize = function _JPEGGlitchEffect_resize(ctx, w, h) {
375
- var _a;
376
- __classPrivateFieldSet(this, _JPEGGlitchEffect_w, w, "f");
377
- __classPrivateFieldSet(this, _JPEGGlitchEffect_h, h, "f");
378
- __classPrivateFieldSet(this, _JPEGGlitchEffect_raw, new Uint8Array(w * h * 4), "f");
379
- __classPrivateFieldSet(this, _JPEGGlitchEffect_imageData, new ImageData(w, h), "f");
380
- if (__classPrivateFieldGet(this, _JPEGGlitchEffect_srcCanvas, "f")) {
381
- __classPrivateFieldGet(this, _JPEGGlitchEffect_srcCanvas, "f").width = w;
382
- __classPrivateFieldGet(this, _JPEGGlitchEffect_srcCanvas, "f").height = h;
383
- }
384
- if (__classPrivateFieldGet(this, _JPEGGlitchEffect_outCanvas, "f")) {
385
- __classPrivateFieldGet(this, _JPEGGlitchEffect_outCanvas, "f").width = w;
386
- __classPrivateFieldGet(this, _JPEGGlitchEffect_outCanvas, "f").height = h;
387
- }
388
- // The readback RT is sized to the processing resolution, so recreate
389
- // it (explicit-size RTs don't auto-track the element).
390
- __classPrivateFieldGet(this, _JPEGGlitchEffect_readRT, "f")?.dispose();
391
- __classPrivateFieldSet(this, _JPEGGlitchEffect_readRT, ctx.createRenderTarget({ size: [w, h] }), "f");
392
- __classPrivateFieldSet(this, _JPEGGlitchEffect_hasResult, false, "f");
393
- __classPrivateFieldSet(this, _JPEGGlitchEffect_dirty, true, "f");
394
- __classPrivateFieldSet(this, _JPEGGlitchEffect_busy, false, "f");
395
- __classPrivateFieldSet(this, _JPEGGlitchEffect_uploadPending, false, "f");
396
- __classPrivateFieldSet(this, _JPEGGlitchEffect_generation, (_a = __classPrivateFieldGet(this, _JPEGGlitchEffect_generation, "f"), _a++, _a), "f");
397
- }, _JPEGGlitchEffect_startGlitch = function _JPEGGlitchEffect_startGlitch(ctx) {
398
- var _a;
399
- const w = __classPrivateFieldGet(this, _JPEGGlitchEffect_w, "f");
400
- const h = __classPrivateFieldGet(this, _JPEGGlitchEffect_h, "f");
401
- const srcCanvas = __classPrivateFieldGet(this, _JPEGGlitchEffect_srcCanvas, "f");
402
- const srcCtx = __classPrivateFieldGet(this, _JPEGGlitchEffect_srcCtx, "f");
403
- const encCanvas = __classPrivateFieldGet(this, _JPEGGlitchEffect_encCanvas, "f");
404
- const encCtx = __classPrivateFieldGet(this, _JPEGGlitchEffect_encCtx, "f");
405
- const imageData = __classPrivateFieldGet(this, _JPEGGlitchEffect_imageData, "f");
406
- if (!srcCanvas ||
407
- !srcCtx ||
408
- !encCanvas ||
409
- !encCtx ||
410
- !imageData ||
411
- !__classPrivateFieldGet(this, _JPEGGlitchEffect_readRT, "f")) {
412
- return;
320
+ #passthrough(ctx) {
321
+ ctx.draw({
322
+ frag: FRAG_PASSTHROUGH,
323
+ uniforms: { src: ctx.src },
324
+ target: ctx.target,
325
+ });
413
326
  }
414
- // Read the element capture back to the CPU. The draw leaves the
415
- // readback RT's framebuffer bound, so readPixels reads from it.
416
- captureContent(ctx, __classPrivateFieldGet(this, _JPEGGlitchEffect_readRT, "f"));
417
- const gl = ctx.gl;
418
- gl.readPixels(0, 0, w, h, gl.RGBA, gl.UNSIGNED_BYTE, __classPrivateFieldGet(this, _JPEGGlitchEffect_raw, "f"));
419
- gl.bindFramebuffer(gl.FRAMEBUFFER, null);
420
- __classPrivateFieldSet(this, _JPEGGlitchEffect_busy, true, "f");
421
- __classPrivateFieldSet(this, _JPEGGlitchEffect_dirty, false, "f");
422
- __classPrivateFieldSet(this, _JPEGGlitchEffect_lastGlitchTime, ctx.time, "f");
423
- // GL framebuffers are bottom-up; flip rows so the canvas is upright.
424
- // Force opaque alpha — JPEG has no alpha channel.
425
- const data = imageData.data;
426
- const rowBytes = w * 4;
427
- for (let y = 0; y < h; y++) {
428
- const srcRow = (h - 1 - y) * rowBytes;
429
- data.set(__classPrivateFieldGet(this, _JPEGGlitchEffect_raw, "f").subarray(srcRow, srcRow + rowBytes), y * rowBytes);
327
+ #shouldGlitch(ctx) {
328
+ if (this.#busy) {
329
+ return false;
330
+ }
331
+ const speed = this.params.speed;
332
+ if (speed > 0) {
333
+ return ctx.time - this.#lastGlitchTime >= 1 / speed;
334
+ }
335
+ return this.#dirty;
430
336
  }
431
- for (let i = 3; i < data.length; i += 4) {
432
- data[i] = 255;
337
+ #resize(ctx, w, h) {
338
+ this.#w = w;
339
+ this.#h = h;
340
+ this.#raw = new Uint8Array(w * h * 4);
341
+ this.#imageData = new ImageData(w, h);
342
+ if (this.#srcCanvas) {
343
+ this.#srcCanvas.width = w;
344
+ this.#srcCanvas.height = h;
345
+ }
346
+ if (this.#outCanvas) {
347
+ this.#outCanvas.width = w;
348
+ this.#outCanvas.height = h;
349
+ }
350
+ // The readback RT is sized to the processing resolution, so recreate
351
+ // it (explicit-size RTs don't auto-track the element).
352
+ this.#readRT?.dispose();
353
+ this.#readRT = ctx.createRenderTarget({ size: [w, h] });
354
+ this.#hasResult = false;
355
+ this.#dirty = true;
356
+ this.#busy = false;
357
+ this.#uploadPending = false;
358
+ this.#generation++;
433
359
  }
434
- // Static glitch (speed 0) stays on variant 0 so it's reproducible;
435
- // animated glitch advances the variant each round.
436
- const variant = this.params.speed > 0 ? __classPrivateFieldGet(this, _JPEGGlitchEffect_glitchCount, "f") : 0;
437
- __classPrivateFieldSet(this, _JPEGGlitchEffect_glitchCount, (_a = __classPrivateFieldGet(this, _JPEGGlitchEffect_glitchCount, "f"), _a++, _a), "f");
438
- const { quality, seed, iterations, randomFlip, vertical } = this.params;
439
- // One seeded sequence drives both the flip decision and the byte
440
- // corruption, so the whole glitch is reproducible from the seed.
441
- const rng = makeRng(seed, variant);
442
- const flip = randomFlip && rng() < 0.5;
443
- srcCtx.putImageData(imageData, 0, 0);
444
- // Rotate the source before corrupting: 180° (flip) balances the tear
445
- // direction, -90° (vertical, i.e. 270°) turns the tears sideways.
446
- // The angles compose; runGlitch rotates the decoded output back.
447
- const srcAngle = ((flip ? 180 : 0) + (vertical ? 270 : 0)) % 360;
448
- const swap = vertical; // a quarter turn swaps width and height
449
- const encW = swap ? h : w;
450
- const encH = swap ? w : h;
451
- encCanvas.width = encW;
452
- encCanvas.height = encH;
453
- drawRotated(encCtx, srcCanvas, w, h, srcAngle, encW, encH);
454
- void __classPrivateFieldGet(this, _JPEGGlitchEffect_instances, "m", _JPEGGlitchEffect_runGlitch).call(this, encCanvas, w, h, quality, iterations, srcAngle, rng, __classPrivateFieldGet(this, _JPEGGlitchEffect_generation, "f"));
455
- }, _JPEGGlitchEffect_uploadOutCanvas = function _JPEGGlitchEffect_uploadOutCanvas(ctx) {
456
- const gl = ctx.gl;
457
- if (!__classPrivateFieldGet(this, _JPEGGlitchEffect_outGLTex, "f") || !__classPrivateFieldGet(this, _JPEGGlitchEffect_outCanvas, "f")) {
458
- return;
360
+ #startGlitch(ctx) {
361
+ const w = this.#w;
362
+ const h = this.#h;
363
+ const srcCanvas = this.#srcCanvas;
364
+ const srcCtx = this.#srcCtx;
365
+ const encCanvas = this.#encCanvas;
366
+ const encCtx = this.#encCtx;
367
+ const imageData = this.#imageData;
368
+ if (!srcCanvas ||
369
+ !srcCtx ||
370
+ !encCanvas ||
371
+ !encCtx ||
372
+ !imageData ||
373
+ !this.#readRT) {
374
+ return;
375
+ }
376
+ // Read the element capture back to the CPU. The draw leaves the
377
+ // readback RT's framebuffer bound, so readPixels reads from it.
378
+ captureContent(ctx, this.#readRT);
379
+ const gl = ctx.gl;
380
+ gl.readPixels(0, 0, w, h, gl.RGBA, gl.UNSIGNED_BYTE, this.#raw);
381
+ gl.bindFramebuffer(gl.FRAMEBUFFER, null);
382
+ this.#busy = true;
383
+ this.#dirty = false;
384
+ this.#lastGlitchTime = ctx.time;
385
+ // GL framebuffers are bottom-up; flip rows so the canvas is upright.
386
+ // Force opaque alpha — JPEG has no alpha channel.
387
+ const data = imageData.data;
388
+ const rowBytes = w * 4;
389
+ for (let y = 0; y < h; y++) {
390
+ const srcRow = (h - 1 - y) * rowBytes;
391
+ data.set(this.#raw.subarray(srcRow, srcRow + rowBytes), y * rowBytes);
392
+ }
393
+ for (let i = 3; i < data.length; i += 4) {
394
+ data[i] = 255;
395
+ }
396
+ // Static glitch (speed 0) stays on variant 0 so it's reproducible;
397
+ // animated glitch advances the variant each round.
398
+ const variant = this.params.speed > 0 ? this.#glitchCount : 0;
399
+ this.#glitchCount++;
400
+ const { quality, seed, iterations, randomFlip, vertical } = this.params;
401
+ // One seeded sequence drives both the flip decision and the byte
402
+ // corruption, so the whole glitch is reproducible from the seed.
403
+ const rng = makeRng(seed, variant);
404
+ const flip = randomFlip && rng() < 0.5;
405
+ srcCtx.putImageData(imageData, 0, 0);
406
+ // Rotate the source before corrupting: 180° (flip) balances the tear
407
+ // direction, -90° (vertical, i.e. 270°) turns the tears sideways.
408
+ // The angles compose; runGlitch rotates the decoded output back.
409
+ const srcAngle = ((flip ? 180 : 0) + (vertical ? 270 : 0)) % 360;
410
+ const swap = vertical; // a quarter turn swaps width and height
411
+ const encW = swap ? h : w;
412
+ const encH = swap ? w : h;
413
+ encCanvas.width = encW;
414
+ encCanvas.height = encH;
415
+ drawRotated(encCtx, srcCanvas, w, h, srcAngle, encW, encH);
416
+ void this.#runGlitch(encCanvas, w, h, quality, iterations, srcAngle, rng, this.#generation);
459
417
  }
460
- gl.bindTexture(gl.TEXTURE_2D, __classPrivateFieldGet(this, _JPEGGlitchEffect_outGLTex, "f"));
461
- gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
462
- gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);
463
- gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, __classPrivateFieldGet(this, _JPEGGlitchEffect_outCanvas, "f"));
464
- gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);
465
- gl.bindTexture(gl.TEXTURE_2D, null);
466
- __classPrivateFieldSet(this, _JPEGGlitchEffect_uploadPending, false, "f");
467
- }, _JPEGGlitchEffect_runGlitch = async function _JPEGGlitchEffect_runGlitch(canvas, w, h, quality, iterations, srcAngle, rng, generation) {
468
- var _a;
469
- try {
470
- const blob = await canvasToJpegBlob(canvas, quality);
471
- const bytes = new Uint8Array(await blob.arrayBuffer());
472
- const header = jpegScanStart(bytes);
473
- glitchBytes(bytes, header, iterations, rng);
474
- const bmp = await createImageBitmap(new Blob([bytes], { type: "image/jpeg" }));
475
- // Discard if a resize happened while we were encoding/decoding.
476
- if (generation === __classPrivateFieldGet(this, _JPEGGlitchEffect_generation, "f") && __classPrivateFieldGet(this, _JPEGGlitchEffect_outCtx, "f")) {
477
- // Rotate the decoded glitch back upright (inverse of the
478
- // source rotation) into the w×h output canvas.
479
- const outAngle = (360 - srcAngle) % 360;
480
- drawRotated(__classPrivateFieldGet(this, _JPEGGlitchEffect_outCtx, "f"), bmp, bmp.width, bmp.height, outAngle, w, h);
481
- __classPrivateFieldSet(this, _JPEGGlitchEffect_hasResult, true, "f");
482
- __classPrivateFieldSet(this, _JPEGGlitchEffect_producedFrames, (_a = __classPrivateFieldGet(this, _JPEGGlitchEffect_producedFrames, "f"), _a++, _a), "f");
483
- // New pixels — upload to the display texture on the next draw.
484
- __classPrivateFieldSet(this, _JPEGGlitchEffect_uploadPending, true, "f");
418
+ // Upload the decoded glitch canvas into our display texture. FLIP_Y
419
+ // matches the framework's canvas-as-image convention so FRAG_OUTPUT
420
+ // (which samples by uvContent) shows it upright.
421
+ #uploadOutCanvas(ctx) {
422
+ const gl = ctx.gl;
423
+ if (!this.#outGLTex || !this.#outCanvas) {
424
+ return;
485
425
  }
486
- bmp.close();
426
+ gl.bindTexture(gl.TEXTURE_2D, this.#outGLTex);
427
+ gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
428
+ gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);
429
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, this.#outCanvas);
430
+ gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);
431
+ gl.bindTexture(gl.TEXTURE_2D, null);
432
+ this.#uploadPending = false;
487
433
  }
488
- catch {
489
- // Corrupted past the decoder's tolerance — keep the last good
490
- // frame and try again on the next tick.
434
+ async #runGlitch(canvas, w, h, quality, iterations, srcAngle, rng, generation) {
435
+ try {
436
+ const blob = await canvasToJpegBlob(canvas, quality);
437
+ const bytes = new Uint8Array(await blob.arrayBuffer());
438
+ const header = jpegScanStart(bytes);
439
+ glitchBytes(bytes, header, iterations, rng);
440
+ const bmp = await createImageBitmap(new Blob([bytes], { type: "image/jpeg" }));
441
+ // Discard if a resize happened while we were encoding/decoding.
442
+ if (generation === this.#generation && this.#outCtx) {
443
+ // Rotate the decoded glitch back upright (inverse of the
444
+ // source rotation) into the w×h output canvas.
445
+ const outAngle = (360 - srcAngle) % 360;
446
+ drawRotated(this.#outCtx, bmp, bmp.width, bmp.height, outAngle, w, h);
447
+ this.#hasResult = true;
448
+ this.#producedFrames++;
449
+ // New pixels — upload to the display texture on the next draw.
450
+ this.#uploadPending = true;
451
+ }
452
+ bmp.close();
453
+ }
454
+ catch {
455
+ // Corrupted past the decoder's tolerance — keep the last good
456
+ // frame and try again on the next tick.
457
+ }
458
+ finally {
459
+ if (generation === this.#generation) {
460
+ this.#busy = false;
461
+ }
462
+ }
491
463
  }
492
- finally {
493
- if (generation === __classPrivateFieldGet(this, _JPEGGlitchEffect_generation, "f")) {
494
- __classPrivateFieldSet(this, _JPEGGlitchEffect_busy, false, "f");
464
+ dispose() {
465
+ this.#restoreUnsub?.();
466
+ this.#restoreUnsub = null;
467
+ if (this.#gl && this.#outGLTex) {
468
+ this.#gl.deleteTexture(this.#outGLTex);
495
469
  }
470
+ this.#outGLTex = null;
471
+ this.#gl = null;
472
+ this.#uploadPending = false;
473
+ this.#readRT?.dispose();
474
+ this.#readRT = null;
475
+ this.#outTex = null;
476
+ this.#srcCanvas = null;
477
+ this.#srcCtx = null;
478
+ this.#encCanvas = null;
479
+ this.#encCtx = null;
480
+ this.#outCanvas = null;
481
+ this.#outCtx = null;
482
+ this.#imageData = null;
483
+ this.#raw = new Uint8Array(0);
484
+ this.#hasResult = false;
485
+ this.#busy = false;
486
+ // Forget the processed size so a re-init reallocates the readback RT
487
+ // (which dispose just freed) instead of mistaking it for current.
488
+ this.#w = 0;
489
+ this.#h = 0;
490
+ // Invalidate any in-flight round trip.
491
+ this.#generation++;
496
492
  }
497
- };
493
+ }
498
494
  //# sourceMappingURL=jpeg-glitch.js.map