modern-canvas 0.8.2 → 0.8.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.
package/dist/index.mjs CHANGED
@@ -870,14 +870,14 @@ class Input extends EventEmitter {
870
870
  let IID = 0;
871
871
  class CoreObject extends EventEmitter {
872
872
  instanceId = ++IID;
873
- _customPropertyAccessor;
873
+ _propertyAccessor;
874
874
  _properties = /* @__PURE__ */ new Map();
875
875
  _updatedProperties = /* @__PURE__ */ new Map();
876
876
  _changedProperties = /* @__PURE__ */ new Set();
877
877
  _updatingPromise = Promise.resolve();
878
878
  _updating = false;
879
879
  useCustomPropertyAccessor(accessor) {
880
- this._customPropertyAccessor = accessor;
880
+ this._propertyAccessor = accessor;
881
881
  this.getPropertyDeclarations().forEach((declaration, key) => {
882
882
  const newValue = accessor.get(key, () => void 0);
883
883
  const oldValue = this._properties.get(key);
@@ -901,15 +901,15 @@ class CoreObject extends EventEmitter {
901
901
  if (context?.declaration.protected) {
902
902
  return this[context.internalKey];
903
903
  } else {
904
- return this._customPropertyAccessor ? this._customPropertyAccessor.get(key, () => this._properties.get(key)) : this._properties.get(key);
904
+ return this._propertyAccessor ? this._propertyAccessor.get(key, () => this._properties.get(key)) : this._properties.get(key);
905
905
  }
906
906
  }
907
907
  setter(key, value, context) {
908
908
  if (context?.declaration.protected) {
909
909
  this[context.internalKey] = value;
910
910
  } else {
911
- if (this._customPropertyAccessor) {
912
- this._customPropertyAccessor.set(key, value);
911
+ if (this._propertyAccessor) {
912
+ this._propertyAccessor.set(key, value);
913
913
  }
914
914
  this._properties.set(key, value);
915
915
  }
@@ -7259,7 +7259,7 @@ let Node2D = class extends CanvasItem {
7259
7259
  super._updateProperty(key, value, oldValue, declaration);
7260
7260
  switch (key) {
7261
7261
  case "rotation":
7262
- this.requestRelayout();
7262
+ this.updateGlobalTransform();
7263
7263
  break;
7264
7264
  }
7265
7265
  }
@@ -9351,6 +9351,45 @@ ZoomBlurEffect = __decorateClass$t([
9351
9351
  customNode("ZoomBlurEffect")
9352
9352
  ], ZoomBlurEffect);
9353
9353
 
9354
+ function getDrawOptions(fill, size) {
9355
+ let disableWrapMode = false;
9356
+ const { width, height } = size;
9357
+ const uvTransform = new Transform2D().scale(1 / width, 1 / height);
9358
+ if (fill.cropRect) {
9359
+ const {
9360
+ left = 0,
9361
+ top = 0,
9362
+ right = 0,
9363
+ bottom = 0
9364
+ } = fill.cropRect;
9365
+ uvTransform.scale(
9366
+ Math.abs(1 - (left + right)),
9367
+ Math.abs(1 - (top + bottom))
9368
+ ).translate(left, top);
9369
+ disableWrapMode = true;
9370
+ }
9371
+ if (fill.tile) {
9372
+ const {
9373
+ translateX = 0,
9374
+ translateY = 0,
9375
+ scaleX = 1,
9376
+ scaleY = 1
9377
+ // flip, TODO
9378
+ // alignment, TODO
9379
+ } = fill.tile;
9380
+ uvTransform.translate(-translateX / width, -translateY / height).scale(1 / scaleX, 1 / scaleY);
9381
+ disableWrapMode = true;
9382
+ } else if (fill.stretchRect) {
9383
+ const { left = 0, top = 0, right = 0, bottom = 0 } = fill.stretchRect;
9384
+ uvTransform.scale(
9385
+ Math.abs(1 - (-left + -right)),
9386
+ Math.abs(1 - (-top + -bottom))
9387
+ ).translate(-left, -top);
9388
+ disableWrapMode = true;
9389
+ }
9390
+ return { disableWrapMode, uvTransform };
9391
+ }
9392
+
9354
9393
  var __defProp$l = Object.defineProperty;
9355
9394
  var __decorateClass$s = (decorators, target, key, kind) => {
9356
9395
  var result = void 0 ;
@@ -9417,47 +9456,12 @@ class BaseElement2DFill extends CoreObject {
9417
9456
  this.enabled && (this._texture || this.color)
9418
9457
  );
9419
9458
  }
9420
- _getDrawOptions() {
9421
- let disableWrapMode = false;
9422
- const { width, height } = this.parent.size;
9423
- const uvTransform = new Transform2D().scale(1 / width, 1 / height);
9424
- if (this.cropRect) {
9425
- const {
9426
- left = 0,
9427
- top = 0,
9428
- right = 0,
9429
- bottom = 0
9430
- } = this.cropRect;
9431
- uvTransform.scale(
9432
- Math.abs(1 - (left + right)),
9433
- Math.abs(1 - (top + bottom))
9434
- ).translate(left, top);
9435
- disableWrapMode = true;
9436
- }
9437
- if (this.tile) {
9438
- const {
9439
- translateX = 0,
9440
- translateY = 0,
9441
- scaleX = 1,
9442
- scaleY = 1
9443
- // flip, TODO
9444
- // alignment, TODO
9445
- } = this.tile;
9446
- uvTransform.translate(-translateX / width, -translateY / height).scale(1 / scaleX, 1 / scaleY);
9447
- disableWrapMode = true;
9448
- } else if (this.stretchRect) {
9449
- const { left = 0, top = 0, right = 0, bottom = 0 } = this.stretchRect;
9450
- uvTransform.scale(
9451
- Math.abs(1 - (-left + -right)),
9452
- Math.abs(1 - (-top + -bottom))
9453
- ).translate(-left, -top);
9454
- disableWrapMode = true;
9455
- }
9456
- return { disableWrapMode, uvTransform };
9457
- }
9458
9459
  draw() {
9459
9460
  const ctx = this.parent.context;
9460
- const { uvTransform, disableWrapMode } = this._getDrawOptions();
9461
+ const { uvTransform, disableWrapMode } = getDrawOptions(
9462
+ this,
9463
+ this.parent.size
9464
+ );
9461
9465
  ctx.uvTransform = uvTransform;
9462
9466
  ctx.fillStyle = this._texture ?? this.color;
9463
9467
  ctx.fill({
@@ -9589,7 +9593,10 @@ class BaseElement2DOutline extends BaseElement2DFill {
9589
9593
  }
9590
9594
  draw() {
9591
9595
  const ctx = this.parent.context;
9592
- const { uvTransform, disableWrapMode } = this._getDrawOptions();
9596
+ const { uvTransform, disableWrapMode } = getDrawOptions(
9597
+ this,
9598
+ this.parent.size
9599
+ );
9593
9600
  ctx.lineWidth = this.width || 1;
9594
9601
  ctx.uvTransform = uvTransform;
9595
9602
  ctx.strokeStyle = this._texture ?? this.color;
@@ -9726,7 +9733,7 @@ class BaseElement2DShape extends CoreObject {
9726
9733
  }
9727
9734
  if (!viewBox) {
9728
9735
  const bbox = this._path2DSet.getBoundingBox();
9729
- viewBox = bbox ? [bbox.x, bbox.y, bbox.width, bbox.height] : [0, 0, 1, 1];
9736
+ viewBox = bbox ? [bbox.x, bbox.y, bbox.width || 1, bbox.height || 1] : [0, 0, 1, 1];
9730
9737
  }
9731
9738
  const [x, y, w, h] = viewBox;
9732
9739
  this._path2DSet.paths.forEach((path) => {
@@ -9812,6 +9819,7 @@ class BaseElement2DText extends CoreObject {
9812
9819
  }
9813
9820
  base = new Text();
9814
9821
  measureResult;
9822
+ _textures = [];
9815
9823
  setProperties(properties) {
9816
9824
  return super.setProperties(
9817
9825
  isNone(properties) ? void 0 : normalizeText(properties)
@@ -9825,12 +9833,34 @@ class BaseElement2DText extends CoreObject {
9825
9833
  case "effects":
9826
9834
  case "measureDom":
9827
9835
  case "fonts":
9836
+ this.parent.requestRedraw();
9837
+ break;
9828
9838
  case "fill":
9839
+ this._updateTexture(0, value);
9840
+ break;
9829
9841
  case "outline":
9830
- this.parent.requestRedraw();
9842
+ this._updateTexture(1, value);
9831
9843
  break;
9832
9844
  }
9833
9845
  }
9846
+ async _updateTexture(index, fill) {
9847
+ this._textures[index] = await this._loadTexture(fill);
9848
+ this.parent.requestRedraw();
9849
+ }
9850
+ async _loadTexture(fill) {
9851
+ if (fill.linearGradient || fill.radialGradient) {
9852
+ return new GradientTexture(
9853
+ fill.linearGradient ?? fill.radialGradient,
9854
+ this.parent.size.width,
9855
+ this.parent.size.height
9856
+ );
9857
+ } else if (!isNone(fill.image)) {
9858
+ this.parent.tree?.log(`load image ${fill.image}`);
9859
+ return await assets.texture.load(fill.image);
9860
+ } else {
9861
+ return void 0;
9862
+ }
9863
+ }
9834
9864
  setContent(content) {
9835
9865
  this.content = normalizeTextContent(content);
9836
9866
  }
@@ -9853,14 +9883,61 @@ class BaseElement2DText extends CoreObject {
9853
9883
  this.enabled && !/^\s*$/.test(this.base.toString())
9854
9884
  );
9855
9885
  }
9886
+ _getVertTransform() {
9887
+ const parent = this.parent;
9888
+ if (parent.style.scaleX > 0 && parent.style.scaleY > 0) {
9889
+ return void 0;
9890
+ }
9891
+ const scale = parent.style.scaleX * parent.style.scaleY;
9892
+ const origin = parent.getTransformOrigin();
9893
+ return new Transform2D().translate(-origin.x, -origin.y).scale(scale > 0 ? 1 : -1, 1).translate(origin.x, origin.y);
9894
+ }
9856
9895
  draw() {
9857
9896
  const ctx = this.parent.context;
9858
9897
  this.base.update();
9859
9898
  this.base.pathSets.forEach((pathSet) => {
9860
9899
  pathSet.paths.forEach((path) => {
9861
- ctx.addPath(path);
9862
- ctx.style = { ...path.style };
9863
- ctx.fill();
9900
+ if (path.style.stroke && !isNone(path.style.stroke)) {
9901
+ if (typeof path.style.stroke === "object") {
9902
+ const outline = path.style.stroke;
9903
+ if (outline.enabled !== false && (this._textures[0] || outline.color) && (outline.width === void 0 || outline.width)) {
9904
+ const { uvTransform, disableWrapMode } = getDrawOptions(outline, this.parent.size);
9905
+ ctx.addPath(path);
9906
+ ctx.style = { ...path.style };
9907
+ ctx.lineWidth = outline.width || 1;
9908
+ ctx.uvTransform = uvTransform;
9909
+ ctx.strokeStyle = this._textures[0] ?? outline.color;
9910
+ ctx.lineCap = outline.lineCap;
9911
+ ctx.lineJoin = outline.lineJoin;
9912
+ ctx.vertTransform = this._getVertTransform();
9913
+ ctx.stroke({ disableWrapMode });
9914
+ }
9915
+ } else {
9916
+ ctx.addPath(path);
9917
+ ctx.style = { ...path.style };
9918
+ ctx.vertTransform = this._getVertTransform();
9919
+ ctx.stroke();
9920
+ }
9921
+ }
9922
+ if (path.style.fill && !isNone(path.style.fill)) {
9923
+ if (typeof path.style.fill === "object") {
9924
+ const fill = path.style.fill;
9925
+ if (fill.enabled !== false && (this._textures[1] || fill.color)) {
9926
+ const { uvTransform, disableWrapMode } = getDrawOptions(fill, this.parent.size);
9927
+ ctx.addPath(path);
9928
+ ctx.style = { ...path.style };
9929
+ ctx.uvTransform = uvTransform;
9930
+ ctx.fillStyle = this._textures[1] ?? fill.color;
9931
+ ctx.vertTransform = this._getVertTransform();
9932
+ ctx.fill({ disableWrapMode });
9933
+ }
9934
+ } else {
9935
+ ctx.addPath(path);
9936
+ ctx.style = { ...path.style };
9937
+ ctx.vertTransform = this._getVertTransform();
9938
+ ctx.fill();
9939
+ }
9940
+ }
9864
9941
  });
9865
9942
  });
9866
9943
  }
@@ -14255,7 +14332,11 @@ class Engine extends SceneTree {
14255
14332
  return this.renderer.toPixels();
14256
14333
  }
14257
14334
  toImageData() {
14258
- return new ImageData(this.toPixels(), this.gl.drawingBufferWidth, this.gl.drawingBufferHeight);
14335
+ return new ImageData(
14336
+ this.toPixels(),
14337
+ this.gl.drawingBufferWidth,
14338
+ this.gl.drawingBufferHeight
14339
+ );
14259
14340
  }
14260
14341
  toCanvas2D() {
14261
14342
  const imageData = this.toImageData();
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "modern-canvas",
3
3
  "type": "module",
4
- "version": "0.8.2",
4
+ "version": "0.8.4",
5
5
  "packageManager": "pnpm@9.15.1",
6
6
  "description": "A JavaScript WebGL rendering engine.",
7
7
  "author": "wxm",
@@ -84,7 +84,7 @@
84
84
  "@types/node": "^24.3.0",
85
85
  "bumpp": "^10.2.3",
86
86
  "conventional-changelog-cli": "^5.0.0",
87
- "eslint": "^9.33.0",
87
+ "eslint": "^9.34.0",
88
88
  "lint-staged": "^16.1.5",
89
89
  "lottie-web": "^5.13.0",
90
90
  "modern-gif": "^2.0.4",