swf2js 0.1.0 → 0.1.2
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/README.md +9 -9
- package/package.json +3 -2
- package/src/cli.js +15 -6
- package/src/emitter/bundle-emitter.js +56 -16
- package/src/runtime/avm1-runtime.js +329 -84
- package/src/runtime/avm2-runtime.js +56 -6
- package/src/runtime/embedded-movie-runtime.js +6 -1
- package/src/runtime/index.js +2 -6
- package/src/runtime/movie-runtime.js +2 -1
- package/src/runtime/swflib/assemble.js +34 -0
- package/src/runtime/swflib/bitmap-image.js +4 -0
- package/src/runtime/swflib/bitmap.js +1004 -0
- package/src/runtime/swflib/builtins.js +156 -0
- package/src/runtime/swflib/bytearray.js +949 -0
- package/src/runtime/swflib/compiled-swf.js +3 -0
- package/src/runtime/swflib/context-menu.js +245 -0
- package/src/runtime/swflib/cursor-flag.js +12 -0
- package/src/runtime/swflib/dictionary.js +121 -0
- package/src/runtime/swflib/display.js +823 -0
- package/src/runtime/swflib/events.js +754 -0
- package/src/runtime/swflib/filters.js +298 -0
- package/src/runtime/swflib/flash.js +130 -0
- package/src/runtime/swflib/geom.js +759 -0
- package/src/runtime/swflib/glyph-metrics.js +14 -0
- package/src/runtime/swflib/graphics.js +735 -0
- package/src/runtime/swflib/host.js +2 -0
- package/src/runtime/swflib/inherit.js +22 -0
- package/src/runtime/swflib/input.js +1085 -0
- package/src/runtime/swflib/loader.js +687 -0
- package/src/runtime/swflib/marks.js +16 -0
- package/src/runtime/swflib/matrix3d.js +144 -0
- package/src/runtime/swflib/pointer-bridge.js +8 -0
- package/src/runtime/swflib/projection.js +158 -0
- package/src/runtime/swflib/render.js +1240 -0
- package/src/runtime/swflib/sound.js +1166 -0
- package/src/runtime/swflib/sprite-drag.js +116 -0
- package/src/runtime/swflib/stage-ref.js +9 -0
- package/src/runtime/swflib/stage.js +484 -0
- package/src/runtime/swflib/system.js +987 -0
- package/src/runtime/swflib/text-field.js +519 -0
- package/src/runtime/swflib/text-paint.js +797 -0
- package/src/runtime/swflib/text.js +1387 -0
- package/src/runtime/swflib/transform.js +133 -0
- package/src/runtime/swflib/xml.js +268 -0
- package/src/transpiler/avm1/avm1-ir.js +2 -1
- package/src/transpiler/avm1/avm1-rewrite.js +23 -2
- package/src/transpiler/avm1/avm1-transpiler.js +4 -1
- package/src/transpiler/avm2/avm2-transpiler.js +38 -7
- package/src/transpiler/timeline/timeline-codegen.js +366 -123
- package/src/runtime/swflib.js +0 -14050
|
@@ -0,0 +1,735 @@
|
|
|
1
|
+
import { bitmapDisplayImage } from './bitmap-image.js';
|
|
2
|
+
import { cursorFlag } from './cursor-flag.js';
|
|
3
|
+
import { DisplayObject, DisplayObjectContainer, cssColor, gradientStops } from './display.js';
|
|
4
|
+
import { Matrix, Rectangle } from './geom.js';
|
|
5
|
+
import { spriteMark } from './marks.js';
|
|
6
|
+
import {
|
|
7
|
+
startSpriteMouseDrag, stopSpriteMouseDrag, startSpriteTouchDrag, stopSpriteTouchDrag,
|
|
8
|
+
} from './sprite-drag.js';
|
|
9
|
+
|
|
10
|
+
export class GraphicsPathCommand {
|
|
11
|
+
static NO_OP = 0;
|
|
12
|
+
static MOVE_TO = 1;
|
|
13
|
+
static LINE_TO = 2;
|
|
14
|
+
static CURVE_TO = 3;
|
|
15
|
+
static WIDE_LINE_TO = 4;
|
|
16
|
+
static WIDE_MOVE_TO = 5;
|
|
17
|
+
static CUBIC_CURVE_TO = 6;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export class GraphicsPathWinding {
|
|
21
|
+
static EVEN_ODD = 'evenOdd';
|
|
22
|
+
static NON_ZERO = 'nonZero';
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Canvas-backed AS3/AS2 Graphics — records commands, replayed in renderTree. */
|
|
26
|
+
|
|
27
|
+
export class Graphics {
|
|
28
|
+
constructor() {
|
|
29
|
+
this._cmds = [];
|
|
30
|
+
this._fill = null;
|
|
31
|
+
this._line = null;
|
|
32
|
+
}
|
|
33
|
+
clear() {
|
|
34
|
+
this._cmds = [];
|
|
35
|
+
this._fill = null;
|
|
36
|
+
this._line = null;
|
|
37
|
+
}
|
|
38
|
+
beginFill(color, alpha) {
|
|
39
|
+
this._fill = { color: cssColor(color, alpha), alpha: alpha == null ? 1 : +alpha };
|
|
40
|
+
this._cmds.push({ op: 'fill', fill: this._fill });
|
|
41
|
+
}
|
|
42
|
+
beginBitmapFill(bitmap, matrix, repeat, smooth) {
|
|
43
|
+
this._fill = { bitmap: bitmap, matrix: matrix, repeat: repeat !== false, smooth: !!smooth };
|
|
44
|
+
this._cmds.push({ op: 'fill', fill: this._fill });
|
|
45
|
+
}
|
|
46
|
+
beginGradientFill(type, colors, alphas, ratios, matrix, spreadMethod, interpolationMethod, focalPointRatio) {
|
|
47
|
+
this._fill = {
|
|
48
|
+
gradientType: type || 'linear', colors: colors || [], alphas: alphas || [], ratios: ratios || [], matrix: matrix || null,
|
|
49
|
+
spreadMethod: spreadMethod || 'pad', interpolationMethod: interpolationMethod || 'rgb', focalPointRatio: +focalPointRatio || 0,
|
|
50
|
+
}
|
|
51
|
+
this._cmds.push({ op: 'fill', fill: this._fill });
|
|
52
|
+
}
|
|
53
|
+
endFill() {
|
|
54
|
+
this._cmds.push({ op: 'endFill' });
|
|
55
|
+
this._fill = null;
|
|
56
|
+
}
|
|
57
|
+
lineStyle(thickness, color, alpha, pixelHinting, scaleMode, caps, joints, miterLimit) {
|
|
58
|
+
this._line = {
|
|
59
|
+
width: thickness == null ? NaN : Math.min(255, Math.max(0, +thickness)),
|
|
60
|
+
color: cssColor(color == null ? 0 : color, alpha),
|
|
61
|
+
pixelHinting: !!pixelHinting,
|
|
62
|
+
scaleMode: scaleMode || 'normal',
|
|
63
|
+
cap: caps === 'none' ? 'butt' : (caps === 'square' || caps === 'round' ? caps : 'round'),
|
|
64
|
+
join: joints === 'miter' || joints === 'bevel' || joints === 'round' ? joints : 'round',
|
|
65
|
+
miterLimit: miterLimit == null || isNaN(+miterLimit) ? 3 : Math.min(255, Math.max(1, +miterLimit)),
|
|
66
|
+
}
|
|
67
|
+
this._cmds.push({ op: 'lineStyle', line: this._line });
|
|
68
|
+
}
|
|
69
|
+
moveTo(x, y) { this._cmds.push({ op: 'M', x: +x || 0, y: +y || 0 }); };
|
|
70
|
+
lineTo(x, y) { this._cmds.push({ op: 'L', x: +x || 0, y: +y || 0 }); };
|
|
71
|
+
curveTo(cx, cy, x, y) {
|
|
72
|
+
this._cmds.push({ op: 'Q', cx: +cx || 0, cy: +cy || 0, x: +x || 0, y: +y || 0 });
|
|
73
|
+
}
|
|
74
|
+
cubicCurveTo(c1x, c1y, c2x, c2y, x, y) {
|
|
75
|
+
this._cmds.push({ op: 'C', c1x: +c1x || 0, c1y: +c1y || 0, c2x: +c2x || 0, c2y: +c2y || 0, x: +x || 0, y: +y || 0 });
|
|
76
|
+
}
|
|
77
|
+
drawPath(commands, data, winding) {
|
|
78
|
+
if (!commands || !data) return;
|
|
79
|
+
var values = data;
|
|
80
|
+
var index = 0;
|
|
81
|
+
var decoded = [];
|
|
82
|
+
var move = function () {
|
|
83
|
+
if (index + 1 >= values.length) { index = values.length; return null; }
|
|
84
|
+
return { x: +values[index++], y: +values[index++] };
|
|
85
|
+
}
|
|
86
|
+
var rules = GraphicsPathWinding;
|
|
87
|
+
var rule = winding === rules.EVEN_ODD || winding === 'evenodd' ? 'evenodd' : 'nonzero';
|
|
88
|
+
for (var i = 0; i < commands.length; i++) {
|
|
89
|
+
var command = commands[i] | 0;
|
|
90
|
+
switch (command) {
|
|
91
|
+
case GraphicsPathCommand.NO_OP:
|
|
92
|
+
break;
|
|
93
|
+
case GraphicsPathCommand.MOVE_TO:
|
|
94
|
+
case GraphicsPathCommand.LINE_TO: {
|
|
95
|
+
var point = move();
|
|
96
|
+
if (!point) return;
|
|
97
|
+
decoded.push({ op: command === GraphicsPathCommand.MOVE_TO ? 'M' : 'L', x: point.x, y: point.y });
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
case GraphicsPathCommand.CURVE_TO: {
|
|
101
|
+
var control = move(), end = move();
|
|
102
|
+
if (!control || !end) return;
|
|
103
|
+
decoded.push({ op: 'Q', cx: control.x, cy: control.y, x: end.x, y: end.y });
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
case GraphicsPathCommand.WIDE_LINE_TO:
|
|
107
|
+
case GraphicsPathCommand.WIDE_MOVE_TO:
|
|
108
|
+
// The first pair carries obsolete line-width values; coordinates follow it.
|
|
109
|
+
index += 2;
|
|
110
|
+
var widePoint = move();
|
|
111
|
+
if (!widePoint) return;
|
|
112
|
+
decoded.push({ op: command === GraphicsPathCommand.WIDE_MOVE_TO ? 'M' : 'L', x: widePoint.x, y: widePoint.y });
|
|
113
|
+
break;
|
|
114
|
+
case GraphicsPathCommand.CUBIC_CURVE_TO: {
|
|
115
|
+
var c1 = move(), c2 = move(), cubicEnd = move();
|
|
116
|
+
if (!c1 || !c2 || !cubicEnd) return;
|
|
117
|
+
decoded.push({ op: 'C', c1x: c1.x, c1y: c1.y, c2x: c2.x, c2y: c2.y, x: cubicEnd.x, y: cubicEnd.y });
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
default:
|
|
121
|
+
// Unknown commands have no specified payload width; stop so later operands stay aligned.
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
this._cmds.push({ op: 'winding', rule: rule });
|
|
126
|
+
for (var d = 0; d < decoded.length; d++) this._cmds.push(decoded[d]);
|
|
127
|
+
}
|
|
128
|
+
drawTriangles(vertices, indices, uvtData, culling) {
|
|
129
|
+
if (!vertices || vertices.length < 6) return;
|
|
130
|
+
var count = indices && indices.length ? indices.length : vertices.length / 2;
|
|
131
|
+
var mode = culling || 'none';
|
|
132
|
+
var vertexCount = Math.floor(vertices.length / 2);
|
|
133
|
+
var stride = uvtData && uvtData.length >= vertexCount * 3 ? 3 : 2;
|
|
134
|
+
var textured = !!(uvtData && uvtData.length >= vertexCount * 2);
|
|
135
|
+
var triangles = [];
|
|
136
|
+
function vertexIndex(i) { return indices && indices.length ? indices[i] | 0 : i; }
|
|
137
|
+
for (var i = 0; i + 2 < count; i += 3) {
|
|
138
|
+
var ia = vertexIndex(i), ib = vertexIndex(i + 1), ic = vertexIndex(i + 2);
|
|
139
|
+
if (ia < 0 || ib < 0 || ic < 0 || ia * 2 + 1 >= vertices.length || ib * 2 + 1 >= vertices.length || ic * 2 + 1 >= vertices.length) continue;
|
|
140
|
+
var ax = +vertices[ia * 2], ay = +vertices[ia * 2 + 1];
|
|
141
|
+
var bx = +vertices[ib * 2], by = +vertices[ib * 2 + 1];
|
|
142
|
+
var cx = +vertices[ic * 2], cy = +vertices[ic * 2 + 1];
|
|
143
|
+
var area = (bx - ax) * (cy - ay) - (by - ay) * (cx - ax);
|
|
144
|
+
if ((mode === 'positive' && area > 0) || (mode === 'negative' && area < 0)) continue;
|
|
145
|
+
if (textured) {
|
|
146
|
+
var points = [[ax, ay], [bx, by], [cx, cy]];
|
|
147
|
+
var uvs = [ia, ib, ic].map(function (index) {
|
|
148
|
+
return { u: +uvtData[index * stride], v: +uvtData[index * stride + 1], t: stride === 3 ? +uvtData[index * stride + 2] : 1 };
|
|
149
|
+
});
|
|
150
|
+
if (uvs.every(function (uv) { return Number.isFinite(uv.u) && Number.isFinite(uv.v) && Number.isFinite(uv.t) && uv.t !== 0; })) {
|
|
151
|
+
triangles.push({ points: points, uvs: uvs });
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
this._cmds.push(
|
|
156
|
+
{ op: 'M', x: ax, y: ay }, { op: 'L', x: bx, y: by },
|
|
157
|
+
{ op: 'L', x: cx, y: cy }, { op: 'Z' }
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
if (triangles.length) this._cmds.push({ op: 'triangles', triangles: triangles });
|
|
161
|
+
}
|
|
162
|
+
drawRect(x, y, w, h) {
|
|
163
|
+
x = +x || 0; y = +y || 0; w = +w || 0; h = +h || 0;
|
|
164
|
+
this.moveTo(x, y);
|
|
165
|
+
this.lineTo(x + w, y);
|
|
166
|
+
this.lineTo(x + w, y + h);
|
|
167
|
+
this.lineTo(x, y + h);
|
|
168
|
+
this._cmds.push({ op: 'Z' });
|
|
169
|
+
}
|
|
170
|
+
drawCircle(x, y, r) {
|
|
171
|
+
x = +x || 0; y = +y || 0; r = +r || 0;
|
|
172
|
+
this._cmds.push({ op: 'circle', x: x, y: y, r: Math.abs(r) });
|
|
173
|
+
}
|
|
174
|
+
drawEllipse(x, y, width, height) {
|
|
175
|
+
x = +x || 0; y = +y || 0; width = +width || 0; height = +height || 0;
|
|
176
|
+
var cx = x + width / 2, cy = y + height / 2;
|
|
177
|
+
var rx = Math.abs(width) / 2, ry = Math.abs(height) / 2;
|
|
178
|
+
var k = 0.5522847498;
|
|
179
|
+
this._cmds.push(
|
|
180
|
+
{ op: 'M', x: cx + rx, y: cy },
|
|
181
|
+
{ op: 'C', c1x: cx + rx, c1y: cy + ry * k, c2x: cx + rx * k, c2y: cy + ry, x: cx, y: cy + ry },
|
|
182
|
+
{ op: 'C', c1x: cx - rx * k, c1y: cy + ry, c2x: cx - rx, c2y: cy + ry * k, x: cx - rx, y: cy },
|
|
183
|
+
{ op: 'C', c1x: cx - rx, c1y: cy - ry * k, c2x: cx - rx * k, c2y: cy - ry, x: cx, y: cy - ry },
|
|
184
|
+
{ op: 'C', c1x: cx + rx * k, c1y: cy - ry, c2x: cx + rx, c2y: cy - ry * k, x: cx + rx, y: cy },
|
|
185
|
+
{ op: 'Z' }
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
drawRoundRect(x, y, w, h, ellipseW, ellipseH) {
|
|
189
|
+
x = +x || 0; y = +y || 0; w = +w || 0; h = +h || 0;
|
|
190
|
+
var rx = Math.min(Math.abs(+ellipseW || 0) / 2, Math.abs(w) / 2);
|
|
191
|
+
var ry = Math.min(Math.abs(ellipseH == null ? ellipseW : ellipseH) / 2, Math.abs(h) / 2);
|
|
192
|
+
this.drawRoundRectComplex(x, y, w, h, rx, ry, rx, ry, rx, ry, rx, ry);
|
|
193
|
+
}
|
|
194
|
+
drawRoundRectComplex(x, y, width, height,
|
|
195
|
+
topLeftRadiusX, topLeftRadiusY, topRightRadiusX, topRightRadiusY,
|
|
196
|
+
bottomLeftRadiusX, bottomLeftRadiusY, bottomRightRadiusX, bottomRightRadiusY) {
|
|
197
|
+
x = +x || 0; y = +y || 0; width = +width || 0; height = +height || 0;
|
|
198
|
+
var left = Math.min(x, x + width), right = Math.max(x, x + width);
|
|
199
|
+
var top = Math.min(y, y + height), bottom = Math.max(y, y + height);
|
|
200
|
+
function rx(value) { return Math.max(0, Math.min(Math.abs(+value || 0), (right - left) / 2)); }
|
|
201
|
+
function ry(value) { return Math.max(0, Math.min(Math.abs(+value || 0), (bottom - top) / 2)); }
|
|
202
|
+
var tlx = rx(topLeftRadiusX), tly = ry(topLeftRadiusY);
|
|
203
|
+
var trx = rx(topRightRadiusX), try_ = ry(topRightRadiusY);
|
|
204
|
+
var blx = rx(bottomLeftRadiusX), bly = ry(bottomLeftRadiusY);
|
|
205
|
+
var brx = rx(bottomRightRadiusX), bry = ry(bottomRightRadiusY);
|
|
206
|
+
var k = 0.5522847498;
|
|
207
|
+
this._cmds.push(
|
|
208
|
+
{ op: 'M', x: left + tlx, y: top },
|
|
209
|
+
{ op: 'L', x: right - trx, y: top },
|
|
210
|
+
{ op: 'C', c1x: right - trx + trx * k, c1y: top, c2x: right, c2y: top + try_ - try_ * k, x: right, y: top + try_ },
|
|
211
|
+
{ op: 'L', x: right, y: bottom - bry },
|
|
212
|
+
{ op: 'C', c1x: right, c1y: bottom - bry + bry * k, c2x: right - brx + brx * k, c2y: bottom, x: right - brx, y: bottom },
|
|
213
|
+
{ op: 'L', x: left + blx, y: bottom },
|
|
214
|
+
{ op: 'C', c1x: left + blx - blx * k, c1y: bottom, c2x: left, c2y: bottom - bly + bly * k, x: left, y: bottom - bly },
|
|
215
|
+
{ op: 'L', x: left, y: top + tly },
|
|
216
|
+
{ op: 'C', c1x: left, c1y: top + tly - tly * k, c2x: left + tlx - tlx * k, c2y: top, x: left + tlx, y: top },
|
|
217
|
+
{ op: 'Z' }
|
|
218
|
+
);
|
|
219
|
+
}
|
|
220
|
+
getBounds(includeStrokes) {
|
|
221
|
+
var xs = [], ys = [], strokePadding = 0, line = null;
|
|
222
|
+
for (var i = 0; i < this._cmds.length; i++) {
|
|
223
|
+
var c = this._cmds[i];
|
|
224
|
+
if (c.op === 'lineStyle') line = c.line;
|
|
225
|
+
else if (includeStrokes !== false && line && line.width >= 0 && c.op !== 'M' &&
|
|
226
|
+
(c.x != null || c.op === 'Z' || c.op === 'triangles' || c.op === 'path')) {
|
|
227
|
+
// The local envelope is conservative for square caps and miter joins.
|
|
228
|
+
var extension = Math.max(line.cap === 'square' ? Math.SQRT2 : 1, line.join === 'miter' ? line.miterLimit : 1);
|
|
229
|
+
strokePadding = Math.max(strokePadding, (line.width || 1) * extension / 2);
|
|
230
|
+
}
|
|
231
|
+
if (c.x != null) xs.push(c.x);
|
|
232
|
+
if (c.y != null) ys.push(c.y);
|
|
233
|
+
if (c.cx != null) xs.push(c.cx);
|
|
234
|
+
if (c.cy != null) ys.push(c.cy);
|
|
235
|
+
if (c.c1x != null) { xs.push(c.c1x, c.c2x); ys.push(c.c1y, c.c2y); }
|
|
236
|
+
if (c.r != null) { xs.push(c.x - c.r, c.x + c.r); ys.push(c.y - c.r, c.y + c.r); }
|
|
237
|
+
if (c.bounds) { xs.push(c.bounds[0], c.bounds[2]); ys.push(c.bounds[1], c.bounds[3]); }
|
|
238
|
+
if (c.op === 'triangles') for (var ti = 0; ti < c.triangles.length; ti++) {
|
|
239
|
+
for (var pi = 0; pi < c.triangles[ti].points.length; pi++) {
|
|
240
|
+
xs.push(c.triangles[ti].points[pi][0]); ys.push(c.triangles[ti].points[pi][1]);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
if (!xs.length || !ys.length) return new Rectangle();
|
|
245
|
+
var x0 = Math.min.apply(Math, xs), y0 = Math.min.apply(Math, ys);
|
|
246
|
+
return new Rectangle(x0 - strokePadding, y0 - strokePadding,
|
|
247
|
+
Math.max.apply(Math, xs) - x0 + strokePadding * 2,
|
|
248
|
+
Math.max.apply(Math, ys) - y0 + strokePadding * 2);
|
|
249
|
+
}
|
|
250
|
+
containsPoint(x, y, matrix, fillsOnly) {
|
|
251
|
+
if (typeof Path2D === 'undefined' || typeof Path2D.prototype?.moveTo !== 'function' ||
|
|
252
|
+
typeof document === 'undefined') return false;
|
|
253
|
+
var ctx = Graphics._hitCtx;
|
|
254
|
+
if (!ctx) {
|
|
255
|
+
var canvas = document.createElement('canvas'); canvas.width = canvas.height = 1;
|
|
256
|
+
ctx = Graphics._hitCtx = canvas.getContext('2d');
|
|
257
|
+
}
|
|
258
|
+
if (!ctx) return false;
|
|
259
|
+
var path = new Path2D(), found = false, filling = false;
|
|
260
|
+
var fillRule = 'nonzero', strokes = graphicsStrokeRuns();
|
|
261
|
+
function hitStroke(run) {
|
|
262
|
+
if (fillsOnly || typeof ctx.isPointInStroke !== 'function') return false;
|
|
263
|
+
var plan = graphicsStrokePlan(run.line, matrix);
|
|
264
|
+
var point = plan.control ? plan.control(x, y) : [x, y];
|
|
265
|
+
applyGraphicsLineStyle(ctx, run.line, plan.width);
|
|
266
|
+
return ctx.isPointInStroke(buildGraphicsStrokePath(run, plan), point[0], point[1]);
|
|
267
|
+
}
|
|
268
|
+
function flush() {
|
|
269
|
+
return (found && filling && ctx.isPointInPath(path, x, y, fillRule)) || strokes.flush(hitStroke);
|
|
270
|
+
}
|
|
271
|
+
for (var i = 0; i < this._cmds.length; i++) {
|
|
272
|
+
var c = this._cmds[i];
|
|
273
|
+
if (c.op === 'winding' || c.op === 'fill' || c.op === 'endFill') {
|
|
274
|
+
if (flush()) return true;
|
|
275
|
+
path = new Path2D(); found = false;
|
|
276
|
+
if (c.op === 'winding') fillRule = c.rule || 'nonzero';
|
|
277
|
+
else filling = c.op === 'fill';
|
|
278
|
+
} else if (c.op === 'lineStyle') {
|
|
279
|
+
strokes.setLine(c.line);
|
|
280
|
+
} else if (c.op === 'path') {
|
|
281
|
+
if (flush()) return true;
|
|
282
|
+
path = new Path2D(); found = false;
|
|
283
|
+
if ((filling && ctx.isPointInPath(c.path, x, y)) || strokes.visit({ path: c.path }, hitStroke)) return true;
|
|
284
|
+
} else if (c.op === 'triangles') {
|
|
285
|
+
if (flush()) return true;
|
|
286
|
+
path = new Path2D(); found = false;
|
|
287
|
+
for (var ti = 0; ti < c.triangles.length; ti++) {
|
|
288
|
+
var triangleRun = { commands: graphicsTriangleCommands(c.triangles[ti].points) };
|
|
289
|
+
if ((filling && ctx.isPointInPath(buildGraphicsStrokePath(triangleRun, {}), x, y, fillRule)) ||
|
|
290
|
+
strokes.visit(triangleRun, hitStroke)) return true;
|
|
291
|
+
}
|
|
292
|
+
} else {
|
|
293
|
+
appendGraphicsPath(path, c);
|
|
294
|
+
strokes.append(c);
|
|
295
|
+
found = true;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
return flush();
|
|
299
|
+
}
|
|
300
|
+
_paint(ctx, maskFill) {
|
|
301
|
+
if (!ctx || !this._cmds.length) return;
|
|
302
|
+
var path = null;
|
|
303
|
+
var pathHasCommands = false;
|
|
304
|
+
var filling = false;
|
|
305
|
+
var fillStyle = null;
|
|
306
|
+
var activeFill = null;
|
|
307
|
+
var strokes = graphicsStrokeRuns();
|
|
308
|
+
var fillRule = 'nonzero';
|
|
309
|
+
function paintStroke(run) {
|
|
310
|
+
var matrix = null;
|
|
311
|
+
if ((run.line.width === 0 || run.line.pixelHinting || run.line.scaleMode !== 'normal') && typeof ctx.getTransform === 'function') {
|
|
312
|
+
var m = ctx.getTransform();
|
|
313
|
+
matrix = new Matrix(m.a, m.b, m.c, m.d, m.e, m.f);
|
|
314
|
+
}
|
|
315
|
+
var plan = graphicsStrokePlan(run.line, matrix);
|
|
316
|
+
var strokePath = buildGraphicsStrokePath(run, plan);
|
|
317
|
+
applyGraphicsLineStyle(ctx, run.line, plan.width);
|
|
318
|
+
ctx.strokeStyle = run.line.color;
|
|
319
|
+
if (plan.pen && typeof ctx.getTransform === 'function') {
|
|
320
|
+
var pen = plan.pen;
|
|
321
|
+
ctx.save();
|
|
322
|
+
ctx.setTransform(pen.a, pen.b, pen.c, pen.d, pen.tx, pen.ty);
|
|
323
|
+
ctx.stroke(strokePath);
|
|
324
|
+
ctx.restore();
|
|
325
|
+
} else ctx.stroke(strokePath);
|
|
326
|
+
}
|
|
327
|
+
function paintFill(path, rule) {
|
|
328
|
+
ctx.fillStyle = fillStyle;
|
|
329
|
+
if (activeFill?.bitmap) {
|
|
330
|
+
var smoothing = ctx.imageSmoothingEnabled;
|
|
331
|
+
ctx.imageSmoothingEnabled = activeFill.smooth;
|
|
332
|
+
try { ctx.fill(path, rule); }
|
|
333
|
+
finally { ctx.imageSmoothingEnabled = smoothing; }
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
var m = activeFill?.gradientType && activeFill.matrix;
|
|
337
|
+
if (m && typeof path.addPath === 'function' && typeof ctx.transform === 'function') {
|
|
338
|
+
var determinant = m.a * m.d - m.b * m.c;
|
|
339
|
+
if (determinant && Number.isFinite(determinant)) {
|
|
340
|
+
var a = m.d / determinant, b = -m.b / determinant;
|
|
341
|
+
var c = -m.c / determinant, d = m.a / determinant;
|
|
342
|
+
var e = -a * m.tx - c * m.ty, f = -b * m.tx - d * m.ty;
|
|
343
|
+
if ([a, b, c, d, e, f].every(Number.isFinite)) {
|
|
344
|
+
// Canvas uses the current transform when painting a gradient.
|
|
345
|
+
var gradientPath = new Path2D();
|
|
346
|
+
gradientPath.addPath(path, { a: a, b: b, c: c, d: d, e: e, f: f });
|
|
347
|
+
ctx.save();
|
|
348
|
+
try {
|
|
349
|
+
ctx.transform(m.a, m.b, m.c, m.d, m.tx, m.ty);
|
|
350
|
+
ctx.fill(gradientPath, rule);
|
|
351
|
+
} finally { ctx.restore(); }
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
ctx.fill(path, rule);
|
|
357
|
+
}
|
|
358
|
+
function flush() {
|
|
359
|
+
if (!path) return;
|
|
360
|
+
if (pathHasCommands && filling && fillStyle) paintFill(path, fillRule);
|
|
361
|
+
strokes.flush(paintStroke);
|
|
362
|
+
path = null;
|
|
363
|
+
pathHasCommands = false;
|
|
364
|
+
}
|
|
365
|
+
function paintTexturedTriangle(triangle, img) {
|
|
366
|
+
var p = triangle.points, uv = triangle.uvs;
|
|
367
|
+
var w = img.naturalWidth || img.videoWidth || img.width || 0;
|
|
368
|
+
var h = img.naturalHeight || img.videoHeight || img.height || 0;
|
|
369
|
+
if (!w || !h) return false;
|
|
370
|
+
var source = uv.map(function (value) { return [value.u * w, value.v * h]; });
|
|
371
|
+
var det = (source[1][0] - source[0][0]) * (source[2][1] - source[0][1]) - (source[2][0] - source[0][0]) * (source[1][1] - source[0][1]);
|
|
372
|
+
if (Math.abs(det) < 1e-10) return false;
|
|
373
|
+
var x1 = p[1][0] - p[0][0], y1 = p[1][1] - p[0][1];
|
|
374
|
+
var x2 = p[2][0] - p[0][0], y2 = p[2][1] - p[0][1];
|
|
375
|
+
var a = (x1 * (source[2][1] - source[0][1]) - x2 * (source[1][1] - source[0][1])) / det;
|
|
376
|
+
var c = (x2 * (source[1][0] - source[0][0]) - x1 * (source[2][0] - source[0][0])) / det;
|
|
377
|
+
var b = (y1 * (source[2][1] - source[0][1]) - y2 * (source[1][1] - source[0][1])) / det;
|
|
378
|
+
var d = (y2 * (source[1][0] - source[0][0]) - y1 * (source[2][0] - source[0][0])) / det;
|
|
379
|
+
var e = p[0][0] - a * source[0][0] - c * source[0][1];
|
|
380
|
+
var f = p[0][1] - b * source[0][0] - d * source[0][1];
|
|
381
|
+
try {
|
|
382
|
+
ctx.save();
|
|
383
|
+
var clip = new Path2D(); clip.moveTo(p[0][0], p[0][1]); clip.lineTo(p[1][0], p[1][1]); clip.lineTo(p[2][0], p[2][1]); clip.closePath();
|
|
384
|
+
ctx.clip(clip);
|
|
385
|
+
ctx.transform(a, b, c, d, e, f);
|
|
386
|
+
ctx.imageSmoothingEnabled = activeFill.smooth;
|
|
387
|
+
ctx.drawImage(img, 0, 0);
|
|
388
|
+
ctx.restore();
|
|
389
|
+
return true;
|
|
390
|
+
} catch (_) { try { ctx.restore(); } catch (_) {} return false; }
|
|
391
|
+
}
|
|
392
|
+
function lerpPoint(a, b, t) { return [a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t]; }
|
|
393
|
+
function perspectiveUv(vertices, weights) {
|
|
394
|
+
var denominator = 0, u = 0, v = 0;
|
|
395
|
+
for (var k = 0; k < 3; k++) {
|
|
396
|
+
var q = weights[k] / vertices[k].t;
|
|
397
|
+
denominator += q; u += q * vertices[k].u; v += q * vertices[k].v;
|
|
398
|
+
}
|
|
399
|
+
return { u: u / denominator, v: v / denominator, t: 1 };
|
|
400
|
+
}
|
|
401
|
+
function paintTriangle(triangle, img) {
|
|
402
|
+
var p = triangle.points, uv = triangle.uvs;
|
|
403
|
+
var minT = Math.min(uv[0].t, uv[1].t, uv[2].t), maxT = Math.max(uv[0].t, uv[1].t, uv[2].t);
|
|
404
|
+
var subdivisions = maxT / minT > 1.05 ? Math.min(12, Math.max(2, Math.ceil(Math.log(maxT / minT) / Math.log(1.2)))) : 1;
|
|
405
|
+
if (subdivisions === 1) { paintTexturedTriangle(triangle, img); return; }
|
|
406
|
+
var grid = [];
|
|
407
|
+
for (var row = 0; row <= subdivisions; row++) {
|
|
408
|
+
grid[row] = [];
|
|
409
|
+
for (var col = 0; col <= row; col++) {
|
|
410
|
+
var wa = 1 - row / subdivisions, wb = (row - col) / subdivisions, wc = col / subdivisions;
|
|
411
|
+
var weights = [wa, wb, wc];
|
|
412
|
+
grid[row][col] = {
|
|
413
|
+
p: [wa * p[0][0] + wb * p[1][0] + wc * p[2][0], wa * p[0][1] + wb * p[1][1] + wc * p[2][1]],
|
|
414
|
+
uv: perspectiveUv(uv, weights), weights: weights,
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
for (var r = 0; r < subdivisions; r++) for (var c = 0; c <= r; c++) {
|
|
419
|
+
var a = grid[r][c], b = grid[r + 1][c], d = grid[r + 1][c + 1];
|
|
420
|
+
paintTexturedTriangle({ points: [a.p, b.p, d.p], uvs: [a.uv, b.uv, d.uv] }, img);
|
|
421
|
+
if (c < r) {
|
|
422
|
+
var e = grid[r + 1][c + 1], f = grid[r][c + 1];
|
|
423
|
+
paintTexturedTriangle({ points: [a.p, e.p, f.p], uvs: [a.uv, e.uv, f.uv] }, img);
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
for (var i = 0; i < this._cmds.length; i++) {
|
|
428
|
+
var c = this._cmds[i];
|
|
429
|
+
if (c.op === 'winding') {
|
|
430
|
+
flush();
|
|
431
|
+
fillRule = c.rule || 'nonzero';
|
|
432
|
+
} else if (c.op === 'fill') {
|
|
433
|
+
flush();
|
|
434
|
+
filling = true;
|
|
435
|
+
activeFill = maskFill ? null : c.fill || null;
|
|
436
|
+
if (maskFill) {
|
|
437
|
+
fillStyle = '#fff';
|
|
438
|
+
} else if (c.fill && c.fill.gradientType) {
|
|
439
|
+
var focal = c.fill.focalPointRatio * 819.2;
|
|
440
|
+
var grad = c.fill.gradientType === 'radial'
|
|
441
|
+
? ctx.createRadialGradient(focal, 0, 0, 0, 0, 819.2)
|
|
442
|
+
: ctx.createLinearGradient(-819.2, 0, 819.2, 0);
|
|
443
|
+
var stops = gradientStops(c.fill);
|
|
444
|
+
for (var gi = 0; gi < stops.length; gi++) {
|
|
445
|
+
try { grad.addColorStop(stops[gi].ratio, cssColor(stops[gi].color, stops[gi].alpha)); } catch (_) {}
|
|
446
|
+
}
|
|
447
|
+
fillStyle = grad;
|
|
448
|
+
} else if (c.fill && c.fill.bitmap) {
|
|
449
|
+
var bmp = c.fill.bitmap;
|
|
450
|
+
var img = bmp && (bitmapDisplayImage(bmp.bitmapData || bmp) || bmp);
|
|
451
|
+
if (img && typeof document !== 'undefined') {
|
|
452
|
+
try {
|
|
453
|
+
fillStyle = ctx.createPattern(img, c.fill.repeat ? 'repeat' : 'no-repeat') || '#888';
|
|
454
|
+
if (fillStyle && c.fill.matrix && typeof fillStyle.setTransform === 'function') {
|
|
455
|
+
var bm = c.fill.matrix;
|
|
456
|
+
fillStyle.setTransform(typeof DOMMatrix !== 'undefined'
|
|
457
|
+
? new DOMMatrix([bm.a, bm.b, bm.c, bm.d, bm.tx, bm.ty])
|
|
458
|
+
: { a: bm.a, b: bm.b, c: bm.c, d: bm.d, e: bm.tx, f: bm.ty });
|
|
459
|
+
}
|
|
460
|
+
} catch (_) { fillStyle = '#888'; }
|
|
461
|
+
} else fillStyle = '#888';
|
|
462
|
+
} else {
|
|
463
|
+
fillStyle = (c.fill && c.fill.color) || '#000';
|
|
464
|
+
}
|
|
465
|
+
path = new Path2D();
|
|
466
|
+
} else if (c.op === 'endFill') {
|
|
467
|
+
flush();
|
|
468
|
+
filling = false;
|
|
469
|
+
fillStyle = null;
|
|
470
|
+
activeFill = null;
|
|
471
|
+
} else if (c.op === 'lineStyle') {
|
|
472
|
+
if (!maskFill) strokes.setLine(c.line);
|
|
473
|
+
} else if (c.op === 'path') {
|
|
474
|
+
flush();
|
|
475
|
+
if (filling && fillStyle) paintFill(c.path, 'nonzero');
|
|
476
|
+
strokes.visit({ path: c.path }, paintStroke);
|
|
477
|
+
} else if (c.op === 'triangles') {
|
|
478
|
+
flush();
|
|
479
|
+
var bitmap = activeFill && activeFill.bitmap;
|
|
480
|
+
var image = bitmap && (bitmapDisplayImage(bitmap.bitmapData || bitmap) || bitmap);
|
|
481
|
+
for (var ti = 0; ti < c.triangles.length; ti++) {
|
|
482
|
+
var triangle = c.triangles[ti];
|
|
483
|
+
var triangleRun = { commands: graphicsTriangleCommands(triangle.points) };
|
|
484
|
+
if (filling && triangle.uvs && image && typeof ctx.clip === 'function' && typeof ctx.transform === 'function' && typeof ctx.drawImage === 'function') {
|
|
485
|
+
paintTriangle(triangle, image);
|
|
486
|
+
} else if (filling && fillStyle) {
|
|
487
|
+
paintFill(buildGraphicsStrokePath(triangleRun, {}), fillRule);
|
|
488
|
+
}
|
|
489
|
+
strokes.visit(triangleRun, paintStroke);
|
|
490
|
+
}
|
|
491
|
+
} else {
|
|
492
|
+
if (!path) path = new Path2D();
|
|
493
|
+
appendGraphicsPath(path, c);
|
|
494
|
+
strokes.append(c);
|
|
495
|
+
pathHasCommands = true;
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
flush();
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
export function appendGraphicsPath(path, command, anchor, control, offset) {
|
|
504
|
+
var op = command.op;
|
|
505
|
+
if (op === 'Z') { path.closePath(); return; }
|
|
506
|
+
if (op === 'circle') {
|
|
507
|
+
path.moveTo(command.x + command.r, command.y);
|
|
508
|
+
path.arc(command.x, command.y, command.r, 0, Math.PI * 2);
|
|
509
|
+
path.closePath();
|
|
510
|
+
return;
|
|
511
|
+
}
|
|
512
|
+
var p = anchor ? anchor(command.x, command.y, offset) : [command.x, command.y];
|
|
513
|
+
if (op === 'M') path.moveTo(p[0], p[1]);
|
|
514
|
+
else if (op === 'L') path.lineTo(p[0], p[1]);
|
|
515
|
+
else if (op === 'Q') {
|
|
516
|
+
var q = control ? control(command.cx, command.cy) : [command.cx, command.cy];
|
|
517
|
+
path.quadraticCurveTo(q[0], q[1], p[0], p[1]);
|
|
518
|
+
} else if (op === 'C') {
|
|
519
|
+
var c1 = control ? control(command.c1x, command.c1y) : [command.c1x, command.c1y];
|
|
520
|
+
var c2 = control ? control(command.c2x, command.c2y) : [command.c2x, command.c2y];
|
|
521
|
+
path.bezierCurveTo(c1[0], c1[1], c2[0], c2[1], p[0], p[1]);
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
// Move the centerline into pen coordinates while keeping its stage position fixed.
|
|
526
|
+
export function graphicsStrokePlan(line, matrix) {
|
|
527
|
+
var width = line.width === 0 ? 1 : line.width;
|
|
528
|
+
var fixed = line.width === 0 || line.scaleMode === 'none';
|
|
529
|
+
if (!fixed && !line.pixelHinting && line.scaleMode === 'normal') return { width: width };
|
|
530
|
+
matrix = matrix || new Matrix();
|
|
531
|
+
var det = matrix.a * matrix.d - matrix.b * matrix.c;
|
|
532
|
+
var invertible = isFinite(det) && Math.abs(det) >= 1e-12;
|
|
533
|
+
var pen, pathMatrix;
|
|
534
|
+
if (fixed) {
|
|
535
|
+
pen = new Matrix();
|
|
536
|
+
pathMatrix = matrix;
|
|
537
|
+
} else if (invertible && (line.scaleMode === 'horizontal' || line.scaleMode === 'vertical')) {
|
|
538
|
+
var sx = line.scaleMode === 'horizontal' ? Math.hypot(matrix.a, matrix.b) : 1;
|
|
539
|
+
var sy = line.scaleMode === 'vertical' ? Math.hypot(matrix.c, matrix.d) : 1;
|
|
540
|
+
pen = new Matrix(matrix.a / sx, matrix.b / sx, matrix.c / sy, matrix.d / sy, matrix.tx, matrix.ty);
|
|
541
|
+
pathMatrix = new Matrix(sx, 0, 0, sy);
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
var effectivePen = pen || matrix;
|
|
545
|
+
var hinting = line.pixelHinting && invertible;
|
|
546
|
+
var oddWidth = false;
|
|
547
|
+
if (hinting) {
|
|
548
|
+
var scaleX = Math.hypot(effectivePen.a, effectivePen.b);
|
|
549
|
+
var scaleY = Math.hypot(effectivePen.c, effectivePen.d);
|
|
550
|
+
var dot = effectivePen.a * effectivePen.c + effectivePen.b * effectivePen.d;
|
|
551
|
+
if (Math.abs(scaleX - scaleY) <= scaleX * 1e-8 && Math.abs(dot) <= scaleX * scaleY * 1e-8) {
|
|
552
|
+
var outputWidth = Math.max(1, Math.round(width * scaleX));
|
|
553
|
+
width = outputWidth / scaleX;
|
|
554
|
+
oddWidth = outputWidth % 2 === 1;
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
function toPen(x, y) {
|
|
559
|
+
return [pathMatrix.a * x + pathMatrix.c * y + pathMatrix.tx,
|
|
560
|
+
pathMatrix.b * x + pathMatrix.d * y + pathMatrix.ty];
|
|
561
|
+
}
|
|
562
|
+
function snap(value, half) {
|
|
563
|
+
return half ? Math.floor(value) + 0.5 : Math.round(value);
|
|
564
|
+
}
|
|
565
|
+
function hint(x, y, axes) {
|
|
566
|
+
var px = snap(matrix.a * x + matrix.c * y + matrix.tx, axes & 1) - matrix.tx;
|
|
567
|
+
var py = snap(matrix.b * x + matrix.d * y + matrix.ty, axes & 2) - matrix.ty;
|
|
568
|
+
var lx = (matrix.d * px - matrix.c * py) / det, ly = (-matrix.b * px + matrix.a * py) / det;
|
|
569
|
+
return pathMatrix ? toPen(lx, ly) : [lx, ly];
|
|
570
|
+
}
|
|
571
|
+
function hintCircle(command) {
|
|
572
|
+
// Transform the analytic arc by its snapped device-space bounds. An
|
|
573
|
+
// ellipse stays analytic, including when the parent matrix has shear.
|
|
574
|
+
var x = matrix.a * command.x + matrix.c * command.y + matrix.tx;
|
|
575
|
+
var y = matrix.b * command.x + matrix.d * command.y + matrix.ty;
|
|
576
|
+
var rx = command.r * Math.hypot(matrix.a, matrix.c);
|
|
577
|
+
var ry = command.r * Math.hypot(matrix.b, matrix.d);
|
|
578
|
+
var left = snap(x - rx, oddWidth), right = snap(x + rx, oddWidth);
|
|
579
|
+
var top = snap(y - ry, oddWidth), bottom = snap(y + ry, oddWidth);
|
|
580
|
+
if (!(right > left && bottom > top)) return pathMatrix;
|
|
581
|
+
var sx = (right - left) / (2 * rx), sy = (bottom - top) / (2 * ry);
|
|
582
|
+
var adjusted = new Matrix(matrix.a, matrix.b, matrix.c, matrix.d, matrix.tx, matrix.ty);
|
|
583
|
+
adjusted.concat(new Matrix(sx, 0, 0, sy, (left + right) / 2 - x * sx, (top + bottom) / 2 - y * sy));
|
|
584
|
+
return adjusted.concat(new Matrix(effectivePen.a, effectivePen.b, effectivePen.c, effectivePen.d,
|
|
585
|
+
effectivePen.tx, effectivePen.ty).invert());
|
|
586
|
+
}
|
|
587
|
+
return {
|
|
588
|
+
width: width, pen: pen, pathMatrix: pathMatrix,
|
|
589
|
+
control: pathMatrix ? toPen : null,
|
|
590
|
+
anchor: hinting ? hint : pathMatrix ? toPen : null,
|
|
591
|
+
circle: hinting ? hintCircle : null,
|
|
592
|
+
oddWidthMatrix: oddWidth ? matrix : null,
|
|
593
|
+
};
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
export function graphicsStrokeAnchorOffsets(commands, matrix) {
|
|
597
|
+
var offsets = new Map(), start, previous;
|
|
598
|
+
for (var command of commands) {
|
|
599
|
+
var next = command.op === 'Z' ? start : command.op === 'circle' ? null : command;
|
|
600
|
+
if (previous && next && (command.op === 'L' || command.op === 'Z')) {
|
|
601
|
+
var dx = next.x - previous.x, dy = next.y - previous.y;
|
|
602
|
+
var x = Math.abs(matrix.a * dx + matrix.c * dy);
|
|
603
|
+
var y = Math.abs(matrix.b * dx + matrix.d * dy);
|
|
604
|
+
// Bit 0 shifts stage x for vertical edges, bit 1 shifts stage y for horizontal edges.
|
|
605
|
+
var axes = x < 1e-8 && y > 1e-8 ? 1 : y < 1e-8 && x > 1e-8 ? 2 : 0;
|
|
606
|
+
if (axes) {
|
|
607
|
+
offsets.set(previous, (offsets.get(previous) || 0) | axes);
|
|
608
|
+
offsets.set(next, (offsets.get(next) || 0) | axes);
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
if (command.op === 'M' || command.op === 'circle') start = next;
|
|
612
|
+
previous = next;
|
|
613
|
+
}
|
|
614
|
+
return offsets;
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
export function buildGraphicsStrokePath(run, plan) {
|
|
618
|
+
if (run.path && !plan.pathMatrix) return run.path;
|
|
619
|
+
var path = new Path2D();
|
|
620
|
+
function addTransformed(source, m) {
|
|
621
|
+
path.addPath(source, { a: m.a, b: m.b, c: m.c, d: m.d, e: m.tx, f: m.ty });
|
|
622
|
+
}
|
|
623
|
+
if (run.path) addTransformed(run.path, plan.pathMatrix);
|
|
624
|
+
else {
|
|
625
|
+
var offsets = plan.oddWidthMatrix && graphicsStrokeAnchorOffsets(run.commands, plan.oddWidthMatrix);
|
|
626
|
+
for (var i = 0; i < run.commands.length; i++) {
|
|
627
|
+
var command = run.commands[i];
|
|
628
|
+
var circleMatrix = command.op === 'circle' && (plan.circle ? plan.circle(command) : plan.pathMatrix);
|
|
629
|
+
if (circleMatrix) {
|
|
630
|
+
var circle = new Path2D();
|
|
631
|
+
appendGraphicsPath(circle, command);
|
|
632
|
+
addTransformed(circle, circleMatrix);
|
|
633
|
+
} else appendGraphicsPath(path, command, plan.anchor, plan.control, offsets && offsets.get(command));
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
return path;
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
export function graphicsTriangleCommands(points) {
|
|
640
|
+
return [
|
|
641
|
+
{ op: 'M', x: points[0][0], y: points[0][1] },
|
|
642
|
+
{ op: 'L', x: points[1][0], y: points[1][1] },
|
|
643
|
+
{ op: 'L', x: points[2][0], y: points[2][1] },
|
|
644
|
+
{ op: 'Z' },
|
|
645
|
+
];
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
export function applyGraphicsLineStyle(ctx, line, width) {
|
|
649
|
+
ctx.lineWidth = width;
|
|
650
|
+
ctx.lineCap = line.cap;
|
|
651
|
+
ctx.lineJoin = line.join;
|
|
652
|
+
ctx.miterLimit = line.miterLimit;
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
// Split stroke paths at lineStyle changes without splitting the fill path.
|
|
656
|
+
export function graphicsStrokeRuns() {
|
|
657
|
+
var line = null, commands = null, runs = [], startsAtSubpath = true;
|
|
658
|
+
var x = 0, y = 0, startX = 0, startY = 0;
|
|
659
|
+
return {
|
|
660
|
+
setLine: function (nextLine) {
|
|
661
|
+
line = nextLine && nextLine.width >= 0 ? nextLine : null;
|
|
662
|
+
commands = null;
|
|
663
|
+
},
|
|
664
|
+
append: function (command) {
|
|
665
|
+
if (line) {
|
|
666
|
+
if (!commands) {
|
|
667
|
+
commands = [];
|
|
668
|
+
if (command.op !== 'M' && command.op !== 'circle') commands.push({ op: 'M', x: x, y: y });
|
|
669
|
+
startsAtSubpath = x === startX && y === startY;
|
|
670
|
+
runs.push({ commands: commands, line: line });
|
|
671
|
+
}
|
|
672
|
+
if (command.op === 'M' || command.op === 'circle') startsAtSubpath = true;
|
|
673
|
+
if (command.op === 'Z' && !startsAtSubpath) commands.push({ op: 'L', x: startX, y: startY });
|
|
674
|
+
else commands.push(command);
|
|
675
|
+
}
|
|
676
|
+
if (command.op === 'M') { startX = command.x; startY = command.y; }
|
|
677
|
+
if (command.op === 'Z') { x = startX; y = startY; }
|
|
678
|
+
else if (command.op === 'circle') { startX = x = command.x + command.r; startY = y = command.y; }
|
|
679
|
+
else if (command.x != null) { x = command.x; y = command.y; }
|
|
680
|
+
},
|
|
681
|
+
flush: function (visit) {
|
|
682
|
+
var hit = runs.some(visit);
|
|
683
|
+
runs = [];
|
|
684
|
+
commands = null;
|
|
685
|
+
return hit;
|
|
686
|
+
},
|
|
687
|
+
visit: function (run, visit) {
|
|
688
|
+
if (!line) return false;
|
|
689
|
+
run.line = line;
|
|
690
|
+
return visit(run);
|
|
691
|
+
},
|
|
692
|
+
};
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
|
|
696
|
+
export class Sprite extends DisplayObjectContainer {
|
|
697
|
+
constructor() {
|
|
698
|
+
super();
|
|
699
|
+
this.graphics = new Graphics();
|
|
700
|
+
this._dropTarget = null;
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
startDrag(lockCenter, bounds) {
|
|
704
|
+
startSpriteMouseDrag(this, lockCenter, bounds);
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
stopDrag() {
|
|
708
|
+
stopSpriteMouseDrag(this);
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
startTouchDrag(id, lockCenter, bounds) {
|
|
712
|
+
startSpriteTouchDrag(this, id, lockCenter, bounds);
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
stopTouchDrag(id) {
|
|
716
|
+
stopSpriteTouchDrag(this, id);
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
Sprite.prototype[spriteMark] = true;
|
|
720
|
+
Object.defineProperties(Sprite.prototype, {
|
|
721
|
+
buttonMode: cursorFlag('_buttonMode', false),
|
|
722
|
+
useHandCursor: cursorFlag('_useHandCursor', true),
|
|
723
|
+
dropTarget: {
|
|
724
|
+
configurable: true,
|
|
725
|
+
enumerable: true,
|
|
726
|
+
get: function () { return this._dropTarget == null ? null : this._dropTarget; },
|
|
727
|
+
},
|
|
728
|
+
});
|
|
729
|
+
|
|
730
|
+
export class Shape extends DisplayObject {
|
|
731
|
+
constructor() {
|
|
732
|
+
super();
|
|
733
|
+
this.graphics = new Graphics();
|
|
734
|
+
}
|
|
735
|
+
}
|