1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.segmentBounce = exports.calcClosestPtOnSegment = exports.parsePaths = exports.drawPolygonMaskPath = exports.drawPolygonMask = void 0;
|
4 | const Utils_1 = require("../../Utils");
|
5 | function drawPolygonMask(context, rawData, stroke) {
|
6 | const color = (0, Utils_1.colorToRgb)(stroke.color);
|
7 | if (!color) {
|
8 | return;
|
9 | }
|
10 | context.beginPath();
|
11 | context.moveTo(rawData[0].x, rawData[0].y);
|
12 | for (const item of rawData) {
|
13 | context.lineTo(item.x, item.y);
|
14 | }
|
15 | context.closePath();
|
16 | context.strokeStyle = (0, Utils_1.getStyleFromRgb)(color);
|
17 | context.lineWidth = stroke.width;
|
18 | context.stroke();
|
19 | }
|
20 | exports.drawPolygonMask = drawPolygonMask;
|
21 | function drawPolygonMaskPath(context, path, stroke, position) {
|
22 | context.translate(position.x, position.y);
|
23 | const color = (0, Utils_1.colorToRgb)(stroke.color);
|
24 | if (!color) {
|
25 | return;
|
26 | }
|
27 | context.strokeStyle = (0, Utils_1.getStyleFromRgb)(color, stroke.opacity);
|
28 | context.lineWidth = stroke.width;
|
29 | context.stroke(path);
|
30 | }
|
31 | exports.drawPolygonMaskPath = drawPolygonMaskPath;
|
32 | function parsePaths(paths, scale, offset) {
|
33 | var _a;
|
34 | const res = [];
|
35 | for (const path of paths) {
|
36 | const segments = path.element.pathSegList;
|
37 | const len = (_a = segments === null || segments === void 0 ? void 0 : segments.numberOfItems) !== null && _a !== void 0 ? _a : 0;
|
38 | const p = {
|
39 | x: 0,
|
40 | y: 0,
|
41 | };
|
42 | for (let i = 0; i < len; i++) {
|
43 | const segment = segments === null || segments === void 0 ? void 0 : segments.getItem(i);
|
44 | const svgPathSeg = window.SVGPathSeg;
|
45 | switch (segment === null || segment === void 0 ? void 0 : segment.pathSegType) {
|
46 | case svgPathSeg.PATHSEG_MOVETO_ABS:
|
47 | case svgPathSeg.PATHSEG_LINETO_ABS:
|
48 | case svgPathSeg.PATHSEG_CURVETO_CUBIC_ABS:
|
49 | case svgPathSeg.PATHSEG_CURVETO_QUADRATIC_ABS:
|
50 | case svgPathSeg.PATHSEG_ARC_ABS:
|
51 | case svgPathSeg.PATHSEG_CURVETO_CUBIC_SMOOTH_ABS:
|
52 | case svgPathSeg.PATHSEG_CURVETO_QUADRATIC_SMOOTH_ABS: {
|
53 | const absSeg = segment;
|
54 | p.x = absSeg.x;
|
55 | p.y = absSeg.y;
|
56 | break;
|
57 | }
|
58 | case svgPathSeg.PATHSEG_LINETO_HORIZONTAL_ABS:
|
59 | p.x = segment.x;
|
60 | break;
|
61 | case svgPathSeg.PATHSEG_LINETO_VERTICAL_ABS:
|
62 | p.y = segment.y;
|
63 | break;
|
64 | case svgPathSeg.PATHSEG_LINETO_REL:
|
65 | case svgPathSeg.PATHSEG_MOVETO_REL:
|
66 | case svgPathSeg.PATHSEG_CURVETO_CUBIC_REL:
|
67 | case svgPathSeg.PATHSEG_CURVETO_QUADRATIC_REL:
|
68 | case svgPathSeg.PATHSEG_ARC_REL:
|
69 | case svgPathSeg.PATHSEG_CURVETO_CUBIC_SMOOTH_REL:
|
70 | case svgPathSeg.PATHSEG_CURVETO_QUADRATIC_SMOOTH_REL: {
|
71 | const relSeg = segment;
|
72 | p.x += relSeg.x;
|
73 | p.y += relSeg.y;
|
74 | break;
|
75 | }
|
76 | case svgPathSeg.PATHSEG_LINETO_HORIZONTAL_REL:
|
77 | p.x += segment.x;
|
78 | break;
|
79 | case svgPathSeg.PATHSEG_LINETO_VERTICAL_REL:
|
80 | p.y += segment.y;
|
81 | break;
|
82 | case svgPathSeg.PATHSEG_UNKNOWN:
|
83 | case svgPathSeg.PATHSEG_CLOSEPATH:
|
84 | continue;
|
85 | }
|
86 | res.push({
|
87 | x: p.x * scale + offset.x,
|
88 | y: p.y * scale + offset.y,
|
89 | });
|
90 | }
|
91 | }
|
92 | return res;
|
93 | }
|
94 | exports.parsePaths = parsePaths;
|
95 | function calcClosestPtOnSegment(s1, s2, pos) {
|
96 | const { dx, dy } = (0, Utils_1.getDistances)(pos, s1);
|
97 | const { dx: dxx, dy: dyy } = (0, Utils_1.getDistances)(s2, s1);
|
98 | const t = (dx * dxx + dy * dyy) / (dxx ** 2 + dyy ** 2);
|
99 | let x = s1.x + dxx * t;
|
100 | let y = s1.y + dyy * t;
|
101 | if (t < 0) {
|
102 | x = s1.x;
|
103 | y = s1.y;
|
104 | }
|
105 | else if (t > 1) {
|
106 | x = s2.x;
|
107 | y = s2.y;
|
108 | }
|
109 | return { x: x, y: y, isOnSegment: t >= 0 && t <= 1 };
|
110 | }
|
111 | exports.calcClosestPtOnSegment = calcClosestPtOnSegment;
|
112 | function segmentBounce(start, stop, velocity) {
|
113 | const { dx, dy } = (0, Utils_1.getDistances)(start, stop);
|
114 | const wallAngle = Math.atan2(dy, dx);
|
115 | const wallNormalX = Math.sin(wallAngle);
|
116 | const wallNormalY = -Math.cos(wallAngle);
|
117 | const d = 2 * (velocity.x * wallNormalX + velocity.y * wallNormalY);
|
118 | velocity.x -= d * wallNormalX;
|
119 | velocity.y -= d * wallNormalY;
|
120 | }
|
121 | exports.segmentBounce = segmentBounce;
|