modern-canvas 0.8.2 → 0.8.3

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.
package/dist/index.cjs CHANGED
@@ -876,14 +876,14 @@ class Input extends modernIdoc.EventEmitter {
876
876
  let IID = 0;
877
877
  class CoreObject extends modernIdoc.EventEmitter {
878
878
  instanceId = ++IID;
879
- _customPropertyAccessor;
879
+ _propertyAccessor;
880
880
  _properties = /* @__PURE__ */ new Map();
881
881
  _updatedProperties = /* @__PURE__ */ new Map();
882
882
  _changedProperties = /* @__PURE__ */ new Set();
883
883
  _updatingPromise = Promise.resolve();
884
884
  _updating = false;
885
885
  useCustomPropertyAccessor(accessor) {
886
- this._customPropertyAccessor = accessor;
886
+ this._propertyAccessor = accessor;
887
887
  this.getPropertyDeclarations().forEach((declaration, key) => {
888
888
  const newValue = accessor.get(key, () => void 0);
889
889
  const oldValue = this._properties.get(key);
@@ -907,15 +907,15 @@ class CoreObject extends modernIdoc.EventEmitter {
907
907
  if (context?.declaration.protected) {
908
908
  return this[context.internalKey];
909
909
  } else {
910
- return this._customPropertyAccessor ? this._customPropertyAccessor.get(key, () => this._properties.get(key)) : this._properties.get(key);
910
+ return this._propertyAccessor ? this._propertyAccessor.get(key, () => this._properties.get(key)) : this._properties.get(key);
911
911
  }
912
912
  }
913
913
  setter(key, value, context) {
914
914
  if (context?.declaration.protected) {
915
915
  this[context.internalKey] = value;
916
916
  } else {
917
- if (this._customPropertyAccessor) {
918
- this._customPropertyAccessor.set(key, value);
917
+ if (this._propertyAccessor) {
918
+ this._propertyAccessor.set(key, value);
919
919
  }
920
920
  this._properties.set(key, value);
921
921
  }
@@ -7265,7 +7265,7 @@ exports.Node2D = class Node2D extends exports.CanvasItem {
7265
7265
  super._updateProperty(key, value, oldValue, declaration);
7266
7266
  switch (key) {
7267
7267
  case "rotation":
7268
- this.requestRelayout();
7268
+ this.updateGlobalTransform();
7269
7269
  break;
7270
7270
  }
7271
7271
  }
@@ -9357,6 +9357,45 @@ exports.ZoomBlurEffect = __decorateClass$t([
9357
9357
  customNode("ZoomBlurEffect")
9358
9358
  ], exports.ZoomBlurEffect);
9359
9359
 
9360
+ function getDrawOptions(fill, size) {
9361
+ let disableWrapMode = false;
9362
+ const { width, height } = size;
9363
+ const uvTransform = new Transform2D().scale(1 / width, 1 / height);
9364
+ if (fill.cropRect) {
9365
+ const {
9366
+ left = 0,
9367
+ top = 0,
9368
+ right = 0,
9369
+ bottom = 0
9370
+ } = fill.cropRect;
9371
+ uvTransform.scale(
9372
+ Math.abs(1 - (left + right)),
9373
+ Math.abs(1 - (top + bottom))
9374
+ ).translate(left, top);
9375
+ disableWrapMode = true;
9376
+ }
9377
+ if (fill.tile) {
9378
+ const {
9379
+ translateX = 0,
9380
+ translateY = 0,
9381
+ scaleX = 1,
9382
+ scaleY = 1
9383
+ // flip, TODO
9384
+ // alignment, TODO
9385
+ } = fill.tile;
9386
+ uvTransform.translate(-translateX / width, -translateY / height).scale(1 / scaleX, 1 / scaleY);
9387
+ disableWrapMode = true;
9388
+ } else if (fill.stretchRect) {
9389
+ const { left = 0, top = 0, right = 0, bottom = 0 } = fill.stretchRect;
9390
+ uvTransform.scale(
9391
+ Math.abs(1 - (-left + -right)),
9392
+ Math.abs(1 - (-top + -bottom))
9393
+ ).translate(-left, -top);
9394
+ disableWrapMode = true;
9395
+ }
9396
+ return { disableWrapMode, uvTransform };
9397
+ }
9398
+
9360
9399
  var __defProp$l = Object.defineProperty;
9361
9400
  var __decorateClass$s = (decorators, target, key, kind) => {
9362
9401
  var result = void 0 ;
@@ -9423,47 +9462,12 @@ class BaseElement2DFill extends CoreObject {
9423
9462
  this.enabled && (this._texture || this.color)
9424
9463
  );
9425
9464
  }
9426
- _getDrawOptions() {
9427
- let disableWrapMode = false;
9428
- const { width, height } = this.parent.size;
9429
- const uvTransform = new Transform2D().scale(1 / width, 1 / height);
9430
- if (this.cropRect) {
9431
- const {
9432
- left = 0,
9433
- top = 0,
9434
- right = 0,
9435
- bottom = 0
9436
- } = this.cropRect;
9437
- uvTransform.scale(
9438
- Math.abs(1 - (left + right)),
9439
- Math.abs(1 - (top + bottom))
9440
- ).translate(left, top);
9441
- disableWrapMode = true;
9442
- }
9443
- if (this.tile) {
9444
- const {
9445
- translateX = 0,
9446
- translateY = 0,
9447
- scaleX = 1,
9448
- scaleY = 1
9449
- // flip, TODO
9450
- // alignment, TODO
9451
- } = this.tile;
9452
- uvTransform.translate(-translateX / width, -translateY / height).scale(1 / scaleX, 1 / scaleY);
9453
- disableWrapMode = true;
9454
- } else if (this.stretchRect) {
9455
- const { left = 0, top = 0, right = 0, bottom = 0 } = this.stretchRect;
9456
- uvTransform.scale(
9457
- Math.abs(1 - (-left + -right)),
9458
- Math.abs(1 - (-top + -bottom))
9459
- ).translate(-left, -top);
9460
- disableWrapMode = true;
9461
- }
9462
- return { disableWrapMode, uvTransform };
9463
- }
9464
9465
  draw() {
9465
9466
  const ctx = this.parent.context;
9466
- const { uvTransform, disableWrapMode } = this._getDrawOptions();
9467
+ const { uvTransform, disableWrapMode } = getDrawOptions(
9468
+ this,
9469
+ this.parent.size
9470
+ );
9467
9471
  ctx.uvTransform = uvTransform;
9468
9472
  ctx.fillStyle = this._texture ?? this.color;
9469
9473
  ctx.fill({
@@ -9595,7 +9599,10 @@ class BaseElement2DOutline extends BaseElement2DFill {
9595
9599
  }
9596
9600
  draw() {
9597
9601
  const ctx = this.parent.context;
9598
- const { uvTransform, disableWrapMode } = this._getDrawOptions();
9602
+ const { uvTransform, disableWrapMode } = getDrawOptions(
9603
+ this,
9604
+ this.parent.size
9605
+ );
9599
9606
  ctx.lineWidth = this.width || 1;
9600
9607
  ctx.uvTransform = uvTransform;
9601
9608
  ctx.strokeStyle = this._texture ?? this.color;
@@ -9818,6 +9825,7 @@ class BaseElement2DText extends CoreObject {
9818
9825
  }
9819
9826
  base = new modernText.Text();
9820
9827
  measureResult;
9828
+ _textures = [];
9821
9829
  setProperties(properties) {
9822
9830
  return super.setProperties(
9823
9831
  modernIdoc.isNone(properties) ? void 0 : modernIdoc.normalizeText(properties)
@@ -9831,12 +9839,34 @@ class BaseElement2DText extends CoreObject {
9831
9839
  case "effects":
9832
9840
  case "measureDom":
9833
9841
  case "fonts":
9842
+ this.parent.requestRedraw();
9843
+ break;
9834
9844
  case "fill":
9845
+ this._updateTexture(0, value);
9846
+ break;
9835
9847
  case "outline":
9836
- this.parent.requestRedraw();
9848
+ this._updateTexture(1, value);
9837
9849
  break;
9838
9850
  }
9839
9851
  }
9852
+ async _updateTexture(index, fill) {
9853
+ this._textures[index] = await this._loadTexture(fill);
9854
+ this.parent.requestRedraw();
9855
+ }
9856
+ async _loadTexture(fill) {
9857
+ if (fill.linearGradient || fill.radialGradient) {
9858
+ return new GradientTexture(
9859
+ fill.linearGradient ?? fill.radialGradient,
9860
+ this.parent.size.width,
9861
+ this.parent.size.height
9862
+ );
9863
+ } else if (!modernIdoc.isNone(fill.image)) {
9864
+ this.parent.tree?.log(`load image ${fill.image}`);
9865
+ return await assets.texture.load(fill.image);
9866
+ } else {
9867
+ return void 0;
9868
+ }
9869
+ }
9840
9870
  setContent(content) {
9841
9871
  this.content = modernIdoc.normalizeTextContent(content);
9842
9872
  }
@@ -9864,9 +9894,45 @@ class BaseElement2DText extends CoreObject {
9864
9894
  this.base.update();
9865
9895
  this.base.pathSets.forEach((pathSet) => {
9866
9896
  pathSet.paths.forEach((path) => {
9867
- ctx.addPath(path);
9868
- ctx.style = { ...path.style };
9869
- ctx.fill();
9897
+ if (path.style.stroke && !modernIdoc.isNone(path.style.stroke)) {
9898
+ if (typeof path.style.stroke === "object") {
9899
+ const outline = path.style.stroke;
9900
+ if (outline.enabled !== false && (this._textures[0] || outline.color) && (outline.width === void 0 || outline.width)) {
9901
+ const { uvTransform, disableWrapMode } = getDrawOptions(outline, this.parent.size);
9902
+ ctx.addPath(path);
9903
+ ctx.style = { ...path.style };
9904
+ ctx.lineWidth = outline.width || 1;
9905
+ ctx.uvTransform = uvTransform;
9906
+ ctx.strokeStyle = this._textures[0] ?? outline.color;
9907
+ ctx.lineCap = outline.lineCap;
9908
+ ctx.lineJoin = outline.lineJoin;
9909
+ ctx.stroke({ disableWrapMode });
9910
+ }
9911
+ } else {
9912
+ ctx.addPath(path);
9913
+ ctx.style = { ...path.style };
9914
+ ctx.stroke();
9915
+ }
9916
+ }
9917
+ if (path.style.fill && !modernIdoc.isNone(path.style.fill)) {
9918
+ if (typeof path.style.fill === "object") {
9919
+ const fill = path.style.fill;
9920
+ if (fill.enabled !== false && (this._textures[1] || fill.color)) {
9921
+ const { uvTransform, disableWrapMode } = getDrawOptions(fill, this.parent.size);
9922
+ ctx.addPath(path);
9923
+ ctx.style = { ...path.style };
9924
+ ctx.uvTransform = uvTransform;
9925
+ ctx.fillStyle = this._textures[1] ?? fill.color;
9926
+ ctx.fill({
9927
+ disableWrapMode
9928
+ });
9929
+ }
9930
+ } else {
9931
+ ctx.addPath(path);
9932
+ ctx.style = { ...path.style };
9933
+ ctx.fill();
9934
+ }
9935
+ }
9870
9936
  });
9871
9937
  });
9872
9938
  }
@@ -14261,7 +14327,11 @@ class Engine extends SceneTree {
14261
14327
  return this.renderer.toPixels();
14262
14328
  }
14263
14329
  toImageData() {
14264
- return new ImageData(this.toPixels(), this.gl.drawingBufferWidth, this.gl.drawingBufferHeight);
14330
+ return new ImageData(
14331
+ this.toPixels(),
14332
+ this.gl.drawingBufferWidth,
14333
+ this.gl.drawingBufferHeight
14334
+ );
14265
14335
  }
14266
14336
  toCanvas2D() {
14267
14337
  const imageData = this.toImageData();
package/dist/index.d.cts CHANGED
@@ -260,7 +260,7 @@ interface CoreObject {
260
260
  }
261
261
  declare class CoreObject extends EventEmitter implements Required<ReactiveObject> {
262
262
  readonly instanceId: number;
263
- protected _customPropertyAccessor?: CustomPropertyAccessor;
263
+ protected _propertyAccessor?: CustomPropertyAccessor;
264
264
  protected _properties: Map<string, unknown>;
265
265
  protected _updatedProperties: Map<string, unknown>;
266
266
  protected _changedProperties: Set<string>;
@@ -894,7 +894,7 @@ declare class WebGLRenderer extends Renderer {
894
894
  reset(): void;
895
895
  flush(): void;
896
896
  free(): void;
897
- toPixels(x?: number, y?: number, width?: number, height?: number): Uint8ClampedArray;
897
+ toPixels(x?: number, y?: number, width?: number, height?: number): Uint8ClampedArray<ArrayBuffer>;
898
898
  }
899
899
 
900
900
  interface Renderable {
@@ -2015,10 +2015,6 @@ declare class BaseElement2DFill extends CoreObject {
2015
2015
  loadTexture(): Promise<Texture2D | undefined>;
2016
2016
  protected _updateTexture(): Promise<void>;
2017
2017
  canDraw(): boolean;
2018
- protected _getDrawOptions(): {
2019
- disableWrapMode: boolean;
2020
- uvTransform: Transform2D;
2021
- };
2022
2018
  draw(): void;
2023
2019
  }
2024
2020
 
@@ -2098,9 +2094,12 @@ declare class BaseElement2DText extends CoreObject {
2098
2094
  fonts: Text['fonts'];
2099
2095
  readonly base: Text;
2100
2096
  measureResult?: MeasureResult;
2097
+ protected _textures: (Texture2D | undefined)[];
2101
2098
  constructor(parent: BaseElement2D);
2102
2099
  setProperties(properties?: Text$1): this;
2103
2100
  protected _updateProperty(key: string, value: any, oldValue: any, declaration?: PropertyDeclaration): void;
2101
+ protected _updateTexture(index: number, fill: NormalizedFill): Promise<void>;
2102
+ protected _loadTexture(fill: NormalizedFill): Promise<Texture2D | undefined>;
2104
2103
  setContent(content: TextContent): void;
2105
2104
  measure(): MeasureResult;
2106
2105
  updateMeasure(): this;
@@ -3369,7 +3368,7 @@ declare class Engine extends SceneTree {
3369
3368
  render(delta?: number): void;
3370
3369
  start(): Promise<void>;
3371
3370
  free(): void;
3372
- toPixels(): Uint8ClampedArray;
3371
+ toPixels(): Uint8ClampedArray<ArrayBuffer>;
3373
3372
  toImageData(): ImageData;
3374
3373
  toCanvas2D(): HTMLCanvasElement;
3375
3374
  }
package/dist/index.d.mts CHANGED
@@ -260,7 +260,7 @@ interface CoreObject {
260
260
  }
261
261
  declare class CoreObject extends EventEmitter implements Required<ReactiveObject> {
262
262
  readonly instanceId: number;
263
- protected _customPropertyAccessor?: CustomPropertyAccessor;
263
+ protected _propertyAccessor?: CustomPropertyAccessor;
264
264
  protected _properties: Map<string, unknown>;
265
265
  protected _updatedProperties: Map<string, unknown>;
266
266
  protected _changedProperties: Set<string>;
@@ -894,7 +894,7 @@ declare class WebGLRenderer extends Renderer {
894
894
  reset(): void;
895
895
  flush(): void;
896
896
  free(): void;
897
- toPixels(x?: number, y?: number, width?: number, height?: number): Uint8ClampedArray;
897
+ toPixels(x?: number, y?: number, width?: number, height?: number): Uint8ClampedArray<ArrayBuffer>;
898
898
  }
899
899
 
900
900
  interface Renderable {
@@ -2015,10 +2015,6 @@ declare class BaseElement2DFill extends CoreObject {
2015
2015
  loadTexture(): Promise<Texture2D | undefined>;
2016
2016
  protected _updateTexture(): Promise<void>;
2017
2017
  canDraw(): boolean;
2018
- protected _getDrawOptions(): {
2019
- disableWrapMode: boolean;
2020
- uvTransform: Transform2D;
2021
- };
2022
2018
  draw(): void;
2023
2019
  }
2024
2020
 
@@ -2098,9 +2094,12 @@ declare class BaseElement2DText extends CoreObject {
2098
2094
  fonts: Text['fonts'];
2099
2095
  readonly base: Text;
2100
2096
  measureResult?: MeasureResult;
2097
+ protected _textures: (Texture2D | undefined)[];
2101
2098
  constructor(parent: BaseElement2D);
2102
2099
  setProperties(properties?: Text$1): this;
2103
2100
  protected _updateProperty(key: string, value: any, oldValue: any, declaration?: PropertyDeclaration): void;
2101
+ protected _updateTexture(index: number, fill: NormalizedFill): Promise<void>;
2102
+ protected _loadTexture(fill: NormalizedFill): Promise<Texture2D | undefined>;
2104
2103
  setContent(content: TextContent): void;
2105
2104
  measure(): MeasureResult;
2106
2105
  updateMeasure(): this;
@@ -3369,7 +3368,7 @@ declare class Engine extends SceneTree {
3369
3368
  render(delta?: number): void;
3370
3369
  start(): Promise<void>;
3371
3370
  free(): void;
3372
- toPixels(): Uint8ClampedArray;
3371
+ toPixels(): Uint8ClampedArray<ArrayBuffer>;
3373
3372
  toImageData(): ImageData;
3374
3373
  toCanvas2D(): HTMLCanvasElement;
3375
3374
  }
package/dist/index.d.ts CHANGED
@@ -260,7 +260,7 @@ interface CoreObject {
260
260
  }
261
261
  declare class CoreObject extends EventEmitter implements Required<ReactiveObject> {
262
262
  readonly instanceId: number;
263
- protected _customPropertyAccessor?: CustomPropertyAccessor;
263
+ protected _propertyAccessor?: CustomPropertyAccessor;
264
264
  protected _properties: Map<string, unknown>;
265
265
  protected _updatedProperties: Map<string, unknown>;
266
266
  protected _changedProperties: Set<string>;
@@ -894,7 +894,7 @@ declare class WebGLRenderer extends Renderer {
894
894
  reset(): void;
895
895
  flush(): void;
896
896
  free(): void;
897
- toPixels(x?: number, y?: number, width?: number, height?: number): Uint8ClampedArray;
897
+ toPixels(x?: number, y?: number, width?: number, height?: number): Uint8ClampedArray<ArrayBuffer>;
898
898
  }
899
899
 
900
900
  interface Renderable {
@@ -2015,10 +2015,6 @@ declare class BaseElement2DFill extends CoreObject {
2015
2015
  loadTexture(): Promise<Texture2D | undefined>;
2016
2016
  protected _updateTexture(): Promise<void>;
2017
2017
  canDraw(): boolean;
2018
- protected _getDrawOptions(): {
2019
- disableWrapMode: boolean;
2020
- uvTransform: Transform2D;
2021
- };
2022
2018
  draw(): void;
2023
2019
  }
2024
2020
 
@@ -2098,9 +2094,12 @@ declare class BaseElement2DText extends CoreObject {
2098
2094
  fonts: Text['fonts'];
2099
2095
  readonly base: Text;
2100
2096
  measureResult?: MeasureResult;
2097
+ protected _textures: (Texture2D | undefined)[];
2101
2098
  constructor(parent: BaseElement2D);
2102
2099
  setProperties(properties?: Text$1): this;
2103
2100
  protected _updateProperty(key: string, value: any, oldValue: any, declaration?: PropertyDeclaration): void;
2101
+ protected _updateTexture(index: number, fill: NormalizedFill): Promise<void>;
2102
+ protected _loadTexture(fill: NormalizedFill): Promise<Texture2D | undefined>;
2104
2103
  setContent(content: TextContent): void;
2105
2104
  measure(): MeasureResult;
2106
2105
  updateMeasure(): this;
@@ -3369,7 +3368,7 @@ declare class Engine extends SceneTree {
3369
3368
  render(delta?: number): void;
3370
3369
  start(): Promise<void>;
3371
3370
  free(): void;
3372
- toPixels(): Uint8ClampedArray;
3371
+ toPixels(): Uint8ClampedArray<ArrayBuffer>;
3373
3372
  toImageData(): ImageData;
3374
3373
  toCanvas2D(): HTMLCanvasElement;
3375
3374
  }