@vectojs/three 0.1.4 → 0.1.5

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.
@@ -51,6 +51,16 @@ export declare class ThreeRenderer implements IRenderer {
51
51
  closePath(): void;
52
52
  arc(x: number, y: number, radius: number, startAngle: number, endAngle: number, counterclockwise?: boolean): void;
53
53
  roundRect(x: number, y: number, width: number, height: number, radii: number | number[]): void;
54
+ /**
55
+ * Textures cached per drawImage source (identity-keyed). A frame that
56
+ * redraws the same image no longer re-uploads it. Mutable sources (a canvas
57
+ * you repaint) must call {@link invalidateImage} after changing pixels.
58
+ * Entries are skipped by the per-frame disposer and released on
59
+ * invalidation or {@link dispose}.
60
+ */
61
+ private imageTextureCache;
62
+ /** Drop the cached texture for a source whose pixels changed. */
63
+ invalidateImage(source: CanvasImageSource): void;
54
64
  drawImage(source: CanvasImageSource, dx: number, dy: number, dw: number, dh: number): void;
55
65
  fill(colorOrGradient: string | any): void;
56
66
  private fillSolidShape;
package/dist/index.js CHANGED
@@ -310,15 +310,35 @@ var ThreeRenderer = class {
310
310
  this.currentPath.lineTo(x, y + r_tl);
311
311
  this.currentPath.arc(x + r_tl, y + r_tl, r_tl, Math.PI, -Math.PI / 2, false);
312
312
  }
313
+ /**
314
+ * Textures cached per drawImage source (identity-keyed). A frame that
315
+ * redraws the same image no longer re-uploads it. Mutable sources (a canvas
316
+ * you repaint) must call {@link invalidateImage} after changing pixels.
317
+ * Entries are skipped by the per-frame disposer and released on
318
+ * invalidation or {@link dispose}.
319
+ */
320
+ imageTextureCache = /* @__PURE__ */ new Map();
321
+ /** Drop the cached texture for a source whose pixels changed. */
322
+ invalidateImage(source) {
323
+ const cached = this.imageTextureCache.get(source);
324
+ if (cached) {
325
+ cached.dispose();
326
+ this.imageTextureCache.delete(source);
327
+ }
328
+ }
313
329
  drawImage(source, dx, dy, dw, dh) {
314
- let texture;
315
- if (source instanceof HTMLImageElement || source instanceof HTMLCanvasElement || typeof ImageBitmap !== "undefined" && source instanceof ImageBitmap) {
316
- texture = new THREE.CanvasTexture(source);
317
- } else {
318
- texture = new THREE.Texture(source);
319
- texture.needsUpdate = true;
330
+ let texture = this.imageTextureCache.get(source);
331
+ if (!texture) {
332
+ if (source instanceof HTMLImageElement || source instanceof HTMLCanvasElement || typeof ImageBitmap !== "undefined" && source instanceof ImageBitmap) {
333
+ texture = new THREE.CanvasTexture(source);
334
+ } else {
335
+ texture = new THREE.Texture(source);
336
+ texture.needsUpdate = true;
337
+ }
338
+ texture.minFilter = THREE.LinearFilter;
339
+ texture.userData.vectoCached = true;
340
+ this.imageTextureCache.set(source, texture);
320
341
  }
321
- texture.minFilter = THREE.LinearFilter;
322
342
  const material = new THREE.MeshBasicMaterial({
323
343
  map: texture,
324
344
  transparent: true,
@@ -616,6 +636,10 @@ var ThreeRenderer = class {
616
636
  entry.texture.dispose();
617
637
  }
618
638
  this.textTextureCache.clear();
639
+ for (const texture of this.imageTextureCache.values()) {
640
+ texture.dispose();
641
+ }
642
+ this.imageTextureCache.clear();
619
643
  this.currentPath = null;
620
644
  this.stack.length = 0;
621
645
  this.alphaStack.length = 0;
package/dist/index.mjs CHANGED
@@ -272,15 +272,35 @@ var ThreeRenderer = class {
272
272
  this.currentPath.lineTo(x, y + r_tl);
273
273
  this.currentPath.arc(x + r_tl, y + r_tl, r_tl, Math.PI, -Math.PI / 2, false);
274
274
  }
275
+ /**
276
+ * Textures cached per drawImage source (identity-keyed). A frame that
277
+ * redraws the same image no longer re-uploads it. Mutable sources (a canvas
278
+ * you repaint) must call {@link invalidateImage} after changing pixels.
279
+ * Entries are skipped by the per-frame disposer and released on
280
+ * invalidation or {@link dispose}.
281
+ */
282
+ imageTextureCache = /* @__PURE__ */ new Map();
283
+ /** Drop the cached texture for a source whose pixels changed. */
284
+ invalidateImage(source) {
285
+ const cached = this.imageTextureCache.get(source);
286
+ if (cached) {
287
+ cached.dispose();
288
+ this.imageTextureCache.delete(source);
289
+ }
290
+ }
275
291
  drawImage(source, dx, dy, dw, dh) {
276
- let texture;
277
- if (source instanceof HTMLImageElement || source instanceof HTMLCanvasElement || typeof ImageBitmap !== "undefined" && source instanceof ImageBitmap) {
278
- texture = new THREE.CanvasTexture(source);
279
- } else {
280
- texture = new THREE.Texture(source);
281
- texture.needsUpdate = true;
292
+ let texture = this.imageTextureCache.get(source);
293
+ if (!texture) {
294
+ if (source instanceof HTMLImageElement || source instanceof HTMLCanvasElement || typeof ImageBitmap !== "undefined" && source instanceof ImageBitmap) {
295
+ texture = new THREE.CanvasTexture(source);
296
+ } else {
297
+ texture = new THREE.Texture(source);
298
+ texture.needsUpdate = true;
299
+ }
300
+ texture.minFilter = THREE.LinearFilter;
301
+ texture.userData.vectoCached = true;
302
+ this.imageTextureCache.set(source, texture);
282
303
  }
283
- texture.minFilter = THREE.LinearFilter;
284
304
  const material = new THREE.MeshBasicMaterial({
285
305
  map: texture,
286
306
  transparent: true,
@@ -578,6 +598,10 @@ var ThreeRenderer = class {
578
598
  entry.texture.dispose();
579
599
  }
580
600
  this.textTextureCache.clear();
601
+ for (const texture of this.imageTextureCache.values()) {
602
+ texture.dispose();
603
+ }
604
+ this.imageTextureCache.clear();
581
605
  this.currentPath = null;
582
606
  this.stack.length = 0;
583
607
  this.alphaStack.length = 0;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vectojs/three",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },