modern-canvas 0.8.0 → 0.8.2

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
@@ -1124,6 +1124,9 @@ class Color {
1124
1124
  }
1125
1125
  return (alpha * 255 << 24) + (r << 16) + (g << 8) + b;
1126
1126
  }
1127
+ toInt8Array() {
1128
+ return [this.r8, this.g8, this.b8, this.a8];
1129
+ }
1127
1130
  toArray() {
1128
1131
  return [this.r, this.g, this.b, this.a];
1129
1132
  }
@@ -2537,8 +2540,8 @@ class WebGLBatch2DModule extends WebGLModule {
2537
2540
  _state = WebGLState.for2D();
2538
2541
  _batchSize = 4096 * 4;
2539
2542
  _drawCallUid = 0;
2540
- _defaultModulate = 4294967295;
2541
- _defaultBackgroundColor = 0;
2543
+ _defaultModulate = [255, 255, 255, 255];
2544
+ _defaultBackgroundColor = [0, 0, 0, 0];
2542
2545
  _batchables = [];
2543
2546
  _vertexCount = 0;
2544
2547
  _indexCount = 0;
@@ -2556,7 +2559,7 @@ class WebGLBatch2DModule extends WebGLModule {
2556
2559
  // 1
2557
2560
  aBackgroundColor: { size: 4, normalized: true, type: "unsigned_byte" },
2558
2561
  // 1
2559
- aDisableWrapMode: { size: 4, normalized: true, type: "unsigned_byte" }
2562
+ aDisableWrapMode: { size: 1, normalized: true, type: "float" }
2560
2563
  // 1
2561
2564
  };
2562
2565
  _vertexSize = 1 + 2 + 2 + 1 + 1 + 1;
@@ -2576,7 +2579,7 @@ attribute vec2 aPosition;
2576
2579
  attribute vec2 aUv;
2577
2580
  attribute vec4 aModulate;
2578
2581
  attribute vec4 aBackgroundColor;
2579
- attribute vec4 aDisableWrapMode;
2582
+ attribute float aDisableWrapMode;
2580
2583
 
2581
2584
  uniform mat3 projectionMatrix;
2582
2585
  uniform mat3 viewMatrix;
@@ -2588,7 +2591,7 @@ varying float vTextureId;
2588
2591
  varying vec2 vUv;
2589
2592
  varying vec4 vModulate;
2590
2593
  varying vec4 vBackgroundColor;
2591
- varying vec4 vDisableWrapMode;
2594
+ varying float vDisableWrapMode;
2592
2595
 
2593
2596
  vec2 roundPixels(vec2 position, vec2 targetSize) {
2594
2597
  return (floor(((position * 0.5 + 0.5) * targetSize) + 0.5) / targetSize) * 2.0 - 1.0;
@@ -2603,6 +2606,7 @@ void main(void) {
2603
2606
  vTextureId = aTextureId;
2604
2607
  vec3 pos = projectionMatrix * viewMatrix * modelMatrix * vec3(aPosition, 1.0);
2605
2608
  gl_Position = vec4(roundPixels(pos.xy, vec2(canvasWidth, canvasHeight)), 0.0, 1.0);
2609
+ // gl_Position = vec4(pos.xy, 0.0, 1.0);
2606
2610
 
2607
2611
  vUv = aUv;
2608
2612
  vModulate = aModulate * modulate;
@@ -2614,20 +2618,20 @@ varying float vTextureId;
2614
2618
  varying vec2 vUv;
2615
2619
  varying vec4 vModulate;
2616
2620
  varying vec4 vBackgroundColor;
2617
- varying vec4 vDisableWrapMode;
2621
+ varying float vDisableWrapMode;
2618
2622
 
2619
2623
  uniform sampler2D samplers[${maxTextureUnits}];
2620
2624
 
2621
2625
  void main(void) {
2622
- vec4 color;
2623
- if (vDisableWrapMode.x > 0.0 && (vUv.x < 0.0 || vUv.y < 0.0 || vUv.x > 1.0 || vUv.y > 1.0))
2626
+ vec4 color = vec4(0.0, 0.0, 0.0, 0.0);
2627
+ if (vDisableWrapMode > 0.0 && (vUv.x < 0.0 || vUv.y < 0.0 || vUv.x > 1.0 || vUv.y > 1.0))
2624
2628
  {
2625
- color = vec4(0.0, 0.0, 0.0, 0.0);
2629
+ //
2626
2630
  }
2627
2631
  else
2628
2632
  if (vTextureId < 0.0)
2629
2633
  {
2630
- color = vec4(0.0, 0.0, 0.0, 0.0);
2634
+ //
2631
2635
  }${Array.from({ length: maxTextureUnits }, (_, i) => {
2632
2636
  let text = " ";
2633
2637
  if (i >= 0) {
@@ -2721,7 +2725,7 @@ void main(void) {
2721
2725
  const textureMaxUnits = this._renderer.texture.maxUnits;
2722
2726
  const attributeBuffer = this._getAttributeBuffer(vertexCount);
2723
2727
  const float32View = new Float32Array(attributeBuffer);
2724
- const uint32View = new Uint32Array(attributeBuffer);
2728
+ const uint8View = new Uint8Array(attributeBuffer);
2725
2729
  const indexBuffer = this._getIndexBuffer(indexCount);
2726
2730
  let aIndex = 0;
2727
2731
  let iIndex = 0;
@@ -2762,7 +2766,7 @@ void main(void) {
2762
2766
  drawCall.first = iIndex;
2763
2767
  }
2764
2768
  const iIndexStart = aIndex / this._vertexSize;
2765
- const textureLocation = (texture2 ? textureLocationMap.get(texture2) : 255) ?? 255;
2769
+ const textureLocation = (texture2 ? textureLocationMap.get(texture2) : -1) ?? -1;
2766
2770
  const disableWrapModeInt = disableWrapMode ? 1 : 0;
2767
2771
  for (let len2 = vertices.length, i2 = 0; i2 < len2; i2 += 2) {
2768
2772
  float32View[aIndex++] = textureLocation;
@@ -2770,9 +2774,23 @@ void main(void) {
2770
2774
  float32View[aIndex++] = vertices[i2 + 1];
2771
2775
  float32View[aIndex++] = uvs[i2];
2772
2776
  float32View[aIndex++] = uvs[i2 + 1];
2773
- uint32View[aIndex++] = modulate;
2774
- uint32View[aIndex++] = backgroundColor;
2775
- uint32View[aIndex++] = disableWrapModeInt;
2777
+ if (modulate) {
2778
+ const aU8Index = aIndex * 4;
2779
+ uint8View[aU8Index] = modulate[0];
2780
+ uint8View[aU8Index + 1] = modulate[1];
2781
+ uint8View[aU8Index + 2] = modulate[2];
2782
+ uint8View[aU8Index + 3] = modulate[3];
2783
+ }
2784
+ aIndex++;
2785
+ if (backgroundColor) {
2786
+ const aU8Index = aIndex * 4;
2787
+ uint8View[aU8Index] = backgroundColor[0];
2788
+ uint8View[aU8Index + 1] = backgroundColor[1];
2789
+ uint8View[aU8Index + 2] = backgroundColor[2];
2790
+ uint8View[aU8Index + 3] = backgroundColor[3];
2791
+ }
2792
+ aIndex++;
2793
+ float32View[aIndex++] = disableWrapModeInt;
2776
2794
  }
2777
2795
  for (let len2 = indices.length, i2 = 0; i2 < len2; i2++) {
2778
2796
  indexBuffer[iIndex++] = iIndexStart + indices[i2];
@@ -5557,14 +5575,20 @@ class CanvasContext extends modernPath2d.Path2D {
5557
5575
  // custom
5558
5576
  uvTransform;
5559
5577
  vertTransform;
5560
- _defaultStyle = Texture2D.EMPTY;
5561
5578
  _draws = [];
5562
- _toTexture(source) {
5563
- if (source instanceof Texture2D) {
5564
- return source;
5565
- } else {
5566
- return ColorTexture.get(source);
5579
+ _parseDrawStyle(source) {
5580
+ if (source) {
5581
+ if (source instanceof Texture2D) {
5582
+ return {
5583
+ texture: source
5584
+ };
5585
+ } else {
5586
+ return {
5587
+ backgroundColor: new Color(source).toInt8Array()
5588
+ };
5589
+ }
5567
5590
  }
5591
+ return {};
5568
5592
  }
5569
5593
  stroke(options) {
5570
5594
  if (!this.curves.length) {
@@ -5585,12 +5609,12 @@ class CanvasContext extends modernPath2d.Path2D {
5585
5609
  }
5586
5610
  this._draws.push({
5587
5611
  ...options,
5612
+ ...this._parseDrawStyle(strokeStyle),
5588
5613
  type: "stroke",
5589
5614
  path: new modernPath2d.Path2D(this),
5590
- texture: strokeStyle ? this._toTexture(strokeStyle) : this._defaultStyle,
5591
5615
  uvTransform: this.uvTransform,
5592
5616
  vertTransform: this.vertTransform,
5593
- style: {
5617
+ lineStyle: {
5594
5618
  alignment: this.strokeAlignment ?? 0.5,
5595
5619
  cap: this.lineCap ?? "butt",
5596
5620
  join: this.lineJoin ?? "miter",
@@ -5625,9 +5649,9 @@ class CanvasContext extends modernPath2d.Path2D {
5625
5649
  }
5626
5650
  this._draws.push({
5627
5651
  ...options,
5652
+ ...this._parseDrawStyle(fillStyle),
5628
5653
  type: "fill",
5629
5654
  path: new modernPath2d.Path2D(this),
5630
- texture: fillStyle ? this._toTexture(fillStyle) : this._defaultStyle,
5631
5655
  uvTransform: this.uvTransform,
5632
5656
  vertTransform: this.vertTransform
5633
5657
  });
@@ -5703,17 +5727,20 @@ class CanvasContext extends modernPath2d.Path2D {
5703
5727
  current.path.strokeTriangulate({
5704
5728
  vertices,
5705
5729
  indices,
5706
- lineStyle: current.style,
5730
+ lineStyle: current.lineStyle,
5707
5731
  flipAlignment: false,
5708
5732
  closed: true
5709
5733
  });
5710
5734
  }
5711
- this.buildUvs(0, vertices, uvs, current.texture, current.uvTransform);
5735
+ if (current.texture) {
5736
+ this.buildUvs(0, vertices, uvs, current.texture, current.uvTransform);
5737
+ }
5712
5738
  batchables.push({
5713
5739
  vertices: new Float32Array(vertices),
5714
5740
  indices: new Float32Array(indices),
5715
5741
  uvs: new Float32Array(uvs),
5716
5742
  texture: current.texture,
5743
+ backgroundColor: current.backgroundColor,
5717
5744
  type: current.type,
5718
5745
  disableWrapMode: current.disableWrapMode,
5719
5746
  vertTransform: current.vertTransform
@@ -6485,7 +6512,7 @@ exports.CanvasItem = class CanvasItem extends exports.TimelineNode {
6485
6512
  return batchables.map((batchable) => {
6486
6513
  return {
6487
6514
  ...batchable,
6488
- modulate: this._modulate.toArgb(this.globalOpacity, true),
6515
+ modulate: this._modulate.toInt8Array(),
6489
6516
  blendMode: this.blendMode
6490
6517
  };
6491
6518
  });
package/dist/index.d.cts CHANGED
@@ -335,6 +335,7 @@ declare class Color {
335
335
  toHslString(): string;
336
336
  toHsv(): HsvaColor;
337
337
  toArgb(alpha?: number, applyToRGB?: boolean): number;
338
+ toInt8Array(): [number, number, number, number];
338
339
  toArray(): [number, number, number, number];
339
340
  }
340
341
 
@@ -979,13 +980,13 @@ interface Batchable2D {
979
980
  indices: Float32Array;
980
981
  uvs?: Float32Array;
981
982
  texture?: WebGLTexture;
982
- backgroundColor?: number;
983
- modulate?: number;
983
+ backgroundColor?: number[];
984
+ modulate?: number[];
984
985
  blendMode?: WebGLBlendMode;
985
986
  disableWrapMode?: boolean;
986
987
  }
987
988
  interface Shader {
988
- update: (attributeBuffer: ArrayBuffer, indexBuffer: Uint16Array) => void;
989
+ update: (attributeBuffer: ArrayBuffer, indexBuffer: Uint16Array<ArrayBuffer>) => void;
989
990
  draw: (options?: WebGLDrawOptions) => void;
990
991
  }
991
992
  declare class WebGLBatch2DModule extends WebGLModule {
@@ -993,13 +994,13 @@ declare class WebGLBatch2DModule extends WebGLModule {
993
994
  protected _state: WebGLState;
994
995
  protected _batchSize: number;
995
996
  protected _drawCallUid: number;
996
- protected _defaultModulate: number;
997
- protected _defaultBackgroundColor: number;
997
+ protected _defaultModulate: number[];
998
+ protected _defaultBackgroundColor: number[];
998
999
  protected _batchables: Batchable2D[];
999
1000
  protected _vertexCount: number;
1000
1001
  protected _indexCount: number;
1001
1002
  protected _attributeBuffer: ArrayBuffer[];
1002
- protected _indexBuffers: Uint16Array[];
1003
+ protected _indexBuffers: Uint16Array<ArrayBuffer>[];
1003
1004
  protected _shaders: Map<number, Shader>;
1004
1005
  protected _attributes: Record<string, Partial<WebGLVertexAttrib>>;
1005
1006
  protected _vertexSize: number;
@@ -1019,7 +1020,7 @@ declare class WebGLBatch2DModule extends WebGLModule {
1019
1020
  * @param size - minimum required capacity
1020
1021
  * @returns - buffer that can fit `size` indices.
1021
1022
  */
1022
- protected _getIndexBuffer(size: number): Uint16Array;
1023
+ protected _getIndexBuffer(size: number): Uint16Array<ArrayBuffer>;
1023
1024
  }
1024
1025
  /**
1025
1026
  * Rounds to next power of two.
@@ -1472,7 +1473,7 @@ interface CanvasBatchable extends Batchable2D {
1472
1473
  interface StrokeDraw extends Partial<CanvasBatchable> {
1473
1474
  type: 'stroke';
1474
1475
  path: Path2D;
1475
- style: LineStyle;
1476
+ lineStyle: LineStyle;
1476
1477
  uvTransform?: UvTransform;
1477
1478
  }
1478
1479
  interface FillDraw extends Partial<CanvasBatchable> {
@@ -1490,9 +1491,11 @@ declare class CanvasContext extends Path2D {
1490
1491
  miterLimit?: number;
1491
1492
  uvTransform?: UvTransform;
1492
1493
  vertTransform?: VertTransform;
1493
- protected _defaultStyle: Texture2D<Texture2DSource>;
1494
1494
  protected _draws: (StrokeDraw | FillDraw)[];
1495
- protected _toTexture(source: Color$1 | Texture2D): Texture2D;
1495
+ protected _parseDrawStyle(source?: Color$1 | Texture2D): {
1496
+ texture?: Texture2D;
1497
+ backgroundColor?: number[];
1498
+ };
1496
1499
  stroke(options?: Partial<StrokeDraw>): void;
1497
1500
  fillRect(x: number, y: number, width: number, height: number): void;
1498
1501
  strokeRect(x: number, y: number, width: number, height: number): void;
package/dist/index.d.mts CHANGED
@@ -335,6 +335,7 @@ declare class Color {
335
335
  toHslString(): string;
336
336
  toHsv(): HsvaColor;
337
337
  toArgb(alpha?: number, applyToRGB?: boolean): number;
338
+ toInt8Array(): [number, number, number, number];
338
339
  toArray(): [number, number, number, number];
339
340
  }
340
341
 
@@ -979,13 +980,13 @@ interface Batchable2D {
979
980
  indices: Float32Array;
980
981
  uvs?: Float32Array;
981
982
  texture?: WebGLTexture;
982
- backgroundColor?: number;
983
- modulate?: number;
983
+ backgroundColor?: number[];
984
+ modulate?: number[];
984
985
  blendMode?: WebGLBlendMode;
985
986
  disableWrapMode?: boolean;
986
987
  }
987
988
  interface Shader {
988
- update: (attributeBuffer: ArrayBuffer, indexBuffer: Uint16Array) => void;
989
+ update: (attributeBuffer: ArrayBuffer, indexBuffer: Uint16Array<ArrayBuffer>) => void;
989
990
  draw: (options?: WebGLDrawOptions) => void;
990
991
  }
991
992
  declare class WebGLBatch2DModule extends WebGLModule {
@@ -993,13 +994,13 @@ declare class WebGLBatch2DModule extends WebGLModule {
993
994
  protected _state: WebGLState;
994
995
  protected _batchSize: number;
995
996
  protected _drawCallUid: number;
996
- protected _defaultModulate: number;
997
- protected _defaultBackgroundColor: number;
997
+ protected _defaultModulate: number[];
998
+ protected _defaultBackgroundColor: number[];
998
999
  protected _batchables: Batchable2D[];
999
1000
  protected _vertexCount: number;
1000
1001
  protected _indexCount: number;
1001
1002
  protected _attributeBuffer: ArrayBuffer[];
1002
- protected _indexBuffers: Uint16Array[];
1003
+ protected _indexBuffers: Uint16Array<ArrayBuffer>[];
1003
1004
  protected _shaders: Map<number, Shader>;
1004
1005
  protected _attributes: Record<string, Partial<WebGLVertexAttrib>>;
1005
1006
  protected _vertexSize: number;
@@ -1019,7 +1020,7 @@ declare class WebGLBatch2DModule extends WebGLModule {
1019
1020
  * @param size - minimum required capacity
1020
1021
  * @returns - buffer that can fit `size` indices.
1021
1022
  */
1022
- protected _getIndexBuffer(size: number): Uint16Array;
1023
+ protected _getIndexBuffer(size: number): Uint16Array<ArrayBuffer>;
1023
1024
  }
1024
1025
  /**
1025
1026
  * Rounds to next power of two.
@@ -1472,7 +1473,7 @@ interface CanvasBatchable extends Batchable2D {
1472
1473
  interface StrokeDraw extends Partial<CanvasBatchable> {
1473
1474
  type: 'stroke';
1474
1475
  path: Path2D;
1475
- style: LineStyle;
1476
+ lineStyle: LineStyle;
1476
1477
  uvTransform?: UvTransform;
1477
1478
  }
1478
1479
  interface FillDraw extends Partial<CanvasBatchable> {
@@ -1490,9 +1491,11 @@ declare class CanvasContext extends Path2D {
1490
1491
  miterLimit?: number;
1491
1492
  uvTransform?: UvTransform;
1492
1493
  vertTransform?: VertTransform;
1493
- protected _defaultStyle: Texture2D<Texture2DSource>;
1494
1494
  protected _draws: (StrokeDraw | FillDraw)[];
1495
- protected _toTexture(source: Color$1 | Texture2D): Texture2D;
1495
+ protected _parseDrawStyle(source?: Color$1 | Texture2D): {
1496
+ texture?: Texture2D;
1497
+ backgroundColor?: number[];
1498
+ };
1496
1499
  stroke(options?: Partial<StrokeDraw>): void;
1497
1500
  fillRect(x: number, y: number, width: number, height: number): void;
1498
1501
  strokeRect(x: number, y: number, width: number, height: number): void;
package/dist/index.d.ts CHANGED
@@ -335,6 +335,7 @@ declare class Color {
335
335
  toHslString(): string;
336
336
  toHsv(): HsvaColor;
337
337
  toArgb(alpha?: number, applyToRGB?: boolean): number;
338
+ toInt8Array(): [number, number, number, number];
338
339
  toArray(): [number, number, number, number];
339
340
  }
340
341
 
@@ -979,13 +980,13 @@ interface Batchable2D {
979
980
  indices: Float32Array;
980
981
  uvs?: Float32Array;
981
982
  texture?: WebGLTexture;
982
- backgroundColor?: number;
983
- modulate?: number;
983
+ backgroundColor?: number[];
984
+ modulate?: number[];
984
985
  blendMode?: WebGLBlendMode;
985
986
  disableWrapMode?: boolean;
986
987
  }
987
988
  interface Shader {
988
- update: (attributeBuffer: ArrayBuffer, indexBuffer: Uint16Array) => void;
989
+ update: (attributeBuffer: ArrayBuffer, indexBuffer: Uint16Array<ArrayBuffer>) => void;
989
990
  draw: (options?: WebGLDrawOptions) => void;
990
991
  }
991
992
  declare class WebGLBatch2DModule extends WebGLModule {
@@ -993,13 +994,13 @@ declare class WebGLBatch2DModule extends WebGLModule {
993
994
  protected _state: WebGLState;
994
995
  protected _batchSize: number;
995
996
  protected _drawCallUid: number;
996
- protected _defaultModulate: number;
997
- protected _defaultBackgroundColor: number;
997
+ protected _defaultModulate: number[];
998
+ protected _defaultBackgroundColor: number[];
998
999
  protected _batchables: Batchable2D[];
999
1000
  protected _vertexCount: number;
1000
1001
  protected _indexCount: number;
1001
1002
  protected _attributeBuffer: ArrayBuffer[];
1002
- protected _indexBuffers: Uint16Array[];
1003
+ protected _indexBuffers: Uint16Array<ArrayBuffer>[];
1003
1004
  protected _shaders: Map<number, Shader>;
1004
1005
  protected _attributes: Record<string, Partial<WebGLVertexAttrib>>;
1005
1006
  protected _vertexSize: number;
@@ -1019,7 +1020,7 @@ declare class WebGLBatch2DModule extends WebGLModule {
1019
1020
  * @param size - minimum required capacity
1020
1021
  * @returns - buffer that can fit `size` indices.
1021
1022
  */
1022
- protected _getIndexBuffer(size: number): Uint16Array;
1023
+ protected _getIndexBuffer(size: number): Uint16Array<ArrayBuffer>;
1023
1024
  }
1024
1025
  /**
1025
1026
  * Rounds to next power of two.
@@ -1472,7 +1473,7 @@ interface CanvasBatchable extends Batchable2D {
1472
1473
  interface StrokeDraw extends Partial<CanvasBatchable> {
1473
1474
  type: 'stroke';
1474
1475
  path: Path2D;
1475
- style: LineStyle;
1476
+ lineStyle: LineStyle;
1476
1477
  uvTransform?: UvTransform;
1477
1478
  }
1478
1479
  interface FillDraw extends Partial<CanvasBatchable> {
@@ -1490,9 +1491,11 @@ declare class CanvasContext extends Path2D {
1490
1491
  miterLimit?: number;
1491
1492
  uvTransform?: UvTransform;
1492
1493
  vertTransform?: VertTransform;
1493
- protected _defaultStyle: Texture2D<Texture2DSource>;
1494
1494
  protected _draws: (StrokeDraw | FillDraw)[];
1495
- protected _toTexture(source: Color$1 | Texture2D): Texture2D;
1495
+ protected _parseDrawStyle(source?: Color$1 | Texture2D): {
1496
+ texture?: Texture2D;
1497
+ backgroundColor?: number[];
1498
+ };
1496
1499
  stroke(options?: Partial<StrokeDraw>): void;
1497
1500
  fillRect(x: number, y: number, width: number, height: number): void;
1498
1501
  strokeRect(x: number, y: number, width: number, height: number): void;