@lightningjs/renderer 3.0.0-beta20 → 3.0.0-beta21

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.
Files changed (152) hide show
  1. package/dist/src/core/CoreNode.d.ts +53 -7
  2. package/dist/src/core/CoreNode.js +175 -65
  3. package/dist/src/core/CoreNode.js.map +1 -1
  4. package/dist/src/core/CoreTextNode.d.ts +1 -1
  5. package/dist/src/core/CoreTextNode.js +3 -5
  6. package/dist/src/core/CoreTextNode.js.map +1 -1
  7. package/dist/src/core/CoreTextureManager.js +1 -1
  8. package/dist/src/core/CoreTextureManager.js.map +1 -1
  9. package/dist/src/core/Stage.d.ts +2 -1
  10. package/dist/src/core/Stage.js +9 -7
  11. package/dist/src/core/Stage.js.map +1 -1
  12. package/dist/src/core/TextureMemoryManager.d.ts +1 -1
  13. package/dist/src/core/TextureMemoryManager.js +3 -3
  14. package/dist/src/core/TextureMemoryManager.js.map +1 -1
  15. package/dist/src/core/animations/Animation.d.ts +21 -0
  16. package/dist/src/core/animations/Animation.js +194 -0
  17. package/dist/src/core/animations/Animation.js.map +1 -0
  18. package/dist/src/core/animations/Playback.d.ts +64 -0
  19. package/dist/src/core/animations/Playback.js +169 -0
  20. package/dist/src/core/animations/Playback.js.map +1 -0
  21. package/dist/src/core/animations/Transition.d.ts +27 -0
  22. package/dist/src/core/animations/Transition.js +52 -0
  23. package/dist/src/core/animations/Transition.js.map +1 -0
  24. package/dist/src/core/animations/utils.d.ts +2 -0
  25. package/dist/src/core/animations/utils.js +136 -0
  26. package/dist/src/core/animations/utils.js.map +1 -0
  27. package/dist/src/core/lib/ImageWorker.d.ts +2 -2
  28. package/dist/src/core/lib/ImageWorker.js +30 -11
  29. package/dist/src/core/lib/ImageWorker.js.map +1 -1
  30. package/dist/src/core/lib/WebGlContextWrapper.js +1 -1
  31. package/dist/src/core/lib/WebGlContextWrapper.js.map +1 -1
  32. package/dist/src/core/lib/utils.d.ts +6 -2
  33. package/dist/src/core/lib/utils.js +21 -21
  34. package/dist/src/core/lib/utils.js.map +1 -1
  35. package/dist/src/core/renderers/CoreRenderer.d.ts +1 -31
  36. package/dist/src/core/renderers/CoreRenderer.js.map +1 -1
  37. package/dist/src/core/renderers/CoreShaderNode.d.ts +4 -0
  38. package/dist/src/core/renderers/CoreShaderNode.js +15 -0
  39. package/dist/src/core/renderers/CoreShaderNode.js.map +1 -1
  40. package/dist/src/core/renderers/canvas/CanvasRenderer.d.ts +3 -3
  41. package/dist/src/core/renderers/canvas/CanvasRenderer.js +38 -33
  42. package/dist/src/core/renderers/canvas/CanvasRenderer.js.map +1 -1
  43. package/dist/src/core/renderers/canvas/CanvasShaderNode.d.ts +1 -2
  44. package/dist/src/core/renderers/canvas/CanvasShaderNode.js.map +1 -1
  45. package/dist/src/core/renderers/webgl/SdfRenderOp.d.ts +33 -0
  46. package/dist/src/core/renderers/webgl/SdfRenderOp.js +97 -0
  47. package/dist/src/core/renderers/webgl/SdfRenderOp.js.map +1 -0
  48. package/dist/src/core/renderers/webgl/WebGlCoreCtxTexture.js +1 -1
  49. package/dist/src/core/renderers/webgl/WebGlCoreCtxTexture.js.map +1 -1
  50. package/dist/src/core/renderers/webgl/WebGlCtxTexture.js +12 -8
  51. package/dist/src/core/renderers/webgl/WebGlCtxTexture.js.map +1 -1
  52. package/dist/src/core/renderers/webgl/WebGlRenderOp.d.ts +2 -3
  53. package/dist/src/core/renderers/webgl/WebGlRenderOp.js +1 -3
  54. package/dist/src/core/renderers/webgl/WebGlRenderOp.js.map +1 -1
  55. package/dist/src/core/renderers/webgl/WebGlRenderer.d.ts +6 -18
  56. package/dist/src/core/renderers/webgl/WebGlRenderer.js +48 -61
  57. package/dist/src/core/renderers/webgl/WebGlRenderer.js.map +1 -1
  58. package/dist/src/core/renderers/webgl/WebGlShaderNode.d.ts +2 -4
  59. package/dist/src/core/renderers/webgl/WebGlShaderNode.js.map +1 -1
  60. package/dist/src/core/renderers/webgl/WebGlShaderProgram.d.ts +3 -4
  61. package/dist/src/core/renderers/webgl/WebGlShaderProgram.js +40 -29
  62. package/dist/src/core/renderers/webgl/WebGlShaderProgram.js.map +1 -1
  63. package/dist/src/core/shaders/canvas/Border.d.ts +8 -2
  64. package/dist/src/core/shaders/canvas/Border.js +62 -23
  65. package/dist/src/core/shaders/canvas/Border.js.map +1 -1
  66. package/dist/src/core/shaders/canvas/HolePunch.js +2 -1
  67. package/dist/src/core/shaders/canvas/HolePunch.js.map +1 -1
  68. package/dist/src/core/shaders/canvas/LinearGradient.js +5 -3
  69. package/dist/src/core/shaders/canvas/LinearGradient.js.map +1 -1
  70. package/dist/src/core/shaders/canvas/RadialGradient.js +7 -5
  71. package/dist/src/core/shaders/canvas/RadialGradient.js.map +1 -1
  72. package/dist/src/core/shaders/canvas/Rounded.js +2 -2
  73. package/dist/src/core/shaders/canvas/Rounded.js.map +1 -1
  74. package/dist/src/core/shaders/canvas/RoundedWithBorder.d.ts +6 -3
  75. package/dist/src/core/shaders/canvas/RoundedWithBorder.js +39 -9
  76. package/dist/src/core/shaders/canvas/RoundedWithBorder.js.map +1 -1
  77. package/dist/src/core/shaders/canvas/RoundedWithBorderAndShadow.d.ts +2 -3
  78. package/dist/src/core/shaders/canvas/RoundedWithBorderAndShadow.js +44 -7
  79. package/dist/src/core/shaders/canvas/RoundedWithBorderAndShadow.js.map +1 -1
  80. package/dist/src/core/shaders/canvas/RoundedWithShadow.js +5 -4
  81. package/dist/src/core/shaders/canvas/RoundedWithShadow.js.map +1 -1
  82. package/dist/src/core/shaders/canvas/Shadow.js +4 -2
  83. package/dist/src/core/shaders/canvas/Shadow.js.map +1 -1
  84. package/dist/src/core/shaders/canvas/utils/render.d.ts +1 -1
  85. package/dist/src/core/shaders/canvas/utils/render.js +31 -18
  86. package/dist/src/core/shaders/canvas/utils/render.js.map +1 -1
  87. package/dist/src/core/shaders/templates/BorderTemplate.d.ts +10 -0
  88. package/dist/src/core/shaders/templates/BorderTemplate.js +20 -0
  89. package/dist/src/core/shaders/templates/BorderTemplate.js.map +1 -1
  90. package/dist/src/core/shaders/webgl/Border.js +72 -14
  91. package/dist/src/core/shaders/webgl/Border.js.map +1 -1
  92. package/dist/src/core/shaders/webgl/RoundedWithBorder.js +101 -31
  93. package/dist/src/core/shaders/webgl/RoundedWithBorder.js.map +1 -1
  94. package/dist/src/core/shaders/webgl/RoundedWithBorderAndShadow.js +102 -38
  95. package/dist/src/core/shaders/webgl/RoundedWithBorderAndShadow.js.map +1 -1
  96. package/dist/src/core/shaders/webgl/RoundedWithShadow.js +5 -4
  97. package/dist/src/core/shaders/webgl/RoundedWithShadow.js.map +1 -1
  98. package/dist/src/core/shaders/webgl/SdfShadowShader.d.ts +9 -0
  99. package/dist/src/core/shaders/webgl/SdfShadowShader.js +100 -0
  100. package/dist/src/core/shaders/webgl/SdfShadowShader.js.map +1 -0
  101. package/dist/src/core/shaders/webgl/Shadow.js +12 -6
  102. package/dist/src/core/shaders/webgl/Shadow.js.map +1 -1
  103. package/dist/src/core/text-rendering/SdfTextRenderer.js +12 -20
  104. package/dist/src/core/text-rendering/SdfTextRenderer.js.map +1 -1
  105. package/dist/src/core/utils.d.ts +1 -1
  106. package/dist/src/main-api/Inspector.d.ts +1 -1
  107. package/dist/src/main-api/Inspector.js +4 -1
  108. package/dist/src/main-api/Inspector.js.map +1 -1
  109. package/dist/src/main-api/Renderer.d.ts +10 -0
  110. package/dist/src/main-api/Renderer.js +2 -0
  111. package/dist/src/main-api/Renderer.js.map +1 -1
  112. package/dist/tsconfig.dist.tsbuildinfo +1 -1
  113. package/package.json +1 -1
  114. package/src/core/Autosizer.ts +224 -0
  115. package/src/core/CoreNode.test.ts +116 -2
  116. package/src/core/CoreNode.ts +247 -78
  117. package/src/core/CoreTextNode.ts +3 -5
  118. package/src/core/CoreTextureManager.ts +1 -1
  119. package/src/core/Stage.ts +10 -7
  120. package/src/core/TextureMemoryManager.ts +3 -3
  121. package/src/core/lib/ImageWorker.ts +36 -11
  122. package/src/core/lib/WebGlContextWrapper.ts +1 -1
  123. package/src/core/lib/utils.ts +28 -25
  124. package/src/core/renderers/CoreRenderer.ts +1 -32
  125. package/src/core/renderers/CoreShaderNode.ts +20 -0
  126. package/src/core/renderers/canvas/CanvasRenderer.ts +43 -51
  127. package/src/core/renderers/canvas/CanvasShaderNode.ts +1 -2
  128. package/src/core/renderers/webgl/SdfRenderOp.ts +105 -0
  129. package/src/core/renderers/webgl/WebGlCtxTexture.ts +16 -9
  130. package/src/core/renderers/webgl/WebGlRenderer.ts +56 -78
  131. package/src/core/renderers/webgl/WebGlShaderNode.ts +2 -7
  132. package/src/core/renderers/webgl/WebGlShaderProgram.ts +48 -38
  133. package/src/core/shaders/canvas/Border.ts +86 -29
  134. package/src/core/shaders/canvas/HolePunch.ts +2 -1
  135. package/src/core/shaders/canvas/LinearGradient.ts +8 -6
  136. package/src/core/shaders/canvas/RadialGradient.ts +7 -10
  137. package/src/core/shaders/canvas/Rounded.ts +5 -5
  138. package/src/core/shaders/canvas/RoundedWithBorder.ts +68 -18
  139. package/src/core/shaders/canvas/RoundedWithBorderAndShadow.ts +71 -23
  140. package/src/core/shaders/canvas/RoundedWithShadow.ts +6 -5
  141. package/src/core/shaders/canvas/Shadow.ts +7 -5
  142. package/src/core/shaders/canvas/utils/render.ts +45 -36
  143. package/src/core/shaders/templates/BorderTemplate.ts +30 -0
  144. package/src/core/shaders/webgl/Border.ts +72 -15
  145. package/src/core/shaders/webgl/RoundedWithBorder.ts +101 -31
  146. package/src/core/shaders/webgl/RoundedWithBorderAndShadow.ts +102 -38
  147. package/src/core/shaders/webgl/RoundedWithShadow.ts +5 -4
  148. package/src/core/shaders/webgl/Shadow.ts +12 -6
  149. package/src/core/text-rendering/SdfTextRenderer.ts +18 -21
  150. package/src/main-api/Inspector.ts +6 -3
  151. package/src/main-api/Renderer.ts +13 -0
  152. package/src/core/renderers/webgl/WebGlRenderOp.ts +0 -170
@@ -38,14 +38,14 @@ export const Rounded: CanvasShaderType<RoundedProps, ComputedRoundedValues> = {
38
38
  node.h,
39
39
  );
40
40
  },
41
- render(ctx, quad, renderContext) {
41
+ render(ctx, node, renderContext) {
42
42
  const path = new Path2D();
43
43
  roundRect(
44
44
  path,
45
- quad.tx,
46
- quad.ty,
47
- quad.width,
48
- quad.height,
45
+ node.globalTransform!.tx,
46
+ node.globalTransform!.ty,
47
+ node.props.w,
48
+ node.props.h,
49
49
  this.computed.radius!,
50
50
  );
51
51
  ctx.clip(path);
@@ -26,11 +26,15 @@ import type { ComputedBorderValues } from './Border.js';
26
26
  import type { ComputedRoundedValues } from './Rounded.js';
27
27
  import { roundedRectWithBorder } from './utils/render.js';
28
28
 
29
- type ComputedValues = ComputedRoundedValues & ComputedBorderValues;
29
+ export type ComputedRoundedWithBorderValues = ComputedRoundedValues &
30
+ ComputedBorderValues & {
31
+ outerBorderRadius: Vec4;
32
+ innerBorderRadius: Vec4;
33
+ };
30
34
 
31
35
  export const RoundedWithBorder: CanvasShaderType<
32
36
  RoundedWithBorderProps,
33
- ComputedValues
37
+ ComputedRoundedWithBorderValues
34
38
  > = {
35
39
  props: RoundedWithBorderTemplate.props,
36
40
  saveAndRestore: true,
@@ -44,28 +48,74 @@ export const RoundedWithBorder: CanvasShaderType<
44
48
  this.computed.radius = radius;
45
49
  this.computed.borderColor = this.toColorString(props['border-color']);
46
50
  this.computed.borderAsym = !valuesAreEqual(props['border-w'] as number[]);
47
- //following vec4 convention 0, 1, 2, 3 => x, y, z, w;
48
- const [x, y, z, w] = props['border-w'] as Vec4;
49
- this.computed.borderRadius = [
50
- Math.max(0.0, radius[0] - Math.max(x, w) * 0.5),
51
- Math.max(0.0, radius[1] - Math.max(x, y) * 0.5),
52
- Math.max(0.0, radius[2] - Math.max(z, y) * 0.5),
53
- Math.max(0.0, radius[3] - Math.max(z, w) * 0.5),
51
+ const borderAlign = this.props!['border-align'] as number;
52
+ const borderGap = this.props!['border-gap'] as number;
53
+
54
+ // Calculate outer and inner rectangle dimensions
55
+ const [t, r, b, l] = this.props!['border-w'] as Vec4;
56
+
57
+ const outerX = (this.computed.outerX = -l * borderAlign - borderGap);
58
+ const outerY = (this.computed.outerY = -t * borderAlign - borderGap);
59
+ let outerW = 0;
60
+ let outerH = 0;
61
+
62
+ if (r > 0) {
63
+ outerW += r * borderAlign + borderGap;
64
+ }
65
+ if (l > 0) {
66
+ outerW += l * borderAlign + borderGap;
67
+ }
68
+
69
+ if (b > 0) {
70
+ outerH += b * borderAlign + borderGap;
71
+ }
72
+ if (t > 0) {
73
+ outerH += t * borderAlign + borderGap;
74
+ }
75
+
76
+ this.computed.outerW = outerW;
77
+ this.computed.outerH = outerH;
78
+
79
+ this.computed.innerX = outerX + l;
80
+ this.computed.innerY = outerY + t;
81
+ this.computed.innerW = outerW - l - r;
82
+ this.computed.innerH = outerH - t - b;
83
+
84
+ this.computed.outerBorderRadius = [
85
+ Math.max(0.0, radius[0] + (Math.max(l, r) * borderAlign + borderGap)),
86
+ Math.max(0.0, radius[1] + (Math.max(t, b) * borderAlign + borderGap)),
87
+ Math.max(0.0, radius[2] + (Math.max(b, t) * borderAlign + borderGap)),
88
+ Math.max(0.0, radius[3] + (Math.max(l, r) * borderAlign + borderGap)),
89
+ ];
90
+
91
+ this.computed.innerBorderRadius = [
92
+ Math.max(0.0, this.computed.outerBorderRadius[0] - Math.max(l, r)),
93
+ Math.max(0.0, this.computed.outerBorderRadius[1] - Math.max(t, b)),
94
+ Math.max(0.0, this.computed.outerBorderRadius[2] - Math.max(b, t)),
95
+ Math.max(0.0, this.computed.outerBorderRadius[3] - Math.max(l, r)),
54
96
  ];
55
97
  },
56
- render(ctx, quad, renderContext) {
57
- const computed = this.computed as ComputedValues;
98
+ render(ctx, node, renderContext) {
99
+ const computed = this.computed as ComputedRoundedWithBorderValues;
58
100
  roundedRectWithBorder(
59
101
  ctx,
60
- quad.tx,
61
- quad.ty,
62
- quad.width,
63
- quad.height,
102
+ node.globalTransform!.tx,
103
+ node.globalTransform!.ty,
104
+ node.props.w,
105
+ node.props.h,
64
106
  computed.radius,
65
- this.props!['border-w'] as Vec4,
66
- computed.borderRadius,
107
+ this.props!['border-gap'] as number,
108
+ computed.outerX,
109
+ computed.outerY,
110
+ computed.outerW,
111
+ computed.outerH,
112
+ computed.outerBorderRadius,
113
+ computed.innerX,
114
+ computed.innerY,
115
+ computed.innerW,
116
+ computed.innerH,
117
+ computed.innerBorderRadius,
67
118
  computed.borderColor,
68
- computed.borderAsym,
69
119
  renderContext,
70
120
  );
71
121
  },
@@ -22,14 +22,11 @@ import {
22
22
  RoundedWithBorderAndShadowTemplate,
23
23
  type RoundedWithBorderAndShadowProps,
24
24
  } from '../templates/RoundedWithBorderAndShadowTemplate.js';
25
- import type { ComputedBorderValues } from './Border.js';
26
- import type { ComputedRoundedValues } from './Rounded.js';
25
+ import type { ComputedRoundedWithBorderValues } from './RoundedWithBorder.js';
27
26
  import type { ComputedShadowValues } from './Shadow.js';
28
27
  import * as render from './utils/render.js';
29
28
 
30
- type ComputedValues = ComputedRoundedValues &
31
- ComputedBorderValues &
32
- ComputedShadowValues;
29
+ type ComputedValues = ComputedRoundedWithBorderValues & ComputedShadowValues;
33
30
 
34
31
  export const RoundedWithBorderAndShadow: CanvasShaderType<
35
32
  RoundedWithBorderAndShadowProps,
@@ -47,25 +44,68 @@ export const RoundedWithBorderAndShadow: CanvasShaderType<
47
44
  this.computed.radius = radius;
48
45
  this.computed.borderColor = this.toColorString(props['border-color']);
49
46
  this.computed.borderAsym = !valuesAreEqual(props['border-w'] as number[]);
50
- const borderWidth = props['border-w'] as Vec4;
51
- this.computed.borderRadius = radius.map((value, index) =>
52
- Math.max(0, value - borderWidth[index]! * 0.5),
53
- ) as Vec4;
47
+ const borderAlign = this.props!['border-align'] as number;
48
+ const borderGap = this.props!['border-gap'] as number;
49
+
50
+ // Calculate outer and inner rectangle dimensions
51
+ const [t, r, b, l] = this.props!['border-w'] as Vec4;
52
+
53
+ const outerX = (this.computed.outerX = -l * borderAlign - borderGap);
54
+ const outerY = (this.computed.outerY = -t * borderAlign - borderGap);
55
+ let outerW = 0;
56
+ let outerH = 0;
57
+
58
+ if (r > 0) {
59
+ outerW += r * borderAlign + borderGap;
60
+ }
61
+ if (l > 0) {
62
+ outerW += l * borderAlign + borderGap;
63
+ }
64
+
65
+ if (b > 0) {
66
+ outerH += b * borderAlign + borderGap;
67
+ }
68
+ if (t > 0) {
69
+ outerH += t * borderAlign + borderGap;
70
+ }
71
+
72
+ this.computed.outerW = outerW;
73
+ this.computed.outerH = outerH;
74
+
75
+ this.computed.innerX = outerX + l;
76
+ this.computed.innerY = outerY + t;
77
+ this.computed.innerW = outerW - l - r;
78
+ this.computed.innerH = outerH - t - b;
79
+
80
+ this.computed.outerBorderRadius = [
81
+ Math.max(0.0, radius[0] + (Math.max(l, r) * borderAlign + borderGap)),
82
+ Math.max(0.0, radius[1] + (Math.max(t, b) * borderAlign + borderGap)),
83
+ Math.max(0.0, radius[2] + (Math.max(b, t) * borderAlign + borderGap)),
84
+ Math.max(0.0, radius[3] + (Math.max(l, r) * borderAlign + borderGap)),
85
+ ];
86
+
87
+ this.computed.innerBorderRadius = [
88
+ Math.max(0.0, this.computed.outerBorderRadius[0] - Math.max(l, r)),
89
+ Math.max(0.0, this.computed.outerBorderRadius[1] - Math.max(t, b)),
90
+ Math.max(0.0, this.computed.outerBorderRadius[2] - Math.max(b, t)),
91
+ Math.max(0.0, this.computed.outerBorderRadius[3] - Math.max(l, r)),
92
+ ];
54
93
 
55
94
  this.computed.shadowColor = this.toColorString(props['shadow-color']);
56
- this.computed.shadowRadius = radius.map(
95
+ this.computed.shadowRadius = this.computed.outerBorderRadius.map(
57
96
  (value) => value + props['shadow-blur'],
58
97
  ) as Vec4;
59
98
  },
60
- render(ctx, quad, renderContext) {
61
- const { tx, ty, width, height } = quad;
99
+ render(ctx, node, renderContext) {
100
+ const { tx, ty } = node.globalTransform!;
101
+ const { w, h } = node.props;
62
102
  const computed = this.computed as ComputedValues;
63
103
  render.shadow(
64
104
  ctx,
65
- tx,
66
- ty,
67
- height,
68
- width,
105
+ tx + computed.outerX,
106
+ ty + computed.outerY,
107
+ w + computed.outerW,
108
+ h + computed.outerH,
69
109
  computed.shadowColor,
70
110
  this.props!['shadow-projection'],
71
111
  computed.shadowRadius,
@@ -73,15 +113,23 @@ export const RoundedWithBorderAndShadow: CanvasShaderType<
73
113
  );
74
114
  render.roundedRectWithBorder(
75
115
  ctx,
76
- quad.tx,
77
- quad.ty,
78
- quad.width,
79
- quad.height,
116
+ tx,
117
+ ty,
118
+ w,
119
+ h,
80
120
  computed.radius,
81
- this.props!['border-w'] as Vec4,
82
- computed.borderRadius,
121
+ this.props!['border-gap'] as number,
122
+ computed.outerX,
123
+ computed.outerY,
124
+ computed.outerW,
125
+ computed.outerH,
126
+ computed.outerBorderRadius,
127
+ computed.innerX,
128
+ computed.innerY,
129
+ computed.innerW,
130
+ computed.innerH,
131
+ computed.innerBorderRadius,
83
132
  computed.borderColor,
84
- computed.borderAsym,
85
133
  renderContext,
86
134
  );
87
135
  },
@@ -47,15 +47,16 @@ export const RoundedWithShadow: CanvasShaderType<
47
47
  (value) => value + props['shadow-blur'],
48
48
  ) as Vec4;
49
49
  },
50
- render(ctx, quad, renderContext) {
51
- const { tx, ty, width, height } = quad;
50
+ render(ctx, node, renderContext) {
51
+ const { tx, ty } = node.globalTransform!;
52
+ const { w, h } = node.props;
52
53
  const computed = this.computed as ComputedValues;
53
54
  render.shadow(
54
55
  ctx,
55
56
  tx,
56
57
  ty,
57
- width,
58
- height,
58
+ w,
59
+ h,
59
60
  computed.shadowColor,
60
61
  this.props!['shadow-projection'],
61
62
  computed.shadowRadius,
@@ -63,7 +64,7 @@ export const RoundedWithShadow: CanvasShaderType<
63
64
  );
64
65
 
65
66
  const path = new Path2D();
66
- render.roundRect(path, tx, ty, width, height, computed.radius);
67
+ render.roundRect(path, tx, ty, w, h, computed.radius);
67
68
  ctx.clip(path);
68
69
  renderContext();
69
70
  },
@@ -35,13 +35,15 @@ export const Shadow: CanvasShaderType<ShadowProps, ComputedShadowValues> = {
35
35
  const blur = this.props!['blur'];
36
36
  this.computed.shadowRadius = [blur, blur, blur, blur];
37
37
  },
38
- render(ctx, quad, renderContext) {
38
+ render(ctx, node, renderContext) {
39
+ const { tx, ty } = node.globalTransform!;
40
+ const { w, h } = node.props;
39
41
  shadow(
40
42
  ctx,
41
- quad.tx,
42
- quad.ty,
43
- quad.width,
44
- quad.height,
43
+ tx,
44
+ ty,
45
+ w,
46
+ h,
45
47
  this.computed.shadowColor!,
46
48
  this.props!['projection'],
47
49
  this.computed.shadowRadius!,
@@ -48,55 +48,64 @@ export function roundedRectWithBorder(
48
48
  width: number,
49
49
  height: number,
50
50
  radius: Vec4,
51
- borderWidth: Vec4,
52
- borderRadius: Vec4,
51
+ borderGap: number,
52
+ outerX: number,
53
+ outerY: number,
54
+ outerW: number,
55
+ outerH: number,
56
+ outerBorderRadius: Vec4,
57
+ innerX: number,
58
+ innerY: number,
59
+ innerW: number,
60
+ innerH: number,
61
+ innerBorderRadius: Vec4,
53
62
  borderColor: string,
54
- borderAsym: boolean,
55
63
  renderContext: () => void,
56
64
  ) {
57
- if (borderAsym === false) {
58
- const bWidth = borderWidth[0];
59
- const bHalfWidth = bWidth * 0.5;
65
+ outerX += x;
66
+ outerY += y;
67
+ outerW += width;
68
+ outerH += height;
69
+
70
+ innerX += x;
71
+ innerY += y;
72
+ innerW += width;
73
+ innerH += height;
74
+
75
+ // no gap render strategy - to avoid artifacts between border and node
76
+ if (borderGap === 0) {
77
+ //draw outer border rounded rect
78
+ ctx.beginPath();
79
+ roundRect(ctx, outerX, outerY, outerW, outerH, outerBorderRadius);
80
+ ctx.fillStyle = borderColor;
81
+ ctx.fill();
82
+ ctx.closePath();
83
+
60
84
  const path = new Path2D();
61
- roundRect(path, x, y, width, height, radius);
85
+ roundRect(path, innerX, innerY, innerW, innerH, innerBorderRadius as Vec4);
62
86
  ctx.clip(path);
63
-
64
87
  renderContext();
65
-
66
- if (bWidth > 0) {
67
- ctx.beginPath();
68
- ctx.lineWidth = bWidth;
69
- ctx.strokeStyle = borderColor;
70
- roundRect(
71
- ctx,
72
- x + bHalfWidth,
73
- y + bHalfWidth,
74
- width - bWidth,
75
- height - bWidth,
76
- borderRadius,
77
- );
78
- ctx.stroke();
79
- }
80
88
  return;
81
89
  }
82
90
 
83
- ctx.beginPath();
84
- roundRect(ctx, x, y, width, height, radius as Vec4);
85
- ctx.fillStyle = borderColor;
86
- ctx.fill();
87
- const { 0: t, 1: r, 2: b, 3: l } = borderWidth as Vec4;
91
+ // with gap render strategy
88
92
 
93
+ //draw node content first
94
+ ctx.save();
89
95
  const path = new Path2D();
90
- roundRect(
91
- path,
92
- x + l,
93
- y + t,
94
- width - (l + r),
95
- height - (t + b),
96
- borderRadius as Vec4,
97
- );
96
+ roundRect(path, x, y, width, height, radius);
98
97
  ctx.clip(path);
99
98
  renderContext();
99
+ ctx.restore();
100
+
101
+ //draw border by clipping inner area from outer area
102
+ ctx.save();
103
+ const borderPath = new Path2D();
104
+ roundRect(borderPath, outerX, outerY, outerW, outerH, outerBorderRadius);
105
+ roundRect(borderPath, innerX, innerY, innerW, innerH, innerBorderRadius);
106
+ ctx.fillStyle = borderColor;
107
+ ctx.fill(borderPath, 'evenodd');
108
+ ctx.restore();
100
109
  }
101
110
 
102
111
  export function shadow(
@@ -35,6 +35,16 @@ export interface BorderProps {
35
35
  * @default 0xffffffff
36
36
  */
37
37
  color: number;
38
+ /**
39
+ * Alignment of the border
40
+ *
41
+ * @default 'inside'
42
+ */
43
+ align: number | 'inside' | 'center' | 'outside';
44
+ /**
45
+ * Gap between node and border baseline. Default is 0. and generally edge of node)
46
+ */
47
+ gap: number;
38
48
  /**
39
49
  * Top width
40
50
  */
@@ -69,6 +79,26 @@ export function getBorderProps<P extends string>(
69
79
  },
70
80
  },
71
81
  [pf + 'color']: 0xffffffff,
82
+ [pf + 'align']: {
83
+ default: 0, //inside,
84
+ resolve(value) {
85
+ if (!isNaN(value)) {
86
+ return value as number;
87
+ }
88
+ if (typeof value === 'string') {
89
+ switch (value) {
90
+ case 'inside':
91
+ return 0;
92
+ case 'center':
93
+ return 0.5;
94
+ case 'outside':
95
+ return 1;
96
+ }
97
+ }
98
+ return this.default;
99
+ },
100
+ },
101
+ [pf + 'gap']: 0,
72
102
  [pf + 'top']: {
73
103
  default: 0,
74
104
  set(value, props) {
@@ -14,8 +14,6 @@
14
14
  *
15
15
  * SPDX-License-Identifier: Apache-2.0
16
16
  */
17
-
18
- import { valuesAreEqual } from '../../lib/utils.js';
19
17
  import {
20
18
  BorderTemplate,
21
19
  type BorderProps,
@@ -28,6 +26,8 @@ export const Border: WebGlShaderType<BorderProps> = {
28
26
  update(node) {
29
27
  this.uniform4fa('u_borderWidth', this.props!.w as Vec4);
30
28
  this.uniformRGBA('u_borderColor', this.props!.color);
29
+ this.uniform1f('u_borderGap', this.props!.gap as number);
30
+ this.uniform1f('u_borderAlign', this.props!.align as number);
31
31
  },
32
32
  vertex: `
33
33
  # ifdef GL_FRAGMENT_PRECISION_HIGH
@@ -47,27 +47,67 @@ export const Border: WebGlShaderType<BorderProps> = {
47
47
 
48
48
  uniform vec4 u_radius;
49
49
  uniform vec4 u_borderWidth;
50
+ uniform float u_borderGap;
51
+ uniform float u_borderAlign;
50
52
 
51
53
  varying vec4 v_color;
52
54
  varying vec2 v_textureCoords;
53
55
  varying vec2 v_nodeCoords;
54
56
 
55
57
  varying vec2 v_innerSize;
58
+ varying vec2 v_outerSize;
59
+ varying vec2 v_outerBorderUv;
60
+ varying vec2 v_innerBorderUv;
56
61
  varying vec2 v_halfDimensions;
62
+ varying float v_edgeWidth;
57
63
 
58
64
  void main() {
59
- vec2 normalized = a_position * u_pixelRatio;
60
65
  vec2 screenSpace = vec2(2.0 / u_resolution.x, -2.0 / u_resolution.y);
66
+ vec2 edge = clamp(a_nodeCoords * 2.0 - vec2(1.0), -1.0, 1.0);
67
+ v_edgeWidth = 1.0 / u_pixelRatio;
68
+
69
+ float borderTop = u_borderWidth.x;
70
+ float borderRight = u_borderWidth.y;
71
+ float borderBottom = u_borderWidth.z;
72
+ float borderLeft = u_borderWidth.w;
73
+
74
+ v_outerBorderUv = vec2(0.0);
75
+ v_innerBorderUv = vec2(0.0);
76
+
77
+ vec2 borderSize = vec2(borderRight + borderLeft, borderTop + borderBottom);
78
+ vec2 extraSize = borderSize * u_borderAlign;
79
+ float gapLeft = step(0.001, borderLeft) * u_borderGap;
80
+ float gapRight = step(0.001, borderRight) * u_borderGap;
81
+ float gapTop = step(0.001, borderTop) * u_borderGap;
82
+ float gapBottom = step(0.001, borderBottom) * u_borderGap;
83
+ vec2 gapSize = vec2(gapLeft + gapRight, gapTop + gapBottom);
84
+
85
+ v_outerSize = (u_dimensions + gapSize + extraSize) * 0.5;
86
+ v_innerSize = v_outerSize - borderSize * 0.5;
87
+
88
+ // Use sign() to avoid branching
89
+ vec2 borderDiff = vec2(borderRight - borderLeft, borderBottom - borderTop);
90
+ vec2 signDiff = sign(borderDiff);
91
+ borderDiff = abs(borderDiff);
92
+
93
+ vec2 gapDiff = vec2(gapRight - gapLeft, gapBottom - gapTop);
94
+ vec2 signGapDiff = sign(gapDiff);
95
+ gapDiff = abs(gapDiff);
96
+
97
+ v_outerBorderUv = -signDiff * borderDiff * u_borderAlign * 0.5 - signGapDiff * gapDiff * 0.5;
98
+ v_innerBorderUv = v_outerBorderUv + signDiff * borderDiff * 0.5;
99
+
100
+ vec2 edgeOffsetExtra = step(u_dimensions * 0.5, v_outerSize) * edge * (extraSize + u_borderGap);
101
+ vec2 borderEdge = edgeOffsetExtra;
102
+
103
+ vec2 vertexPos = (a_position + edge + borderEdge) * u_pixelRatio;
104
+ gl_Position = vec4(vertexPos.x * screenSpace.x - 1.0, -sign(screenSpace.y) * (vertexPos.y * -abs(screenSpace.y)) + 1.0, 0.0, 1.0);
61
105
 
62
106
  v_color = a_color;
63
- v_nodeCoords = a_nodeCoords;
64
- v_textureCoords = a_textureCoords;
107
+ v_nodeCoords = a_nodeCoords + (screenSpace + borderEdge) / (u_dimensions);
108
+ v_textureCoords = a_textureCoords + (screenSpace + borderEdge) / (u_dimensions);
65
109
 
66
110
  v_halfDimensions = u_dimensions * 0.5;
67
- v_innerSize = vec2(u_dimensions.x - (u_borderWidth[3] + u_borderWidth[1]), u_dimensions.y - (u_borderWidth[0] + u_borderWidth[2])) * 0.5 - 0.5;
68
-
69
- gl_Position = vec4(normalized.x * screenSpace.x - 1.0, normalized.y * -abs(screenSpace.y) + 1.0, 0.0, 1.0);
70
- gl_Position.y = -sign(screenSpace.y) * gl_Position.y;
71
111
  }
72
112
  `,
73
113
  fragment: `
@@ -86,13 +126,18 @@ export const Border: WebGlShaderType<BorderProps> = {
86
126
 
87
127
  uniform vec4 u_borderWidth;
88
128
  uniform vec4 u_borderColor;
129
+ uniform float u_borderGap;
89
130
 
90
131
  varying vec4 v_color;
91
132
  varying vec2 v_nodeCoords;
92
133
  varying vec2 v_textureCoords;
93
134
 
94
135
  varying vec2 v_innerSize;
136
+ varying vec2 v_outerSize;
137
+ varying vec2 v_outerBorderUv;
138
+ varying vec2 v_innerBorderUv;
95
139
  varying vec2 v_halfDimensions;
140
+ varying float v_edgeWidth;
96
141
 
97
142
  float box(vec2 p, vec2 s) {
98
143
  vec2 q = abs(p) - s;
@@ -101,16 +146,28 @@ export const Border: WebGlShaderType<BorderProps> = {
101
146
 
102
147
  void main() {
103
148
  vec4 color = texture2D(u_texture, v_textureCoords) * v_color;
149
+ vec4 resultColor = vec4(0.0);
104
150
  vec2 boxUv = v_nodeCoords.xy * u_dimensions - v_halfDimensions;
105
151
 
106
- boxUv.x += u_borderWidth.y > u_borderWidth.w ? (u_borderWidth.y - u_borderWidth.w) * 0.5 : -(u_borderWidth.w - u_borderWidth.y) * 0.5;
107
- boxUv.y += u_borderWidth.z > u_borderWidth.x ? (u_borderWidth.z - u_borderWidth.x) * 0.5 : -(u_borderWidth.x - u_borderWidth.z) * 0.5;
152
+ float outerDist = box(boxUv + v_outerBorderUv, v_outerSize - v_edgeWidth);
153
+ float innerDist = box(boxUv + v_innerBorderUv, v_innerSize - v_edgeWidth);
154
+
155
+ if(u_borderGap == 0.0) {
156
+ resultColor = mix(resultColor, u_borderColor, 1.0 - smoothstep(-0.5 * v_edgeWidth, 0.5 * v_edgeWidth, outerDist));
157
+ resultColor = mix(resultColor, color, 1.0 - smoothstep(-0.5 * v_edgeWidth, 0.5 * v_edgeWidth, innerDist));
158
+ gl_FragColor = resultColor * u_alpha;
159
+ return;
160
+ }
161
+
162
+ float nodeDist = box(boxUv, v_halfDimensions - v_edgeWidth);
163
+ float nodeAlpha = 1.0 - smoothstep(-0.5 * v_edgeWidth, 0.5 * v_edgeWidth, nodeDist);
108
164
 
109
- float innerDist = box(boxUv, v_innerSize);
110
- float innerAlpha = 1.0 - smoothstep(0.0, 1.0, innerDist);
165
+ float borderDist = max(-innerDist, outerDist);
166
+ float borderAlpha = 1.0 - smoothstep(-0.5 * v_edgeWidth, 0.5 * v_edgeWidth, borderDist);
167
+ resultColor = mix(resultColor, color, nodeAlpha);
168
+ resultColor = mix(resultColor, u_borderColor, borderAlpha * u_borderColor.a);
111
169
 
112
- vec4 resColor = mix(u_borderColor, color, innerAlpha);
113
- gl_FragColor = resColor * u_alpha;
170
+ gl_FragColor = resultColor * u_alpha;
114
171
  }
115
172
  `,
116
173
  };