@open-pencil/scene-graph 0.17.0-beta.1 → 0.17.0-beta.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/dist/arrow-caps.d.ts.map +1 -1
- package/dist/arrow-caps.js +2 -5
- package/dist/arrow-caps.js.map +1 -1
- package/dist/behaviours/index.d.ts +2 -2
- package/dist/behaviours/index.js +2 -2
- package/dist/behaviours/kinds.d.ts +11 -5
- package/dist/behaviours/kinds.d.ts.map +1 -1
- package/dist/behaviours/kinds.js +76 -75
- package/dist/behaviours/kinds.js.map +1 -1
- package/dist/behaviours/layers.d.ts.map +1 -1
- package/dist/behaviours/layers.js +3 -2
- package/dist/behaviours/layers.js.map +1 -1
- package/dist/behaviours/model.d.ts +7 -4
- package/dist/behaviours/model.d.ts.map +1 -1
- package/dist/behaviours/model.js +16 -9
- package/dist/behaviours/model.js.map +1 -1
- package/dist/behaviours/schema.d.ts +118 -4
- package/dist/behaviours/schema.d.ts.map +1 -1
- package/dist/behaviours/schema.js +18 -3
- package/dist/behaviours/schema.js.map +1 -1
- package/dist/behaviours/spec.d.ts.map +1 -1
- package/dist/behaviours/spec.js +3 -2
- package/dist/behaviours/spec.js.map +1 -1
- package/dist/bindings.js +9 -1
- package/dist/bindings.js.map +1 -1
- package/dist/checkpoint.d.ts +31 -2
- package/dist/checkpoint.d.ts.map +1 -1
- package/dist/checkpoint.js +176 -5
- package/dist/checkpoint.js.map +1 -1
- package/dist/components/index.d.ts +3 -3
- package/dist/components/index.js +3 -3
- package/dist/components/properties.d.ts +13 -1
- package/dist/components/properties.d.ts.map +1 -1
- package/dist/components/properties.js +58 -3
- package/dist/components/properties.js.map +1 -1
- package/dist/components/variants.d.ts +16 -10
- package/dist/components/variants.d.ts.map +1 -1
- package/dist/components/variants.js +39 -13
- package/dist/components/variants.js.map +1 -1
- package/dist/connectors/hit.d.ts.map +1 -1
- package/dist/connectors/hit.js +1 -7
- package/dist/connectors/hit.js.map +1 -1
- package/dist/connectors/pieces.d.ts.map +1 -1
- package/dist/connectors/pieces.js +2 -5
- package/dist/connectors/pieces.js.map +1 -1
- package/dist/copy.d.ts.map +1 -1
- package/dist/copy.js +9 -15
- package/dist/copy.js.map +1 -1
- package/dist/css/blend.d.ts +11 -0
- package/dist/css/blend.d.ts.map +1 -0
- package/dist/css/blend.js +30 -0
- package/dist/css/blend.js.map +1 -0
- package/dist/css/index.d.ts +2 -1
- package/dist/css/index.js +2 -1
- package/dist/cubic.d.ts +7 -0
- package/dist/cubic.d.ts.map +1 -0
- package/dist/cubic.js +17 -0
- package/dist/cubic.js.map +1 -0
- package/dist/export-format.d.ts +14 -1
- package/dist/export-format.d.ts.map +1 -1
- package/dist/export-format.js +26 -1
- package/dist/export-format.js.map +1 -1
- package/dist/fields/text.d.ts +2 -2
- package/dist/fields/text.d.ts.map +1 -1
- package/dist/fields/text.js +2 -1
- package/dist/fields/text.js.map +1 -1
- package/dist/figjam/index.d.ts +2 -2
- package/dist/figjam/index.js +2 -2
- package/dist/figjam/palette.d.ts +13 -2
- package/dist/figjam/palette.d.ts.map +1 -1
- package/dist/figjam/palette.js +30 -2
- package/dist/figjam/palette.js.map +1 -1
- package/dist/generated/index.d.ts +2 -2
- package/dist/generated/index.js +2 -2
- package/dist/generated/shape-with-text.d.ts +7 -2
- package/dist/generated/shape-with-text.d.ts.map +1 -1
- package/dist/generated/shape-with-text.js +20 -3
- package/dist/generated/shape-with-text.js.map +1 -1
- package/dist/geometry.d.ts +30 -46
- package/dist/geometry.d.ts.map +1 -1
- package/dist/geometry.js +63 -131
- package/dist/geometry.js.map +1 -1
- package/dist/graph.d.ts +1 -0
- package/dist/graph.d.ts.map +1 -1
- package/dist/graph.js +15 -3
- package/dist/graph.js.map +1 -1
- package/dist/guides.d.ts +3 -1
- package/dist/guides.d.ts.map +1 -1
- package/dist/guides.js +5 -1
- package/dist/guides.js.map +1 -1
- package/dist/hit-test.d.ts.map +1 -1
- package/dist/hit-test.js +42 -29
- package/dist/hit-test.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/instances/fields.d.ts +2 -2
- package/dist/instances/fields.d.ts.map +1 -1
- package/dist/instances/fields.js +8 -0
- package/dist/instances/fields.js.map +1 -1
- package/dist/instances/index.d.ts +1 -3
- package/dist/instances/index.d.ts.map +1 -1
- package/dist/instances/index.js +36 -30
- package/dist/instances/index.js.map +1 -1
- package/dist/instances/kept-overrides.js +204 -0
- package/dist/instances/kept-overrides.js.map +1 -0
- package/dist/instances/main-component.d.ts +3 -1
- package/dist/instances/main-component.d.ts.map +1 -1
- package/dist/instances/main-component.js +10 -2
- package/dist/instances/main-component.js.map +1 -1
- package/dist/instances/overrides.d.ts +4 -1
- package/dist/instances/overrides.d.ts.map +1 -1
- package/dist/instances/overrides.js +18 -2
- package/dist/instances/overrides.js.map +1 -1
- package/dist/instances/swap.js +288 -0
- package/dist/instances/swap.js.map +1 -0
- package/dist/instances/sync.js +84 -32
- package/dist/instances/sync.js.map +1 -1
- package/dist/node-defaults.d.ts.map +1 -1
- package/dist/node-defaults.js +1 -1
- package/dist/node-defaults.js.map +1 -1
- package/dist/outline/flatten.d.ts +15 -0
- package/dist/outline/flatten.d.ts.map +1 -0
- package/dist/outline/flatten.js +189 -0
- package/dist/outline/flatten.js.map +1 -0
- package/dist/outline/heads.js +34 -0
- package/dist/outline/heads.js.map +1 -0
- package/dist/outline/hit.d.ts +21 -0
- package/dist/outline/hit.d.ts.map +1 -0
- package/dist/outline/hit.js +276 -0
- package/dist/outline/hit.js.map +1 -0
- package/dist/outline/index.d.ts +5 -0
- package/dist/outline/index.js +5 -0
- package/dist/outline/point.d.ts +16 -0
- package/dist/outline/point.d.ts.map +1 -0
- package/dist/outline/point.js +42 -0
- package/dist/outline/point.js.map +1 -0
- package/dist/outline/shapes.d.ts +35 -0
- package/dist/outline/shapes.d.ts.map +1 -0
- package/dist/outline/shapes.js +181 -0
- package/dist/outline/shapes.js.map +1 -0
- package/dist/plugin-data/fields.d.ts +2 -1
- package/dist/plugin-data/fields.d.ts.map +1 -1
- package/dist/plugin-data/fields.js +19 -4
- package/dist/plugin-data/fields.js.map +1 -1
- package/dist/scaling/tree.js +1 -1
- package/dist/shared-styles.d.ts +14 -1
- package/dist/shared-styles.d.ts.map +1 -1
- package/dist/shared-styles.js +63 -2
- package/dist/shared-styles.js.map +1 -1
- package/dist/slots/content.d.ts.map +1 -1
- package/dist/slots/content.js +2 -0
- package/dist/slots/content.js.map +1 -1
- package/dist/slots/frames.d.ts +2 -0
- package/dist/slots/frames.d.ts.map +1 -1
- package/dist/slots/frames.js +7 -2
- package/dist/slots/frames.js.map +1 -1
- package/dist/text/index.d.ts +2 -2
- package/dist/text/index.js +3 -3
- package/dist/text/lengths.d.ts +6 -1
- package/dist/text/lengths.d.ts.map +1 -1
- package/dist/text/lengths.js +10 -1
- package/dist/text/lengths.js.map +1 -1
- package/dist/text/overflow.d.ts +2 -2
- package/dist/text/overflow.js.map +1 -1
- package/dist/text/picture.d.ts +1 -2
- package/dist/text/picture.d.ts.map +1 -1
- package/dist/text/picture.js +1 -4
- package/dist/text/picture.js.map +1 -1
- package/dist/transfer/index.js +1 -1
- package/dist/transfer/nodes.js +9 -5
- package/dist/transfer/nodes.js.map +1 -1
- package/dist/types.d.ts +27 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/variables/bindings.d.ts +28 -5
- package/dist/variables/bindings.d.ts.map +1 -1
- package/dist/variables/bindings.js +104 -14
- package/dist/variables/bindings.js.map +1 -1
- package/dist/variables/fields.d.ts +19 -1
- package/dist/variables/fields.d.ts.map +1 -1
- package/dist/variables/fields.js +36 -1
- package/dist/variables/fields.js.map +1 -1
- package/dist/variables/index.d.ts +3 -3
- package/dist/variables/index.d.ts.map +1 -1
- package/dist/variables/index.js +23 -8
- package/dist/variables/index.js.map +1 -1
- package/package.json +7 -1
- package/src/arrow-caps.ts +2 -5
- package/src/behaviours/kinds.ts +51 -34
- package/src/behaviours/layers.ts +4 -2
- package/src/behaviours/model.ts +22 -11
- package/src/behaviours/schema.ts +20 -4
- package/src/behaviours/spec.ts +8 -2
- package/src/bindings.ts +11 -2
- package/src/checkpoint.ts +251 -4
- package/src/components/properties.ts +92 -2
- package/src/components/variants.ts +51 -30
- package/src/connectors/hit.ts +1 -12
- package/src/connectors/pieces.ts +2 -5
- package/src/copy.ts +9 -15
- package/src/css/blend.ts +27 -0
- package/src/css/index.ts +1 -0
- package/src/cubic.ts +14 -0
- package/src/export-format.ts +29 -0
- package/src/fields/text.ts +3 -1
- package/src/figjam/palette.ts +29 -1
- package/src/generated/shape-with-text.ts +28 -2
- package/src/geometry.ts +78 -198
- package/src/graph.ts +27 -3
- package/src/guides.ts +5 -0
- package/src/hit-test.ts +67 -54
- package/src/instances/fields.ts +8 -0
- package/src/instances/index.ts +58 -31
- package/src/instances/kept-overrides.ts +280 -0
- package/src/instances/main-component.ts +12 -0
- package/src/instances/overrides.ts +27 -1
- package/src/instances/swap.ts +451 -0
- package/src/instances/sync.ts +141 -42
- package/src/node-defaults.ts +2 -1
- package/src/outline/flatten.ts +190 -0
- package/src/outline/heads.ts +52 -0
- package/src/outline/hit.ts +369 -0
- package/src/outline/index.ts +4 -0
- package/src/outline/point.ts +59 -0
- package/src/outline/shapes.ts +207 -0
- package/src/plugin-data/fields.ts +15 -2
- package/src/shared-styles.ts +82 -1
- package/src/slots/content.ts +2 -0
- package/src/slots/frames.ts +9 -7
- package/src/text/lengths.ts +10 -0
- package/src/text/overflow.ts +2 -2
- package/src/text/picture.ts +0 -4
- package/src/transfer/nodes.ts +7 -5
- package/src/types.ts +28 -0
- package/src/variables/bindings.ts +125 -14
- package/src/variables/fields.ts +46 -1
- package/src/variables/index.ts +35 -2
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
import Matrix from "../matrix.js";
|
|
2
|
+
import { getWorldMatrix } from "../coordinate.js";
|
|
3
|
+
import { arrowCapOverflow, isArrowCap, lineStrokeEnds } from "../arrow-caps.js";
|
|
4
|
+
import { containsByWinding, distanceToLines, distanceToSegment } from "./point.js";
|
|
5
|
+
import { hitsArrowHeads } from "./heads.js";
|
|
6
|
+
import { shapeOutline } from "./shapes.js";
|
|
7
|
+
import { compact } from "es-toolkit";
|
|
8
|
+
//#region src/outline/hit.ts
|
|
9
|
+
/** Points tested along each edge near the point for whether the filled result changes there. */
|
|
10
|
+
const EDGE_SAMPLES = 8;
|
|
11
|
+
/** How far a flattened curve may stray from the shape when nothing tighter is asked for. */
|
|
12
|
+
const DEFAULT_TOLERANCE = .25;
|
|
13
|
+
/**
|
|
14
|
+
* Whether a paint makes its layer clickable, as in Figma: a visible paint that shows something.
|
|
15
|
+
* A paint at 0% opacity, or a gradient whose stops are all transparent, does not; 1% does, and
|
|
16
|
+
* the layer's own opacity does not matter.
|
|
17
|
+
*/
|
|
18
|
+
function isHitPaint(paint) {
|
|
19
|
+
if (!paint.visible || !(paint.opacity > 0)) return false;
|
|
20
|
+
if (paint.gradientStops) return paint.gradientStops.some((stop) => stop.color.a > 0);
|
|
21
|
+
if (paint.type === "SOLID") return paint.color.a > 0;
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
function insideArea(area, point) {
|
|
25
|
+
return containsByWinding(area.contours, area.windingRule, point);
|
|
26
|
+
}
|
|
27
|
+
/** A stroke's weight; with independent borders the widest side, for layers without per-side lines. */
|
|
28
|
+
function strokeWeightOf(node, stroke) {
|
|
29
|
+
if (node.independentStrokeWeights) return Math.max(node.borderTopWeight, node.borderRightWeight, node.borderBottomWeight, node.borderLeftWeight);
|
|
30
|
+
return Number.isFinite(stroke.weight) ? stroke.weight : node.strokeWeight;
|
|
31
|
+
}
|
|
32
|
+
/** The farthest a layer's clickable strokes reach outside its box, in its own units. */
|
|
33
|
+
function strokeReach(node) {
|
|
34
|
+
let reach = 0;
|
|
35
|
+
for (const stroke of node.strokes) {
|
|
36
|
+
if (!isHitPaint(stroke)) continue;
|
|
37
|
+
const weight = strokeWeightOf(node, stroke);
|
|
38
|
+
const side = node.type === "LINE" || stroke.align === "OUTSIDE";
|
|
39
|
+
reach = Math.max(reach, side ? weight : weight / 2);
|
|
40
|
+
}
|
|
41
|
+
const hitStrokes = node.strokes.filter(isHitPaint);
|
|
42
|
+
return Math.max(reach, arrowCapOverflow(hitStrokes, node.strokeCap, node.vectorNetwork));
|
|
43
|
+
}
|
|
44
|
+
function localPoint(graph, node, px, py) {
|
|
45
|
+
const inverse = Matrix.invert(getWorldMatrix(node, graph));
|
|
46
|
+
if (!inverse) return null;
|
|
47
|
+
const [x, y] = Matrix.mapPoints(inverse, [px, py]);
|
|
48
|
+
return {
|
|
49
|
+
x,
|
|
50
|
+
y
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Whether an outline made of `edges` passes within `radius` of the point, counting a stretch of
|
|
55
|
+
* edge only where `insideAt` changes from one side of it to the other: edges that cancel or that
|
|
56
|
+
* lie hidden inside or outside the result count for nothing.
|
|
57
|
+
*/
|
|
58
|
+
function changingEdgeWithin(edges, insideAt, point, radius, tolerance) {
|
|
59
|
+
const step = Math.max(1e-6, Math.min(tolerance, radius / 4) / 4);
|
|
60
|
+
for (const edge of edges) for (let i = 0; i + 1 < edge.length; i++) {
|
|
61
|
+
const a = edge[i];
|
|
62
|
+
const b = edge[i + 1];
|
|
63
|
+
if (a.x === b.x && a.y === b.y) continue;
|
|
64
|
+
if (distanceToSegment(point, a, b) > radius) continue;
|
|
65
|
+
const dx = b.x - a.x;
|
|
66
|
+
const dy = b.y - a.y;
|
|
67
|
+
const length = Math.hypot(dx, dy);
|
|
68
|
+
const nx = -dy / length;
|
|
69
|
+
const ny = dx / length;
|
|
70
|
+
const centre = ((point.x - a.x) * dx + (point.y - a.y) * dy) / (length * length);
|
|
71
|
+
const offset = Math.abs((point.x - a.x) * nx + (point.y - a.y) * ny);
|
|
72
|
+
const half = Math.sqrt(Math.max(0, radius * radius - offset * offset)) / length;
|
|
73
|
+
const from = Math.max(0, centre - half);
|
|
74
|
+
const to = Math.min(1, centre + half);
|
|
75
|
+
for (let k = 0; k <= EDGE_SAMPLES; k++) {
|
|
76
|
+
const t = from + (to - from) * k / EDGE_SAMPLES;
|
|
77
|
+
const qx = a.x + dx * t;
|
|
78
|
+
const qy = a.y + dy * t;
|
|
79
|
+
const left = insideAt(qx + nx * step, qy + ny * step);
|
|
80
|
+
const right = insideAt(qx - nx * step, qy - ny * step);
|
|
81
|
+
if (left !== null && right !== null && left !== right) return true;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
function measureLine(line, ends, point) {
|
|
87
|
+
const reach = {
|
|
88
|
+
distance: distanceToLines([line], point),
|
|
89
|
+
beyond: 0,
|
|
90
|
+
across: 0
|
|
91
|
+
};
|
|
92
|
+
if (!ends || line.length < 2) return reach;
|
|
93
|
+
const tips = [[
|
|
94
|
+
ends[0],
|
|
95
|
+
line[0],
|
|
96
|
+
line[1]
|
|
97
|
+
], [
|
|
98
|
+
ends[1],
|
|
99
|
+
line[line.length - 1],
|
|
100
|
+
line[line.length - 2]
|
|
101
|
+
]];
|
|
102
|
+
for (const [open, tip, next] of tips) {
|
|
103
|
+
if (!open) continue;
|
|
104
|
+
const dx = tip.x - next.x;
|
|
105
|
+
const dy = tip.y - next.y;
|
|
106
|
+
const length = Math.hypot(dx, dy);
|
|
107
|
+
if (length === 0) continue;
|
|
108
|
+
const along = ((point.x - tip.x) * dx + (point.y - tip.y) * dy) / length;
|
|
109
|
+
if (along <= 0 || Math.hypot(point.x - tip.x, point.y - tip.y) > reach.distance + 1e-9) continue;
|
|
110
|
+
reach.beyond = along;
|
|
111
|
+
reach.across = Math.abs((point.x - tip.x) * dy - (point.y - tip.y) * dx) / length;
|
|
112
|
+
}
|
|
113
|
+
return reach;
|
|
114
|
+
}
|
|
115
|
+
function probeOutline(outline, point, tolerance) {
|
|
116
|
+
const insideAt = (x, y) => outline.areas.some((area) => insideArea(area, {
|
|
117
|
+
x,
|
|
118
|
+
y
|
|
119
|
+
}));
|
|
120
|
+
const edges = outline.areas.flatMap((area) => area.contours.map(closedLoop));
|
|
121
|
+
const { strokeAreas } = outline;
|
|
122
|
+
return {
|
|
123
|
+
inside: insideAt(point.x, point.y),
|
|
124
|
+
local: point,
|
|
125
|
+
edgeWithin: (radius) => changingEdgeWithin(edges, insideAt, point, radius, tolerance),
|
|
126
|
+
lines: outline.lines.map((line, index) => measureLine(line, outline.lineEnds?.[index], point)),
|
|
127
|
+
lineWeights: outline.lineWeights,
|
|
128
|
+
closed: outline.areas.some((area) => area.contours.length > 0),
|
|
129
|
+
centered: outline.strokeCentered,
|
|
130
|
+
strokeAreaWithin: strokeAreas ? (radius) => strokeAreas.some((area) => insideArea(area, point)) || changingEdgeWithin(strokeAreas.flatMap((area) => area.contours.map(closedLoop)), (x, y) => strokeAreas.some((area) => insideArea(area, {
|
|
131
|
+
x,
|
|
132
|
+
y
|
|
133
|
+
})), point, radius, tolerance) : void 0
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
function closedLoop(contour) {
|
|
137
|
+
const first = contour.at(0);
|
|
138
|
+
return first ? [...contour, first] : contour;
|
|
139
|
+
}
|
|
140
|
+
const BOOLEAN_COMBINE = {
|
|
141
|
+
UNION: (hits) => hits.some(Boolean),
|
|
142
|
+
INTERSECT: (hits) => hits.length > 0 && hits.every(Boolean),
|
|
143
|
+
SUBTRACT: ([first = false, ...rest]) => first && !rest.some(Boolean),
|
|
144
|
+
EXCLUDE: (hits) => compact(hits).length % 2 === 1
|
|
145
|
+
};
|
|
146
|
+
/** The lines an operand draws its outline along, in canvas coordinates. */
|
|
147
|
+
function operandEdges(graph, node, tolerance) {
|
|
148
|
+
if (node.type === "BOOLEAN_OPERATION" && node.fillGeometry.length === 0) return graph.getChildren(node.id).filter((child) => child.visible).flatMap((child) => operandEdges(graph, child, tolerance));
|
|
149
|
+
const outline = node.type === "BOOLEAN_OPERATION" ? shapeOutline({
|
|
150
|
+
...node,
|
|
151
|
+
type: "VECTOR",
|
|
152
|
+
vectorNetwork: null
|
|
153
|
+
}, tolerance) : shapeOutline(node, tolerance);
|
|
154
|
+
if (!outline) return [];
|
|
155
|
+
const world = getWorldMatrix(node, graph);
|
|
156
|
+
return [...outline.lines, ...outline.areas.flatMap((area) => area.contours.map(closedLoop))].map((line) => line.map((point) => {
|
|
157
|
+
const [x, y] = Matrix.mapPoints(world, [point.x, point.y]);
|
|
158
|
+
return {
|
|
159
|
+
x,
|
|
160
|
+
y
|
|
161
|
+
};
|
|
162
|
+
}));
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* A boolean operation without saved geometry, combined from its operands as it draws. Its outline
|
|
166
|
+
* runs along the operand edges where the combined result changes from one side to the other, so
|
|
167
|
+
* edges hidden inside or outside the result count for nothing. Each side is probed at most a
|
|
168
|
+
* quarter of a screen pixel out, so a result thinner than that, which draws no visible pixel, is
|
|
169
|
+
* not seen.
|
|
170
|
+
*/
|
|
171
|
+
function probeBoolean(graph, node, px, py, tolerance) {
|
|
172
|
+
const operands = graph.getChildren(node.id).filter((child) => child.visible);
|
|
173
|
+
if (operands.length === 0) return null;
|
|
174
|
+
const combine = BOOLEAN_COMBINE[node.booleanOperation ?? "UNION"];
|
|
175
|
+
const insideAt = (x, y) => {
|
|
176
|
+
const hits = [];
|
|
177
|
+
for (const operand of operands) {
|
|
178
|
+
const probe = probeNode(graph, operand, x, y, tolerance);
|
|
179
|
+
if (!probe) return null;
|
|
180
|
+
hits.push(probe.inside);
|
|
181
|
+
}
|
|
182
|
+
return combine(hits);
|
|
183
|
+
};
|
|
184
|
+
const inside = insideAt(px, py);
|
|
185
|
+
if (inside === null) return null;
|
|
186
|
+
const point = {
|
|
187
|
+
x: px,
|
|
188
|
+
y: py
|
|
189
|
+
};
|
|
190
|
+
const edges = operands.flatMap((operand) => operandEdges(graph, operand, tolerance));
|
|
191
|
+
return {
|
|
192
|
+
inside,
|
|
193
|
+
edgeWithin: (radius) => changingEdgeWithin(edges, insideAt, point, radius, tolerance),
|
|
194
|
+
lines: [],
|
|
195
|
+
closed: true,
|
|
196
|
+
strokeOnEdge: true
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
function probeNode(graph, node, px, py, tolerance) {
|
|
200
|
+
if (node.type === "BOOLEAN_OPERATION" && node.fillGeometry.length === 0) return probeBoolean(graph, node, px, py, tolerance);
|
|
201
|
+
const outline = node.type === "BOOLEAN_OPERATION" ? shapeOutline({
|
|
202
|
+
...node,
|
|
203
|
+
type: "VECTOR",
|
|
204
|
+
vectorNetwork: null
|
|
205
|
+
}, tolerance) : shapeOutline(node, tolerance);
|
|
206
|
+
if (!outline) return null;
|
|
207
|
+
const point = localPoint(graph, node, px, py);
|
|
208
|
+
return point ? probeOutline(outline, point, tolerance) : null;
|
|
209
|
+
}
|
|
210
|
+
/** How far from a line a stroke's band reaches toward the point, beyond which it misses. */
|
|
211
|
+
function strokeReachAt(align, weight, inside) {
|
|
212
|
+
if (align === "INSIDE") return inside ? weight : 0;
|
|
213
|
+
if (align === "OUTSIDE") return inside ? 0 : weight;
|
|
214
|
+
return weight / 2;
|
|
215
|
+
}
|
|
216
|
+
function strokeHits(node, stroke, probe, pad) {
|
|
217
|
+
if (probe.strokeAreaWithin) return probe.strokeAreaWithin(pad);
|
|
218
|
+
const align = probe.closed && !probe.centered ? stroke.align : "CENTER";
|
|
219
|
+
if (probe.strokeOnEdge) {
|
|
220
|
+
const weight = strokeWeightOf(node, stroke);
|
|
221
|
+
return probe.edgeWithin(strokeReachAt(align, weight, probe.inside) + pad);
|
|
222
|
+
}
|
|
223
|
+
const cap = stroke.cap ?? node.strokeCap;
|
|
224
|
+
const flatEnds = cap === "NONE";
|
|
225
|
+
const reaches = (line, band) => {
|
|
226
|
+
if (flatEnds && line.beyond > 0) return Math.hypot(line.beyond, Math.max(0, line.across - band)) <= pad;
|
|
227
|
+
return line.distance <= band + pad;
|
|
228
|
+
};
|
|
229
|
+
if (node.type === "LINE" && probe.local) {
|
|
230
|
+
const weight = strokeWeightOf(node, stroke);
|
|
231
|
+
if (!(weight > 0)) return false;
|
|
232
|
+
const { start, end } = lineStrokeEnds(node.width, node.height, weight, cap);
|
|
233
|
+
if (reaches(measureLine([start, end], [true, true], probe.local), weight / 2)) return true;
|
|
234
|
+
if (!isArrowCap(cap)) return false;
|
|
235
|
+
const still = {
|
|
236
|
+
x: 0,
|
|
237
|
+
y: 0
|
|
238
|
+
};
|
|
239
|
+
return hitsArrowHeads({
|
|
240
|
+
vertices: [start, end],
|
|
241
|
+
segments: [{
|
|
242
|
+
start: 0,
|
|
243
|
+
end: 1,
|
|
244
|
+
tangentStart: still,
|
|
245
|
+
tangentEnd: still
|
|
246
|
+
}],
|
|
247
|
+
regions: []
|
|
248
|
+
}, cap, weight, probe.local, pad);
|
|
249
|
+
}
|
|
250
|
+
const network = node.type === "VECTOR" ? node.vectorNetwork : null;
|
|
251
|
+
if (network && probe.local) {
|
|
252
|
+
const weight = strokeWeightOf(node, stroke);
|
|
253
|
+
if (weight > 0 && hitsArrowHeads(network, cap, weight, probe.local, pad)) return true;
|
|
254
|
+
}
|
|
255
|
+
return probe.lines.some((line, index) => {
|
|
256
|
+
const weight = probe.lineWeights?.[index] ?? strokeWeightOf(node, stroke);
|
|
257
|
+
if (!(weight > 0)) return false;
|
|
258
|
+
return reaches(line, strokeReachAt(align, weight, probe.inside));
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Whether a click at the canvas point lands on the shape, as in Figma: inside the area a
|
|
263
|
+
* clickable fill covers, or on the band a clickable stroke draws, either grown by `pad` canvas
|
|
264
|
+
* units. A shape whose paints show nothing is never hit. Null for a layer without an outline to
|
|
265
|
+
* test, which is hit by its box.
|
|
266
|
+
*/
|
|
267
|
+
function hitsShape(graph, node, px, py, pad = 0) {
|
|
268
|
+
const probe = probeNode(graph, node, px, py, pad > 0 ? Math.min(DEFAULT_TOLERANCE, pad / 4) : DEFAULT_TOLERANCE);
|
|
269
|
+
if (!probe) return null;
|
|
270
|
+
if (node.fills.filter(isHitPaint).length > 0 && probe.closed && (probe.inside || probe.edgeWithin(pad))) return true;
|
|
271
|
+
return node.strokes.some((stroke) => isHitPaint(stroke) && strokeHits(node, stroke, probe, pad));
|
|
272
|
+
}
|
|
273
|
+
//#endregion
|
|
274
|
+
export { hitsShape, isHitPaint, strokeReach };
|
|
275
|
+
|
|
276
|
+
//# sourceMappingURL=hit.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hit.js","names":[],"sources":["../../src/outline/hit.ts"],"sourcesContent":["import { compact } from 'es-toolkit'\n\nimport { arrowCapOverflow, isArrowCap, lineStrokeEnds } from '../arrow-caps'\nimport { getWorldMatrix } from '../coordinate'\nimport type { SceneGraph } from '../graph'\nimport Matrix from '../matrix'\nimport type { Vector } from '../primitives'\nimport type { Fill, SceneNode, Stroke } from '../types'\nimport { hitsArrowHeads } from './heads'\nimport { containsByWinding, distanceToLines, distanceToSegment } from './point'\nimport { shapeOutline, type OutlineArea, type ShapeOutline } from './shapes'\n\n/** Points tested along each edge near the point for whether the filled result changes there. */\nconst EDGE_SAMPLES = 8\n\n/** How far a flattened curve may stray from the shape when nothing tighter is asked for. */\nconst DEFAULT_TOLERANCE = 0.25\n\n/**\n * Whether a paint makes its layer clickable, as in Figma: a visible paint that shows something.\n * A paint at 0% opacity, or a gradient whose stops are all transparent, does not; 1% does, and\n * the layer's own opacity does not matter.\n */\nexport function isHitPaint(paint: Fill): boolean {\n if (!paint.visible || !(paint.opacity > 0)) return false\n if (paint.gradientStops) return paint.gradientStops.some((stop) => stop.color.a > 0)\n if (paint.type === 'SOLID') return paint.color.a > 0\n return true\n}\n\nfunction insideArea(area: OutlineArea, point: Vector): boolean {\n return containsByWinding(area.contours, area.windingRule, point)\n}\n\n/** A stroke's weight; with independent borders the widest side, for layers without per-side lines. */\nfunction strokeWeightOf(node: SceneNode, stroke: Stroke): number {\n if (node.independentStrokeWeights) {\n return Math.max(\n node.borderTopWeight,\n node.borderRightWeight,\n node.borderBottomWeight,\n node.borderLeftWeight\n )\n }\n return Number.isFinite(stroke.weight) ? stroke.weight : node.strokeWeight\n}\n\n/** The farthest a layer's clickable strokes reach outside its box, in its own units. */\nexport function strokeReach(node: SceneNode): number {\n let reach = 0\n for (const stroke of node.strokes) {\n if (!isHitPaint(stroke)) continue\n const weight = strokeWeightOf(node, stroke)\n // A line's stroke lies a full weight to one side of it.\n const side = node.type === 'LINE' || stroke.align === 'OUTSIDE'\n reach = Math.max(reach, side ? weight : weight / 2)\n }\n // Arrow heads reach past the stroke.\n const hitStrokes = node.strokes.filter(isHitPaint)\n return Math.max(reach, arrowCapOverflow(hitStrokes, node.strokeCap, node.vectorNetwork))\n}\n\nfunction localPoint(graph: SceneGraph, node: SceneNode, px: number, py: number): Vector | null {\n const inverse = Matrix.invert(getWorldMatrix(node, graph))\n if (!inverse) return null\n const [x, y] = Matrix.mapPoints(inverse, [px, py])\n return { x, y }\n}\n\ninterface Probe {\n inside: boolean\n /** The point in the layer's own coordinates, where its outline has one. */\n local?: Vector\n /**\n * Whether the outline of the filled area passes within `radius` of the point, counting only\n * edges where the filled result changes from one side to the other.\n */\n edgeWithin: (radius: number) => boolean\n /** Each line a stroke follows, measured from the point in canvas units. */\n lines: LineReach[]\n /** Each line's own stroke weight, where sides differ. */\n lineWeights?: number[]\n /** Whether the layer has an area of its own; strokes on lines are always centred. */\n closed: boolean\n /** Strokes centre on the lines whatever their alignment. */\n centered?: boolean\n /** Strokes follow the filled area's outline, for a shape combined from operands. */\n strokeOnEdge?: boolean\n /** Whether the point lies on or within `radius` of the saved outline a stroke fills. */\n strokeAreaWithin?: (radius: number) => boolean\n}\n\n/**\n * Whether an outline made of `edges` passes within `radius` of the point, counting a stretch of\n * edge only where `insideAt` changes from one side of it to the other: edges that cancel or that\n * lie hidden inside or outside the result count for nothing.\n */\nfunction changingEdgeWithin(\n edges: Vector[][],\n insideAt: (x: number, y: number) => boolean | null,\n point: Vector,\n radius: number,\n tolerance: number\n): boolean {\n const step = Math.max(1e-6, Math.min(tolerance, radius / 4) / 4)\n for (const edge of edges) {\n for (let i = 0; i + 1 < edge.length; i++) {\n const a = edge[i]\n const b = edge[i + 1]\n if (a.x === b.x && a.y === b.y) continue\n if (distanceToSegment(point, a, b) > radius) continue\n const dx = b.x - a.x\n const dy = b.y - a.y\n const length = Math.hypot(dx, dy)\n const nx = -dy / length\n const ny = dx / length\n // The part of the edge inside the circle, sampled evenly.\n const centre = ((point.x - a.x) * dx + (point.y - a.y) * dy) / (length * length)\n const offset = Math.abs((point.x - a.x) * nx + (point.y - a.y) * ny)\n const half = Math.sqrt(Math.max(0, radius * radius - offset * offset)) / length\n const from = Math.max(0, centre - half)\n const to = Math.min(1, centre + half)\n for (let k = 0; k <= EDGE_SAMPLES; k++) {\n const t = from + ((to - from) * k) / EDGE_SAMPLES\n const qx = a.x + dx * t\n const qy = a.y + dy * t\n const left = insideAt(qx + nx * step, qy + ny * step)\n const right = insideAt(qx - nx * step, qy - ny * step)\n if (left !== null && right !== null && left !== right) return true\n }\n }\n }\n return false\n}\n\n/**\n * How a point lies from a line: its distance, and past an open end how far beyond the end it is\n * along the line and how far across it, for a flat cap that paints nothing past the end.\n */\ninterface LineReach {\n distance: number\n beyond: number\n across: number\n}\n\nfunction measureLine(\n line: Vector[],\n ends: [boolean, boolean] | undefined,\n point: Vector\n): LineReach {\n const reach: LineReach = { distance: distanceToLines([line], point), beyond: 0, across: 0 }\n if (!ends || line.length < 2) return reach\n // Past an open end, the point projects beyond the first or last segment.\n const tips: Array<[boolean, Vector, Vector]> = [\n [ends[0], line[0], line[1]],\n [ends[1], line[line.length - 1], line[line.length - 2]]\n ]\n for (const [open, tip, next] of tips) {\n if (!open) continue\n const dx = tip.x - next.x\n const dy = tip.y - next.y\n const length = Math.hypot(dx, dy)\n if (length === 0) continue\n const along = ((point.x - tip.x) * dx + (point.y - tip.y) * dy) / length\n if (along <= 0 || Math.hypot(point.x - tip.x, point.y - tip.y) > reach.distance + 1e-9) continue\n reach.beyond = along\n reach.across = Math.abs((point.x - tip.x) * dy - (point.y - tip.y) * dx) / length\n }\n return reach\n}\n\nfunction probeOutline(outline: ShapeOutline, point: Vector, tolerance: number): Probe {\n const insideAt = (x: number, y: number) =>\n outline.areas.some((area) => insideArea(area, { x, y }))\n const edges = outline.areas.flatMap((area) => area.contours.map(closedLoop))\n const { strokeAreas } = outline\n return {\n inside: insideAt(point.x, point.y),\n local: point,\n edgeWithin: (radius) => changingEdgeWithin(edges, insideAt, point, radius, tolerance),\n lines: outline.lines.map((line, index) => measureLine(line, outline.lineEnds?.[index], point)),\n lineWeights: outline.lineWeights,\n closed: outline.areas.some((area) => area.contours.length > 0),\n centered: outline.strokeCentered,\n strokeAreaWithin: strokeAreas\n ? (radius) =>\n strokeAreas.some((area) => insideArea(area, point)) ||\n changingEdgeWithin(\n strokeAreas.flatMap((area) => area.contours.map(closedLoop)),\n (x, y) => strokeAreas.some((area) => insideArea(area, { x, y })),\n point,\n radius,\n tolerance\n )\n : undefined\n }\n}\n\nfunction closedLoop(contour: Vector[]): Vector[] {\n const first = contour.at(0)\n return first ? [...contour, first] : contour\n}\n\nconst BOOLEAN_COMBINE: Record<\n NonNullable<SceneNode['booleanOperation']>,\n (hits: boolean[]) => boolean\n> = {\n UNION: (hits) => hits.some(Boolean),\n INTERSECT: (hits) => hits.length > 0 && hits.every(Boolean),\n SUBTRACT: ([first = false, ...rest]) => first && !rest.some(Boolean),\n EXCLUDE: (hits) => compact(hits).length % 2 === 1\n}\n\n/** The lines an operand draws its outline along, in canvas coordinates. */\nfunction operandEdges(graph: SceneGraph, node: SceneNode, tolerance: number): Vector[][] {\n if (node.type === 'BOOLEAN_OPERATION' && node.fillGeometry.length === 0) {\n return graph\n .getChildren(node.id)\n .filter((child) => child.visible)\n .flatMap((child) => operandEdges(graph, child, tolerance))\n }\n const outline =\n node.type === 'BOOLEAN_OPERATION'\n ? shapeOutline({ ...node, type: 'VECTOR', vectorNetwork: null }, tolerance)\n : shapeOutline(node, tolerance)\n if (!outline) return []\n const world = getWorldMatrix(node, graph)\n const local = [\n ...outline.lines,\n ...outline.areas.flatMap((area) => area.contours.map(closedLoop))\n ]\n return local.map((line) =>\n line.map((point) => {\n const [x, y] = Matrix.mapPoints(world, [point.x, point.y])\n return { x, y }\n })\n )\n}\n\n/**\n * A boolean operation without saved geometry, combined from its operands as it draws. Its outline\n * runs along the operand edges where the combined result changes from one side to the other, so\n * edges hidden inside or outside the result count for nothing. Each side is probed at most a\n * quarter of a screen pixel out, so a result thinner than that, which draws no visible pixel, is\n * not seen.\n */\nfunction probeBoolean(\n graph: SceneGraph,\n node: SceneNode,\n px: number,\n py: number,\n tolerance: number\n): Probe | null {\n const operands = graph.getChildren(node.id).filter((child) => child.visible)\n if (operands.length === 0) return null\n const combine = BOOLEAN_COMBINE[node.booleanOperation ?? 'UNION']\n const insideAt = (x: number, y: number): boolean | null => {\n const hits: boolean[] = []\n for (const operand of operands) {\n const probe = probeNode(graph, operand, x, y, tolerance)\n if (!probe) return null\n hits.push(probe.inside)\n }\n return combine(hits)\n }\n const inside = insideAt(px, py)\n if (inside === null) return null\n const point = { x: px, y: py }\n const edges = operands.flatMap((operand) => operandEdges(graph, operand, tolerance))\n return {\n inside,\n edgeWithin: (radius) => changingEdgeWithin(edges, insideAt, point, radius, tolerance),\n lines: [],\n closed: true,\n strokeOnEdge: true\n }\n}\n\nfunction probeNode(\n graph: SceneGraph,\n node: SceneNode,\n px: number,\n py: number,\n tolerance: number\n): Probe | null {\n if (node.type === 'BOOLEAN_OPERATION' && node.fillGeometry.length === 0) {\n return probeBoolean(graph, node, px, py, tolerance)\n }\n const outline =\n node.type === 'BOOLEAN_OPERATION'\n ? shapeOutline({ ...node, type: 'VECTOR', vectorNetwork: null }, tolerance)\n : shapeOutline(node, tolerance)\n if (!outline) return null\n const point = localPoint(graph, node, px, py)\n return point ? probeOutline(outline, point, tolerance) : null\n}\n\n/** How far from a line a stroke's band reaches toward the point, beyond which it misses. */\nfunction strokeReachAt(align: Stroke['align'], weight: number, inside: boolean): number {\n if (align === 'INSIDE') return inside ? weight : 0\n if (align === 'OUTSIDE') return inside ? 0 : weight\n return weight / 2\n}\n\nfunction strokeHits(node: SceneNode, stroke: Stroke, probe: Probe, pad: number): boolean {\n if (probe.strokeAreaWithin) return probe.strokeAreaWithin(pad)\n const align = probe.closed && !probe.centered ? stroke.align : 'CENTER'\n if (probe.strokeOnEdge) {\n const weight = strokeWeightOf(node, stroke)\n return probe.edgeWithin(strokeReachAt(align, weight, probe.inside) + pad)\n }\n // A flat cap ends the stroke at the line's end; round and square caps paint past it.\n const cap = stroke.cap ?? node.strokeCap\n const flatEnds = cap === 'NONE'\n const reaches = (line: LineReach, band: number): boolean => {\n if (flatEnds && line.beyond > 0) {\n // Past a flat end, the distance to the painted band's square end.\n return Math.hypot(line.beyond, Math.max(0, line.across - band)) <= pad\n }\n return line.distance <= band + pad\n }\n if (node.type === 'LINE' && probe.local) {\n // Figma strokes a line a full weight to one side, along the path the renderer draws.\n const weight = strokeWeightOf(node, stroke)\n if (!(weight > 0)) return false\n const { start, end } = lineStrokeEnds(node.width, node.height, weight, cap)\n if (reaches(measureLine([start, end], [true, true], probe.local), weight / 2)) return true\n if (!isArrowCap(cap)) return false\n const still = { x: 0, y: 0 }\n const shaft = {\n vertices: [start, end],\n segments: [{ start: 0, end: 1, tangentStart: still, tangentEnd: still }],\n regions: []\n }\n return hitsArrowHeads(shaft, cap, weight, probe.local, pad)\n }\n const network = node.type === 'VECTOR' ? node.vectorNetwork : null\n if (network && probe.local) {\n const weight = strokeWeightOf(node, stroke)\n if (weight > 0 && hitsArrowHeads(network, cap, weight, probe.local, pad)) return true\n }\n return probe.lines.some((line, index) => {\n const weight = probe.lineWeights?.[index] ?? strokeWeightOf(node, stroke)\n // A side with no weight paints no border.\n if (!(weight > 0)) return false\n return reaches(line, strokeReachAt(align, weight, probe.inside))\n })\n}\n\n/**\n * Whether a click at the canvas point lands on the shape, as in Figma: inside the area a\n * clickable fill covers, or on the band a clickable stroke draws, either grown by `pad` canvas\n * units. A shape whose paints show nothing is never hit. Null for a layer without an outline to\n * test, which is hit by its box.\n */\nexport function hitsShape(\n graph: SceneGraph,\n node: SceneNode,\n px: number,\n py: number,\n pad = 0\n): boolean | null {\n const tolerance = pad > 0 ? Math.min(DEFAULT_TOLERANCE, pad / 4) : DEFAULT_TOLERANCE\n const probe = probeNode(graph, node, px, py, tolerance)\n if (!probe) return null\n const fills = node.fills.filter(isHitPaint)\n if (fills.length > 0 && probe.closed && (probe.inside || probe.edgeWithin(pad))) return true\n return node.strokes.some((stroke) => isHitPaint(stroke) && strokeHits(node, stroke, probe, pad))\n}\n"],"mappings":";;;;;;;;;AAaA,MAAM,eAAe;;AAGrB,MAAM,oBAAoB;;;;;;AAO1B,SAAgB,WAAW,OAAsB;CAC/C,IAAI,CAAC,MAAM,WAAW,EAAE,MAAM,UAAU,IAAI,OAAO;CACnD,IAAI,MAAM,eAAe,OAAO,MAAM,cAAc,MAAM,SAAS,KAAK,MAAM,IAAI,CAAC;CACnF,IAAI,MAAM,SAAS,SAAS,OAAO,MAAM,MAAM,IAAI;CACnD,OAAO;AACT;AAEA,SAAS,WAAW,MAAmB,OAAwB;CAC7D,OAAO,kBAAkB,KAAK,UAAU,KAAK,aAAa,KAAK;AACjE;;AAGA,SAAS,eAAe,MAAiB,QAAwB;CAC/D,IAAI,KAAK,0BACP,OAAO,KAAK,IACV,KAAK,iBACL,KAAK,mBACL,KAAK,oBACL,KAAK,gBACP;CAEF,OAAO,OAAO,SAAS,OAAO,MAAM,IAAI,OAAO,SAAS,KAAK;AAC/D;;AAGA,SAAgB,YAAY,MAAyB;CACnD,IAAI,QAAQ;CACZ,KAAK,MAAM,UAAU,KAAK,SAAS;EACjC,IAAI,CAAC,WAAW,MAAM,GAAG;EACzB,MAAM,SAAS,eAAe,MAAM,MAAM;EAE1C,MAAM,OAAO,KAAK,SAAS,U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AAM,IAAI,IADvB,MAAM,IAAI,KAAK,IAAI,mBAAmB,MAAM,CAAC,IAAI,iBACb;CACtD,IAAI,CAAC,OAAO,OAAO;CAEnB,IADc,KAAK,MAAM,OAAO,UACxB,CAAC,CAAC,SAAS,KAAK,MAAM,WAAW,MAAM,UAAU,MAAM,WAAW,GAAG,IAAI,OAAO;CACxF,OAAO,KAAK,QAAQ,MAAM,WAAW,WAAW,MAAM,KAAK,WAAW,MAAM,QAAQ,OAAO,GAAG,CAAC;AACjG"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { hitsShape, isHitPaint, strokeReach } from "./hit.js";
|
|
2
|
+
import { OutlineArea, ShapeOutline, shapeOutline } from "./shapes.js";
|
|
3
|
+
import { blobContours, loopPoints, segmentPoints } from "./flatten.js";
|
|
4
|
+
import { containsByWinding, distanceToLines, distanceToSegment, windingNumber } from "./point.js";
|
|
5
|
+
export { type OutlineArea, type ShapeOutline, blobContours, containsByWinding, distanceToLines, distanceToSegment, hitsShape, isHitPaint, loopPoints, segmentPoints, shapeOutline, strokeReach, windingNumber };
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { containsByWinding, distanceToLines, distanceToSegment, windingNumber } from "./point.js";
|
|
2
|
+
import { blobContours, loopPoints, segmentPoints } from "./flatten.js";
|
|
3
|
+
import { shapeOutline } from "./shapes.js";
|
|
4
|
+
import { hitsShape, isHitPaint, strokeReach } from "./hit.js";
|
|
5
|
+
export { blobContours, containsByWinding, distanceToLines, distanceToSegment, hitsShape, isHitPaint, loopPoints, segmentPoints, shapeOutline, strokeReach, windingNumber };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Vector } from "../primitives/index.js";
|
|
2
|
+
import { WindingRule } from "../types.js";
|
|
3
|
+
//#region src/outline/point.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* The signed number of times closed contours wind around `point`, each closing back to its start
|
|
6
|
+
* as a path fill closes it.
|
|
7
|
+
*/
|
|
8
|
+
declare function windingNumber(contours: Iterable<Vector[]>, point: Vector): number;
|
|
9
|
+
/** Whether contours filled by `windingRule` cover the point. */
|
|
10
|
+
declare function containsByWinding(contours: Iterable<Vector[]>, windingRule: WindingRule, point: Vector): boolean;
|
|
11
|
+
declare function distanceToSegment(p: Vector, a: Vector, b: Vector): number;
|
|
12
|
+
/** The distance from the point to the nearest of the polylines. */
|
|
13
|
+
declare function distanceToLines(lines: readonly Vector[][], point: Vector): number;
|
|
14
|
+
//#endregion
|
|
15
|
+
export { containsByWinding, distanceToLines, distanceToSegment, windingNumber };
|
|
16
|
+
//# sourceMappingURL=point.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"point.d.ts","names":[],"sources":["../../src/outline/point.ts"],"mappings":";;;;;;;iBAOgB,cAAc,UAAU,SAAS,WAAW,OAAO;;iBAmBnD,kBACd,UAAU,SAAS,WACnB,aAAa,aACb,OAAO;iBAMO,kBAAkB,GAAG,QAAQ,GAAG,QAAQ,GAAG;;iBAY3C,gBAAgB,gBAAgB,YAAY,OAAO"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
//#region src/outline/point.ts
|
|
2
|
+
/**
|
|
3
|
+
* The signed number of times closed contours wind around `point`, each closing back to its start
|
|
4
|
+
* as a path fill closes it.
|
|
5
|
+
*/
|
|
6
|
+
function windingNumber(contours, point) {
|
|
7
|
+
let winding = 0;
|
|
8
|
+
for (const contour of contours) for (let i = 0; i < contour.length; i++) {
|
|
9
|
+
const a = contour[i];
|
|
10
|
+
const b = contour[(i + 1) % contour.length];
|
|
11
|
+
const side = (b.x - a.x) * (point.y - a.y) - (point.x - a.x) * (b.y - a.y);
|
|
12
|
+
if (a.y <= point.y) {
|
|
13
|
+
if (b.y > point.y && side > 0) winding++;
|
|
14
|
+
} else if (b.y <= point.y && side < 0) winding--;
|
|
15
|
+
}
|
|
16
|
+
return winding;
|
|
17
|
+
}
|
|
18
|
+
/** Whether contours filled by `windingRule` cover the point. */
|
|
19
|
+
function containsByWinding(contours, windingRule, point) {
|
|
20
|
+
const winding = windingNumber(contours, point);
|
|
21
|
+
return windingRule === "EVENODD" ? winding % 2 !== 0 : winding !== 0;
|
|
22
|
+
}
|
|
23
|
+
function distanceToSegment(p, a, b) {
|
|
24
|
+
const dx = b.x - a.x;
|
|
25
|
+
const dy = b.y - a.y;
|
|
26
|
+
const lengthSquared = dx * dx + dy * dy;
|
|
27
|
+
const t = lengthSquared === 0 ? 0 : Math.max(0, Math.min(1, ((p.x - a.x) * dx + (p.y - a.y) * dy) / lengthSquared));
|
|
28
|
+
return Math.hypot(p.x - (a.x + t * dx), p.y - (a.y + t * dy));
|
|
29
|
+
}
|
|
30
|
+
/** The distance from the point to the nearest of the polylines. */
|
|
31
|
+
function distanceToLines(lines, point) {
|
|
32
|
+
let nearest = Infinity;
|
|
33
|
+
for (const line of lines) {
|
|
34
|
+
if (line.length === 1) nearest = Math.min(nearest, Math.hypot(point.x - line[0].x, point.y - line[0].y));
|
|
35
|
+
for (let i = 0; i + 1 < line.length; i++) nearest = Math.min(nearest, distanceToSegment(point, line[i], line[i + 1]));
|
|
36
|
+
}
|
|
37
|
+
return nearest;
|
|
38
|
+
}
|
|
39
|
+
//#endregion
|
|
40
|
+
export { containsByWinding, distanceToLines, distanceToSegment, windingNumber };
|
|
41
|
+
|
|
42
|
+
//# sourceMappingURL=point.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"point.js","names":[],"sources":["../../src/outline/point.ts"],"sourcesContent":["import type { Vector } from '../primitives'\nimport type { WindingRule } from '../types'\n\n/**\n * The signed number of times closed contours wind around `point`, each closing back to its start\n * as a path fill closes it.\n */\nexport function windingNumber(contours: Iterable<Vector[]>, point: Vector): number {\n let winding = 0\n for (const contour of contours) {\n for (let i = 0; i < contour.length; i++) {\n const a = contour[i]\n const b = contour[(i + 1) % contour.length]\n // Which side of the edge a→b the point is on, positive when left.\n const side = (b.x - a.x) * (point.y - a.y) - (point.x - a.x) * (b.y - a.y)\n if (a.y <= point.y) {\n if (b.y > point.y && side > 0) winding++\n } else if (b.y <= point.y && side < 0) {\n winding--\n }\n }\n }\n return winding\n}\n\n/** Whether contours filled by `windingRule` cover the point. */\nexport function containsByWinding(\n contours: Iterable<Vector[]>,\n windingRule: WindingRule,\n point: Vector\n): boolean {\n const winding = windingNumber(contours, point)\n return windingRule === 'EVENODD' ? winding % 2 !== 0 : winding !== 0\n}\n\nexport function distanceToSegment(p: Vector, a: Vector, b: Vector): number {\n const dx = b.x - a.x\n const dy = b.y - a.y\n const lengthSquared = dx * dx + dy * dy\n const t =\n lengthSquared === 0\n ? 0\n : Math.max(0, Math.min(1, ((p.x - a.x) * dx + (p.y - a.y) * dy) / lengthSquared))\n return Math.hypot(p.x - (a.x + t * dx), p.y - (a.y + t * dy))\n}\n\n/** The distance from the point to the nearest of the polylines. */\nexport function distanceToLines(lines: readonly Vector[][], point: Vector): number {\n let nearest = Infinity\n for (const line of lines) {\n if (line.length === 1) {\n nearest = Math.min(nearest, Math.hypot(point.x - line[0].x, point.y - line[0].y))\n }\n for (let i = 0; i + 1 < line.length; i++) {\n nearest = Math.min(nearest, distanceToSegment(point, line[i], line[i + 1]))\n }\n }\n return nearest\n}\n"],"mappings":";;;;;AAOA,SAAgB,cAAc,UAA8B,OAAuB;CACjF,IAAI,UAAU;CACd,KAAK,MAAM,WAAW,UACpB,KAAK,IAAI,IAAI,GAAG,IAAI,QAAQ,QAAQ,KAAK;EACvC,MAAM,IAAI,QAAQ;EAClB,MAAM,IAAI,SAAS,IAAI,KAAK,QAAQ;EAEpC,MAAM,QAAQ,EAAE,IAAI,EAAE,MAAM,MAAM,IAAI,EAAE,MAAM,MAAM,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE;EACxE,IAAI,EAAE,KAAK,MAAM,GACX;OAAA,EAAE,IAAI,MAAM,KAAK,OAAO,GAAG;EAAA,OAC1B,IAAI,EAAE,KAAK,MAAM,KAAK,OAAO,GAClC;CAEJ;CAEF,OAAO;AACT;;AAGA,SAAgB,kBACd,UACA,aACA,OACS;CACT,MAAM,UAAU,cAAc,UAAU,KAAK;CAC7C,OAAO,gBAAgB,YAAY,UAAU,MAAM,IAAI,YAAY;AACrE;AAEA,SAAgB,kBAAkB,GAAW,GAAW,GAAmB;CACzE,MAAM,KAAK,EAAE,IAAI,EAAE;CACnB,MAAM,KAAK,EAAE,IAAI,EAAE;CACnB,MAAM,gBAAgB,KAAK,KAAK,KAAK;CACrC,MAAM,IACJ,kBAAkB,IACd,IACA,KAAK,IAAI,GAAG,KAAK,IAAI,KAAK,EAAE,IAAI,EAAE,KAAK,MAAM,EAAE,IAAI,EAAE,KAAK,MAAM,aAAa,CAAC;CACpF,OAAO,KAAK,MAAM,EAAE,KAAK,EAAE,IAAI,IAAI,KAAK,EAAE,KAAK,EAAE,IAAI,IAAI,GAAG;AAC9D;;AAGA,SAAgB,gBAAgB,OAA4B,OAAuB;CACjF,IAAI,UAAU;CACd,KAAK,MAAM,QAAQ,OAAO;EACxB,IAAI,KAAK,WAAW,GAClB,UAAU,KAAK,IAAI,SAAS,KAAK,MAAM,MAAM,IAAI,KAAK,EAAE,CAAC,GAAG,MAAM,IAAI,KAAK,EAAE,CAAC,CAAC,CAAC;EAElF,KAAK,IAAI,IAAI,GAAG,IAAI,IAAI,KAAK,QAAQ,KACnC,UAAU,KAAK,IAAI,SAAS,kBAAkB,OAAO,KAAK,IAAI,KAAK,IAAI,EAAE,CAAC;CAE9E;CACA,OAAO;AACT"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Vector } from "../primitives/index.js";
|
|
2
|
+
import { SceneNode, WindingRule } from "../types.js";
|
|
3
|
+
//#region src/outline/shapes.d.ts
|
|
4
|
+
/** Contours filled together under one winding rule. */
|
|
5
|
+
interface OutlineArea {
|
|
6
|
+
windingRule: WindingRule;
|
|
7
|
+
contours: Vector[][];
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* A shape's geometry in its own coordinates, curves flattened: the areas its fills cover and the
|
|
11
|
+
* lines its strokes follow, closed contours ending where they began.
|
|
12
|
+
*/
|
|
13
|
+
interface ShapeOutline {
|
|
14
|
+
areas: OutlineArea[];
|
|
15
|
+
lines: Vector[][];
|
|
16
|
+
/** Each line's own stroke weight, where sides differ, as a rectangle's independent borders. */
|
|
17
|
+
lineWeights?: number[];
|
|
18
|
+
/**
|
|
19
|
+
* Strokes centre on the lines whatever their alignment, as on a network without regions that is
|
|
20
|
+
* open or has no saved stroke geometry.
|
|
21
|
+
*/
|
|
22
|
+
strokeCentered?: boolean;
|
|
23
|
+
/** For each line, whether its start and its end are open ends, where a stroke's cap is drawn. */
|
|
24
|
+
lineEnds?: Array<[boolean, boolean]>;
|
|
25
|
+
/** The saved outline a stroke fills, for a shape whose stroke has no lines to follow. */
|
|
26
|
+
strokeAreas?: OutlineArea[];
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* The outline a click is tested against, in the node's own coordinates, with curves within
|
|
30
|
+
* `tolerance` of the shape; null for layers tested by their box, such as text.
|
|
31
|
+
*/
|
|
32
|
+
declare function shapeOutline(node: SceneNode, tolerance: number): ShapeOutline | null;
|
|
33
|
+
//#endregion
|
|
34
|
+
export { OutlineArea, ShapeOutline, shapeOutline };
|
|
35
|
+
//# sourceMappingURL=shapes.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shapes.d.ts","names":[],"sources":["../../src/outline/shapes.ts"],"mappings":";;;;UAaiB;EACf,aAAa;EACb,UAAU;;;;;;UAOK;EACf,OAAO;EACP,OAAO;;EAEP;;;;;EAKA;;EAEA,WAAW;;EAEX,cAAc;;;;;;iBA2IA,aAAa,MAAM,WAAW,oBAAoB"}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import { polygonOutline } from "../polygon.js";
|
|
2
|
+
import { arcPoints, blobContours, commandContours, loopPoints, segmentPoints, segmentRuns } from "./flatten.js";
|
|
3
|
+
//#region src/outline/shapes.ts
|
|
4
|
+
function closed(contour) {
|
|
5
|
+
const first = contour.at(0);
|
|
6
|
+
const last = contour.at(-1);
|
|
7
|
+
if (!first || !last || first.x === last.x && first.y === last.y) return contour;
|
|
8
|
+
return [...contour, first];
|
|
9
|
+
}
|
|
10
|
+
function fromContours(contours, windingRule = "NONZERO") {
|
|
11
|
+
return {
|
|
12
|
+
areas: [{
|
|
13
|
+
windingRule,
|
|
14
|
+
contours
|
|
15
|
+
}],
|
|
16
|
+
lines: contours.map(closed)
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
function cornerRadii(node) {
|
|
20
|
+
const radii = node.independentCorners ? [
|
|
21
|
+
node.topLeftRadius,
|
|
22
|
+
node.topRightRadius,
|
|
23
|
+
node.bottomRightRadius,
|
|
24
|
+
node.bottomLeftRadius
|
|
25
|
+
] : [
|
|
26
|
+
node.cornerRadius,
|
|
27
|
+
node.cornerRadius,
|
|
28
|
+
node.cornerRadius,
|
|
29
|
+
node.cornerRadius
|
|
30
|
+
];
|
|
31
|
+
const limit = Math.min(node.width, node.height) / 2;
|
|
32
|
+
return radii.map((radius) => Math.max(0, Math.min(radius, limit)));
|
|
33
|
+
}
|
|
34
|
+
function rectangleContour(node, tolerance) {
|
|
35
|
+
const { width: w, height: h } = node;
|
|
36
|
+
const [tl, tr, br, bl] = cornerRadii(node);
|
|
37
|
+
const corner = (cx, cy, r, from) => r > 0 ? arcPoints(cx, cy, r, r, from, from + Math.PI / 2, tolerance) : [{
|
|
38
|
+
x: cx,
|
|
39
|
+
y: cy
|
|
40
|
+
}];
|
|
41
|
+
return [
|
|
42
|
+
...corner(w - tr, tr, tr, -Math.PI / 2),
|
|
43
|
+
...corner(w - br, h - br, br, 0),
|
|
44
|
+
...corner(bl, h - bl, bl, Math.PI / 2),
|
|
45
|
+
...corner(tl, tl, tl, Math.PI)
|
|
46
|
+
];
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* A rectangle's four sides with the weight each draws its border at: top, right, bottom, left.
|
|
50
|
+
* Each side runs from the middle of one rounded corner to the middle of the next, as its border
|
|
51
|
+
* is drawn round them.
|
|
52
|
+
*/
|
|
53
|
+
function rectangleSides(node, tolerance) {
|
|
54
|
+
const { width: w, height: h } = node;
|
|
55
|
+
const [tl, tr, br, bl] = cornerRadii(node);
|
|
56
|
+
const quarter = Math.PI / 4;
|
|
57
|
+
const arc = (cx, cy, r, from) => r > 0 ? arcPoints(cx, cy, r, r, from, from + quarter, tolerance) : [{
|
|
58
|
+
x: cx,
|
|
59
|
+
y: cy
|
|
60
|
+
}];
|
|
61
|
+
return {
|
|
62
|
+
lines: [
|
|
63
|
+
[...arc(tl, tl, tl, 5 * quarter), ...arc(w - tr, tr, tr, -2 * quarter)],
|
|
64
|
+
[...arc(w - tr, tr, tr, -quarter), ...arc(w - br, h - br, br, 0)],
|
|
65
|
+
[...arc(w - br, h - br, br, quarter), ...arc(bl, h - bl, bl, 2 * quarter)],
|
|
66
|
+
[...arc(bl, h - bl, bl, 3 * quarter), ...arc(tl, tl, tl, 4 * quarter)]
|
|
67
|
+
],
|
|
68
|
+
lineWeights: [
|
|
69
|
+
node.borderTopWeight,
|
|
70
|
+
node.borderRightWeight,
|
|
71
|
+
node.borderBottomWeight,
|
|
72
|
+
node.borderLeftWeight
|
|
73
|
+
]
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
const FULL_TURN = Math.PI * 2;
|
|
77
|
+
function ellipseContours(node, tolerance) {
|
|
78
|
+
const rx = node.width / 2;
|
|
79
|
+
const ry = node.height / 2;
|
|
80
|
+
const arc = node.arcData;
|
|
81
|
+
const start = arc?.startingAngle ?? 0;
|
|
82
|
+
const end = arc?.endingAngle ?? FULL_TURN;
|
|
83
|
+
const inner = arc?.innerRadius ?? 0;
|
|
84
|
+
const whole = Math.abs(end - start) >= FULL_TURN - 1e-6;
|
|
85
|
+
const outer = arcPoints(rx, ry, rx, ry, start, whole ? start + FULL_TURN : end, tolerance);
|
|
86
|
+
if (inner <= 0) return [whole ? outer : [{
|
|
87
|
+
x: rx,
|
|
88
|
+
y: ry
|
|
89
|
+
}, ...outer]];
|
|
90
|
+
const hole = arcPoints(rx, ry, rx * inner, ry * inner, whole ? start + FULL_TURN : end, start, tolerance);
|
|
91
|
+
return whole ? [outer, hole] : [[...outer, ...hole]];
|
|
92
|
+
}
|
|
93
|
+
function vectorOutline(node, tolerance) {
|
|
94
|
+
const network = node.vectorNetwork;
|
|
95
|
+
const geometry = node.fillGeometry.map((path) => ({
|
|
96
|
+
windingRule: path.windingRule,
|
|
97
|
+
contours: path.commandsBlob.length > 0 ? blobContours(path.commandsBlob, tolerance) : []
|
|
98
|
+
}));
|
|
99
|
+
if (!network) {
|
|
100
|
+
if (geometry.length === 0 && node.strokeGeometry.length === 0) return null;
|
|
101
|
+
return {
|
|
102
|
+
areas: geometry,
|
|
103
|
+
lines: [],
|
|
104
|
+
strokeAreas: node.strokeGeometry.map((path) => ({
|
|
105
|
+
windingRule: path.windingRule,
|
|
106
|
+
contours: path.commandsBlob.length > 0 ? blobContours(path.commandsBlob, tolerance) : []
|
|
107
|
+
}))
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
const degree = /* @__PURE__ */ new Map();
|
|
111
|
+
for (const segment of network.segments) {
|
|
112
|
+
degree.set(segment.start, (degree.get(segment.start) ?? 0) + 1);
|
|
113
|
+
degree.set(segment.end, (degree.get(segment.end) ?? 0) + 1);
|
|
114
|
+
}
|
|
115
|
+
const lines = [];
|
|
116
|
+
const lineEnds = [];
|
|
117
|
+
for (const [index, segment] of network.segments.entries()) {
|
|
118
|
+
const points = segmentPoints(network, index, tolerance);
|
|
119
|
+
if (!points) continue;
|
|
120
|
+
lines.push(points);
|
|
121
|
+
lineEnds.push([degree.get(segment.start) === 1, degree.get(segment.end) === 1]);
|
|
122
|
+
}
|
|
123
|
+
const open = lineEnds.some(([start, end]) => start || end);
|
|
124
|
+
const strokeCentered = network.regions.length === 0 && (open || node.strokeGeometry.length === 0);
|
|
125
|
+
if (geometry.length > 0) return {
|
|
126
|
+
areas: geometry,
|
|
127
|
+
lines,
|
|
128
|
+
lineEnds,
|
|
129
|
+
strokeCentered
|
|
130
|
+
};
|
|
131
|
+
return {
|
|
132
|
+
areas: network.regions.length > 0 ? network.regions.map((region) => ({
|
|
133
|
+
windingRule: region.windingRule,
|
|
134
|
+
contours: region.loops.map((loop) => loopPoints(network, loop, tolerance))
|
|
135
|
+
})) : [{
|
|
136
|
+
windingRule: "NONZERO",
|
|
137
|
+
contours: segmentRuns(network, tolerance)
|
|
138
|
+
}],
|
|
139
|
+
lines,
|
|
140
|
+
lineEnds,
|
|
141
|
+
strokeCentered
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* The outline a click is tested against, in the node's own coordinates, with curves within
|
|
146
|
+
* `tolerance` of the shape; null for layers tested by their box, such as text.
|
|
147
|
+
*/
|
|
148
|
+
function shapeOutline(node, tolerance) {
|
|
149
|
+
switch (node.type) {
|
|
150
|
+
case "RECTANGLE":
|
|
151
|
+
case "ROUNDED_RECTANGLE": {
|
|
152
|
+
const outline = fromContours([rectangleContour(node, tolerance)]);
|
|
153
|
+
return node.independentStrokeWeights ? {
|
|
154
|
+
...outline,
|
|
155
|
+
...rectangleSides(node, tolerance)
|
|
156
|
+
} : outline;
|
|
157
|
+
}
|
|
158
|
+
case "ELLIPSE":
|
|
159
|
+
if (node.fillGeometry.length > 0) return vectorOutline(node, tolerance);
|
|
160
|
+
return fromContours(ellipseContours(node, tolerance));
|
|
161
|
+
case "POLYGON":
|
|
162
|
+
case "STAR": return fromContours(commandContours(polygonOutline(node), tolerance));
|
|
163
|
+
case "LINE": return {
|
|
164
|
+
areas: [],
|
|
165
|
+
lines: [[{
|
|
166
|
+
x: 0,
|
|
167
|
+
y: 0
|
|
168
|
+
}, {
|
|
169
|
+
x: node.width,
|
|
170
|
+
y: node.height
|
|
171
|
+
}]],
|
|
172
|
+
lineEnds: [[true, true]]
|
|
173
|
+
};
|
|
174
|
+
case "VECTOR": return vectorOutline(node, tolerance);
|
|
175
|
+
default: return null;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
//#endregion
|
|
179
|
+
export { shapeOutline };
|
|
180
|
+
|
|
181
|
+
//# sourceMappingURL=shapes.js.map
|