modern-canvas 0.8.1 → 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.mjs CHANGED
@@ -1118,6 +1118,9 @@ class Color {
1118
1118
  }
1119
1119
  return (alpha * 255 << 24) + (r << 16) + (g << 8) + b;
1120
1120
  }
1121
+ toInt8Array() {
1122
+ return [this.r8, this.g8, this.b8, this.a8];
1123
+ }
1121
1124
  toArray() {
1122
1125
  return [this.r, this.g, this.b, this.a];
1123
1126
  }
@@ -2531,8 +2534,8 @@ class WebGLBatch2DModule extends WebGLModule {
2531
2534
  _state = WebGLState.for2D();
2532
2535
  _batchSize = 4096 * 4;
2533
2536
  _drawCallUid = 0;
2534
- _defaultModulate = 4294967295;
2535
- _defaultBackgroundColor = 0;
2537
+ _defaultModulate = [255, 255, 255, 255];
2538
+ _defaultBackgroundColor = [0, 0, 0, 0];
2536
2539
  _batchables = [];
2537
2540
  _vertexCount = 0;
2538
2541
  _indexCount = 0;
@@ -2550,7 +2553,7 @@ class WebGLBatch2DModule extends WebGLModule {
2550
2553
  // 1
2551
2554
  aBackgroundColor: { size: 4, normalized: true, type: "unsigned_byte" },
2552
2555
  // 1
2553
- aDisableWrapMode: { size: 4, normalized: true, type: "unsigned_byte" }
2556
+ aDisableWrapMode: { size: 1, normalized: true, type: "float" }
2554
2557
  // 1
2555
2558
  };
2556
2559
  _vertexSize = 1 + 2 + 2 + 1 + 1 + 1;
@@ -2570,7 +2573,7 @@ attribute vec2 aPosition;
2570
2573
  attribute vec2 aUv;
2571
2574
  attribute vec4 aModulate;
2572
2575
  attribute vec4 aBackgroundColor;
2573
- attribute vec4 aDisableWrapMode;
2576
+ attribute float aDisableWrapMode;
2574
2577
 
2575
2578
  uniform mat3 projectionMatrix;
2576
2579
  uniform mat3 viewMatrix;
@@ -2582,7 +2585,7 @@ varying float vTextureId;
2582
2585
  varying vec2 vUv;
2583
2586
  varying vec4 vModulate;
2584
2587
  varying vec4 vBackgroundColor;
2585
- varying vec4 vDisableWrapMode;
2588
+ varying float vDisableWrapMode;
2586
2589
 
2587
2590
  vec2 roundPixels(vec2 position, vec2 targetSize) {
2588
2591
  return (floor(((position * 0.5 + 0.5) * targetSize) + 0.5) / targetSize) * 2.0 - 1.0;
@@ -2597,6 +2600,7 @@ void main(void) {
2597
2600
  vTextureId = aTextureId;
2598
2601
  vec3 pos = projectionMatrix * viewMatrix * modelMatrix * vec3(aPosition, 1.0);
2599
2602
  gl_Position = vec4(roundPixels(pos.xy, vec2(canvasWidth, canvasHeight)), 0.0, 1.0);
2603
+ // gl_Position = vec4(pos.xy, 0.0, 1.0);
2600
2604
 
2601
2605
  vUv = aUv;
2602
2606
  vModulate = aModulate * modulate;
@@ -2608,20 +2612,20 @@ varying float vTextureId;
2608
2612
  varying vec2 vUv;
2609
2613
  varying vec4 vModulate;
2610
2614
  varying vec4 vBackgroundColor;
2611
- varying vec4 vDisableWrapMode;
2615
+ varying float vDisableWrapMode;
2612
2616
 
2613
2617
  uniform sampler2D samplers[${maxTextureUnits}];
2614
2618
 
2615
2619
  void main(void) {
2616
- vec4 color;
2617
- if (vDisableWrapMode.x > 0.0 && (vUv.x < 0.0 || vUv.y < 0.0 || vUv.x > 1.0 || vUv.y > 1.0))
2620
+ vec4 color = vec4(0.0, 0.0, 0.0, 0.0);
2621
+ if (vDisableWrapMode > 0.0 && (vUv.x < 0.0 || vUv.y < 0.0 || vUv.x > 1.0 || vUv.y > 1.0))
2618
2622
  {
2619
- color = vec4(0.0, 0.0, 0.0, 0.0);
2623
+ //
2620
2624
  }
2621
2625
  else
2622
2626
  if (vTextureId < 0.0)
2623
2627
  {
2624
- color = vec4(0.0, 0.0, 0.0, 0.0);
2628
+ //
2625
2629
  }${Array.from({ length: maxTextureUnits }, (_, i) => {
2626
2630
  let text = " ";
2627
2631
  if (i >= 0) {
@@ -2715,7 +2719,7 @@ void main(void) {
2715
2719
  const textureMaxUnits = this._renderer.texture.maxUnits;
2716
2720
  const attributeBuffer = this._getAttributeBuffer(vertexCount);
2717
2721
  const float32View = new Float32Array(attributeBuffer);
2718
- const uint32View = new Uint32Array(attributeBuffer);
2722
+ const uint8View = new Uint8Array(attributeBuffer);
2719
2723
  const indexBuffer = this._getIndexBuffer(indexCount);
2720
2724
  let aIndex = 0;
2721
2725
  let iIndex = 0;
@@ -2756,7 +2760,7 @@ void main(void) {
2756
2760
  drawCall.first = iIndex;
2757
2761
  }
2758
2762
  const iIndexStart = aIndex / this._vertexSize;
2759
- const textureLocation = (texture2 ? textureLocationMap.get(texture2) : 255) ?? 255;
2763
+ const textureLocation = (texture2 ? textureLocationMap.get(texture2) : -1) ?? -1;
2760
2764
  const disableWrapModeInt = disableWrapMode ? 1 : 0;
2761
2765
  for (let len2 = vertices.length, i2 = 0; i2 < len2; i2 += 2) {
2762
2766
  float32View[aIndex++] = textureLocation;
@@ -2764,9 +2768,23 @@ void main(void) {
2764
2768
  float32View[aIndex++] = vertices[i2 + 1];
2765
2769
  float32View[aIndex++] = uvs[i2];
2766
2770
  float32View[aIndex++] = uvs[i2 + 1];
2767
- uint32View[aIndex++] = modulate;
2768
- uint32View[aIndex++] = backgroundColor;
2769
- uint32View[aIndex++] = disableWrapModeInt;
2771
+ if (modulate) {
2772
+ const aU8Index = aIndex * 4;
2773
+ uint8View[aU8Index] = modulate[0];
2774
+ uint8View[aU8Index + 1] = modulate[1];
2775
+ uint8View[aU8Index + 2] = modulate[2];
2776
+ uint8View[aU8Index + 3] = modulate[3];
2777
+ }
2778
+ aIndex++;
2779
+ if (backgroundColor) {
2780
+ const aU8Index = aIndex * 4;
2781
+ uint8View[aU8Index] = backgroundColor[0];
2782
+ uint8View[aU8Index + 1] = backgroundColor[1];
2783
+ uint8View[aU8Index + 2] = backgroundColor[2];
2784
+ uint8View[aU8Index + 3] = backgroundColor[3];
2785
+ }
2786
+ aIndex++;
2787
+ float32View[aIndex++] = disableWrapModeInt;
2770
2788
  }
2771
2789
  for (let len2 = indices.length, i2 = 0; i2 < len2; i2++) {
2772
2790
  indexBuffer[iIndex++] = iIndexStart + indices[i2];
@@ -5551,14 +5569,20 @@ class CanvasContext extends Path2D {
5551
5569
  // custom
5552
5570
  uvTransform;
5553
5571
  vertTransform;
5554
- _defaultStyle = Texture2D.EMPTY;
5555
5572
  _draws = [];
5556
- _toTexture(source) {
5557
- if (source instanceof Texture2D) {
5558
- return source;
5559
- } else {
5560
- return ColorTexture.get(source);
5573
+ _parseDrawStyle(source) {
5574
+ if (source) {
5575
+ if (source instanceof Texture2D) {
5576
+ return {
5577
+ texture: source
5578
+ };
5579
+ } else {
5580
+ return {
5581
+ backgroundColor: new Color(source).toInt8Array()
5582
+ };
5583
+ }
5561
5584
  }
5585
+ return {};
5562
5586
  }
5563
5587
  stroke(options) {
5564
5588
  if (!this.curves.length) {
@@ -5579,12 +5603,12 @@ class CanvasContext extends Path2D {
5579
5603
  }
5580
5604
  this._draws.push({
5581
5605
  ...options,
5606
+ ...this._parseDrawStyle(strokeStyle),
5582
5607
  type: "stroke",
5583
5608
  path: new Path2D(this),
5584
- texture: strokeStyle ? this._toTexture(strokeStyle) : this._defaultStyle,
5585
5609
  uvTransform: this.uvTransform,
5586
5610
  vertTransform: this.vertTransform,
5587
- style: {
5611
+ lineStyle: {
5588
5612
  alignment: this.strokeAlignment ?? 0.5,
5589
5613
  cap: this.lineCap ?? "butt",
5590
5614
  join: this.lineJoin ?? "miter",
@@ -5619,9 +5643,9 @@ class CanvasContext extends Path2D {
5619
5643
  }
5620
5644
  this._draws.push({
5621
5645
  ...options,
5646
+ ...this._parseDrawStyle(fillStyle),
5622
5647
  type: "fill",
5623
5648
  path: new Path2D(this),
5624
- texture: fillStyle ? this._toTexture(fillStyle) : this._defaultStyle,
5625
5649
  uvTransform: this.uvTransform,
5626
5650
  vertTransform: this.vertTransform
5627
5651
  });
@@ -5697,17 +5721,20 @@ class CanvasContext extends Path2D {
5697
5721
  current.path.strokeTriangulate({
5698
5722
  vertices,
5699
5723
  indices,
5700
- lineStyle: current.style,
5724
+ lineStyle: current.lineStyle,
5701
5725
  flipAlignment: false,
5702
5726
  closed: true
5703
5727
  });
5704
5728
  }
5705
- this.buildUvs(0, vertices, uvs, current.texture, current.uvTransform);
5729
+ if (current.texture) {
5730
+ this.buildUvs(0, vertices, uvs, current.texture, current.uvTransform);
5731
+ }
5706
5732
  batchables.push({
5707
5733
  vertices: new Float32Array(vertices),
5708
5734
  indices: new Float32Array(indices),
5709
5735
  uvs: new Float32Array(uvs),
5710
5736
  texture: current.texture,
5737
+ backgroundColor: current.backgroundColor,
5711
5738
  type: current.type,
5712
5739
  disableWrapMode: current.disableWrapMode,
5713
5740
  vertTransform: current.vertTransform
@@ -6479,7 +6506,7 @@ let CanvasItem = class extends TimelineNode {
6479
6506
  return batchables.map((batchable) => {
6480
6507
  return {
6481
6508
  ...batchable,
6482
- modulate: this._modulate.toArgb(this.globalOpacity, true),
6509
+ modulate: this._modulate.toInt8Array(),
6483
6510
  blendMode: this.blendMode
6484
6511
  };
6485
6512
  });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "modern-canvas",
3
3
  "type": "module",
4
- "version": "0.8.1",
4
+ "version": "0.8.2",
5
5
  "packageManager": "pnpm@9.15.1",
6
6
  "description": "A JavaScript WebGL rendering engine.",
7
7
  "author": "wxm",