modern-canvas 0.2.0 → 0.2.1

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
@@ -1918,6 +1918,12 @@ class Rect2 {
1918
1918
  get top() {
1919
1919
  return this.position.y;
1920
1920
  }
1921
+ get right() {
1922
+ return this.x + this.width;
1923
+ }
1924
+ get bottom() {
1925
+ return this.y + this.height;
1926
+ }
1921
1927
  get width() {
1922
1928
  return this.size.x;
1923
1929
  }
@@ -1958,6 +1964,9 @@ class Rect2 {
1958
1964
  );
1959
1965
  return this;
1960
1966
  }
1967
+ toArray() {
1968
+ return [this.x, this.y, this.width, this.height];
1969
+ }
1961
1970
  }
1962
1971
 
1963
1972
  class Transform2D extends Matrix3 {
@@ -5263,10 +5272,10 @@ class CanvasTexture extends Texture2D {
5263
5272
  _updateProperty(key, value, oldValue, declaration) {
5264
5273
  switch (key) {
5265
5274
  case "width":
5266
- this.source.width = value * this.pixelRatio;
5275
+ this.source.width = Math.max(1, Math.ceil(value * this.pixelRatio));
5267
5276
  break;
5268
5277
  case "height":
5269
- this.source.height = value * this.pixelRatio;
5278
+ this.source.height = Math.max(1, Math.ceil(value * this.pixelRatio));
5270
5279
  break;
5271
5280
  }
5272
5281
  super._updateProperty(key, value, oldValue, declaration);
@@ -5960,9 +5969,6 @@ let Node = class extends CoreObject {
5960
5969
  }
5961
5970
  /** Children */
5962
5971
  _children = [];
5963
- get children() {
5964
- return this.getChildren();
5965
- }
5966
5972
  get siblingIndex() {
5967
5973
  return this.getIndex();
5968
5974
  }
@@ -6227,17 +6233,17 @@ let Node = class extends CoreObject {
6227
6233
  return this;
6228
6234
  }
6229
6235
  removeChildren() {
6230
- this.children.forEach((child) => this.removeChild(child));
6236
+ this.getChildren().forEach((child) => this.removeChild(child));
6231
6237
  }
6232
6238
  remove() {
6233
6239
  this._parent?.removeChild(this);
6234
6240
  }
6235
6241
  forEach(fn) {
6236
- this.children.forEach(fn);
6242
+ this.getChildren().forEach(fn);
6237
6243
  return this;
6238
6244
  }
6239
6245
  deepForEach(fn) {
6240
- this.children.forEach((child) => {
6246
+ this.getChildren().forEach((child) => {
6241
6247
  fn(child);
6242
6248
  child.deepForEach(fn);
6243
6249
  });
@@ -6274,7 +6280,7 @@ let Node = class extends CoreObject {
6274
6280
  return {
6275
6281
  tag: this.tag,
6276
6282
  props: super.toJSON(),
6277
- children: this.children.map((child) => child.toJSON())
6283
+ children: this.getChildren().map((child) => child.toJSON())
6278
6284
  };
6279
6285
  }
6280
6286
  static parse(JSON) {
@@ -6411,7 +6417,6 @@ let CanvasItem = class extends TimelineNode {
6411
6417
  }
6412
6418
  /** @internal */
6413
6419
  opacity = 1;
6414
- size = { width: 0, height: 0 };
6415
6420
  _parentOpacity;
6416
6421
  _modulate = new Color(4294967295);
6417
6422
  _backgroundImage;
@@ -6475,19 +6480,6 @@ let CanvasItem = class extends TimelineNode {
6475
6480
  case "visibility":
6476
6481
  this.visible = value === "visible";
6477
6482
  break;
6478
- case "width":
6479
- case "height":
6480
- this._updateSize();
6481
- break;
6482
- }
6483
- }
6484
- _updateSize() {
6485
- if (this.inheritSize) {
6486
- this.size.width = this.style.width || this._parent?.size?.width || this._parent?.width;
6487
- this.size.height = this.style.height || this._parent?.size?.height || this._parent?.height;
6488
- } else {
6489
- this.size.width = this.style.width;
6490
- this.size.height = this.style.height;
6491
6483
  }
6492
6484
  }
6493
6485
  _updateBackgroundColor() {
@@ -6503,14 +6495,10 @@ let CanvasItem = class extends TimelineNode {
6503
6495
  this.requestRedraw();
6504
6496
  }
6505
6497
  _updateOpacity() {
6506
- const parentOpacity = this._parent?.opacity;
6507
- if (parentOpacity !== this._parentOpacity) {
6508
- this._parentOpacity = parentOpacity;
6509
- const opacity = this.style.getComputedOpacity() * (this._parent?.opacity ?? 1);
6510
- if (this.opacity !== opacity) {
6511
- this.opacity = opacity;
6512
- this.requestRepaint();
6513
- }
6498
+ const opacity = this.style.getComputedOpacity() * (this._parent?.opacity ?? 1);
6499
+ if (this.opacity !== opacity) {
6500
+ this.opacity = opacity;
6501
+ this.requestRepaint();
6514
6502
  }
6515
6503
  }
6516
6504
  _updateVisible() {
@@ -6543,9 +6531,10 @@ let CanvasItem = class extends TimelineNode {
6543
6531
  }
6544
6532
  _process(delta) {
6545
6533
  this._updateVisible();
6546
- this._updateOpacity();
6547
- if (this.inheritSize && (!this.size.width || !this.size.height || this.size.width !== this.style.width || this.size.height !== this.style.height)) {
6548
- this._updateSize();
6534
+ const parentOpacity = this._parent?.opacity;
6535
+ if (parentOpacity !== this._parentOpacity) {
6536
+ this._parentOpacity = parentOpacity;
6537
+ this._updateOpacity();
6549
6538
  }
6550
6539
  super._process(delta);
6551
6540
  }
@@ -6584,8 +6573,7 @@ let CanvasItem = class extends TimelineNode {
6584
6573
  }
6585
6574
  }
6586
6575
  _drawBoundingRect() {
6587
- const { borderRadius } = this.style;
6588
- const { width, height } = this.size;
6576
+ const { borderRadius, width, height } = this.style;
6589
6577
  if (width && height) {
6590
6578
  if (borderRadius) {
6591
6579
  this.context.roundRect(0, 0, width, height, borderRadius);
@@ -6673,9 +6661,6 @@ __decorateClass$y([
6673
6661
  __decorateClass$y([
6674
6662
  property()
6675
6663
  ], CanvasItem.prototype, "blendMode", 2);
6676
- __decorateClass$y([
6677
- property()
6678
- ], CanvasItem.prototype, "inheritSize", 2);
6679
6664
  CanvasItem = __decorateClass$y([
6680
6665
  customNode("CanvasItem")
6681
6666
  ], CanvasItem);
@@ -6837,11 +6822,13 @@ let Viewport = class extends Node {
6837
6822
  case "y":
6838
6823
  this.requestUpload();
6839
6824
  this._projection.translate(this.x, this.y);
6825
+ this.emit("updateRect");
6840
6826
  break;
6841
6827
  case "width":
6842
6828
  case "height":
6843
6829
  this.requestUpload();
6844
6830
  this._projection.resize(this.width, this.height);
6831
+ this.emit("updateRect");
6845
6832
  break;
6846
6833
  }
6847
6834
  }
@@ -6910,6 +6897,9 @@ let Viewport = class extends Node {
6910
6897
  this._tree?.setCurrentViewport(undefined);
6911
6898
  }
6912
6899
  }
6900
+ getRect() {
6901
+ return new Rect2(this.x, this.y, this.width, this.height);
6902
+ }
6913
6903
  toProjectionArray(transpose = false) {
6914
6904
  return this._projection.toArray(transpose);
6915
6905
  }
@@ -7654,7 +7644,7 @@ let Text2D = class extends TextureRect2D {
7654
7644
  }
7655
7645
  _drawContent() {
7656
7646
  if (!this.split) {
7657
- const onText2DRender = this.children?.find((child) => "onText2DRender" in child)?.onText2DRender;
7647
+ const onText2DRender = this.getChildren()?.find((child) => "onText2DRender" in child)?.onText2DRender;
7658
7648
  if (onText2DRender) {
7659
7649
  onText2DRender();
7660
7650
  } else {
@@ -7844,7 +7834,7 @@ let Animation = class extends TimelineNode {
7844
7834
  _updateProperty(key, value, oldValue, declaration) {
7845
7835
  super._updateProperty(key, value, oldValue, declaration);
7846
7836
  switch (key) {
7847
- case "mode":
7837
+ case "animationMode":
7848
7838
  case "keyframes":
7849
7839
  this._updateKeyframes();
7850
7840
  break;
@@ -9502,7 +9492,7 @@ let Effect = class extends TimelineNode {
9502
9492
  _parseTargetArea() {
9503
9493
  if (this._mode === "parent" && this._parent && "getRect" in this._parent) {
9504
9494
  const rect = this._parent.getRect();
9505
- if (rect instanceof Rect2) {
9495
+ if (rect) {
9506
9496
  return [
9507
9497
  rect.left / this.viewport1.width,
9508
9498
  rect.top / this.viewport1.height,
@@ -11357,13 +11347,40 @@ var __decorateClass$3 = (decorators, target, key, kind) => {
11357
11347
  let Control = class extends CanvasItem {
11358
11348
  constructor(properties, children = []) {
11359
11349
  super();
11350
+ this._parentUpdateRect = this._parentUpdateRect.bind(this);
11360
11351
  this.setProperties(properties);
11361
11352
  this.append(children);
11362
11353
  }
11354
+ _parented(parent) {
11355
+ super._parented(parent);
11356
+ parent.on("updateRect", this._parentUpdateRect);
11357
+ }
11358
+ _unparented(oldParent) {
11359
+ super._unparented(oldParent);
11360
+ oldParent.off("updateRect", this._parentUpdateRect);
11361
+ }
11362
+ _parentUpdateRect() {
11363
+ const rect = this._parent.getRect();
11364
+ this.style.left = rect.left;
11365
+ this.style.top = rect.top;
11366
+ this.style.width = rect.width;
11367
+ this.style.height = rect.height;
11368
+ }
11363
11369
  _input(event, key) {
11364
11370
  super._input(event, key);
11365
11371
  this._guiInput(event, key);
11366
11372
  }
11373
+ _updateStyleProperty(key, value, oldValue, declaration) {
11374
+ super._updateStyleProperty(key, value, oldValue, declaration);
11375
+ switch (key) {
11376
+ case "width":
11377
+ case "height":
11378
+ case "left":
11379
+ case "top":
11380
+ this.emit("updateRect");
11381
+ break;
11382
+ }
11383
+ }
11367
11384
  // eslint-disable-next-line unused-imports/no-unused-vars
11368
11385
  _guiInput(event, key) {
11369
11386
  }
@@ -11408,7 +11425,6 @@ let Ruler = class extends Control {
11408
11425
  _updateProperty(key, value, oldValue, declaration) {
11409
11426
  super._updateProperty(key, value, oldValue, declaration);
11410
11427
  switch (key) {
11411
- case "pixelRatio":
11412
11428
  case "offsetX":
11413
11429
  case "offsetY":
11414
11430
  case "thickness":
@@ -11417,20 +11433,27 @@ let Ruler = class extends Control {
11417
11433
  case "markBackgroundColor":
11418
11434
  case "markColor":
11419
11435
  case "gap":
11436
+ case "scale":
11420
11437
  this.requestRedraw();
11421
11438
  break;
11422
11439
  }
11423
11440
  }
11424
- _updateSize() {
11425
- super._updateSize();
11426
- this.requestRedraw();
11441
+ _updateStyleProperty(key, value, oldValue, declaration) {
11442
+ super._updateStyleProperty(key, value, oldValue, declaration);
11443
+ switch (key) {
11444
+ case "width":
11445
+ case "height":
11446
+ this.texture[key] = value;
11447
+ this.requestRedraw();
11448
+ break;
11449
+ }
11427
11450
  }
11428
11451
  _drawTexture() {
11429
- const { width, height } = this.size;
11430
- this.style.width = width;
11431
- this.style.height = height;
11432
11452
  const {
11433
- pixelRatio,
11453
+ width,
11454
+ height
11455
+ } = this.style;
11456
+ const {
11434
11457
  offsetX,
11435
11458
  offsetY,
11436
11459
  thickness,
@@ -11438,14 +11461,13 @@ let Ruler = class extends Control {
11438
11461
  markBackgroundColor,
11439
11462
  markColor,
11440
11463
  color,
11441
- gap: _gap
11464
+ gap: _gap,
11465
+ scale: _scale
11442
11466
  } = this;
11443
- const _scale = 1;
11444
11467
  const canvas = this.texture.source;
11445
- canvas.width = Math.max(1, Math.ceil(width * pixelRatio));
11446
- canvas.height = Math.max(1, Math.ceil(height * pixelRatio));
11447
11468
  const ctx = canvas.getContext("2d");
11448
- ctx.scale(this.pixelRatio, this.pixelRatio);
11469
+ ctx.reset();
11470
+ ctx.scale(this.texture.pixelRatio, this.texture.pixelRatio);
11449
11471
  const x = Math.round(offsetX);
11450
11472
  const y = Math.round(offsetY);
11451
11473
  ctx.beginPath();
@@ -11527,7 +11549,7 @@ let Ruler = class extends Control {
11527
11549
  ctx.stroke();
11528
11550
  this.texture.requestUpload();
11529
11551
  }
11530
- _drawContent() {
11552
+ _draw() {
11531
11553
  this._drawTexture();
11532
11554
  const texture = this.texture;
11533
11555
  if (texture?.valid) {
@@ -11536,13 +11558,11 @@ let Ruler = class extends Control {
11536
11558
  this.style.width / texture.width,
11537
11559
  this.style.height / texture.height
11538
11560
  );
11539
- super._drawContent();
11561
+ this.context.rect(0, 0, texture.width, texture.height);
11562
+ this.context.fill();
11540
11563
  }
11541
11564
  }
11542
11565
  };
11543
- __decorateClass$2([
11544
- property({ default: 2 })
11545
- ], Ruler.prototype, "pixelRatio", 2);
11546
11566
  __decorateClass$2([
11547
11567
  property({ default: 0 })
11548
11568
  ], Ruler.prototype, "offsetX", 2);
@@ -11567,6 +11587,9 @@ __decorateClass$2([
11567
11587
  __decorateClass$2([
11568
11588
  property({ default: 300 })
11569
11589
  ], Ruler.prototype, "gap", 2);
11590
+ __decorateClass$2([
11591
+ property({ default: 1 })
11592
+ ], Ruler.prototype, "scale", 2);
11570
11593
  Ruler = __decorateClass$2([
11571
11594
  customNode("Ruler")
11572
11595
  ], Ruler);
@@ -11582,8 +11605,49 @@ var __decorateClass$1 = (decorators, target, key, kind) => {
11582
11605
  return result;
11583
11606
  };
11584
11607
  let ScrollBar = class extends Control {
11585
- //
11608
+ constructor(properties, children = []) {
11609
+ super();
11610
+ this.setProperties(properties).append(children);
11611
+ }
11612
+ _parentUpdateRect() {
11613
+ super._parentUpdateRect();
11614
+ const rect = this._parent.getRect();
11615
+ if (rect && rect.width && rect.height) {
11616
+ if (this.direction === "vertical") {
11617
+ this.style.width = 10;
11618
+ this.style.height = rect.height - this.padding * 2;
11619
+ this.style.left = rect.right - this.style.width;
11620
+ this.style.top = this.padding;
11621
+ } else {
11622
+ this.style.height = 10;
11623
+ this.style.width = rect.width - this.padding * 2;
11624
+ this.style.left = this.padding;
11625
+ this.style.top = rect.bottom - this.style.height;
11626
+ }
11627
+ }
11628
+ this.requestRedraw();
11629
+ }
11630
+ _guiInput(event, key) {
11631
+ super._guiInput(event, key);
11632
+ }
11633
+ _draw() {
11634
+ this.context.roundRect(
11635
+ this.style.left,
11636
+ this.style.top,
11637
+ this.style.width,
11638
+ this.style.height,
11639
+ this.style.borderRadius ?? 20
11640
+ );
11641
+ this.context.fillStyle = 15;
11642
+ this.context.fill();
11643
+ }
11586
11644
  };
11645
+ __decorateClass$1([
11646
+ property({ default: "vertical" })
11647
+ ], ScrollBar.prototype, "direction", 2);
11648
+ __decorateClass$1([
11649
+ property({ default: 10 })
11650
+ ], ScrollBar.prototype, "padding", 2);
11587
11651
  ScrollBar = __decorateClass$1([
11588
11652
  customNode("ScrollBar")
11589
11653
  ], ScrollBar);
@@ -12002,7 +12066,7 @@ class Assets {
12002
12066
  }
12003
12067
  const assets = new Assets().use(new FontLoader()).use(new GifLoader()).use(new JsonLoader()).use(new LottieLoader()).use(new TextLoader()).use(new TextureLoader()).use(new VideoLoader());
12004
12068
 
12005
- class CanvasEditor extends CanvasItem {
12069
+ class CanvasEditor extends Control {
12006
12070
  name = "CanvasEditor";
12007
12071
  hover = new Node2D({
12008
12072
  name: "hover",
@@ -12035,13 +12099,15 @@ class CanvasEditor extends CanvasItem {
12035
12099
  scaler = new Scaler({
12036
12100
  internalMode: "back"
12037
12101
  }).on("updateScale", (scale) => {
12038
- this.ruler.gap = scale * 300;
12102
+ this.ruler.scale = scale;
12039
12103
  });
12040
12104
  xScrollBar = new ScrollBar({
12041
- internalMode: "back"
12105
+ internalMode: "back",
12106
+ direction: "horizontal"
12042
12107
  });
12043
12108
  yScrollBar = new ScrollBar({
12044
- internalMode: "back"
12109
+ internalMode: "back",
12110
+ direction: "vertical"
12045
12111
  });
12046
12112
  drawboard = new Node2D({
12047
12113
  name: "drawboard",
@@ -12063,8 +12129,7 @@ class CanvasEditor extends CanvasItem {
12063
12129
  ruler = new Ruler({
12064
12130
  name: "ruler",
12065
12131
  offsetX: 100,
12066
- offsetY: 100,
12067
- inheritSize: true
12132
+ offsetY: 100
12068
12133
  }).append(
12069
12134
  this.drawboard,
12070
12135
  this.hover,
@@ -12081,10 +12146,9 @@ class CanvasEditor extends CanvasItem {
12081
12146
  this._onPointermove = this._onPointermove.bind(this);
12082
12147
  this._onPointerup = this._onPointerup.bind(this);
12083
12148
  this.append(this.ruler);
12084
- this.inheritSize = true;
12085
12149
  }
12086
- _input(event, key) {
12087
- super._input(event, key);
12150
+ _guiInput(event, key) {
12151
+ super._guiInput(event, key);
12088
12152
  switch (key) {
12089
12153
  case "pointerdown":
12090
12154
  this._onPointerdown(event);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "modern-canvas",
3
3
  "type": "module",
4
- "version": "0.2.0",
4
+ "version": "0.2.1",
5
5
  "packageManager": "pnpm@9.15.1",
6
6
  "description": "A JavaScript WebGL rendering engine.",
7
7
  "author": "wxm",