@lightningtv/solid 3.0.0-21 → 3.0.0-23
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/src/core/animation.js +1 -2
- package/dist/src/core/animation.js.map +1 -1
- package/dist/src/core/config.d.ts +2 -0
- package/dist/src/core/config.js +10 -0
- package/dist/src/core/config.js.map +1 -1
- package/dist/src/core/dom-renderer/domRenderer.d.ts +137 -0
- package/dist/src/core/dom-renderer/domRenderer.js +1545 -0
- package/dist/src/core/dom-renderer/domRenderer.js.map +1 -0
- package/dist/src/core/dom-renderer/domRendererTypes.d.ts +117 -0
- package/dist/src/core/dom-renderer/domRendererTypes.js +2 -0
- package/dist/src/core/dom-renderer/domRendererTypes.js.map +1 -0
- package/dist/src/core/dom-renderer/domRendererUtils.d.ts +16 -0
- package/dist/src/core/dom-renderer/domRendererUtils.js +132 -0
- package/dist/src/core/dom-renderer/domRendererUtils.js.map +1 -0
- package/dist/src/core/elementNode.d.ts +260 -6
- package/dist/src/core/elementNode.js +22 -18
- package/dist/src/core/elementNode.js.map +1 -1
- package/dist/src/core/focusManager.js +10 -3
- package/dist/src/core/focusManager.js.map +1 -1
- package/dist/src/primitives/utils/createSpriteMap.js +2 -2
- package/dist/src/primitives/utils/createSpriteMap.js.map +1 -1
- package/dist/src/primitives/utils/handleNavigation.js +3 -3
- package/dist/src/primitives/utils/handleNavigation.js.map +1 -1
- package/dist/src/render.d.ts +1 -1
- package/dist/src/shaders/RoundedWithBorder.d.ts +3 -0
- package/dist/src/shaders/RoundedWithBorder.js +217 -0
- package/dist/src/shaders/RoundedWithBorder.js.map +1 -0
- package/dist/src/shaders/index.d.ts +2 -0
- package/dist/src/shaders/index.js +3 -0
- package/dist/src/shaders/index.js.map +1 -0
- package/dist/src/shaders/templates/RoundedWithBorderTemplate.d.ts +20 -0
- package/dist/src/shaders/templates/RoundedWithBorderTemplate.js +93 -0
- package/dist/src/shaders/templates/RoundedWithBorderTemplate.js.map +1 -0
- package/dist/src/shaders/utils.d.ts +3 -0
- package/dist/src/shaders/utils.js +31 -0
- package/dist/src/shaders/utils.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +11 -1
- package/src/core/animation.ts +4 -2
- package/src/core/config.ts +12 -0
- package/src/core/elementNode.ts +285 -30
- package/src/core/focusManager.ts +11 -1
- package/src/primitives/utils/createSpriteMap.ts +2 -2
- package/src/primitives/utils/handleNavigation.ts +3 -3
- package/src/shaders/RoundedWithBorder.ts +245 -0
- package/src/shaders/index.ts +2 -0
- package/src/shaders/templates/RoundedWithBorderTemplate.ts +110 -0
- package/src/shaders/utils.ts +44 -0
- package/src/core/timings.ts +0 -261
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
import { calcFactoredRadiusArray, type Vec4 } from './utils.js';
|
|
2
|
+
import type { WebGlShaderType } from '@lightningjs/renderer/webgl';
|
|
3
|
+
import {
|
|
4
|
+
RoundedWithBorderTemplate,
|
|
5
|
+
type RoundedWithBorderProps,
|
|
6
|
+
} from './templates/RoundedWithBorderTemplate.js';
|
|
7
|
+
|
|
8
|
+
interface CoreNode {
|
|
9
|
+
w: number;
|
|
10
|
+
h: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const RoundedWithBorder: WebGlShaderType<RoundedWithBorderProps> = {
|
|
14
|
+
props: RoundedWithBorderTemplate.props,
|
|
15
|
+
update(node: CoreNode) {
|
|
16
|
+
const props = this.props!;
|
|
17
|
+
const borderWidth = props['border-w'] as Vec4;
|
|
18
|
+
const borderGap = props['border-gap'] || 0;
|
|
19
|
+
|
|
20
|
+
this.uniformRGBA('u_borderColor', props['border-color']);
|
|
21
|
+
this.uniform4fa('u_borderWidth', borderWidth);
|
|
22
|
+
this.uniform1f('u_borderGap', borderGap);
|
|
23
|
+
this.uniformRGBA('u_borderGapColor', props['border-gapColor']);
|
|
24
|
+
|
|
25
|
+
const origWidth = node.w;
|
|
26
|
+
const origHeight = node.h;
|
|
27
|
+
this.uniform2f('u_dimensions_orig', origWidth, origHeight);
|
|
28
|
+
|
|
29
|
+
const expandedWidth =
|
|
30
|
+
origWidth + borderWidth[3] + borderWidth[1] + borderGap * 2; // original + left + right + 2*gap
|
|
31
|
+
const expandedHeight =
|
|
32
|
+
origHeight + borderWidth[0] + borderWidth[2] + borderGap * 2; // original + top + bottom + 2*gap
|
|
33
|
+
|
|
34
|
+
// u_dimensions for the shader's SDF functions should be the expanded size
|
|
35
|
+
this.uniform2f('u_dimensions', expandedWidth, expandedHeight);
|
|
36
|
+
|
|
37
|
+
// The `radius` property is for the content rectangle.
|
|
38
|
+
// Factor it against the original dimensions to prevent self-intersection.
|
|
39
|
+
const contentRadius = calcFactoredRadiusArray(
|
|
40
|
+
this.props!.radius as Vec4,
|
|
41
|
+
origWidth,
|
|
42
|
+
origHeight,
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
// From the content radius, calculate the outer radius of the border.
|
|
46
|
+
// For each corner, the total radius is content radius + gap + border thickness.
|
|
47
|
+
// Border thickness at a corner is approximated as the max of the two adjacent border sides.
|
|
48
|
+
const bTop = borderWidth[0],
|
|
49
|
+
bRight = borderWidth[1],
|
|
50
|
+
bBottom = borderWidth[2],
|
|
51
|
+
bLeft = borderWidth[3];
|
|
52
|
+
const outerRadius: Vec4 = [
|
|
53
|
+
Math.max(0, contentRadius[0] + borderGap + Math.max(bTop, bLeft)), // top-left
|
|
54
|
+
Math.max(0, contentRadius[1] + borderGap + Math.max(bTop, bRight)), // top-right
|
|
55
|
+
Math.max(0, contentRadius[2] + borderGap + Math.max(bBottom, bRight)), // bottom-right
|
|
56
|
+
Math.max(0, contentRadius[3] + borderGap + Math.max(bBottom, bLeft)), // bottom-left
|
|
57
|
+
];
|
|
58
|
+
|
|
59
|
+
// The final radius passed to the shader is the outer radius of the whole shape.
|
|
60
|
+
// It also needs to be factored against the expanded dimensions.
|
|
61
|
+
// The shader will then work inwards to calculate the radii for the gap and content.
|
|
62
|
+
this.uniform4fa(
|
|
63
|
+
'u_radius',
|
|
64
|
+
calcFactoredRadiusArray(outerRadius, expandedWidth, expandedHeight),
|
|
65
|
+
);
|
|
66
|
+
},
|
|
67
|
+
vertex: `
|
|
68
|
+
# ifdef GL_FRAGMENT_PRECISION_HIGH
|
|
69
|
+
precision highp float;
|
|
70
|
+
# else
|
|
71
|
+
precision mediump float;
|
|
72
|
+
# endif
|
|
73
|
+
|
|
74
|
+
attribute vec2 a_position;
|
|
75
|
+
attribute vec2 a_textureCoords;
|
|
76
|
+
attribute vec4 a_color;
|
|
77
|
+
attribute vec2 a_nodeCoords;
|
|
78
|
+
|
|
79
|
+
uniform vec2 u_resolution;
|
|
80
|
+
uniform float u_pixelRatio;
|
|
81
|
+
uniform vec2 u_dimensions;
|
|
82
|
+
uniform vec2 u_dimensions_orig;
|
|
83
|
+
|
|
84
|
+
uniform vec4 u_radius;
|
|
85
|
+
uniform vec4 u_borderWidth;
|
|
86
|
+
uniform float u_borderGap;
|
|
87
|
+
|
|
88
|
+
varying vec4 v_color;
|
|
89
|
+
varying vec2 v_textureCoords;
|
|
90
|
+
varying vec2 v_nodeCoords;
|
|
91
|
+
varying vec4 v_borderEndRadius;
|
|
92
|
+
varying vec2 v_borderEndSize;
|
|
93
|
+
|
|
94
|
+
varying vec4 v_innerRadius;
|
|
95
|
+
varying vec2 v_innerSize;
|
|
96
|
+
varying vec2 v_halfDimensions;
|
|
97
|
+
varying float v_borderZero;
|
|
98
|
+
|
|
99
|
+
void main() {
|
|
100
|
+
vec2 screenSpace = vec2(2.0 / u_resolution.x, -2.0 / u_resolution.y);
|
|
101
|
+
|
|
102
|
+
v_color = a_color;
|
|
103
|
+
v_nodeCoords = a_nodeCoords;
|
|
104
|
+
|
|
105
|
+
float bTop = u_borderWidth.x;
|
|
106
|
+
float bRight = u_borderWidth.y;
|
|
107
|
+
float bBottom = u_borderWidth.z;
|
|
108
|
+
float bLeft = u_borderWidth.w;
|
|
109
|
+
float gap = u_borderGap;
|
|
110
|
+
|
|
111
|
+
// Calculate the offset to expand the quad for border and gap
|
|
112
|
+
vec2 expansionOffset = vec2(0.0);
|
|
113
|
+
if (a_nodeCoords.x == 0.0) { // Left edge vertex
|
|
114
|
+
expansionOffset.x = -(bLeft + gap);
|
|
115
|
+
} else { // Right edge vertex (a_nodeCoords.x == 1.0)
|
|
116
|
+
expansionOffset.x = (bRight + gap);
|
|
117
|
+
}
|
|
118
|
+
if (a_nodeCoords.y == 0.0) { // Top edge vertex
|
|
119
|
+
expansionOffset.y = -(bTop + gap);
|
|
120
|
+
} else { // Bottom edge vertex (a_nodeCoords.y == 1.0)
|
|
121
|
+
expansionOffset.y = (bBottom + gap);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
vec2 expanded_a_position = a_position + expansionOffset;
|
|
125
|
+
vec2 normalized = expanded_a_position * u_pixelRatio;
|
|
126
|
+
|
|
127
|
+
// u_dimensions is expanded, u_dimensions_orig is original content size
|
|
128
|
+
v_textureCoords.x = (a_textureCoords.x * u_dimensions.x - (bLeft + gap)) / u_dimensions_orig.x;
|
|
129
|
+
v_textureCoords.y = (a_textureCoords.y * u_dimensions.y - (bTop + gap)) / u_dimensions_orig.y;
|
|
130
|
+
|
|
131
|
+
v_borderZero = (u_borderWidth.x == 0.0 && u_borderWidth.y == 0.0 && u_borderWidth.z == 0.0 && u_borderWidth.w == 0.0) ? 1.0 : 0.0;
|
|
132
|
+
// If there's no border, there's no gap from the border logic perspective
|
|
133
|
+
// The Rounded shader itself would handle radius if borderZero is true.
|
|
134
|
+
v_halfDimensions = u_dimensions * 0.5; // u_dimensions is now expanded_dimensions
|
|
135
|
+
if(v_borderZero == 0.0) {
|
|
136
|
+
// Calculate radius and size for the inner edge of the border (where the gap begins)
|
|
137
|
+
v_borderEndRadius = vec4(
|
|
138
|
+
max(0.0, u_radius.x - max(bTop, bLeft) - 0.5),
|
|
139
|
+
max(0.0, u_radius.y - max(bTop, bRight) - 0.5),
|
|
140
|
+
max(0.0, u_radius.z - max(bBottom, bRight) - 0.5),
|
|
141
|
+
max(0.0, u_radius.w - max(bBottom, bLeft) - 0.5)
|
|
142
|
+
);
|
|
143
|
+
v_borderEndSize = vec2(
|
|
144
|
+
(u_dimensions.x - (bLeft + bRight) - 1.0),
|
|
145
|
+
(u_dimensions.y - (bTop + bBottom) - 1.0)
|
|
146
|
+
) * 0.5;
|
|
147
|
+
|
|
148
|
+
// Calculate radius and size for the content area (after the gap)
|
|
149
|
+
v_innerRadius = vec4(
|
|
150
|
+
max(0.0, u_radius.x - max(bTop, bLeft) - u_borderGap - 0.5),
|
|
151
|
+
max(0.0, u_radius.y - max(bTop, bRight) - u_borderGap - 0.5),
|
|
152
|
+
max(0.0, u_radius.z - max(bBottom, bRight) - u_borderGap - 0.5),
|
|
153
|
+
max(0.0, u_radius.w - max(bBottom, bLeft) - u_borderGap - 0.5)
|
|
154
|
+
);
|
|
155
|
+
v_innerSize = vec2(
|
|
156
|
+
(u_dimensions.x - (bLeft + bRight) - (u_borderGap * 2.0) - 1.0),
|
|
157
|
+
(u_dimensions.y - (bTop + bBottom) - (u_borderGap * 2.0) - 1.0)
|
|
158
|
+
) * 0.5;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
gl_Position = vec4(normalized.x * screenSpace.x - 1.0, normalized.y * -abs(screenSpace.y) + 1.0, 0.0, 1.0);
|
|
162
|
+
gl_Position.y = -sign(screenSpace.y) * gl_Position.y;
|
|
163
|
+
}
|
|
164
|
+
`,
|
|
165
|
+
fragment: `
|
|
166
|
+
# ifdef GL_FRAGMENT_PRECISION_HIGH
|
|
167
|
+
precision highp float;
|
|
168
|
+
# else
|
|
169
|
+
precision mediump float;
|
|
170
|
+
# endif
|
|
171
|
+
|
|
172
|
+
uniform vec2 u_resolution;
|
|
173
|
+
uniform float u_pixelRatio;
|
|
174
|
+
uniform float u_alpha;
|
|
175
|
+
uniform vec2 u_dimensions;
|
|
176
|
+
uniform sampler2D u_texture;
|
|
177
|
+
|
|
178
|
+
uniform vec4 u_radius;
|
|
179
|
+
|
|
180
|
+
uniform vec4 u_borderWidth;
|
|
181
|
+
uniform vec4 u_borderColor;
|
|
182
|
+
uniform vec4 u_borderGapColor;
|
|
183
|
+
|
|
184
|
+
varying vec4 v_borderEndRadius;
|
|
185
|
+
varying vec2 v_borderEndSize;
|
|
186
|
+
|
|
187
|
+
varying vec4 v_color;
|
|
188
|
+
varying vec2 v_textureCoords;
|
|
189
|
+
varying vec2 v_nodeCoords;
|
|
190
|
+
|
|
191
|
+
varying vec2 v_halfDimensions;
|
|
192
|
+
varying vec4 v_innerRadius;
|
|
193
|
+
varying vec2 v_innerSize;
|
|
194
|
+
varying float v_borderZero;
|
|
195
|
+
|
|
196
|
+
float roundedBox(vec2 p, vec2 s, vec4 r) {
|
|
197
|
+
r.xy = (p.x > 0.0) ? r.yz : r.xw;
|
|
198
|
+
r.x = (p.y > 0.0) ? r.y : r.x;
|
|
199
|
+
vec2 q = abs(p) - s + r.x;
|
|
200
|
+
return (min(max(q.x, q.y), 0.0) + length(max(q, 0.0))) - r.x;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
void main() {
|
|
204
|
+
vec4 contentTexColor = texture2D(u_texture, v_textureCoords) * v_color;
|
|
205
|
+
|
|
206
|
+
vec2 boxUv = v_nodeCoords.xy * u_dimensions - v_halfDimensions;
|
|
207
|
+
float outerShapeDist = roundedBox(boxUv, v_halfDimensions, u_radius);
|
|
208
|
+
|
|
209
|
+
float edgeWidth = 1.0 / u_pixelRatio;
|
|
210
|
+
float outerShapeAlpha = 1.0 - smoothstep(-0.5 * edgeWidth, 0.5 * edgeWidth, outerShapeDist);
|
|
211
|
+
|
|
212
|
+
if(v_borderZero == 1.0) { // No border, effectively no gap from border logic
|
|
213
|
+
gl_FragColor = mix(vec4(0.0), contentTexColor, outerShapeAlpha) * u_alpha;
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// Adjust boxUv for non-uniform borders
|
|
218
|
+
vec2 adjustedBoxUv = boxUv;
|
|
219
|
+
adjustedBoxUv.x += (u_borderWidth.y - u_borderWidth.w) * 0.5;
|
|
220
|
+
adjustedBoxUv.y += (u_borderWidth.z - u_borderWidth.x) * 0.5;
|
|
221
|
+
|
|
222
|
+
// Inner Border Edge (Gap starts here)
|
|
223
|
+
float borderEndDist = roundedBox(adjustedBoxUv, v_borderEndSize, v_borderEndRadius);
|
|
224
|
+
float borderEndAlpha = 1.0 - smoothstep(-0.5 * edgeWidth, 0.5 * edgeWidth, borderEndDist);
|
|
225
|
+
|
|
226
|
+
// Content Area (Gap ends here)
|
|
227
|
+
float contentDist = roundedBox(adjustedBoxUv, v_innerSize, v_innerRadius);
|
|
228
|
+
float contentAlpha = 1.0 - smoothstep(-0.5 * edgeWidth, 0.5 * edgeWidth, contentDist);
|
|
229
|
+
|
|
230
|
+
// Calculate Masks for mutually exclusive regions based on priority (Border Top, Gap Middle, Content Bottom)
|
|
231
|
+
float borderMask = clamp(outerShapeAlpha - borderEndAlpha, 0.0, 1.0);
|
|
232
|
+
float gapMask = clamp(borderEndAlpha - contentAlpha, 0.0, 1.0);
|
|
233
|
+
|
|
234
|
+
// Composite Layers
|
|
235
|
+
// 1. Content
|
|
236
|
+
vec4 composite = mix(vec4(0.0), contentTexColor, contentAlpha);
|
|
237
|
+
// 2. Gap
|
|
238
|
+
composite = mix(composite, u_borderGapColor, gapMask);
|
|
239
|
+
// 3. Border
|
|
240
|
+
composite = mix(composite, u_borderColor, borderMask);
|
|
241
|
+
|
|
242
|
+
gl_FragColor = composite * u_alpha;
|
|
243
|
+
}
|
|
244
|
+
`,
|
|
245
|
+
};
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import type { ShaderProps } from '@lightningjs/renderer';
|
|
2
|
+
import { toValidVec4, type Vec4 } from '../utils.js';
|
|
3
|
+
|
|
4
|
+
export interface RoundedWithBorderProps {
|
|
5
|
+
radius: Vec4;
|
|
6
|
+
'border-w': Vec4;
|
|
7
|
+
'border-color': number;
|
|
8
|
+
'border-gap': number;
|
|
9
|
+
'border-gapColor': number;
|
|
10
|
+
'top-left': number;
|
|
11
|
+
'top-right': number;
|
|
12
|
+
'bottom-right': number;
|
|
13
|
+
'bottom-left': number;
|
|
14
|
+
'border-top': number;
|
|
15
|
+
'border-right': number;
|
|
16
|
+
'border-bottom': number;
|
|
17
|
+
'border-left': number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export const RoundedWithBorderTemplate = {
|
|
21
|
+
props: {
|
|
22
|
+
radius: {
|
|
23
|
+
default: [0, 0, 0, 0],
|
|
24
|
+
resolve(value) {
|
|
25
|
+
return toValidVec4(value);
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
'top-left': {
|
|
29
|
+
default: 0,
|
|
30
|
+
set(value, props) {
|
|
31
|
+
(props.radius as Vec4)[0] = value;
|
|
32
|
+
},
|
|
33
|
+
get(props) {
|
|
34
|
+
return (props.radius as Vec4)[0];
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
'top-right': {
|
|
38
|
+
default: 0,
|
|
39
|
+
set(value, props) {
|
|
40
|
+
(props.radius as Vec4)[1] = value;
|
|
41
|
+
},
|
|
42
|
+
get(props) {
|
|
43
|
+
return (props.radius as Vec4)[1];
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
'bottom-right': {
|
|
47
|
+
default: 0,
|
|
48
|
+
set(value, props) {
|
|
49
|
+
(props.radius as Vec4)[2] = value;
|
|
50
|
+
},
|
|
51
|
+
get(props) {
|
|
52
|
+
return (props.radius as Vec4)[2];
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
'bottom-left': {
|
|
56
|
+
default: 0,
|
|
57
|
+
set(value, props) {
|
|
58
|
+
(props.radius as Vec4)[3] = value;
|
|
59
|
+
},
|
|
60
|
+
get(props) {
|
|
61
|
+
return (props.radius as Vec4)[3];
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
'border-w': {
|
|
65
|
+
default: [0, 0, 0, 0],
|
|
66
|
+
resolve(value) {
|
|
67
|
+
return toValidVec4(value);
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
'border-color': 0xffffffff,
|
|
71
|
+
'border-gap': 0,
|
|
72
|
+
'border-gapColor': 0x00000000,
|
|
73
|
+
'border-top': {
|
|
74
|
+
default: 0,
|
|
75
|
+
set(value, props) {
|
|
76
|
+
(props['border-w'] as Vec4)[0] = value;
|
|
77
|
+
},
|
|
78
|
+
get(props) {
|
|
79
|
+
return (props['border-w'] as Vec4)[0];
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
'border-right': {
|
|
83
|
+
default: 0,
|
|
84
|
+
set(value, props) {
|
|
85
|
+
(props['border-w'] as Vec4)[1] = value;
|
|
86
|
+
},
|
|
87
|
+
get(props) {
|
|
88
|
+
return (props['border-w'] as Vec4)[1];
|
|
89
|
+
},
|
|
90
|
+
},
|
|
91
|
+
'border-bottom': {
|
|
92
|
+
default: 0,
|
|
93
|
+
set(value, props) {
|
|
94
|
+
(props['border-w'] as Vec4)[2] = value;
|
|
95
|
+
},
|
|
96
|
+
get(props) {
|
|
97
|
+
return (props['border-w'] as Vec4)[2];
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
'border-left': {
|
|
101
|
+
default: 0,
|
|
102
|
+
set(value, props) {
|
|
103
|
+
(props['border-w'] as Vec4)[3] = value;
|
|
104
|
+
},
|
|
105
|
+
get(props) {
|
|
106
|
+
return (props['border-w'] as Vec4)[3];
|
|
107
|
+
},
|
|
108
|
+
},
|
|
109
|
+
} as ShaderProps<RoundedWithBorderProps>,
|
|
110
|
+
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
export type Vec4 = [x: number, y: number, z: number, w: number];
|
|
2
|
+
|
|
3
|
+
export function calcFactoredRadiusArray(
|
|
4
|
+
radius: Vec4,
|
|
5
|
+
width: number,
|
|
6
|
+
height: number,
|
|
7
|
+
out: Vec4 = [0, 0, 0, 0],
|
|
8
|
+
): Vec4 {
|
|
9
|
+
[out[0], out[1], out[2], out[3]] = radius;
|
|
10
|
+
const factor = Math.min(
|
|
11
|
+
width / Math.max(width, radius[0] + radius[1]),
|
|
12
|
+
width / Math.max(width, radius[2] + radius[3]),
|
|
13
|
+
height / Math.max(height, radius[0] + radius[3]),
|
|
14
|
+
height / Math.max(height, radius[1] + radius[2]),
|
|
15
|
+
1,
|
|
16
|
+
);
|
|
17
|
+
out[0] *= factor;
|
|
18
|
+
out[1] *= factor;
|
|
19
|
+
out[2] *= factor;
|
|
20
|
+
out[3] *= factor;
|
|
21
|
+
return out;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function toValidVec4(value: unknown): Vec4 {
|
|
25
|
+
if (typeof value === 'number') {
|
|
26
|
+
return [value, value, value, value];
|
|
27
|
+
}
|
|
28
|
+
if (Array.isArray(value)) {
|
|
29
|
+
switch (value.length) {
|
|
30
|
+
default:
|
|
31
|
+
case 4:
|
|
32
|
+
return value as Vec4;
|
|
33
|
+
case 3:
|
|
34
|
+
return [value[0], value[1], value[2], value[0]];
|
|
35
|
+
case 2:
|
|
36
|
+
return [value[0], value[1], value[0], value[1]];
|
|
37
|
+
case 1:
|
|
38
|
+
return [value[0], value[0], value[0], value[0]];
|
|
39
|
+
case 0:
|
|
40
|
+
break;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return [0, 0, 0, 0];
|
|
44
|
+
}
|
package/src/core/timings.ts
DELETED
|
@@ -1,261 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* If not stated otherwise in this file or this component's LICENSE file the
|
|
3
|
-
* following copyright and licenses apply:
|
|
4
|
-
*
|
|
5
|
-
* Copyright 2023 Comcast Cable Communications Management, LLC.
|
|
6
|
-
*
|
|
7
|
-
* Licensed under the Apache License, Version 2.0 (the License);
|
|
8
|
-
* you may not use this file except in compliance with the License.
|
|
9
|
-
* You may obtain a copy of the License at
|
|
10
|
-
*
|
|
11
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
-
*
|
|
13
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
14
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
15
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16
|
-
* See the License for the specific language governing permissions and
|
|
17
|
-
* limitations under the License.
|
|
18
|
-
*/
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Core Utility Functions
|
|
22
|
-
*
|
|
23
|
-
* @module
|
|
24
|
-
*/
|
|
25
|
-
|
|
26
|
-
export const EPSILON = 0.000001;
|
|
27
|
-
export let ARRAY_TYPE =
|
|
28
|
-
typeof Float32Array !== 'undefined' ? Float32Array : Array;
|
|
29
|
-
export const RANDOM = Math.random;
|
|
30
|
-
export const ANGLE_ORDER = 'zyx';
|
|
31
|
-
const degree = Math.PI / 180;
|
|
32
|
-
|
|
33
|
-
export const setMatrixArrayType = (
|
|
34
|
-
type: Float32ArrayConstructor | ArrayConstructor,
|
|
35
|
-
) => {
|
|
36
|
-
ARRAY_TYPE = type;
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Merges two colors based on a given progress value.
|
|
41
|
-
*
|
|
42
|
-
* This function takes two colors (c1 and c2) represented as 32-bit integers
|
|
43
|
-
* in RGBA format and blends them based on the provided progress value (p).
|
|
44
|
-
* The result is a new color that is a weighted combination of the input colors,
|
|
45
|
-
* where the weight is determined by the progress value.
|
|
46
|
-
*
|
|
47
|
-
* @param {number} c1 - The first color in RGBA format (32-bit integer).
|
|
48
|
-
* @param {number} c2 - The second color in RGBA format (32-bit integer).
|
|
49
|
-
* @param {number} p - The progress value between 0 and 1.
|
|
50
|
-
* @returns {number} The merged color as a 32-bit integer in RGBA format.
|
|
51
|
-
*/
|
|
52
|
-
export function mergeColorProgress(
|
|
53
|
-
rgba1: number,
|
|
54
|
-
rgba2: number,
|
|
55
|
-
p: number,
|
|
56
|
-
): number {
|
|
57
|
-
const r1 = Math.trunc(rgba1 >>> 24);
|
|
58
|
-
const g1 = Math.trunc((rgba1 >>> 16) & 0xff);
|
|
59
|
-
const b1 = Math.trunc((rgba1 >>> 8) & 0xff);
|
|
60
|
-
const a1 = Math.trunc(rgba1 & 0xff);
|
|
61
|
-
|
|
62
|
-
const r2 = Math.trunc(rgba2 >>> 24);
|
|
63
|
-
const g2 = Math.trunc((rgba2 >>> 16) & 0xff);
|
|
64
|
-
const b2 = Math.trunc((rgba2 >>> 8) & 0xff);
|
|
65
|
-
const a2 = Math.trunc(rgba2 & 0xff);
|
|
66
|
-
|
|
67
|
-
const r = Math.round(r2 * p + r1 * (1 - p));
|
|
68
|
-
const g = Math.round(g2 * p + g1 * (1 - p));
|
|
69
|
-
const b = Math.round(b2 * p + b1 * (1 - p));
|
|
70
|
-
const a = Math.round(a2 * p + a1 * (1 - p));
|
|
71
|
-
|
|
72
|
-
return ((r << 24) | (g << 16) | (b << 8) | a) >>> 0;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
export const toRadian = (a: number) => {
|
|
76
|
-
return a * degree;
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
export const equals = (a: number, b: number) => {
|
|
80
|
-
return Math.abs(a - b) <= EPSILON * Math.max(1.0, Math.abs(a), Math.abs(b));
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
export const rand = (min: number, max: number) => {
|
|
84
|
-
return Math.floor(Math.random() * (max - min + 1) + min);
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
export const isPowerOfTwo = (value: number) => {
|
|
88
|
-
return value && !(value & (value - 1));
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
const getTimingBezier = (
|
|
92
|
-
a: number,
|
|
93
|
-
b: number,
|
|
94
|
-
c: number,
|
|
95
|
-
d: number,
|
|
96
|
-
): ((time: number) => number | undefined) => {
|
|
97
|
-
const xc = 3.0 * a;
|
|
98
|
-
const xb = 3.0 * (c - a) - xc;
|
|
99
|
-
const xa = 1.0 - xc - xb;
|
|
100
|
-
const yc = 3.0 * b;
|
|
101
|
-
const yb = 3.0 * (d - b) - yc;
|
|
102
|
-
const ya = 1.0 - yc - yb;
|
|
103
|
-
|
|
104
|
-
return function (time: number): number | undefined {
|
|
105
|
-
if (time >= 1.0) {
|
|
106
|
-
return 1;
|
|
107
|
-
}
|
|
108
|
-
if (time <= 0) {
|
|
109
|
-
return 0;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
let t = 0.5,
|
|
113
|
-
cbx,
|
|
114
|
-
cbxd,
|
|
115
|
-
dx;
|
|
116
|
-
|
|
117
|
-
for (let it = 0; it < 20; it++) {
|
|
118
|
-
cbx = t * (t * (t * xa + xb) + xc);
|
|
119
|
-
dx = time - cbx;
|
|
120
|
-
if (dx > -1e-8 && dx < 1e-8) {
|
|
121
|
-
return t * (t * (t * ya + yb) + yc);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
// Cubic bezier derivative.
|
|
125
|
-
cbxd = t * (t * (3 * xa) + 2 * xb) + xc;
|
|
126
|
-
|
|
127
|
-
if (cbxd > 1e-10 && cbxd < 1e-10) {
|
|
128
|
-
// Problematic. Fall back to binary search method.
|
|
129
|
-
break;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
t += dx / cbxd;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
// Fallback: binary search method. This is more reliable when there are near-0 slopes.
|
|
136
|
-
let minT = 0;
|
|
137
|
-
let maxT = 1;
|
|
138
|
-
for (let it = 0; it < 20; it++) {
|
|
139
|
-
t = 0.5 * (minT + maxT);
|
|
140
|
-
|
|
141
|
-
cbx = t * (t * (t * xa + xb) + xc);
|
|
142
|
-
|
|
143
|
-
dx = time - cbx;
|
|
144
|
-
if (dx > -1e-8 && dx < 1e-8) {
|
|
145
|
-
// Solution found!
|
|
146
|
-
return t * (t * (t * ya + yb) + yc);
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
if (dx < 0) {
|
|
150
|
-
maxT = t;
|
|
151
|
-
} else {
|
|
152
|
-
minT = t;
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
};
|
|
156
|
-
};
|
|
157
|
-
|
|
158
|
-
interface TimingFunctionMap {
|
|
159
|
-
[key: string]: (time: number) => number | undefined;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
type TimingLookupArray = number[];
|
|
163
|
-
interface TimingLookup {
|
|
164
|
-
[key: string]: TimingLookupArray;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
const timingMapping: TimingFunctionMap = {};
|
|
168
|
-
|
|
169
|
-
const timingLookup: TimingLookup = {
|
|
170
|
-
ease: [0.25, 0.1, 0.25, 1.0],
|
|
171
|
-
'ease-in': [0.42, 0, 1.0, 1.0],
|
|
172
|
-
'ease-out': [0, 0, 0.58, 1.0],
|
|
173
|
-
'ease-in-out': [0.42, 0, 0.58, 1.0],
|
|
174
|
-
'ease-in-sine': [0.12, 0, 0.39, 0],
|
|
175
|
-
'ease-out-sine': [0.12, 0, 0.39, 0],
|
|
176
|
-
'ease-in-out-sine': [0.37, 0, 0.63, 1],
|
|
177
|
-
'ease-in-cubic': [0.32, 0, 0.67, 0],
|
|
178
|
-
'ease-out-cubic': [0.33, 1, 0.68, 1],
|
|
179
|
-
'ease-in-out-cubic': [0.65, 0, 0.35, 1],
|
|
180
|
-
'ease-in-circ': [0.55, 0, 1, 0.45],
|
|
181
|
-
'ease-out-circ': [0, 0.55, 0.45, 1],
|
|
182
|
-
'ease-in-out-circ': [0.85, 0, 0.15, 1],
|
|
183
|
-
'ease-in-back': [0.36, 0, 0.66, -0.56],
|
|
184
|
-
'ease-out-back': [0.34, 1.56, 0.64, 1],
|
|
185
|
-
'ease-in-out-back': [0.68, -0.6, 0.32, 1.6],
|
|
186
|
-
};
|
|
187
|
-
|
|
188
|
-
const defaultTiming = (t: number): number => t;
|
|
189
|
-
|
|
190
|
-
const parseCubicBezier = (str: string) => {
|
|
191
|
-
//cubic-bezier(0.84, 0.52, 0.56, 0.6)
|
|
192
|
-
const regex = /-?\d*\.?\d+/g;
|
|
193
|
-
const match = str.match(regex);
|
|
194
|
-
|
|
195
|
-
if (match) {
|
|
196
|
-
const [num1, num2, num3, num4] = match;
|
|
197
|
-
const a = parseFloat(num1 || '0.42');
|
|
198
|
-
const b = parseFloat(num2 || '0');
|
|
199
|
-
const c = parseFloat(num3 || '1');
|
|
200
|
-
const d = parseFloat(num4 || '1');
|
|
201
|
-
|
|
202
|
-
const timing = getTimingBezier(a, b, c, d);
|
|
203
|
-
timingMapping[str] = timing;
|
|
204
|
-
|
|
205
|
-
return timing;
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
// parse failed, return linear
|
|
209
|
-
console.warn('Unknown cubic-bezier timing: ' + str);
|
|
210
|
-
return defaultTiming;
|
|
211
|
-
};
|
|
212
|
-
|
|
213
|
-
export const getTimingFunction = (
|
|
214
|
-
str: string,
|
|
215
|
-
): ((time: number) => number | undefined) => {
|
|
216
|
-
if (str === 'linear') {
|
|
217
|
-
return defaultTiming;
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
if (timingMapping[str] !== undefined) {
|
|
221
|
-
return timingMapping[str] || defaultTiming;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
if (str === 'step-start') {
|
|
225
|
-
return () => {
|
|
226
|
-
return 1;
|
|
227
|
-
};
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
if (str === 'step-end') {
|
|
231
|
-
return (time: number) => {
|
|
232
|
-
return time === 1 ? 1 : 0;
|
|
233
|
-
};
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
const lookup = timingLookup[str];
|
|
237
|
-
if (lookup !== undefined) {
|
|
238
|
-
const [a, b, c, d] = lookup;
|
|
239
|
-
// @ts-ignore - TS doesn't understand that we've checked for undefined
|
|
240
|
-
const timing = getTimingBezier(a, b, c, d);
|
|
241
|
-
timingMapping[str] = timing;
|
|
242
|
-
return timing;
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
if (str.startsWith('cubic-bezier')) {
|
|
246
|
-
return parseCubicBezier(str);
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
console.warn('Unknown timing function: ' + str);
|
|
250
|
-
return defaultTiming;
|
|
251
|
-
};
|
|
252
|
-
|
|
253
|
-
/**
|
|
254
|
-
* Convert bytes to string of megabytes with 2 decimal points
|
|
255
|
-
*
|
|
256
|
-
* @param bytes
|
|
257
|
-
* @returns
|
|
258
|
-
*/
|
|
259
|
-
export function bytesToMb(bytes: number) {
|
|
260
|
-
return (bytes / 1024 / 1024).toFixed(2);
|
|
261
|
-
}
|