-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
vector.js
350 lines (289 loc) · 8.29 KB
/
vector.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import SVGPath from '../path';
import PDFObject from '../object';
const { number } = PDFObject;
// This constant is used to approximate a symmetrical arc using a cubic
// Bezier curve.
const KAPPA = 4.0 * ((Math.sqrt(2) - 1.0) / 3.0);
export default {
initVector() {
this._ctm = [1, 0, 0, 1, 0, 0]; // current transformation matrix
return (this._ctmStack = []);
},
save() {
this._ctmStack.push(this._ctm.slice());
// TODO: save/restore colorspace and styles so not setting it unnessesarily all the time?
return this.addContent('q');
},
restore() {
this._ctm = this._ctmStack.pop() || [1, 0, 0, 1, 0, 0];
return this.addContent('Q');
},
closePath() {
return this.addContent('h');
},
lineWidth(w) {
return this.addContent(`${number(w)} w`);
},
_CAP_STYLES: {
BUTT: 0,
ROUND: 1,
SQUARE: 2
},
lineCap(c) {
if (typeof c === 'string') {
c = this._CAP_STYLES[c.toUpperCase()];
}
return this.addContent(`${c} J`);
},
_JOIN_STYLES: {
MITER: 0,
ROUND: 1,
BEVEL: 2
},
lineJoin(j) {
if (typeof j === 'string') {
j = this._JOIN_STYLES[j.toUpperCase()];
}
return this.addContent(`${j} j`);
},
miterLimit(m) {
return this.addContent(`${number(m)} M`);
},
dash(length, options = {}) {
const originalLength = length;
if (!Array.isArray(length)) {
length = [length, options.space || length];
}
const valid = length.every(x => Number.isFinite(x) && x > 0);
if (!valid) {
throw new Error(
`dash(${JSON.stringify(originalLength)}, ${JSON.stringify(
options
)}) invalid, lengths must be numeric and greater than zero`
);
}
length = length.map(number).join(' ');
return this.addContent(`[${length}] ${number(options.phase || 0)} d`);
},
undash() {
return this.addContent('[] 0 d');
},
moveTo(x, y) {
return this.addContent(`${number(x)} ${number(y)} m`);
},
lineTo(x, y) {
return this.addContent(`${number(x)} ${number(y)} l`);
},
bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y) {
return this.addContent(
`${number(cp1x)} ${number(cp1y)} ${number(cp2x)} ${number(cp2y)} ${number(
x
)} ${number(y)} c`
);
},
quadraticCurveTo(cpx, cpy, x, y) {
return this.addContent(
`${number(cpx)} ${number(cpy)} ${number(x)} ${number(y)} v`
);
},
rect(x, y, w, h) {
return this.addContent(
`${number(x)} ${number(y)} ${number(w)} ${number(h)} re`
);
},
roundedRect(x, y, w, h, r) {
if (r == null) {
r = 0;
}
r = Math.min(r, 0.5 * w, 0.5 * h);
// amount to inset control points from corners (see `ellipse`)
const c = r * (1.0 - KAPPA);
this.moveTo(x + r, y);
this.lineTo(x + w - r, y);
this.bezierCurveTo(x + w - c, y, x + w, y + c, x + w, y + r);
this.lineTo(x + w, y + h - r);
this.bezierCurveTo(x + w, y + h - c, x + w - c, y + h, x + w - r, y + h);
this.lineTo(x + r, y + h);
this.bezierCurveTo(x + c, y + h, x, y + h - c, x, y + h - r);
this.lineTo(x, y + r);
this.bezierCurveTo(x, y + c, x + c, y, x + r, y);
return this.closePath();
},
ellipse(x, y, r1, r2) {
// based on http://stackoverflow.com/questions/2172798/how-to-draw-an-oval-in-html5-canvas/2173084#2173084
if (r2 == null) {
r2 = r1;
}
x -= r1;
y -= r2;
const ox = r1 * KAPPA;
const oy = r2 * KAPPA;
const xe = x + r1 * 2;
const ye = y + r2 * 2;
const xm = x + r1;
const ym = y + r2;
this.moveTo(x, ym);
this.bezierCurveTo(x, ym - oy, xm - ox, y, xm, y);
this.bezierCurveTo(xm + ox, y, xe, ym - oy, xe, ym);
this.bezierCurveTo(xe, ym + oy, xm + ox, ye, xm, ye);
this.bezierCurveTo(xm - ox, ye, x, ym + oy, x, ym);
return this.closePath();
},
circle(x, y, radius) {
return this.ellipse(x, y, radius);
},
arc(x, y, radius, startAngle, endAngle, anticlockwise) {
if (anticlockwise == null) {
anticlockwise = false;
}
const TWO_PI = 2.0 * Math.PI;
const HALF_PI = 0.5 * Math.PI;
let deltaAng = endAngle - startAngle;
if (Math.abs(deltaAng) > TWO_PI) {
// draw only full circle if more than that is specified
deltaAng = TWO_PI;
} else if (deltaAng !== 0 && anticlockwise !== deltaAng < 0) {
// necessary to flip direction of rendering
const dir = anticlockwise ? -1 : 1;
deltaAng = dir * TWO_PI + deltaAng;
}
const numSegs = Math.ceil(Math.abs(deltaAng) / HALF_PI);
const segAng = deltaAng / numSegs;
const handleLen = (segAng / HALF_PI) * KAPPA * radius;
let curAng = startAngle;
// component distances between anchor point and control point
let deltaCx = -Math.sin(curAng) * handleLen;
let deltaCy = Math.cos(curAng) * handleLen;
// anchor point
let ax = x + Math.cos(curAng) * radius;
let ay = y + Math.sin(curAng) * radius;
// calculate and render segments
this.moveTo(ax, ay);
for (let segIdx = 0; segIdx < numSegs; segIdx++) {
// starting control point
const cp1x = ax + deltaCx;
const cp1y = ay + deltaCy;
// step angle
curAng += segAng;
// next anchor point
ax = x + Math.cos(curAng) * radius;
ay = y + Math.sin(curAng) * radius;
// next control point delta
deltaCx = -Math.sin(curAng) * handleLen;
deltaCy = Math.cos(curAng) * handleLen;
// ending control point
const cp2x = ax - deltaCx;
const cp2y = ay - deltaCy;
// render segment
this.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, ax, ay);
}
return this;
},
polygon(...points) {
this.moveTo(...(points.shift() || []));
for (let point of points) {
this.lineTo(...(point || []));
}
return this.closePath();
},
path(path) {
SVGPath.apply(this, path);
return this;
},
_windingRule(rule) {
if (/even-?odd/.test(rule)) {
return '*';
}
return '';
},
fill(color, rule) {
if (/(even-?odd)|(non-?zero)/.test(color)) {
rule = color;
color = null;
}
if (color) {
this.fillColor(color);
}
return this.addContent(`f${this._windingRule(rule)}`);
},
stroke(color) {
if (color) {
this.strokeColor(color);
}
return this.addContent('S');
},
fillAndStroke(fillColor, strokeColor, rule) {
if (strokeColor == null) {
strokeColor = fillColor;
}
const isFillRule = /(even-?odd)|(non-?zero)/;
if (isFillRule.test(fillColor)) {
rule = fillColor;
fillColor = null;
}
if (isFillRule.test(strokeColor)) {
rule = strokeColor;
strokeColor = fillColor;
}
if (fillColor) {
this.fillColor(fillColor);
this.strokeColor(strokeColor);
}
return this.addContent(`B${this._windingRule(rule)}`);
},
clip(rule) {
return this.addContent(`W${this._windingRule(rule)} n`);
},
transform(m11, m12, m21, m22, dx, dy) {
// keep track of the current transformation matrix
if (m11 === 1 && m12 === 0 && m21 === 0 && m22 === 1 && dx === 0 && dy === 0) {
// Ignore identity transforms
return this;
}
const m = this._ctm;
const [m0, m1, m2, m3, m4, m5] = m;
m[0] = m0 * m11 + m2 * m12;
m[1] = m1 * m11 + m3 * m12;
m[2] = m0 * m21 + m2 * m22;
m[3] = m1 * m21 + m3 * m22;
m[4] = m0 * dx + m2 * dy + m4;
m[5] = m1 * dx + m3 * dy + m5;
const values = [m11, m12, m21, m22, dx, dy].map(v => number(v)).join(' ');
return this.addContent(`${values} cm`);
},
translate(x, y) {
return this.transform(1, 0, 0, 1, x, y);
},
rotate(angle, options = {}) {
let y;
const rad = (angle * Math.PI) / 180;
const cos = Math.cos(rad);
const sin = Math.sin(rad);
let x = (y = 0);
if (options.origin != null) {
[x, y] = options.origin;
const x1 = x * cos - y * sin;
const y1 = x * sin + y * cos;
x -= x1;
y -= y1;
}
return this.transform(cos, sin, -sin, cos, x, y);
},
scale(xFactor, yFactor, options = {}) {
let y;
if (yFactor == null) {
yFactor = xFactor;
}
if (typeof yFactor === 'object') {
options = yFactor;
yFactor = xFactor;
}
let x = (y = 0);
if (options.origin != null) {
[x, y] = options.origin;
x -= xFactor * x;
y -= yFactor * y;
}
return this.transform(xFactor, 0, 0, yFactor, x, y);
}
};