@tbela99/css-parser 1.2.0 → 1.3.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/CHANGELOG.md +11 -0
- package/README.md +29 -11
- package/dist/index-umd-web.js +2041 -1967
- package/dist/index.cjs +2129 -1964
- package/dist/index.d.ts +1936 -77
- package/dist/lib/ast/expand.js +4 -65
- package/dist/lib/ast/features/calc.js +3 -27
- package/dist/lib/ast/features/inlinecssvariables.js +2 -21
- package/dist/lib/ast/features/prefix.js +2 -1
- package/dist/lib/ast/features/transform.js +4 -3
- package/dist/lib/ast/features/type.js +9 -0
- package/dist/lib/ast/math/expression.js +26 -149
- package/dist/lib/ast/math/math.js +9 -9
- package/dist/lib/ast/minify.js +82 -171
- package/dist/lib/ast/transform/compute.js +4 -37
- package/dist/lib/ast/transform/matrix.js +33 -34
- package/dist/lib/ast/transform/minify.js +32 -51
- package/dist/lib/ast/transform/perspective.js +1 -1
- package/dist/lib/ast/transform/rotate.js +13 -13
- package/dist/lib/ast/transform/scale.js +8 -8
- package/dist/lib/ast/transform/skew.js +4 -4
- package/dist/lib/ast/transform/translate.js +8 -8
- package/dist/lib/ast/transform/utils.js +31 -39
- package/dist/lib/ast/types.js +459 -5
- package/dist/lib/ast/walk.js +18 -0
- package/dist/lib/fs/resolve.js +11 -3
- package/dist/lib/parser/declaration/map.js +1 -0
- package/dist/lib/parser/declaration/set.js +2 -2
- package/dist/lib/parser/parse.js +139 -32
- package/dist/lib/parser/tokenize.js +41 -93
- package/dist/lib/parser/utils/type.js +1 -1
- package/dist/lib/renderer/render.js +61 -30
- package/dist/lib/renderer/sourcemap/sourcemap.js +34 -0
- package/dist/lib/syntax/color/cmyk.js +2 -2
- package/dist/lib/syntax/color/color-mix.js +11 -12
- package/dist/lib/syntax/color/color.js +14 -7
- package/dist/lib/syntax/color/hsl.js +4 -4
- package/dist/lib/syntax/color/hwb.js +27 -8
- package/dist/lib/syntax/color/lab.js +4 -4
- package/dist/lib/syntax/color/lch.js +4 -4
- package/dist/lib/syntax/color/oklab.js +4 -4
- package/dist/lib/syntax/color/oklch.js +4 -4
- package/dist/lib/syntax/color/relativecolor.js +1 -1
- package/dist/lib/syntax/color/rgb.js +4 -4
- package/dist/lib/syntax/color/utils/components.js +15 -3
- package/dist/lib/syntax/color/utils/distance.js +15 -1
- package/dist/lib/syntax/syntax.js +18 -17
- package/dist/lib/syntax/utils.js +1 -1
- package/dist/lib/validation/at-rules/document.js +1 -1
- package/dist/lib/validation/at-rules/import.js +4 -4
- package/dist/lib/validation/at-rules/keyframes.js +0 -11
- package/dist/lib/validation/at-rules/supports.js +6 -6
- package/dist/lib/validation/config.js +0 -4
- package/dist/lib/validation/config.json.js +33 -30
- package/dist/lib/validation/parser/parse.js +0 -8
- package/dist/lib/validation/selector.js +0 -9
- package/dist/lib/validation/syntax.js +17 -135
- package/dist/lib/validation/syntaxes/complex-selector-list.js +0 -11
- package/dist/lib/validation/syntaxes/family-name.js +0 -32
- package/dist/lib/validation/syntaxes/keyframe-selector.js +0 -11
- package/dist/lib/validation/syntaxes/relative-selector-list.js +0 -26
- package/dist/lib/validation/syntaxes/url.js +0 -33
- package/dist/lib/validation/utils/list.js +0 -8
- package/dist/node.js +229 -0
- package/dist/web.js +158 -0
- package/package.json +14 -11
- package/dist/node/index.js +0 -57
- package/dist/node/load.js +0 -20
- package/dist/web/index.js +0 -66
- package/dist/web/load.js +0 -31
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { is2DMatrix, identity } from './utils.js';
|
|
2
2
|
import { EnumToken } from '../types.js';
|
|
3
3
|
import { eq } from '../../parser/utils/eq.js';
|
|
4
4
|
import { getNumber } from '../../syntax/color/color.js';
|
|
@@ -9,7 +9,6 @@ import '../../parser/parse.js';
|
|
|
9
9
|
import '../../parser/tokenize.js';
|
|
10
10
|
import '../../parser/utils/config.js';
|
|
11
11
|
import '../../renderer/sourcemap/lib/encode.js';
|
|
12
|
-
import { minifyNumber } from '../../syntax/utils.js';
|
|
13
12
|
|
|
14
13
|
function parseMatrix(mat) {
|
|
15
14
|
if (mat.typ == EnumToken.IdenTokenType) {
|
|
@@ -31,30 +30,30 @@ function parseMatrix(mat) {
|
|
|
31
30
|
function matrix(values) {
|
|
32
31
|
const matrix = identity();
|
|
33
32
|
if (values.length === 6) {
|
|
34
|
-
matrix[0
|
|
35
|
-
matrix[0
|
|
36
|
-
matrix[1
|
|
37
|
-
matrix[1
|
|
38
|
-
matrix[3
|
|
39
|
-
matrix[3
|
|
33
|
+
matrix[0 * 4 + 0] = values[0];
|
|
34
|
+
matrix[0 * 4 + 1] = values[1];
|
|
35
|
+
matrix[1 * 4 + 0] = values[2];
|
|
36
|
+
matrix[1 * 4 + 1] = values[3];
|
|
37
|
+
matrix[3 * 4 + 0] = values[4];
|
|
38
|
+
matrix[3 * 4 + 1] = values[5];
|
|
40
39
|
}
|
|
41
40
|
else if (values.length === 16) {
|
|
42
|
-
matrix[0
|
|
43
|
-
matrix[0
|
|
44
|
-
matrix[0
|
|
45
|
-
matrix[0
|
|
46
|
-
matrix[1
|
|
47
|
-
matrix[1
|
|
48
|
-
matrix[1
|
|
49
|
-
matrix[1
|
|
50
|
-
matrix[2
|
|
51
|
-
matrix[2
|
|
52
|
-
matrix[2
|
|
53
|
-
matrix[2
|
|
54
|
-
matrix[3
|
|
55
|
-
matrix[3
|
|
56
|
-
matrix[3
|
|
57
|
-
matrix[3
|
|
41
|
+
matrix[0 * 4 + 0] = values[0];
|
|
42
|
+
matrix[0 * 4 + 1] = values[1];
|
|
43
|
+
matrix[0 * 4 + 2] = values[2];
|
|
44
|
+
matrix[0 * 4 + 3] = values[3];
|
|
45
|
+
matrix[1 * 4 + 0] = values[4];
|
|
46
|
+
matrix[1 * 4 + 1] = values[5];
|
|
47
|
+
matrix[1 * 4 + 2] = values[6];
|
|
48
|
+
matrix[1 * 4 + 3] = values[7];
|
|
49
|
+
matrix[2 * 4 + 0] = values[8];
|
|
50
|
+
matrix[2 * 4 + 1] = values[9];
|
|
51
|
+
matrix[2 * 4 + 2] = values[10];
|
|
52
|
+
matrix[2 * 4 + 3] = values[11];
|
|
53
|
+
matrix[3 * 4 + 0] = values[12];
|
|
54
|
+
matrix[3 * 4 + 1] = values[13];
|
|
55
|
+
matrix[3 * 4 + 2] = values[14];
|
|
56
|
+
matrix[3 * 4 + 3] = values[15];
|
|
58
57
|
}
|
|
59
58
|
else {
|
|
60
59
|
return null;
|
|
@@ -62,7 +61,7 @@ function matrix(values) {
|
|
|
62
61
|
return matrix;
|
|
63
62
|
}
|
|
64
63
|
function serialize(matrix) {
|
|
65
|
-
matrix = matrix.
|
|
64
|
+
matrix = matrix.slice();
|
|
66
65
|
// @ts-ignore
|
|
67
66
|
if (eq(matrix, identity())) {
|
|
68
67
|
return {
|
|
@@ -76,19 +75,19 @@ function serialize(matrix) {
|
|
|
76
75
|
typ: EnumToken.FunctionTokenType,
|
|
77
76
|
val: 'matrix',
|
|
78
77
|
chi: [
|
|
79
|
-
matrix[0
|
|
80
|
-
matrix[0
|
|
81
|
-
matrix[1
|
|
82
|
-
matrix[1
|
|
83
|
-
matrix[3
|
|
84
|
-
matrix[3
|
|
78
|
+
matrix[0 * 4 + 0],
|
|
79
|
+
matrix[0 * 4 + 1],
|
|
80
|
+
matrix[1 * 4 + 0],
|
|
81
|
+
matrix[1 * 4 + 1],
|
|
82
|
+
matrix[3 * 4 + 0],
|
|
83
|
+
matrix[3 * 4 + 1]
|
|
85
84
|
].reduce((acc, t) => {
|
|
86
85
|
if (acc.length > 0) {
|
|
87
86
|
acc.push({ typ: EnumToken.CommaTokenType });
|
|
88
87
|
}
|
|
89
88
|
acc.push({
|
|
90
89
|
typ: EnumToken.NumberTokenType,
|
|
91
|
-
val:
|
|
90
|
+
val: t
|
|
92
91
|
});
|
|
93
92
|
return acc;
|
|
94
93
|
}, [])
|
|
@@ -97,13 +96,13 @@ function serialize(matrix) {
|
|
|
97
96
|
return {
|
|
98
97
|
typ: EnumToken.FunctionTokenType,
|
|
99
98
|
val: 'matrix3d',
|
|
100
|
-
chi: matrix.
|
|
99
|
+
chi: matrix.reduce((acc, curr) => {
|
|
101
100
|
if (acc.length > 0) {
|
|
102
101
|
acc.push({ typ: EnumToken.CommaTokenType });
|
|
103
102
|
}
|
|
104
103
|
acc.push({
|
|
105
104
|
typ: EnumToken.NumberTokenType,
|
|
106
|
-
val:
|
|
105
|
+
val: curr
|
|
107
106
|
});
|
|
108
107
|
return acc;
|
|
109
108
|
}, [])
|
|
@@ -36,26 +36,12 @@ function minify(matrix) {
|
|
|
36
36
|
coordinates.delete(i == 0 ? 'x' : i == 1 ? 'y' : 'z');
|
|
37
37
|
}
|
|
38
38
|
}
|
|
39
|
-
// if (coordinates.size == 3) {
|
|
40
|
-
//
|
|
41
|
-
// result.push({
|
|
42
|
-
// typ: EnumToken.FunctionTokenType,
|
|
43
|
-
// val: 'translate3d',
|
|
44
|
-
// chi: [
|
|
45
|
-
// {typ: EnumToken.LengthTokenType, val: round(decomposed.translate[0]) + '', unit: 'px'},
|
|
46
|
-
// {typ: EnumToken.CommaTokenType},
|
|
47
|
-
// {typ: EnumToken.LengthTokenType, val: round(decomposed.translate[1]) + '', unit: 'px'},
|
|
48
|
-
// {typ: EnumToken.CommaTokenType},
|
|
49
|
-
// {typ: EnumToken.LengthTokenType, val: round(decomposed.translate[2]) + '', unit: 'px'}
|
|
50
|
-
// ]
|
|
51
|
-
// })
|
|
52
|
-
// } else
|
|
53
39
|
if (coordinates.size == 1) {
|
|
54
40
|
if (coordinates.has('x')) {
|
|
55
41
|
result.push({
|
|
56
42
|
typ: EnumToken.FunctionTokenType,
|
|
57
43
|
val: 'translate',
|
|
58
|
-
chi: [{ typ: EnumToken.LengthTokenType, val: round(decomposed.translate[0])
|
|
44
|
+
chi: [{ typ: EnumToken.LengthTokenType, val: round(decomposed.translate[0]), unit: 'px' }]
|
|
59
45
|
});
|
|
60
46
|
}
|
|
61
47
|
else {
|
|
@@ -64,7 +50,7 @@ function minify(matrix) {
|
|
|
64
50
|
result.push({
|
|
65
51
|
typ: EnumToken.FunctionTokenType,
|
|
66
52
|
val: 'translate' + axis.toUpperCase(),
|
|
67
|
-
chi: [{ typ: EnumToken.LengthTokenType, val: round(decomposed.translate[index])
|
|
53
|
+
chi: [{ typ: EnumToken.LengthTokenType, val: round(decomposed.translate[index]), unit: 'px' }]
|
|
68
54
|
});
|
|
69
55
|
}
|
|
70
56
|
}
|
|
@@ -75,15 +61,15 @@ function minify(matrix) {
|
|
|
75
61
|
chi: [
|
|
76
62
|
decomposed.translate[0] == 0 ? {
|
|
77
63
|
typ: EnumToken.NumberTokenType,
|
|
78
|
-
|
|
79
|
-
} : { typ: EnumToken.LengthTokenType, val: round(decomposed.translate[0])
|
|
64
|
+
val: 0
|
|
65
|
+
} : { typ: EnumToken.LengthTokenType, val: round(decomposed.translate[0]), unit: 'px' },
|
|
80
66
|
{ typ: EnumToken.CommaTokenType },
|
|
81
67
|
decomposed.translate[1] == 0 ? {
|
|
82
68
|
typ: EnumToken.NumberTokenType,
|
|
83
|
-
|
|
84
|
-
} : { typ: EnumToken.LengthTokenType, val: round(decomposed.translate[1])
|
|
69
|
+
val: 0
|
|
70
|
+
} : { typ: EnumToken.LengthTokenType, val: round(decomposed.translate[1]), unit: 'px' },
|
|
85
71
|
{ typ: EnumToken.CommaTokenType },
|
|
86
|
-
{ typ: EnumToken.LengthTokenType, val: round(decomposed.translate[2])
|
|
72
|
+
{ typ: EnumToken.LengthTokenType, val: round(decomposed.translate[2]), unit: 'px' }
|
|
87
73
|
]
|
|
88
74
|
});
|
|
89
75
|
}
|
|
@@ -92,9 +78,9 @@ function minify(matrix) {
|
|
|
92
78
|
typ: EnumToken.FunctionTokenType,
|
|
93
79
|
val: 'translate',
|
|
94
80
|
chi: [
|
|
95
|
-
{ typ: EnumToken.LengthTokenType, val: round(decomposed.translate[0])
|
|
81
|
+
{ typ: EnumToken.LengthTokenType, val: round(decomposed.translate[0]), unit: 'px' },
|
|
96
82
|
{ typ: EnumToken.CommaTokenType },
|
|
97
|
-
{ typ: EnumToken.LengthTokenType, val: round(decomposed.translate[1])
|
|
83
|
+
{ typ: EnumToken.LengthTokenType, val: round(decomposed.translate[1]), unit: 'px' }
|
|
98
84
|
]
|
|
99
85
|
});
|
|
100
86
|
}
|
|
@@ -108,7 +94,7 @@ function minify(matrix) {
|
|
|
108
94
|
chi: [
|
|
109
95
|
{
|
|
110
96
|
typ: EnumToken.AngleTokenType,
|
|
111
|
-
val:
|
|
97
|
+
val: round(angle),
|
|
112
98
|
unit: 'deg'
|
|
113
99
|
}
|
|
114
100
|
]
|
|
@@ -121,7 +107,7 @@ function minify(matrix) {
|
|
|
121
107
|
chi: [
|
|
122
108
|
{
|
|
123
109
|
typ: EnumToken.AngleTokenType,
|
|
124
|
-
val:
|
|
110
|
+
val: round(angle),
|
|
125
111
|
unit: 'deg'
|
|
126
112
|
}
|
|
127
113
|
]
|
|
@@ -134,7 +120,7 @@ function minify(matrix) {
|
|
|
134
120
|
chi: [
|
|
135
121
|
{
|
|
136
122
|
typ: EnumToken.AngleTokenType,
|
|
137
|
-
val:
|
|
123
|
+
val: round(angle),
|
|
138
124
|
unit: 'deg'
|
|
139
125
|
}
|
|
140
126
|
]
|
|
@@ -147,22 +133,22 @@ function minify(matrix) {
|
|
|
147
133
|
chi: [
|
|
148
134
|
{
|
|
149
135
|
typ: EnumToken.NumberTokenType,
|
|
150
|
-
val:
|
|
136
|
+
val: round(x)
|
|
151
137
|
},
|
|
152
138
|
{ typ: EnumToken.CommaTokenType },
|
|
153
139
|
{
|
|
154
140
|
typ: EnumToken.NumberTokenType,
|
|
155
|
-
val:
|
|
141
|
+
val: round(y)
|
|
156
142
|
},
|
|
157
143
|
{ typ: EnumToken.CommaTokenType },
|
|
158
144
|
{
|
|
159
145
|
typ: EnumToken.NumberTokenType,
|
|
160
|
-
val:
|
|
146
|
+
val: round(z)
|
|
161
147
|
},
|
|
162
148
|
{ typ: EnumToken.CommaTokenType },
|
|
163
149
|
{
|
|
164
150
|
typ: EnumToken.AngleTokenType,
|
|
165
|
-
val:
|
|
151
|
+
val: round(angle),
|
|
166
152
|
unit: 'deg'
|
|
167
153
|
}
|
|
168
154
|
]
|
|
@@ -185,7 +171,7 @@ function minify(matrix) {
|
|
|
185
171
|
typ: EnumToken.FunctionTokenType,
|
|
186
172
|
val: 'skew' + (skew.has('x') ? '' : 'Y'),
|
|
187
173
|
chi: [
|
|
188
|
-
{ typ: EnumToken.AngleTokenType, val:
|
|
174
|
+
{ typ: EnumToken.AngleTokenType, val: round(decomposed.skew[0]), unit: 'deg' }
|
|
189
175
|
]
|
|
190
176
|
});
|
|
191
177
|
}
|
|
@@ -194,9 +180,9 @@ function minify(matrix) {
|
|
|
194
180
|
typ: EnumToken.FunctionTokenType,
|
|
195
181
|
val: 'skew',
|
|
196
182
|
chi: [
|
|
197
|
-
{ typ: EnumToken.AngleTokenType, val:
|
|
183
|
+
{ typ: EnumToken.AngleTokenType, val: round(decomposed.skew[0]), unit: 'deg' },
|
|
198
184
|
{ typ: EnumToken.CommaTokenType },
|
|
199
|
-
{ typ: EnumToken.AngleTokenType, val:
|
|
185
|
+
{ typ: EnumToken.AngleTokenType, val: round(decomposed.skew[1]), unit: 'deg' }
|
|
200
186
|
]
|
|
201
187
|
});
|
|
202
188
|
}
|
|
@@ -218,7 +204,7 @@ function minify(matrix) {
|
|
|
218
204
|
typ: EnumToken.FunctionTokenType,
|
|
219
205
|
val: 'scale' + prefix,
|
|
220
206
|
chi: [
|
|
221
|
-
{ typ: EnumToken.NumberTokenType, val:
|
|
207
|
+
{ typ: EnumToken.NumberTokenType, val: round(prefix == 'Z' ? sz : prefix == 'Y' ? sy : sx) }
|
|
222
208
|
]
|
|
223
209
|
});
|
|
224
210
|
}
|
|
@@ -227,9 +213,9 @@ function minify(matrix) {
|
|
|
227
213
|
typ: EnumToken.FunctionTokenType,
|
|
228
214
|
val: 'scale',
|
|
229
215
|
chi: [
|
|
230
|
-
{ typ: EnumToken.NumberTokenType, val:
|
|
216
|
+
{ typ: EnumToken.NumberTokenType, val: round(sx) },
|
|
231
217
|
{ typ: EnumToken.CommaTokenType },
|
|
232
|
-
{ typ: EnumToken.NumberTokenType, val:
|
|
218
|
+
{ typ: EnumToken.NumberTokenType, val: round(sy) },
|
|
233
219
|
]
|
|
234
220
|
});
|
|
235
221
|
}
|
|
@@ -238,25 +224,15 @@ function minify(matrix) {
|
|
|
238
224
|
typ: EnumToken.FunctionTokenType,
|
|
239
225
|
val: 'scale3d',
|
|
240
226
|
chi: [
|
|
241
|
-
{ typ: EnumToken.NumberTokenType, val:
|
|
227
|
+
{ typ: EnumToken.NumberTokenType, val: round(sx) },
|
|
242
228
|
{ typ: EnumToken.CommaTokenType },
|
|
243
|
-
{ typ: EnumToken.NumberTokenType, val:
|
|
229
|
+
{ typ: EnumToken.NumberTokenType, val: round(sy) },
|
|
244
230
|
{ typ: EnumToken.CommaTokenType },
|
|
245
|
-
{ typ: EnumToken.NumberTokenType, val:
|
|
231
|
+
{ typ: EnumToken.NumberTokenType, val: round(sz) }
|
|
246
232
|
]
|
|
247
233
|
});
|
|
248
234
|
}
|
|
249
235
|
}
|
|
250
|
-
// if (transforms.has('perspective')) {
|
|
251
|
-
//
|
|
252
|
-
// result.push({
|
|
253
|
-
// typ: EnumToken.FunctionTokenType,
|
|
254
|
-
// val: 'perspective',
|
|
255
|
-
// chi: [
|
|
256
|
-
// {typ: EnumToken.Length, val: '' + round(1 / decomposed.perspective[2]), unit: 'px'},
|
|
257
|
-
// ]
|
|
258
|
-
// });
|
|
259
|
-
// }
|
|
260
236
|
// identity
|
|
261
237
|
return result.length == 0 || (result.length == 1 && eqMatrix(identity(), result)) ? [
|
|
262
238
|
{
|
|
@@ -266,10 +242,13 @@ function minify(matrix) {
|
|
|
266
242
|
] : result;
|
|
267
243
|
}
|
|
268
244
|
function eqMatrix(a, b) {
|
|
245
|
+
// console.error(JSON.stringify({a, b}, null, 1));
|
|
269
246
|
let mat = identity();
|
|
270
247
|
let tmp = identity();
|
|
271
248
|
// @ts-ignore
|
|
272
|
-
const data = Array.isArray(a) ? a : parseMatrix(a);
|
|
249
|
+
const data = (Array.isArray(a) ? a : parseMatrix(a));
|
|
250
|
+
// toZero(data);
|
|
251
|
+
// console.error({data});
|
|
273
252
|
for (const transform of b) {
|
|
274
253
|
tmp = computeMatrix([transform], identity());
|
|
275
254
|
if (tmp == null) {
|
|
@@ -277,12 +256,14 @@ function eqMatrix(a, b) {
|
|
|
277
256
|
}
|
|
278
257
|
mat = multiply(mat, tmp);
|
|
279
258
|
}
|
|
259
|
+
// toZero(mat);
|
|
260
|
+
// console.error({mat});
|
|
280
261
|
if (mat == null) {
|
|
281
262
|
return false;
|
|
282
263
|
}
|
|
283
264
|
for (let i = 0; i < 4; i++) {
|
|
284
265
|
for (let j = 0; j < 4; j++) {
|
|
285
|
-
if (Math.abs(mat[i
|
|
266
|
+
if (Math.abs(mat[i * 4 + j] - data[i * 4 + j]) > epsilon) {
|
|
286
267
|
return false;
|
|
287
268
|
}
|
|
288
269
|
}
|
|
@@ -3,7 +3,7 @@ import { identity, multiply } from './utils.js';
|
|
|
3
3
|
function perspective(x, from) {
|
|
4
4
|
const matrix = identity();
|
|
5
5
|
// @ts-ignore
|
|
6
|
-
matrix[2
|
|
6
|
+
matrix[2 * 4 + 3] = typeof x == 'object' && x.val == 'none' ? 0 : x == 0 ? Number.NEGATIVE_INFINITY : -1 / x;
|
|
7
7
|
return multiply(from, matrix);
|
|
8
8
|
}
|
|
9
9
|
|
|
@@ -17,23 +17,23 @@ function rotate3D(angle, x, y, z, from) {
|
|
|
17
17
|
x *= unit;
|
|
18
18
|
y *= unit;
|
|
19
19
|
z *= unit;
|
|
20
|
-
matrix[0
|
|
21
|
-
matrix[0
|
|
22
|
-
matrix[0
|
|
23
|
-
matrix[1
|
|
24
|
-
matrix[1
|
|
25
|
-
matrix[1
|
|
26
|
-
matrix[2
|
|
27
|
-
matrix[2
|
|
28
|
-
matrix[2
|
|
20
|
+
matrix[0 * 4 + 0] = 1 - 2 * (y * y + z * z) * sq;
|
|
21
|
+
matrix[0 * 4 + 1] = 2 * (x * y * sq + z * sc);
|
|
22
|
+
matrix[0 * 4 + 2] = 2 * (x * z * sq - y * sc);
|
|
23
|
+
matrix[1 * 4 + 0] = 2 * (x * y * sq - z * sc);
|
|
24
|
+
matrix[1 * 4 + 1] = 1 - 2 * (x * x + z * z) * sq;
|
|
25
|
+
matrix[1 * 4 + 2] = 2 * (y * z * sq + x * sc);
|
|
26
|
+
matrix[2 * 4 + 0] = 2 * (x * z * sq + y * sc);
|
|
27
|
+
matrix[2 * 4 + 1] = 2 * (y * z * sq - x * sc);
|
|
28
|
+
matrix[2 * 4 + 2] = 1 - 2 * (x * x + y * y) * sq;
|
|
29
29
|
return multiply(from, matrix);
|
|
30
30
|
}
|
|
31
31
|
function rotate(angle, from) {
|
|
32
32
|
const matrix = identity();
|
|
33
|
-
matrix[0
|
|
34
|
-
matrix[0
|
|
35
|
-
matrix[1
|
|
36
|
-
matrix[1
|
|
33
|
+
matrix[0 * 4 + 0] = Math.cos(angle);
|
|
34
|
+
matrix[0 * 4 + 1] = Math.sin(angle);
|
|
35
|
+
matrix[1 * 4 + 0] = -Math.sin(angle);
|
|
36
|
+
matrix[1 * 4 + 1] = Math.cos(angle);
|
|
37
37
|
return multiply(from, matrix);
|
|
38
38
|
}
|
|
39
39
|
|
|
@@ -2,30 +2,30 @@ import { identity, multiply } from './utils.js';
|
|
|
2
2
|
|
|
3
3
|
function scaleX(x, from) {
|
|
4
4
|
const matrix = identity();
|
|
5
|
-
matrix[0
|
|
5
|
+
matrix[0 * 4 + 0] = x;
|
|
6
6
|
return multiply(from, matrix);
|
|
7
7
|
}
|
|
8
8
|
function scaleY(y, from) {
|
|
9
9
|
const matrix = identity();
|
|
10
|
-
matrix[1
|
|
10
|
+
matrix[1 * 4 + 1] = y;
|
|
11
11
|
return multiply(from, matrix);
|
|
12
12
|
}
|
|
13
13
|
function scaleZ(z, from) {
|
|
14
14
|
const matrix = identity();
|
|
15
|
-
matrix[2
|
|
15
|
+
matrix[2 * 4 + 2] = z;
|
|
16
16
|
return multiply(from, matrix);
|
|
17
17
|
}
|
|
18
18
|
function scale(x, y, from) {
|
|
19
19
|
const matrix = identity();
|
|
20
|
-
matrix[0
|
|
21
|
-
matrix[1
|
|
20
|
+
matrix[0 * 4 + 0] = x;
|
|
21
|
+
matrix[1 * 4 + 1] = y;
|
|
22
22
|
return multiply(from, matrix);
|
|
23
23
|
}
|
|
24
24
|
function scale3d(x, y, z, from) {
|
|
25
25
|
const matrix = identity();
|
|
26
|
-
matrix[0
|
|
27
|
-
matrix[1
|
|
28
|
-
matrix[2
|
|
26
|
+
matrix[0 * 4 + 0] = x;
|
|
27
|
+
matrix[1 * 4 + 1] = y;
|
|
28
|
+
matrix[2 * 4 + 2] = z;
|
|
29
29
|
return multiply(from, matrix);
|
|
30
30
|
}
|
|
31
31
|
|
|
@@ -2,20 +2,20 @@ import { identity, multiply } from './utils.js';
|
|
|
2
2
|
|
|
3
3
|
function skewX(x, from) {
|
|
4
4
|
const matrix = identity();
|
|
5
|
-
matrix[1
|
|
5
|
+
matrix[1 * 4 + 0] = Math.tan(x);
|
|
6
6
|
return multiply(from, matrix);
|
|
7
7
|
}
|
|
8
8
|
function skewY(y, from) {
|
|
9
9
|
const matrix = identity();
|
|
10
|
-
matrix[0
|
|
10
|
+
matrix[0 * 4 + 1] = Math.tan(y);
|
|
11
11
|
return multiply(from, matrix);
|
|
12
12
|
}
|
|
13
13
|
// convert angle to radian
|
|
14
14
|
function skew(values, from) {
|
|
15
15
|
const matrix = identity();
|
|
16
|
-
matrix[1
|
|
16
|
+
matrix[1 * 4 + 0] = Math.tan(values[0]);
|
|
17
17
|
if (values.length > 1) {
|
|
18
|
-
matrix[0
|
|
18
|
+
matrix[0 * 4 + 1] = Math.tan(values[1]);
|
|
19
19
|
}
|
|
20
20
|
return multiply(from, matrix);
|
|
21
21
|
}
|
|
@@ -2,30 +2,30 @@ import { identity, multiply } from './utils.js';
|
|
|
2
2
|
|
|
3
3
|
function translateX(x, from) {
|
|
4
4
|
const matrix = identity();
|
|
5
|
-
matrix[3
|
|
5
|
+
matrix[3 * 4 + 0] = x;
|
|
6
6
|
return multiply(from, matrix);
|
|
7
7
|
}
|
|
8
8
|
function translateY(y, from) {
|
|
9
9
|
const matrix = identity();
|
|
10
|
-
matrix[3
|
|
10
|
+
matrix[3 * 4 + 1] = y;
|
|
11
11
|
return multiply(from, matrix);
|
|
12
12
|
}
|
|
13
13
|
function translateZ(z, from) {
|
|
14
14
|
const matrix = identity();
|
|
15
|
-
matrix[3
|
|
15
|
+
matrix[3 * 4 + 2] = z;
|
|
16
16
|
return multiply(from, matrix);
|
|
17
17
|
}
|
|
18
18
|
function translate(translate, from) {
|
|
19
19
|
const matrix = identity();
|
|
20
|
-
matrix[3
|
|
21
|
-
matrix[3
|
|
20
|
+
matrix[3 * 4 + 0] = translate[0];
|
|
21
|
+
matrix[3 * 4 + 1] = translate[1] ?? 0;
|
|
22
22
|
return multiply(from, matrix);
|
|
23
23
|
}
|
|
24
24
|
function translate3d(translate, from) {
|
|
25
25
|
const matrix = identity();
|
|
26
|
-
matrix[3
|
|
27
|
-
matrix[3
|
|
28
|
-
matrix[3
|
|
26
|
+
matrix[3 * 4 + 0] = translate[0];
|
|
27
|
+
matrix[3 * 4 + 1] = translate[1];
|
|
28
|
+
matrix[3 * 4 + 2] = translate[2];
|
|
29
29
|
return multiply(from, matrix);
|
|
30
30
|
}
|
|
31
31
|
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
const epsilon = 1e-5;
|
|
2
2
|
function identity() {
|
|
3
|
-
return [
|
|
3
|
+
return [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
|
|
4
4
|
}
|
|
5
5
|
function pLength(point) {
|
|
6
|
-
// Calcul de la norme euclidienne
|
|
7
6
|
return Math.sqrt(point[0] * point[0] + point[1] * point[1] + point[2] * point[2]);
|
|
8
7
|
}
|
|
9
8
|
function normalize(point) {
|
|
@@ -12,23 +11,19 @@ function normalize(point) {
|
|
|
12
11
|
return norm === 0 ? [0, 0, 0] : [x / norm, y / norm, z / norm];
|
|
13
12
|
}
|
|
14
13
|
function dot(point1, point2) {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
// return point1[0] * point2[0] + point1[1] * point2[1] + point1[2] * point2[2] + point1[3] * point2[3];
|
|
18
|
-
// }
|
|
19
|
-
let result = 0;
|
|
20
|
-
for (let i = 0; i < point1.length; i++) {
|
|
21
|
-
result += point1[i] * point2[i];
|
|
14
|
+
if (point1.length === 4 && point2.length === 4) {
|
|
15
|
+
return point1[0] * point2[0] + point1[1] * point2[1] + point1[2] * point2[2] + point1[3] * point2[3];
|
|
22
16
|
}
|
|
23
|
-
return
|
|
24
|
-
// return point1[0] * point2[0] + point1[1] * point2[1] + point1[2] * point2[2];
|
|
17
|
+
return point1[0] * point2[0] + point1[1] * point2[1] + point1[2] * point2[2];
|
|
25
18
|
}
|
|
26
19
|
function multiply(matrixA, matrixB) {
|
|
27
|
-
let result =
|
|
20
|
+
let result = new Array(16).fill(0);
|
|
28
21
|
for (let i = 0; i < 4; i++) {
|
|
29
22
|
for (let j = 0; j < 4; j++) {
|
|
30
23
|
for (let k = 0; k < 4; k++) {
|
|
31
|
-
|
|
24
|
+
// Utiliser l'indexation linéaire pour accéder aux éléments
|
|
25
|
+
// Pour une matrice 4x4, l'index est (row * 4 + col)
|
|
26
|
+
result[j * 4 + i] += matrixA[k * 4 + i] * matrixB[j * 4 + k];
|
|
32
27
|
}
|
|
33
28
|
}
|
|
34
29
|
}
|
|
@@ -36,20 +31,17 @@ function multiply(matrixA, matrixB) {
|
|
|
36
31
|
}
|
|
37
32
|
function inverse(matrix) {
|
|
38
33
|
// Create augmented matrix [matrix | identity]
|
|
39
|
-
let augmented =
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
i === 2 ? [0, 0, 1, 0] :
|
|
44
|
-
[0, 0, 0, 1])
|
|
45
|
-
]);
|
|
34
|
+
let augmented = [
|
|
35
|
+
1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1,
|
|
36
|
+
1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1
|
|
37
|
+
];
|
|
46
38
|
// Gaussian elimination with partial pivoting
|
|
47
39
|
for (let col = 0; col < 4; col++) {
|
|
48
40
|
// Find pivot row with maximum absolute value
|
|
49
41
|
let maxRow = col;
|
|
50
|
-
let maxVal = Math.abs(augmented[col
|
|
42
|
+
let maxVal = Math.abs(augmented[col * 4 + col]);
|
|
51
43
|
for (let row = col + 1; row < 4; row++) {
|
|
52
|
-
let val = Math.abs(augmented[row
|
|
44
|
+
let val = Math.abs(augmented[row * 4 + col]);
|
|
53
45
|
if (val > maxVal) {
|
|
54
46
|
maxVal = val;
|
|
55
47
|
maxRow = row;
|
|
@@ -64,22 +56,22 @@ function inverse(matrix) {
|
|
|
64
56
|
[augmented[col], augmented[maxRow]] = [augmented[maxRow], augmented[col]];
|
|
65
57
|
}
|
|
66
58
|
// Scale pivot row to make pivot element 1
|
|
67
|
-
let pivot = augmented[col
|
|
59
|
+
let pivot = augmented[col * 4 + col];
|
|
68
60
|
for (let j = 0; j < 8; j++) {
|
|
69
|
-
augmented[col
|
|
61
|
+
augmented[col * 4 + j] /= pivot;
|
|
70
62
|
}
|
|
71
63
|
// Eliminate column in other rows
|
|
72
64
|
for (let row = 0; row < 4; row++) {
|
|
73
65
|
if (row !== col) {
|
|
74
|
-
let factor = augmented[row
|
|
66
|
+
let factor = augmented[row * 4 + col];
|
|
75
67
|
for (let j = 0; j < 8; j++) {
|
|
76
|
-
augmented[row
|
|
68
|
+
augmented[row * 4 + j] -= factor * augmented[col * 4 + j];
|
|
77
69
|
}
|
|
78
70
|
}
|
|
79
71
|
}
|
|
80
72
|
}
|
|
81
73
|
// Extract the inverse from the right side of the augmented matrix
|
|
82
|
-
return augmented.
|
|
74
|
+
return augmented.slice(0, 16);
|
|
83
75
|
}
|
|
84
76
|
// function transpose(matrix: Matrix): Matrix {
|
|
85
77
|
// // Crée une nouvelle matrice vide 4x4
|
|
@@ -103,7 +95,7 @@ function round(number) {
|
|
|
103
95
|
// translate3d(25.9808px, 0, 15px ) rotateY(60deg) skewX(49.9999deg) scale(1, 1.2)
|
|
104
96
|
// translate → rotate → skew → scale
|
|
105
97
|
function decompose(original) {
|
|
106
|
-
const matrix = original.
|
|
98
|
+
const matrix = original.slice();
|
|
107
99
|
// Normalize last row
|
|
108
100
|
if (matrix[15] === 0) {
|
|
109
101
|
return null;
|
|
@@ -120,7 +112,7 @@ function decompose(original) {
|
|
|
120
112
|
perspectiveMatrix[11] = 0;
|
|
121
113
|
perspectiveMatrix[15] = 1;
|
|
122
114
|
// @ts-ignore
|
|
123
|
-
const inverted = inverse(original.
|
|
115
|
+
const inverted = inverse(original.slice());
|
|
124
116
|
if (!inverted) {
|
|
125
117
|
return null;
|
|
126
118
|
}
|
|
@@ -243,16 +235,16 @@ function toZero(v) {
|
|
|
243
235
|
// https://drafts.csswg.org/css-transforms-1/#2d-matrix
|
|
244
236
|
function is2DMatrix(matrix) {
|
|
245
237
|
// m13,m14, m23, m24, m31, m32, m34, m43 are all 0
|
|
246
|
-
return matrix[0
|
|
247
|
-
matrix[0
|
|
248
|
-
matrix[1
|
|
249
|
-
matrix[1
|
|
250
|
-
matrix[2
|
|
251
|
-
matrix[2
|
|
252
|
-
matrix[2
|
|
253
|
-
matrix[3
|
|
254
|
-
matrix[2
|
|
255
|
-
matrix[3
|
|
238
|
+
return matrix[0 * 4 + 2] === 0 &&
|
|
239
|
+
matrix[0 * 4 + 3] === 0 &&
|
|
240
|
+
matrix[1 * 4 + 2] === 0 &&
|
|
241
|
+
matrix[1 * 4 + 3] === 0 &&
|
|
242
|
+
matrix[2 * 4 + 0] === 0 &&
|
|
243
|
+
matrix[2 * 4 + 1] === 0 &&
|
|
244
|
+
matrix[2 * 4 + 3] === 0 &&
|
|
245
|
+
matrix[3 * 4 + 2] === 0 &&
|
|
246
|
+
matrix[2 * 4 + 2] === 1 &&
|
|
247
|
+
matrix[3 * 4 + 3] === 1;
|
|
256
248
|
}
|
|
257
249
|
|
|
258
250
|
export { decompose, epsilon, identity, is2DMatrix, multiply, round, toZero };
|