svg-path-interpolator 0.2.5 → 1.0.0
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 +20 -0
- package/README.md +121 -73
- package/lib/SVGPathInterpolator.d.ts +149 -0
- package/lib/SVGPathInterpolator.js +597 -0
- package/lib/cli.d.ts +2 -0
- package/lib/cli.js +27 -0
- package/lib/index.d.ts +13 -0
- package/lib/index.js +27 -0
- package/lib/math/SVGTransform.d.ts +41 -0
- package/lib/math/SVGTransform.js +117 -0
- package/lib/math/calculators.d.ts +54 -0
- package/lib/math/calculators.js +331 -0
- package/lib/math/utils.d.ts +19 -0
- package/{src → lib}/math/utils.js +29 -40
- package/package.json +42 -7
- package/bin/svgpi +0 -51
- package/src/SVGPathInterpolator.js +0 -325
- package/src/math/SVGTransform.js +0 -236
- package/src/math/calculators.js +0 -312
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"author": "Justin Wilaby",
|
|
3
3
|
"name": "svg-path-interpolator",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "1.0.0",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"description": "The SVG Path Interpolator produces point data representing interpolated values within an SVG path.",
|
|
7
7
|
"repository": {
|
|
@@ -9,13 +9,28 @@
|
|
|
9
9
|
"url": "https://github.com/justinwilaby/svg-path-interpolator.git"
|
|
10
10
|
},
|
|
11
11
|
"bin": {
|
|
12
|
-
"svgpi": "./
|
|
12
|
+
"svgpi": "./lib/cli.js"
|
|
13
13
|
},
|
|
14
|
-
"
|
|
14
|
+
"engines": {
|
|
15
|
+
"node": ">=22"
|
|
16
|
+
},
|
|
17
|
+
"type": "module",
|
|
18
|
+
"sideEffects": false,
|
|
19
|
+
"exports": {
|
|
20
|
+
".": {
|
|
21
|
+
"types": "./lib/index.d.ts",
|
|
22
|
+
"import": "./lib/index.js",
|
|
23
|
+
"default": "./lib/index.js"
|
|
24
|
+
},
|
|
25
|
+
"./*": "./lib/*"
|
|
26
|
+
},
|
|
27
|
+
"types": "./lib/index.d.ts",
|
|
28
|
+
"main": "./lib/index.js",
|
|
29
|
+
"browser": "./lib/index.js",
|
|
15
30
|
"files": [
|
|
16
|
-
"
|
|
17
|
-
"
|
|
18
|
-
"
|
|
31
|
+
"CHANGELOG.md",
|
|
32
|
+
"config",
|
|
33
|
+
"lib"
|
|
19
34
|
],
|
|
20
35
|
"keywords": [
|
|
21
36
|
"svg",
|
|
@@ -30,7 +45,27 @@
|
|
|
30
45
|
"curves",
|
|
31
46
|
"polygon"
|
|
32
47
|
],
|
|
48
|
+
"scripts": {
|
|
49
|
+
"build": "tsc -p tsconfig.json",
|
|
50
|
+
"prepack": "npm run build",
|
|
51
|
+
"pretest": "npm run build",
|
|
52
|
+
"test": "node --test",
|
|
53
|
+
"test:coverage": "npm run build && node --test --experimental-test-coverage",
|
|
54
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
55
|
+
"lint": "biome lint .",
|
|
56
|
+
"format:check": "biome format .",
|
|
57
|
+
"format": "biome format --write .",
|
|
58
|
+
"check": "npm run lint && npm run format:check && npm run typecheck && npm run test:coverage && npm run verify:package",
|
|
59
|
+
"verify:package": "node scripts/verify-package.mjs",
|
|
60
|
+
"test:browser": "node scripts/browser-smoke.mjs"
|
|
61
|
+
},
|
|
33
62
|
"dependencies": {
|
|
34
|
-
"sax": "
|
|
63
|
+
"sax-wasm": "3.1.4"
|
|
64
|
+
},
|
|
65
|
+
"devDependencies": {
|
|
66
|
+
"@biomejs/biome": "^2.5.15",
|
|
67
|
+
"@playwright/test": "^1.63.0",
|
|
68
|
+
"@types/node": "26.6.4",
|
|
69
|
+
"typescript": "7.0.2"
|
|
35
70
|
}
|
|
36
71
|
}
|
package/bin/svgpi
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
"use strict";
|
|
4
|
-
//https://pomax.github.io/bezierinfo/
|
|
5
|
-
const fs = require('fs');
|
|
6
|
-
const path = require('path');
|
|
7
|
-
|
|
8
|
-
const SVGPathInterpolator = require('../src/SVGPathInterpolator');
|
|
9
|
-
|
|
10
|
-
const configPath = process.argv[2];
|
|
11
|
-
const svgFile = process.argv[3];
|
|
12
|
-
const outputFile = process.argv[4];
|
|
13
|
-
|
|
14
|
-
function read(file) {
|
|
15
|
-
return new Promise((resolve, reject) => {
|
|
16
|
-
fs.access(file, fs.R_OK, (error) => {
|
|
17
|
-
if (error) {
|
|
18
|
-
console.log(error);
|
|
19
|
-
process.exit(1);
|
|
20
|
-
}
|
|
21
|
-
fs.readFile(file, 'utf8', (error, data) => {
|
|
22
|
-
if (data) {
|
|
23
|
-
resolve(data);
|
|
24
|
-
}
|
|
25
|
-
else {
|
|
26
|
-
reject(error);
|
|
27
|
-
}
|
|
28
|
-
});
|
|
29
|
-
});
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function runJob(configJson) {
|
|
34
|
-
const config = JSON.parse(configJson) || process.exit(1);
|
|
35
|
-
const file = path.normalize(svgFile);
|
|
36
|
-
read(file).then(svg => {
|
|
37
|
-
const json = new SVGPathInterpolator(config).processSvg(svg);
|
|
38
|
-
const jsonStr = JSON.stringify(json, null, (config.pretty ? config.prettyIndent : 0));
|
|
39
|
-
if (outputFile) {
|
|
40
|
-
fs.writeFile(path.normalize(outputFile), jsonStr, err => {
|
|
41
|
-
process.exit(err);
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
else {
|
|
45
|
-
console.log(jsonStr);
|
|
46
|
-
process.exit(0);
|
|
47
|
-
}
|
|
48
|
-
})
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
read(configPath).then(runJob);
|
|
@@ -1,325 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
const parser = require('sax').parser(true);
|
|
3
|
-
const calculators = require('./math/calculators');
|
|
4
|
-
const SVGTransform = require('./math/SVGTransform');
|
|
5
|
-
|
|
6
|
-
const commandRegEx = /(m|l|c|q|z|a|v|h|s|z)(?: ?)([\d+-., ]*)/ig;
|
|
7
|
-
const argumentsRegEx = /([-]?\d+\.*\d*)(?:,| )?/g;
|
|
8
|
-
const transformRegEx = /(matrix|translate|scale|rotate|skewX|skewY)(?:\()(.*)(?:\))/;
|
|
9
|
-
|
|
10
|
-
let _config;
|
|
11
|
-
|
|
12
|
-
function parseArguments(source) {
|
|
13
|
-
const args = [];
|
|
14
|
-
let arg;
|
|
15
|
-
while (arg = argumentsRegEx.exec(source)) {
|
|
16
|
-
isFinite(arg[1]) ? arg[1] = +arg[1] : '' + arg[1];
|
|
17
|
-
args.push(+arg[1]);
|
|
18
|
-
}
|
|
19
|
-
return args;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
function processSVG(data) {
|
|
23
|
-
const openTagsDepth = {};
|
|
24
|
-
const transforms = {};
|
|
25
|
-
const {joinPathData} = _config;
|
|
26
|
-
const interpolatedPaths = joinPathData ? [] : {};
|
|
27
|
-
let i = Date.now();
|
|
28
|
-
|
|
29
|
-
parser.onopentag = node => {
|
|
30
|
-
if (!openTagsDepth[node.name]) {
|
|
31
|
-
openTagsDepth[node.name] = 0;
|
|
32
|
-
}
|
|
33
|
-
const depth = openTagsDepth[node.name]++;
|
|
34
|
-
if (node.attributes.transform) {
|
|
35
|
-
transforms[depth + node.name] = node.attributes.transform;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
if (node.name === 'path') {
|
|
39
|
-
const points = interpolatePath(node.attributes.d);
|
|
40
|
-
applyTransforms(transforms, points);
|
|
41
|
-
if (!joinPathData) {
|
|
42
|
-
let key = node.attributes.id || `path_${i++}`;
|
|
43
|
-
interpolatedPaths[key] = points;
|
|
44
|
-
}
|
|
45
|
-
else {
|
|
46
|
-
interpolatedPaths.push(...points);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
parser.onclosetag = node => {
|
|
52
|
-
const depth = --openTagsDepth[node];
|
|
53
|
-
delete transforms[depth + node];
|
|
54
|
-
};
|
|
55
|
-
parser.write(data).close();
|
|
56
|
-
return interpolatedPaths;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
function applyTransforms(transforms, points) {
|
|
60
|
-
const keys = Object.keys(transforms);
|
|
61
|
-
if (!keys.length) {
|
|
62
|
-
return;
|
|
63
|
-
}
|
|
64
|
-
if (keys.length < 1) {
|
|
65
|
-
keys.sort().reverse();
|
|
66
|
-
}
|
|
67
|
-
keys.forEach(depth => {
|
|
68
|
-
const svgTransform = new SVGTransform();
|
|
69
|
-
const rawTransform = transforms[depth];
|
|
70
|
-
const [, type, rawArguments] = transformRegEx.exec(rawTransform);
|
|
71
|
-
const args = parseArguments(rawArguments);
|
|
72
|
-
svgTransform[type](...args);
|
|
73
|
-
|
|
74
|
-
const len = points.length;
|
|
75
|
-
for (let i = 0; i < len; i += 2) {
|
|
76
|
-
const {x, y} = svgTransform.map(points[i], points[i + 1]);
|
|
77
|
-
points[i] = x - (x % _config.roundToNearest);
|
|
78
|
-
points[i + 1] = y - (y % _config.roundToNearest);
|
|
79
|
-
}
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
function interpolatePath(path) {
|
|
84
|
-
const data = [];
|
|
85
|
-
let subPathStartX = 0;
|
|
86
|
-
let subPathStartY = 0;
|
|
87
|
-
let offsetX = 0;
|
|
88
|
-
let offsetY = 0;
|
|
89
|
-
let args;
|
|
90
|
-
let match;
|
|
91
|
-
let lastCommand = {};
|
|
92
|
-
while (match = commandRegEx.exec(path)) {
|
|
93
|
-
const [, command, rawArguments] = match;
|
|
94
|
-
let points = parseArguments(rawArguments);
|
|
95
|
-
|
|
96
|
-
switch (command) {
|
|
97
|
-
case 'A':
|
|
98
|
-
case 'C':
|
|
99
|
-
case 'L':
|
|
100
|
-
case 'Q':
|
|
101
|
-
points.unshift(offsetX, offsetY);
|
|
102
|
-
args = [points];
|
|
103
|
-
break;
|
|
104
|
-
|
|
105
|
-
case 'S':
|
|
106
|
-
case 's':
|
|
107
|
-
case 'T':
|
|
108
|
-
case 't':
|
|
109
|
-
let lastCtrlX;
|
|
110
|
-
let lastCtrlY;
|
|
111
|
-
const {command:lastC, points:lastP} = lastCommand;
|
|
112
|
-
const reg = command.toLowerCase() === 's' ? /^[cs]$/ : /^[qt]$/;
|
|
113
|
-
if (reg.test(lastC.toLowerCase())) {
|
|
114
|
-
const {length} = lastP;
|
|
115
|
-
lastCtrlY = lastP[length - 3];
|
|
116
|
-
lastCtrlX = lastP[length - 4];
|
|
117
|
-
}
|
|
118
|
-
args = points;
|
|
119
|
-
|
|
120
|
-
if (/^[st]$/.test(command)) {
|
|
121
|
-
applyOffset(offsetX, offsetY, args, 4);
|
|
122
|
-
}
|
|
123
|
-
args.unshift(offsetX, offsetY, lastCtrlX, lastCtrlY);
|
|
124
|
-
args = [args];
|
|
125
|
-
break;
|
|
126
|
-
|
|
127
|
-
case 'a':
|
|
128
|
-
points.unshift(0, 0);
|
|
129
|
-
args = [applyOffset(offsetX, offsetY, points, 7)];
|
|
130
|
-
break;
|
|
131
|
-
|
|
132
|
-
case 'c':
|
|
133
|
-
applyOffset(offsetX, offsetY, points, 6);
|
|
134
|
-
points.unshift(offsetX, offsetY);
|
|
135
|
-
args = [points];
|
|
136
|
-
break;
|
|
137
|
-
|
|
138
|
-
case 'l':
|
|
139
|
-
points.unshift(0, 0);
|
|
140
|
-
args = [applyOffset(offsetX, offsetY, points, 2)];
|
|
141
|
-
break;
|
|
142
|
-
|
|
143
|
-
case 'q':
|
|
144
|
-
points.unshift(0, 0);
|
|
145
|
-
args = [applyOffset(offsetX, offsetY, points, 4)];
|
|
146
|
-
break;
|
|
147
|
-
|
|
148
|
-
case 'H':
|
|
149
|
-
points.unshift(offsetX, offsetY);
|
|
150
|
-
points.push(offsetY);
|
|
151
|
-
args = [points];
|
|
152
|
-
break;
|
|
153
|
-
|
|
154
|
-
case 'h':
|
|
155
|
-
applyOffset(offsetX, offsetX, points, 2); // offsetX, offsetX is intentional
|
|
156
|
-
points.unshift(offsetX, offsetY);
|
|
157
|
-
points.push(offsetY);
|
|
158
|
-
args = [points];
|
|
159
|
-
break;
|
|
160
|
-
|
|
161
|
-
case 'm':
|
|
162
|
-
subPathStartX = points[0] + offsetX;
|
|
163
|
-
subPathStartY = points[1] + offsetY;
|
|
164
|
-
args = [applyOffset(offsetX, offsetY, points.slice(2), 2)];
|
|
165
|
-
break;
|
|
166
|
-
|
|
167
|
-
case 'M':
|
|
168
|
-
subPathStartX = points[0];
|
|
169
|
-
subPathStartY = points[1];
|
|
170
|
-
args = [points.slice(2)];
|
|
171
|
-
break;
|
|
172
|
-
|
|
173
|
-
case 'V':
|
|
174
|
-
points.unshift(offsetX, offsetY, offsetX);
|
|
175
|
-
args = [points];
|
|
176
|
-
break;
|
|
177
|
-
|
|
178
|
-
case 'v':
|
|
179
|
-
applyOffset(offsetY, offsetY, points, 2); // offsetY, offsetY is intentional
|
|
180
|
-
points.unshift(offsetX, offsetY, offsetX);
|
|
181
|
-
args = [points];
|
|
182
|
-
break;
|
|
183
|
-
|
|
184
|
-
case 'z':
|
|
185
|
-
case 'Z':
|
|
186
|
-
points = [offsetX, offsetY, subPathStartX, subPathStartY];
|
|
187
|
-
args = [points];
|
|
188
|
-
break;
|
|
189
|
-
}
|
|
190
|
-
const calculator = calculators[command.toLowerCase()];
|
|
191
|
-
const offsets = {offsetX, offsetY};
|
|
192
|
-
if (calculator) {
|
|
193
|
-
const {minDistance, roundToNearest, sampleFrequency} = _config;
|
|
194
|
-
args.push(minDistance, roundToNearest, sampleFrequency);
|
|
195
|
-
const pts = calculator(...args);
|
|
196
|
-
data.push(...pts);
|
|
197
|
-
const len = ~~points.length;
|
|
198
|
-
|
|
199
|
-
offsetY = points[len - 1];
|
|
200
|
-
offsetX = points[len - 2];
|
|
201
|
-
}
|
|
202
|
-
lastCommand = {command, points, offsets};
|
|
203
|
-
}
|
|
204
|
-
if (_config.trim) {
|
|
205
|
-
trimPathOffsets(data);
|
|
206
|
-
}
|
|
207
|
-
return data;
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
function trimPathOffsets(paths) {
|
|
211
|
-
let minX = Number.POSITIVE_INFINITY;
|
|
212
|
-
let minY = Number.POSITIVE_INFINITY;
|
|
213
|
-
let i = paths.length;
|
|
214
|
-
let x;
|
|
215
|
-
let y;
|
|
216
|
-
if (!i) {
|
|
217
|
-
return;
|
|
218
|
-
}
|
|
219
|
-
while (i -= 2) {
|
|
220
|
-
x = paths[i];
|
|
221
|
-
y = paths[i - 1];
|
|
222
|
-
if (x < minX) {
|
|
223
|
-
minX = x;
|
|
224
|
-
}
|
|
225
|
-
if (y < minY) {
|
|
226
|
-
minY = y;
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
i = paths.length;
|
|
231
|
-
while (i -= 2) {
|
|
232
|
-
paths[i] -= minX;
|
|
233
|
-
paths[i - 1] -= minY;
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
function applyOffset(offsetX, offsetY, coords = [], setLength = 2) {
|
|
238
|
-
for (let i = 0; i < coords.length; i++) {
|
|
239
|
-
if (i && i % +setLength === 0) {
|
|
240
|
-
offsetX = coords[i - 2];
|
|
241
|
-
offsetY = coords[i - 1];
|
|
242
|
-
}
|
|
243
|
-
coords[i] += (i % 2 ? +offsetY : +offsetX);
|
|
244
|
-
}
|
|
245
|
-
return coords;
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
module.exports = class SVGPathInterpolator {
|
|
249
|
-
/**
|
|
250
|
-
* When trim is true, paths that were translated
|
|
251
|
-
* are normalized and will begin at 0,0.
|
|
252
|
-
*
|
|
253
|
-
* @var {boolean} trim
|
|
254
|
-
* @memberOf SVGPathInterpolator#
|
|
255
|
-
* @default false
|
|
256
|
-
*/
|
|
257
|
-
|
|
258
|
-
/**
|
|
259
|
-
* minDistance is the minimum distance between the
|
|
260
|
-
* current and previous points when sampling.
|
|
261
|
-
* If a sample results in a distance less than the
|
|
262
|
-
* specified value, the point is discarded.
|
|
263
|
-
*
|
|
264
|
-
* @var {number} minDistance
|
|
265
|
-
* @memberOf SVGPathInterpolator#
|
|
266
|
-
* @default 0.5
|
|
267
|
-
*/
|
|
268
|
-
|
|
269
|
-
/**
|
|
270
|
-
* roundToNearest is useful when snapping to fractional
|
|
271
|
-
* pixel values. For example, if roundToNearest is .25,
|
|
272
|
-
* a sample resulting in the point 2.343200092,4.6100923
|
|
273
|
-
* will round to 2.25,4.5.
|
|
274
|
-
*
|
|
275
|
-
* @var {number} roundToNearest
|
|
276
|
-
* @memberOf SVGPathInterpolator#
|
|
277
|
-
* @default 0.25
|
|
278
|
-
*/
|
|
279
|
-
|
|
280
|
-
/**
|
|
281
|
-
* sampleFrequency determines the increment of t when sampling.
|
|
282
|
-
* If sampleFrequency is set to .001 , since t iterates from
|
|
283
|
-
* 0 to 1, there will be 1000 points sampled per command
|
|
284
|
-
* but only points that are greater than minDistance are captured.
|
|
285
|
-
*
|
|
286
|
-
* @var {number} sampleFrequency
|
|
287
|
-
* @memberOf SVGPathInterpolator#
|
|
288
|
-
* @default 0.001
|
|
289
|
-
*/
|
|
290
|
-
|
|
291
|
-
/**
|
|
292
|
-
* When true, pretty creates formatted json output.
|
|
293
|
-
*
|
|
294
|
-
* @var {boolean} pretty
|
|
295
|
-
* @memberOf SVGPathInterpolator#
|
|
296
|
-
* @default false
|
|
297
|
-
*/
|
|
298
|
-
|
|
299
|
-
/**
|
|
300
|
-
* Then number of spaces to indent when pretty is true.
|
|
301
|
-
*
|
|
302
|
-
* @var {int} prettyIndent
|
|
303
|
-
* @memberOf SVGPathInterpolator#
|
|
304
|
-
* @default 0
|
|
305
|
-
*/
|
|
306
|
-
|
|
307
|
-
constructor(config = {
|
|
308
|
-
joinPathData: false,
|
|
309
|
-
minDistance: +0.5,
|
|
310
|
-
roundToNearest: +0.25,
|
|
311
|
-
sampleFrequency: +0.001
|
|
312
|
-
}) {
|
|
313
|
-
Object.assign(this, config);
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
processSvg(svg) {
|
|
317
|
-
_config = {
|
|
318
|
-
joinPathData: this.joinPathData,
|
|
319
|
-
minDistance: this.minDistance,
|
|
320
|
-
roundToNearest: this.roundToNearest,
|
|
321
|
-
sampleFrequency: this.sampleFrequency
|
|
322
|
-
};
|
|
323
|
-
return processSVG(svg);
|
|
324
|
-
}
|
|
325
|
-
};
|
package/src/math/SVGTransform.js
DELETED
|
@@ -1,236 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Javascript adaptation of AffineTransform.ccp from
|
|
3
|
-
* https://github.com/WebKit/webkit/blob/66e68cd8d7bf4ea1cf52f31ed9cb242f83ea5b57/Source/WebCore/platform/graphics/transforms/AffineTransform.cpp
|
|
4
|
-
*/
|
|
5
|
-
/*
|
|
6
|
-
* Copyright (C) Research In Motion Limited 2010. All rights reserved.
|
|
7
|
-
*
|
|
8
|
-
* This library is free software; you can redistribute it and/or
|
|
9
|
-
* modify it under the terms of the GNU Library General Public
|
|
10
|
-
* License as published by the Free Software Foundation; either
|
|
11
|
-
* version 2 of the License, or (at your option) any later version.
|
|
12
|
-
*
|
|
13
|
-
* This library is distributed in the hope that it will be useful,
|
|
14
|
-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15
|
-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
16
|
-
* Library General Public License for more details.
|
|
17
|
-
*
|
|
18
|
-
* You should have received a copy of the GNU Library General Public License
|
|
19
|
-
* along with this library; see the file COPYING.LIB. If not, write to
|
|
20
|
-
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
21
|
-
* Boston, MA 02110-1301, USA.
|
|
22
|
-
*/
|
|
23
|
-
/*
|
|
24
|
-
* Copyright (C) 2005, 2006 Apple Inc. All rights reserved.
|
|
25
|
-
* 2010 Dirk Schulze <krit@webkit.org>
|
|
26
|
-
*
|
|
27
|
-
* Redistribution and use in source and binary forms, with or without
|
|
28
|
-
* modification, are permitted provided that the following conditions
|
|
29
|
-
* are met:
|
|
30
|
-
* 1. Redistributions of source code must retain the above copyright
|
|
31
|
-
* notice, this list of conditions and the following disclaimer.
|
|
32
|
-
* 2. Redistributions in binary form must reproduce the above copyright
|
|
33
|
-
* notice, this list of conditions and the following disclaimer in the
|
|
34
|
-
* documentation and/or other materials provided with the distribution.
|
|
35
|
-
*
|
|
36
|
-
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
|
|
37
|
-
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
38
|
-
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
39
|
-
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
|
|
40
|
-
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
41
|
-
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
42
|
-
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
43
|
-
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
|
44
|
-
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
45
|
-
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
46
|
-
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
47
|
-
*/
|
|
48
|
-
const degToRads = require('./utils').degToRads;
|
|
49
|
-
const radToDeg = require('./utils').radToDeg;
|
|
50
|
-
|
|
51
|
-
class SVGTransform {
|
|
52
|
-
constructor(a = 1, b = 0, c = 0, d = 1, e = 0, f = 0) {
|
|
53
|
-
this.m_transform = [+a, +b, +c, +d, +e, +f];
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
makeIdentity() {
|
|
57
|
-
this.setMatrix(1, 0, 0, 1, 0, 0);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
setMatrix(a = 1, b = 0, c = 0, d = 1, e = 0, f = 0) {
|
|
61
|
-
const m_transform = this.m_transform;
|
|
62
|
-
m_transform[0] = +a;
|
|
63
|
-
m_transform[1] = +b;
|
|
64
|
-
m_transform[2] = +c;
|
|
65
|
-
m_transform[3] = +d;
|
|
66
|
-
m_transform[4] = +e;
|
|
67
|
-
m_transform[5] = +f;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
matrix(a = 1, b = 0, c = 0, d = 1, e = 0, f = 0){
|
|
71
|
-
this.setMatrix(a, b, c, d, e, f);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
isIdentity() {
|
|
75
|
-
const m_transform = this.m_transform;
|
|
76
|
-
|
|
77
|
-
return (m_transform[0] === 1 && m_transform[1] === 0
|
|
78
|
-
&& m_transform[2] === 0 && m_transform[3] === 1
|
|
79
|
-
&& m_transform[4] === 0 && m_transform[5] === 0);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
static det(transform) {
|
|
83
|
-
return +(transform[0] * transform[3] - transform[1] * transform[2]);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
get xScale() {
|
|
87
|
-
const m_transform = this.m_transform;
|
|
88
|
-
|
|
89
|
-
return Math.sqrt(m_transform[0] * m_transform[0] + m_transform[1] * m_transform[1]);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
get yScale() {
|
|
93
|
-
const m_transform = this.m_transform;
|
|
94
|
-
return Math.sqrt(m_transform[2] * m_transform[2] + m_transform[3] * m_transform[3]);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
get isInvertible() {
|
|
98
|
-
const determinant = SVGTransform.det(this.m_transform);
|
|
99
|
-
return Boolean(determinant && isFinite(determinant));
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
inverse() {
|
|
103
|
-
const m_transform = this.m_transform;
|
|
104
|
-
const determinant = SVGTransform.det(m_transform);
|
|
105
|
-
const result = new SVGTransform();
|
|
106
|
-
if (this.isIdentityOrTranslation()) {
|
|
107
|
-
result.m_transform[4] = -m_transform[4];
|
|
108
|
-
result.m_transform[5] = -m_transform[5];
|
|
109
|
-
return result;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
result.m_transform[0] = m_transform[3] / determinant;
|
|
113
|
-
result.m_transform[1] = -m_transform[1] / determinant;
|
|
114
|
-
result.m_transform[2] = -m_transform[2] / determinant;
|
|
115
|
-
result.m_transform[3] = m_transform[0] / determinant;
|
|
116
|
-
result.m_transform[4] = (m_transform[2] * m_transform[5] - m_transform[3] * m_transform[4]) / determinant;
|
|
117
|
-
result.m_transform[5] = (m_transform[1] * m_transform[4] - m_transform[0] * m_transform[5]) / determinant;
|
|
118
|
-
|
|
119
|
-
return result;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
isIdentityOrTranslation() {
|
|
123
|
-
const m_transform = this.m_transform;
|
|
124
|
-
return m_transform[0] === 1 && m_transform[1] === 0 && m_transform[2] === 0 && m_transform[3] === 1;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
multiply(other){
|
|
128
|
-
const m_transform = this.m_transform;
|
|
129
|
-
const trans = new SVGTransform();
|
|
130
|
-
const om_transform = other.m_transform;
|
|
131
|
-
const tm_transform = trans.m_transform;
|
|
132
|
-
|
|
133
|
-
tm_transform[0] = om_transform[0] * m_transform[0] + om_transform[1] * m_transform[2];
|
|
134
|
-
tm_transform[1] = om_transform[0] * m_transform[1] + om_transform[1] * m_transform[3];
|
|
135
|
-
tm_transform[2] = om_transform[2] * m_transform[0] + om_transform[3] * m_transform[2];
|
|
136
|
-
tm_transform[3] = om_transform[2] * m_transform[1] + om_transform[3] * m_transform[3];
|
|
137
|
-
tm_transform[4] = om_transform[4] * m_transform[0] + om_transform[5] * m_transform[2] + m_transform[4];
|
|
138
|
-
tm_transform[5] = om_transform[4] * m_transform[1] + om_transform[5] * m_transform[3] + m_transform[5];
|
|
139
|
-
|
|
140
|
-
this.m_transform = trans.m_transform;
|
|
141
|
-
return this;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
rotate(degrees, x, y){
|
|
145
|
-
const translateFlag = Boolean(x !== undefined && y !== undefined);
|
|
146
|
-
if (translateFlag){
|
|
147
|
-
this.translate(x, y);
|
|
148
|
-
}
|
|
149
|
-
const rads = degToRads(degrees);
|
|
150
|
-
const cosAngle = Math.cos(rads);
|
|
151
|
-
const sinAngle = Math.sin(rads);
|
|
152
|
-
const rot = new SVGTransform(cosAngle, sinAngle, -sinAngle, cosAngle, 0, 0);
|
|
153
|
-
|
|
154
|
-
this.multiply(rot);
|
|
155
|
-
|
|
156
|
-
if (translateFlag){
|
|
157
|
-
this.translate(-x, -y);
|
|
158
|
-
}
|
|
159
|
-
return this;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
scale(sx, sy){
|
|
163
|
-
const m_transform = this.m_transform;
|
|
164
|
-
if (sy === undefined){
|
|
165
|
-
sy = sx;
|
|
166
|
-
}
|
|
167
|
-
m_transform[0] *= sx;
|
|
168
|
-
m_transform[1] *= sx;
|
|
169
|
-
m_transform[2] *= sy;
|
|
170
|
-
m_transform[3] *= sy;
|
|
171
|
-
|
|
172
|
-
return this;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
translate(tx, ty){
|
|
176
|
-
const m_transform = this.m_transform;
|
|
177
|
-
if (this.isIdentityOrTranslation()) {
|
|
178
|
-
m_transform[4] += tx;
|
|
179
|
-
m_transform[5] += ty;
|
|
180
|
-
return this;
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
m_transform[4] += tx * m_transform[0] + ty * m_transform[2];
|
|
184
|
-
m_transform[5] += tx * m_transform[1] + ty * m_transform[3];
|
|
185
|
-
return this;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
rotateFromVector(x, y){
|
|
189
|
-
return this.rotate(radToDeg(Math.atan2(y, x)));
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
flipX(){
|
|
193
|
-
return this.scale(-1, 1);
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
flipY(){
|
|
197
|
-
return this.scale(1, -1);
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
shear(sx, sy){
|
|
201
|
-
const m_transform = this.m_transform;
|
|
202
|
-
const a = m_transform[0];
|
|
203
|
-
const b = m_transform[1];
|
|
204
|
-
|
|
205
|
-
m_transform[0] += sy * m_transform[2];
|
|
206
|
-
m_transform[1] += sy * m_transform[3];
|
|
207
|
-
m_transform[2] += sx * a;
|
|
208
|
-
m_transform[3] += sx * b;
|
|
209
|
-
return this;
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
skew(angleX, angleY){
|
|
213
|
-
return this.shear(Math.tan(degToRads(angleX)), Math.tan(degToRads(angleY)));
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
skewX(angle){
|
|
217
|
-
return this.shear(Math.tan(degToRads(angle)), 0);
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
skewY(angle){
|
|
221
|
-
return this.shear(0, Math.tan(degToRads(angle)));
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
map(x, y){
|
|
225
|
-
const m_transform = this.m_transform;
|
|
226
|
-
return {
|
|
227
|
-
x: (m_transform[0] * x + m_transform[2] * y + m_transform[4]),
|
|
228
|
-
y: (m_transform[1] * x + m_transform[3] * y + m_transform[5])
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
mapPoint(point){
|
|
233
|
-
return this.map(point.x, point.y);
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
module.exports = SVGTransform;
|