@pairdraw/scene-graph 0.19.2 → 0.21.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/color/index.d.ts +28 -7
- package/dist/color/index.d.ts.map +1 -1
- package/dist/color/index.js +40 -15
- package/dist/color/index.js.map +1 -1
- package/dist/color/management.d.ts +19 -6
- package/dist/color/management.d.ts.map +1 -1
- package/dist/color/management.js +56 -1
- package/dist/color/management.js.map +1 -1
- package/dist/color/profile.d.ts +32 -0
- package/dist/color/profile.d.ts.map +1 -0
- package/dist/color/profile.js +140 -0
- package/dist/color/profile.js.map +1 -0
- package/dist/comments/index.d.ts +2 -2
- package/dist/comments/index.js +2 -2
- package/dist/comments/merge.d.ts.map +1 -1
- package/dist/comments/merge.js +1 -0
- package/dist/comments/merge.js.map +1 -1
- package/dist/comments/schema.d.ts +3 -1
- package/dist/comments/schema.d.ts.map +1 -1
- package/dist/comments/schema.js +19 -2
- package/dist/comments/schema.js.map +1 -1
- package/dist/comments/threads.d.ts +3 -1
- package/dist/comments/threads.d.ts.map +1 -1
- package/dist/comments/threads.js +2 -1
- package/dist/comments/threads.js.map +1 -1
- package/dist/components/index.d.ts +2 -2
- package/dist/components/index.js +2 -2
- package/dist/components/properties.d.ts.map +1 -1
- package/dist/components/properties.js +7 -7
- package/dist/components/properties.js.map +1 -1
- package/dist/components/variants.d.ts +12 -1
- package/dist/components/variants.d.ts.map +1 -1
- package/dist/components/variants.js +30 -1
- package/dist/components/variants.js.map +1 -1
- package/dist/constants.d.ts +25 -1
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +94 -1
- package/dist/constants.js.map +1 -1
- package/dist/copy.d.ts.map +1 -1
- package/dist/copy.js +17 -1
- package/dist/copy.js.map +1 -1
- package/dist/css/index.d.ts +2 -2
- package/dist/css/index.js +2 -2
- package/dist/css/shadows.d.ts +2 -2
- package/dist/css/shadows.d.ts.map +1 -1
- package/dist/css/shadows.js +4 -4
- package/dist/css/shadows.js.map +1 -1
- package/dist/css/text.d.ts +17 -1
- package/dist/css/text.d.ts.map +1 -1
- package/dist/css/text.js +62 -1
- package/dist/css/text.js.map +1 -1
- package/dist/css/values.d.ts +7 -2
- package/dist/css/values.d.ts.map +1 -1
- package/dist/css/values.js +8 -4
- package/dist/css/values.js.map +1 -1
- package/dist/design-memory/index.d.ts +3 -0
- package/dist/design-memory/index.js +3 -0
- package/dist/design-memory/model.d.ts +33 -0
- package/dist/design-memory/model.d.ts.map +1 -0
- package/dist/design-memory/model.js +142 -0
- package/dist/design-memory/model.js.map +1 -0
- package/dist/design-memory/schema.d.ts +76 -0
- package/dist/design-memory/schema.d.ts.map +1 -0
- package/dist/design-memory/schema.js +72 -0
- package/dist/design-memory/schema.js.map +1 -0
- package/dist/effects.d.ts +7 -4
- package/dist/effects.d.ts.map +1 -1
- package/dist/effects.js +9 -4
- package/dist/effects.js.map +1 -1
- package/dist/empty-values.js +102 -0
- package/dist/empty-values.js.map +1 -0
- package/dist/fields/components.d.ts +9 -0
- package/dist/fields/components.d.ts.map +1 -0
- package/dist/fields/components.js +24 -0
- package/dist/fields/components.js.map +1 -0
- package/dist/fields/index.d.ts +3 -2
- package/dist/fields/index.js +3 -2
- package/dist/fields/layout.d.ts +3 -1
- package/dist/fields/layout.d.ts.map +1 -1
- package/dist/fields/layout.js +10 -1
- package/dist/fields/layout.js.map +1 -1
- package/dist/graph.d.ts +12 -1
- package/dist/graph.d.ts.map +1 -1
- package/dist/graph.js +39 -7
- package/dist/graph.js.map +1 -1
- package/dist/grid-values.d.ts +10 -0
- package/dist/grid-values.d.ts.map +1 -0
- package/dist/grid-values.js +53 -0
- package/dist/grid-values.js.map +1 -0
- package/dist/hit-test/drop.d.ts +20 -0
- package/dist/hit-test/drop.d.ts.map +1 -0
- package/dist/hit-test/drop.js +54 -0
- package/dist/hit-test/drop.js.map +1 -0
- package/dist/hit-test/geometry.js +86 -0
- package/dist/hit-test/geometry.js.map +1 -0
- package/dist/hit-test/masks.d.ts +14 -0
- package/dist/hit-test/masks.d.ts.map +1 -0
- package/dist/hit-test/masks.js +41 -0
- package/dist/hit-test/masks.js.map +1 -0
- package/dist/hit-test/paint.d.ts +6 -0
- package/dist/hit-test/paint.d.ts.map +1 -0
- package/dist/hit-test/paint.js +19 -0
- package/dist/hit-test/paint.js.map +1 -0
- package/dist/hit-test.d.ts +4 -15
- package/dist/hit-test.d.ts.map +1 -1
- package/dist/hit-test.js +5 -173
- package/dist/hit-test.js.map +1 -1
- package/dist/images.d.ts +3 -2
- package/dist/images.d.ts.map +1 -1
- package/dist/images.js +18 -2
- package/dist/images.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/instances/copies.js +2 -1
- package/dist/instances/copies.js.map +1 -1
- package/dist/instances/fields.d.ts.map +1 -1
- package/dist/instances/fields.js +2 -0
- package/dist/instances/fields.js.map +1 -1
- package/dist/instances/index.d.ts +2 -1
- package/dist/instances/index.d.ts.map +1 -1
- package/dist/instances/index.js +13 -12
- package/dist/instances/index.js.map +1 -1
- package/dist/instances/overrides.d.ts +15 -1
- package/dist/instances/overrides.d.ts.map +1 -1
- package/dist/instances/overrides.js +37 -1
- package/dist/instances/overrides.js.map +1 -1
- package/dist/instances/source-walk.d.ts +12 -0
- package/dist/instances/source-walk.d.ts.map +1 -0
- package/dist/instances/swap-overrides.js +3 -3
- package/dist/instances/swap-overrides.js.map +1 -1
- package/dist/instances/sync.js +9 -7
- package/dist/instances/sync.js.map +1 -1
- package/dist/node-defaults.d.ts.map +1 -1
- package/dist/node-defaults.js +43 -38
- package/dist/node-defaults.js.map +1 -1
- package/dist/plugin-data/fields.d.ts +90 -0
- package/dist/plugin-data/fields.d.ts.map +1 -1
- package/dist/plugin-data/fields.js +33 -24
- package/dist/plugin-data/fields.js.map +1 -1
- package/dist/plugin-data/index.d.ts +2 -2
- package/dist/plugin-data/index.js +2 -2
- package/dist/plugin-data/schema.d.ts +6 -1
- package/dist/plugin-data/schema.d.ts.map +1 -1
- package/dist/plugin-data/schema.js +24 -1
- package/dist/plugin-data/schema.js.map +1 -1
- package/dist/preview.d.ts.map +1 -1
- package/dist/preview.js +2 -0
- package/dist/preview.js.map +1 -1
- package/dist/scaling/node.d.ts.map +1 -1
- package/dist/scaling/node.js +5 -1
- package/dist/scaling/node.js.map +1 -1
- package/dist/shaders/animation.d.ts +21 -0
- package/dist/shaders/animation.d.ts.map +1 -0
- package/dist/shaders/animation.js +25 -0
- package/dist/shaders/animation.js.map +1 -0
- package/dist/shaders/index.d.ts +2 -1
- package/dist/shaders/index.js +2 -1
- package/dist/slots/content.d.ts.map +1 -1
- package/dist/slots/content.js +4 -4
- package/dist/slots/content.js.map +1 -1
- package/dist/statuses/index.d.ts +2 -2
- package/dist/statuses/index.js +2 -2
- package/dist/statuses/workflow.d.ts +3 -1
- package/dist/statuses/workflow.d.ts.map +1 -1
- package/dist/statuses/workflow.js +6 -2
- package/dist/statuses/workflow.js.map +1 -1
- package/dist/token-sources/document.d.ts +26 -0
- package/dist/token-sources/document.d.ts.map +1 -0
- package/dist/token-sources/document.js +80 -0
- package/dist/token-sources/document.js.map +1 -0
- package/dist/token-sources/index.d.ts +3 -0
- package/dist/token-sources/index.js +3 -0
- package/dist/token-sources/schema.d.ts +43 -0
- package/dist/token-sources/schema.d.ts.map +1 -0
- package/dist/token-sources/schema.js +50 -0
- package/dist/token-sources/schema.js.map +1 -0
- package/dist/transfer/variables.d.ts.map +1 -1
- package/dist/transfer/variables.js +1 -1
- package/dist/transfer/variables.js.map +1 -1
- package/dist/types.d.ts +135 -15
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/variables/bindings.d.ts +6 -5
- package/dist/variables/bindings.d.ts.map +1 -1
- package/dist/variables/bindings.js +24 -16
- package/dist/variables/bindings.js.map +1 -1
- package/dist/variables/formula/index.d.ts.map +1 -1
- package/dist/variables/formula/index.js +12 -6
- package/dist/variables/formula/index.js.map +1 -1
- package/dist/variables/index.d.ts +14 -1
- package/dist/variables/index.d.ts.map +1 -1
- package/dist/variables/index.js +38 -10
- package/dist/variables/index.js.map +1 -1
- package/dist/variables/paints.d.ts +9 -8
- package/dist/variables/paints.d.ts.map +1 -1
- package/dist/variables/paints.js +35 -26
- package/dist/variables/paints.js.map +1 -1
- package/dist/variables/resolver.d.ts +45 -0
- package/dist/variables/resolver.d.ts.map +1 -0
- package/dist/variables/resolver.js +106 -0
- package/dist/variables/resolver.js.map +1 -0
- package/dist/variations/copy.d.ts +16 -0
- package/dist/variations/copy.d.ts.map +1 -0
- package/dist/variations/copy.js +78 -0
- package/dist/variations/copy.js.map +1 -0
- package/dist/variations/index.d.ts +6 -0
- package/dist/variations/index.js +6 -0
- package/dist/variations/match.d.ts +23 -0
- package/dist/variations/match.d.ts.map +1 -0
- package/dist/variations/match.js +80 -0
- package/dist/variations/match.js.map +1 -0
- package/dist/variations/model.d.ts +39 -0
- package/dist/variations/model.d.ts.map +1 -0
- package/dist/variations/model.js +90 -0
- package/dist/variations/model.js.map +1 -0
- package/dist/variations/pick.d.ts +43 -0
- package/dist/variations/pick.d.ts.map +1 -0
- package/dist/variations/pick.js +211 -0
- package/dist/variations/pick.js.map +1 -0
- package/dist/variations/schema.d.ts +27 -0
- package/dist/variations/schema.d.ts.map +1 -0
- package/dist/variations/schema.js +27 -0
- package/dist/variations/schema.js.map +1 -0
- package/package.json +21 -1
- package/src/color/index.ts +43 -9
- package/src/color/management.ts +54 -1
- package/src/color/profile.ts +131 -0
- package/src/comments/merge.ts +3 -0
- package/src/comments/schema.ts +32 -2
- package/src/comments/threads.ts +5 -2
- package/src/components/properties.ts +13 -7
- package/src/components/variants.ts +39 -0
- package/src/constants.ts +75 -0
- package/src/copy.ts +20 -1
- package/src/css/index.ts +7 -1
- package/src/css/shadows.ts +8 -5
- package/src/css/text.ts +62 -0
- package/src/css/values.ts +12 -5
- package/src/design-memory/index.ts +2 -0
- package/src/design-memory/model.ts +143 -0
- package/src/design-memory/schema.ts +84 -0
- package/src/effects.ts +16 -7
- package/src/empty-values.ts +117 -0
- package/src/fields/components.ts +21 -0
- package/src/fields/index.ts +1 -0
- package/src/fields/layout.ts +10 -0
- package/src/graph.ts +50 -6
- package/src/grid-values.ts +47 -0
- package/src/hit-test/drop.ts +90 -0
- package/src/hit-test/geometry.ts +131 -0
- package/src/hit-test/masks.ts +57 -0
- package/src/hit-test/paint.ts +24 -0
- package/src/hit-test.ts +8 -277
- package/src/images.ts +27 -3
- package/src/instances/copies.ts +2 -1
- package/src/instances/fields.ts +2 -0
- package/src/instances/index.ts +20 -11
- package/src/instances/overrides.ts +49 -0
- package/src/instances/swap-overrides.ts +3 -2
- package/src/instances/sync.ts +11 -7
- package/src/node-defaults.ts +51 -41
- package/src/plugin-data/fields.ts +34 -32
- package/src/plugin-data/index.ts +1 -0
- package/src/plugin-data/schema.ts +30 -0
- package/src/preview.ts +2 -0
- package/src/scaling/node.ts +7 -2
- package/src/shaders/animation.ts +39 -0
- package/src/shaders/index.ts +1 -0
- package/src/slots/content.ts +8 -4
- package/src/statuses/workflow.ts +6 -4
- package/src/token-sources/document.ts +104 -0
- package/src/token-sources/index.ts +2 -0
- package/src/token-sources/schema.ts +59 -0
- package/src/transfer/variables.ts +2 -1
- package/src/types.ts +145 -14
- package/src/variables/bindings.ts +35 -18
- package/src/variables/formula/index.ts +19 -18
- package/src/variables/index.ts +49 -10
- package/src/variables/paints.ts +64 -29
- package/src/variables/resolver.ts +178 -0
- package/src/variations/copy.ts +89 -0
- package/src/variations/index.ts +5 -0
- package/src/variations/match.ts +100 -0
- package/src/variations/model.ts +132 -0
- package/src/variations/pick.ts +293 -0
- package/src/variations/schema.ts +29 -0
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import Matrix from "../matrix.js";
|
|
2
|
+
import { getWorldMatrix } from "../coordinate.js";
|
|
3
|
+
import { geometryBlobPolylines } from "../geometry.js";
|
|
4
|
+
//#region src/hit-test/geometry.ts
|
|
5
|
+
/**
|
|
6
|
+
* How far from a zero-width or zero-height layer, in canvas units, a point still lands on it, so
|
|
7
|
+
* a flat line or an empty box stays clickable; matches a connector line's slop (`connectors/hit.ts`).
|
|
8
|
+
*/
|
|
9
|
+
const DEGENERATE_AXIS_SLOP = 4;
|
|
10
|
+
function hasTransformedAncestor(node, graph, cache) {
|
|
11
|
+
const cached = cache.get(node.id);
|
|
12
|
+
if (cached !== void 0) return cached;
|
|
13
|
+
const parent = node.parentId ? graph.getNode(node.parentId) : void 0;
|
|
14
|
+
const transformed = node.rotation !== 0 || node.flipX || node.flipY || (parent ? hasTransformedAncestor(parent, graph, cache) : false);
|
|
15
|
+
cache.set(node.id, transformed);
|
|
16
|
+
return transformed;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Where the point lies in the node's own coordinates: its offset from the position when nothing
|
|
20
|
+
* above it is turned, else through the inverse world matrix. Null when that cannot be inverted.
|
|
21
|
+
*/
|
|
22
|
+
function localPoint(px, py, node, graph, transformCache) {
|
|
23
|
+
if (!hasTransformedAncestor(node, graph, transformCache)) {
|
|
24
|
+
const absolute = graph.getAbsolutePosition(node.id);
|
|
25
|
+
return {
|
|
26
|
+
x: px - absolute.x,
|
|
27
|
+
y: py - absolute.y
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
const inv = Matrix.invert(getWorldMatrix(node, graph));
|
|
31
|
+
if (!inv) return null;
|
|
32
|
+
const [x, y] = Matrix.mapPoints(inv, [px, py]);
|
|
33
|
+
return {
|
|
34
|
+
x,
|
|
35
|
+
y
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
/** Whether the point lies in the node's box, grown by `pad` canvas units on every side. */
|
|
39
|
+
function containsPoint(px, py, node, graph, transformCache, pad = 0) {
|
|
40
|
+
const local = localPoint(px, py, node, graph, transformCache);
|
|
41
|
+
if (!local) return false;
|
|
42
|
+
const padX = node.width > 0 ? pad : Math.max(pad, DEGENERATE_AXIS_SLOP);
|
|
43
|
+
const padY = node.height > 0 ? pad : Math.max(pad, DEGENERATE_AXIS_SLOP);
|
|
44
|
+
return local.x >= -padX && local.x <= node.width + padX && local.y >= -padY && local.y <= node.height + padY;
|
|
45
|
+
}
|
|
46
|
+
/** Whether a rounded rectangle of `width` × `height` holds the point. */
|
|
47
|
+
function roundedRectContains(point, width, height, radius) {
|
|
48
|
+
if (point.x < 0 || point.y < 0 || point.x > width || point.y > height) return false;
|
|
49
|
+
const r = Math.max(0, Math.min(radius, width / 2, height / 2));
|
|
50
|
+
const cx = Math.min(Math.max(point.x, r), width - r);
|
|
51
|
+
const cy = Math.min(Math.max(point.y, r), height - r);
|
|
52
|
+
return Math.hypot(point.x - cx, point.y - cy) <= r;
|
|
53
|
+
}
|
|
54
|
+
/** Whether the point is inside a closed polyline, by the even-odd rule. */
|
|
55
|
+
function insidePolyline(polyline, x, y) {
|
|
56
|
+
let inside = false;
|
|
57
|
+
for (let i = 0, j = polyline.length - 1; i < polyline.length; j = i++) {
|
|
58
|
+
const a = polyline[i];
|
|
59
|
+
const b = polyline[j];
|
|
60
|
+
if (a.y > y === b.y > y) continue;
|
|
61
|
+
if (x < (b.x - a.x) * (y - a.y) / (b.y - a.y) + a.x) inside = !inside;
|
|
62
|
+
}
|
|
63
|
+
return inside;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Whether a mask lets the point through, by the outline it draws: an ellipse's curve, a rounded
|
|
67
|
+
* rectangle's corners, or a vector's filled path. Other shapes clip by their box.
|
|
68
|
+
*/
|
|
69
|
+
function maskContainsPoint(px, py, mask, graph, transformCache) {
|
|
70
|
+
if (!(mask.type === "ELLIPSE" && mask.width > 0 && mask.height > 0 || mask.type === "RECTANGLE" && mask.cornerRadius > 0 || mask.type === "VECTOR" && mask.fillGeometry.length > 0)) return containsPoint(px, py, mask, graph, transformCache);
|
|
71
|
+
const local = localPoint(px, py, mask, graph, transformCache);
|
|
72
|
+
if (!local) return false;
|
|
73
|
+
if (mask.type === "ELLIPSE") {
|
|
74
|
+
const rx = mask.width / 2;
|
|
75
|
+
const ry = mask.height / 2;
|
|
76
|
+
const dx = (local.x - rx) / rx;
|
|
77
|
+
const dy = (local.y - ry) / ry;
|
|
78
|
+
return dx * dx + dy * dy <= 1;
|
|
79
|
+
}
|
|
80
|
+
if (mask.type === "RECTANGLE") return roundedRectContains(local, mask.width, mask.height, mask.cornerRadius);
|
|
81
|
+
return geometryBlobPolylines(mask.fillGeometry).some((points) => insidePolyline(points, local.x, local.y));
|
|
82
|
+
}
|
|
83
|
+
//#endregion
|
|
84
|
+
export { containsPoint, localPoint, maskContainsPoint };
|
|
85
|
+
|
|
86
|
+
//# sourceMappingURL=geometry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"geometry.js","names":[],"sources":["../../src/hit-test/geometry.ts"],"sourcesContent":["// Where a point lies against a layer's box and a mask's outline, in the layer's own coordinates.\nimport { getWorldMatrix } from '../coordinate'\nimport { geometryBlobPolylines } from '../geometry'\nimport type { SceneGraph } from '../graph'\nimport Matrix from '../matrix'\nimport type { Vector } from '../primitives'\nimport type { SceneNode } from '../types'\n\n/**\n * How far from a zero-width or zero-height layer, in canvas units, a point still lands on it, so\n * a flat line or an empty box stays clickable; matches a connector line's slop (`connectors/hit.ts`).\n */\nconst DEGENERATE_AXIS_SLOP = 4\n\nfunction hasTransformedAncestor(\n node: SceneNode,\n graph: SceneGraph,\n cache: Map<string, boolean>\n): boolean {\n const cached = cache.get(node.id)\n if (cached !== undefined) return cached\n const parent = node.parentId ? graph.getNode(node.parentId) : undefined\n const transformed =\n node.rotation !== 0 ||\n node.flipX ||\n node.flipY ||\n (parent ? hasTransformedAncestor(parent, graph, cache) : false)\n cache.set(node.id, transformed)\n return transformed\n}\n\n/**\n * Where the point lies in the node's own coordinates: its offset from the position when nothing\n * above it is turned, else through the inverse world matrix. Null when that cannot be inverted.\n */\nexport function localPoint(\n px: number,\n py: number,\n node: SceneNode,\n graph: SceneGraph,\n transformCache: Map<string, boolean>\n): Vector | null {\n if (!hasTransformedAncestor(node, graph, transformCache)) {\n const absolute = graph.getAbsolutePosition(node.id)\n return { x: px - absolute.x, y: py - absolute.y }\n }\n const inv = Matrix.invert(getWorldMatrix(node, graph))\n if (!inv) return null\n const [x, y] = Matrix.mapPoints(inv, [px, py])\n return { x, y }\n}\n\n/** Whether the point lies in the node's box, grown by `pad` canvas units on every side. */\nexport function containsPoint(\n px: number,\n py: number,\n node: SceneNode,\n graph: SceneGraph,\n transformCache: Map<string, boolean>,\n pad = 0\n): boolean {\n const local = localPoint(px, py, node, graph, transformCache)\n if (!local) return false\n const padX = node.width > 0 ? pad : Math.max(pad, DEGENERATE_AXIS_SLOP)\n const padY = node.height > 0 ? pad : Math.max(pad, DEGENERATE_AXIS_SLOP)\n return (\n local.x >= -padX &&\n local.x <= node.width + padX &&\n local.y >= -padY &&\n local.y <= node.height + padY\n )\n}\n\n/** Whether a rounded rectangle of `width` × `height` holds the point. */\nfunction roundedRectContains(\n point: Vector,\n width: number,\n height: number,\n radius: number\n): boolean {\n if (point.x < 0 || point.y < 0 || point.x > width || point.y > height) return false\n const r = Math.max(0, Math.min(radius, width / 2, height / 2))\n const cx = Math.min(Math.max(point.x, r), width - r)\n const cy = Math.min(Math.max(point.y, r), height - r)\n return Math.hypot(point.x - cx, point.y - cy) <= r\n}\n\n/** Whether the point is inside a closed polyline, by the even-odd rule. */\nfunction insidePolyline(polyline: readonly Vector[], x: number, y: number): boolean {\n let inside = false\n for (let i = 0, j = polyline.length - 1; i < polyline.length; j = i++) {\n const a = polyline[i]\n const b = polyline[j]\n if (a.y > y === b.y > y) continue\n if (x < ((b.x - a.x) * (y - a.y)) / (b.y - a.y) + a.x) inside = !inside\n }\n return inside\n}\n\n/**\n * Whether a mask lets the point through, by the outline it draws: an ellipse's curve, a rounded\n * rectangle's corners, or a vector's filled path. Other shapes clip by their box.\n */\nexport function maskContainsPoint(\n px: number,\n py: number,\n mask: SceneNode,\n graph: SceneGraph,\n transformCache: Map<string, boolean>\n): boolean {\n const outlined =\n (mask.type === 'ELLIPSE' && mask.width > 0 && mask.height > 0) ||\n (mask.type === 'RECTANGLE' && mask.cornerRadius > 0) ||\n (mask.type === 'VECTOR' && mask.fillGeometry.length > 0)\n if (!outlined) return containsPoint(px, py, mask, graph, transformCache)\n const local = localPoint(px, py, mask, graph, transformCache)\n if (!local) return false\n if (mask.type === 'ELLIPSE') {\n const rx = mask.width / 2\n const ry = mask.height / 2\n const dx = (local.x - rx) / rx\n const dy = (local.y - ry) / ry\n return dx * dx + dy * dy <= 1\n }\n if (mask.type === 'RECTANGLE') {\n return roundedRectContains(local, mask.width, mask.height, mask.cornerRadius)\n }\n return geometryBlobPolylines(mask.fillGeometry).some((points) =>\n insidePolyline(points, local.x, local.y)\n )\n}\n"],"mappings":";;;;;;;;AAYA,MAAM,uBAAuB;AAE7B,SAAS,uBACP,MACA,OACA,OACS;CACT,MAAM,SAAS,MAAM,IAAI,KAAK,EAAE;CAChC,IAAI,WAAW,KAAA,GAAW,OAAO;CACjC,MAAM,SAAS,KAAK,WAAW,MAAM,QAAQ,KAAK,QAAQ,IAAI,KAAA;CAC9D,MAAM,cACJ,KAAK,aAAa,KAClB,KAAK,SACL,KAAK,UACJ,SAAS,uBAAuB,QAAQ,OAAO,KAAK,IAAI;CAC3D,MAAM,IAAI,KAAK,IAAI,WAAW;CAC9B,OAAO;AACT;;;;;AAMA,SAAgB,WACd,IACA,IACA,MACA,OACA,gBACe;CACf,IAAI,CAAC,uBAAuB,MAAM,OAAO,cAAc,GAAG;EACxD,MAAM,WAAW,MAAM,oBAAoB,KAAK,EAAE;EAClD,OAAO;GAAE,GAAG,KAAK,SAAS;GAAG,GAAG,KAAK,SAAS;EAAE;CAClD;CACA,MAAM,MAAM,OAAO,OAAO,eAAe,MAAM,KAAK,CAAC;CACrD,IAAI,CAAC,KAAK,OAAO;CACjB,MAAM,CAAC,GAAG,KAAK,OAAO,UAAU,KAAK,CAAC,IAAI,EAAE,CAAC;CAC7C,OAAO;EAAE;EAAG;CAAE;AAChB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gB,kBACd,IACA,IACA,MACA,OACA,gBACS;CAKT,IAAI,EAHD,KAAK,SAAS,aAAa,KAAK,QAAQ,KAAK,KAAK,SAAS,KAC3D,KAAK,SAAS,eAAe,KAAK,eAAe,KACjD,KAAK,SAAS,YAAY,KAAK,aAAa,SAAS,IACzC,OAAO,cAAc,IAAI,IAAI,MAAM,OAAO,cAAc;CACvE,MAAM,QAAQ,WAAW,IAAI,IAAI,MAAM,OAAO,cAAc;CAC5D,IAAI,CAAC,OAAO,OAAO;CACnB,IAAI,KAAK,SAAS,WAAW;EAC3B,MAAM,KAAK,KAAK,QAAQ;EACxB,MAAM,KAAK,KAAK,SAAS;EACzB,MAAM,MAAM,MAAM,IAAI,MAAM;EAC5B,MAAM,MAAM,MAAM,IAAI,MAAM;EAC5B,OAAO,KAAK,KAAK,KAAK,MAAM;CAC9B;CACA,IAAI,KAAK,SAAS,aAChB,OAAO,oBAAoB,OAAO,KAAK,OAAO,KAAK,QAAQ,KAAK,YAAY;CAE9E,OAAO,sBAAsB,KAAK,YAAY,CAAC,CAAC,MAAM,WACpD,eAAe,QAAQ,MAAM,GAAG,MAAM,CAAC,CACzC;AACF"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { SceneGraph } from "../graph.js";
|
|
2
|
+
import { SceneNode } from "../types.js";
|
|
3
|
+
//#region src/hit-test/masks.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* The masks that clip each child, by index, as the renderer groups them (`core/canvas/masks.ts`):
|
|
6
|
+
* a run of visible masks clips the layers after it up to the next visible mask. Empty when the
|
|
7
|
+
* container has no visible mask.
|
|
8
|
+
*/
|
|
9
|
+
declare function clippingMasks(graph: SceneGraph, childIds: readonly string[]): SceneNode[][];
|
|
10
|
+
/** Whether a point lies outside every mask of a run that clips a layer, so the layer hides it. */
|
|
11
|
+
declare function isPointMaskedOut(graph: SceneGraph, masks: readonly SceneNode[] | undefined, px: number, py: number): boolean;
|
|
12
|
+
//#endregion
|
|
13
|
+
export { clippingMasks, isPointMaskedOut };
|
|
14
|
+
//# sourceMappingURL=masks.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"masks.d.ts","names":[],"sources":["../../src/hit-test/masks.ts"],"mappings":";;;;;;;;iBAcgB,cAAc,OAAO,YAAY,8BAA8B;;iBAmC/D,iBACd,OAAO,YACP,gBAAgB,yBAChB,YACA"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { maskContainsPoint } from "./geometry.js";
|
|
2
|
+
//#region src/hit-test/masks.ts
|
|
3
|
+
function isVisibleMask(node) {
|
|
4
|
+
return node !== void 0 && node.visible && node.isMask;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* The masks that clip each child, by index, as the renderer groups them (`core/canvas/masks.ts`):
|
|
8
|
+
* a run of visible masks clips the layers after it up to the next visible mask. Empty when the
|
|
9
|
+
* container has no visible mask.
|
|
10
|
+
*/
|
|
11
|
+
function clippingMasks(graph, childIds) {
|
|
12
|
+
const children = childIds.map((childId) => graph.nodes.get(childId));
|
|
13
|
+
if (!children.some(isVisibleMask)) return [];
|
|
14
|
+
const result = [];
|
|
15
|
+
let run = [];
|
|
16
|
+
let previousWasMask = false;
|
|
17
|
+
for (const child of children) {
|
|
18
|
+
if (isVisibleMask(child)) {
|
|
19
|
+
run = previousWasMask ? [...run, child] : [child];
|
|
20
|
+
previousWasMask = true;
|
|
21
|
+
result.push([]);
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
previousWasMask = false;
|
|
25
|
+
result.push(run);
|
|
26
|
+
}
|
|
27
|
+
return result;
|
|
28
|
+
}
|
|
29
|
+
/** Whether the masks that clip a layer leave the point outside every one of their outlines. */
|
|
30
|
+
function isMaskedOut(px, py, masks, graph, transformCache) {
|
|
31
|
+
if (!masks) return false;
|
|
32
|
+
return masks.length > 0 && !masks.some((mask) => maskContainsPoint(px, py, mask, graph, transformCache));
|
|
33
|
+
}
|
|
34
|
+
/** Whether a point lies outside every mask of a run that clips a layer, so the layer hides it. */
|
|
35
|
+
function isPointMaskedOut(graph, masks, px, py) {
|
|
36
|
+
return isMaskedOut(px, py, masks, graph, /* @__PURE__ */ new Map());
|
|
37
|
+
}
|
|
38
|
+
//#endregion
|
|
39
|
+
export { clippingMasks, isMaskedOut, isPointMaskedOut };
|
|
40
|
+
|
|
41
|
+
//# sourceMappingURL=masks.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"masks.js","names":[],"sources":["../../src/hit-test/masks.ts"],"sourcesContent":["// The masks that clip a container's children, and whether they hide a point.\nimport type { SceneGraph } from '../graph'\nimport type { SceneNode } from '../types'\nimport { maskContainsPoint } from './geometry'\n\nfunction isVisibleMask(node: SceneNode | undefined): node is SceneNode {\n return node !== undefined && node.visible && node.isMask\n}\n\n/**\n * The masks that clip each child, by index, as the renderer groups them (`core/canvas/masks.ts`):\n * a run of visible masks clips the layers after it up to the next visible mask. Empty when the\n * container has no visible mask.\n */\nexport function clippingMasks(graph: SceneGraph, childIds: readonly string[]): SceneNode[][] {\n const children = childIds.map((childId) => graph.nodes.get(childId))\n if (!children.some(isVisibleMask)) return []\n const result: SceneNode[][] = []\n let run: SceneNode[] = []\n let previousWasMask = false\n for (const child of children) {\n if (isVisibleMask(child)) {\n run = previousWasMask ? [...run, child] : [child]\n previousWasMask = true\n result.push([])\n continue\n }\n previousWasMask = false\n result.push(run)\n }\n return result\n}\n\n/** Whether the masks that clip a layer leave the point outside every one of their outlines. */\nexport function isMaskedOut(\n px: number,\n py: number,\n masks: readonly SceneNode[] | undefined,\n graph: SceneGraph,\n transformCache: Map<string, boolean>\n): boolean {\n if (!masks) return false\n return (\n masks.length > 0 &&\n !masks.some((mask) => maskContainsPoint(px, py, mask, graph, transformCache))\n )\n}\n\n/** Whether a point lies outside every mask of a run that clips a layer, so the layer hides it. */\nexport function isPointMaskedOut(\n graph: SceneGraph,\n masks: readonly SceneNode[] | undefined,\n px: number,\n py: number\n): boolean {\n return isMaskedOut(px, py, masks, graph, new Map())\n}\n"],"mappings":";;AAKA,SAAS,cAAc,MAAgD;CACrE,OAAO,SAAS,KAAA,KAAa,KAAK,WAAW,KAAK;AACpD;;;;;;AAOA,SAAgB,cAAc,OAAmB,UAA4C;CAC3F,MAAM,WAAW,SAAS,KAAK,YAAY,MAAM,MAAM,IAAI,OAAO,CAAC;CACnE,IAAI,CAAC,SAAS,KAAK,aAAa,GAAG,OAAO,CAAC;CAC3C,MAAM,SAAwB,CAAC;CAC/B,IAAI,MAAmB,CAAC;CACxB,IAAI,kBAAkB;CACtB,KAAK,MAAM,SAAS,UAAU;EAC5B,IAAI,cAAc,KAAK,GAAG;GACxB,MAAM,kBAAkB,CAAC,GAAG,KAAK,KAAK,IAAI,CAAC,KAAK;GAChD,kBAAkB;GAClB,OAAO,KAAK,CAAC,CAAC;GACd;EACF;EACA,kBAAkB;EAClB,OAAO,KAAK,GAAG;CACjB;CACA,OAAO;AACT;;AAGA,SAAgB,YACd,IACA,IACA,OACA,OACA,gBACS;CACT,IAAI,CAAC,OAAO,OAAO;CACnB,OACE,MAAM,SAAS,KACf,CAAC,MAAM,MAAM,SAAS,kBAAkB,IAAI,IAAI,MAAM,OAAO,cAAc,CAAC;AAEhF;;AAGA,SAAgB,iBACd,OACA,OACA,IACA,IACS;CACT,OAAO,YAAY,IAAI,IAAI,OAAO,uBAAO,IAAI,IAAI,CAAC;AACpD"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"paint.d.ts","names":[],"sources":["../../src/hit-test/paint.ts"],"mappings":";;iBAqBgB,WAAW,MAAM"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
//#region src/hit-test/paint.ts
|
|
2
|
+
/**
|
|
3
|
+
* Whether a paint shows, so its container's empty area counts when a click reaches past
|
|
4
|
+
* containers: a paint at 0% opacity, or a gradient whose stops are all transparent, does not,
|
|
5
|
+
* while 1% does, and the layer's own opacity does not matter.
|
|
6
|
+
*/
|
|
7
|
+
function isHitPaint(paint) {
|
|
8
|
+
if (!paint.visible || !(paint.opacity > 0)) return false;
|
|
9
|
+
if (paint.type === "SOLID") return paint.color.a > 0;
|
|
10
|
+
if (paint.type === "GRADIENT_LINEAR" || paint.type === "GRADIENT_RADIAL" || paint.type === "GRADIENT_ANGULAR" || paint.type === "GRADIENT_DIAMOND") return (paint.gradientStops ?? []).some((stop) => stop.color.a > 0);
|
|
11
|
+
return true;
|
|
12
|
+
}
|
|
13
|
+
function showsPaint(node) {
|
|
14
|
+
return node.fills.some(isHitPaint) || node.strokes.some(isHitPaint);
|
|
15
|
+
}
|
|
16
|
+
//#endregion
|
|
17
|
+
export { showsPaint };
|
|
18
|
+
|
|
19
|
+
//# sourceMappingURL=paint.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"paint.js","names":[],"sources":["../../src/hit-test/paint.ts"],"sourcesContent":["// Which layers show a paint where a pointer lands, so their area takes it.\nimport type { SceneNode } from '../types'\n\n/**\n * Whether a paint shows, so its container's empty area counts when a click reaches past\n * containers: a paint at 0% opacity, or a gradient whose stops are all transparent, does not,\n * while 1% does, and the layer's own opacity does not matter.\n */\nfunction isHitPaint(paint: SceneNode['fills'][number]): boolean {\n if (!paint.visible || !(paint.opacity > 0)) return false\n if (paint.type === 'SOLID') return paint.color.a > 0\n if (\n paint.type === 'GRADIENT_LINEAR' ||\n paint.type === 'GRADIENT_RADIAL' ||\n paint.type === 'GRADIENT_ANGULAR' ||\n paint.type === 'GRADIENT_DIAMOND'\n )\n return (paint.gradientStops ?? []).some((stop) => stop.color.a > 0)\n return true\n}\n\nexport function showsPaint(node: SceneNode): boolean {\n return node.fills.some(isHitPaint) || node.strokes.some(isHitPaint)\n}\n"],"mappings":";;;;;;AAQA,SAAS,WAAW,OAA4C;CAC9D,IAAI,CAAC,MAAM,WAAW,EAAE,MAAM,UAAU,IAAI,OAAO;CACnD,IAAI,MAAM,SAAS,SAAS,OAAO,MAAM,MAAM,IAAI;CACnD,IACE,MAAM,SAAS,qBACf,MAAM,SAAS,qBACf,MAAM,SAAS,sBACf,MAAM,SAAS,oBAEf,QAAQ,MAAM,iBAAiB,CAAC,EAAA,CAAG,MAAM,SAAS,KAAK,MAAM,IAAI,CAAC;CACpE,OAAO;AACT;AAEA,SAAgB,WAAW,MAA0B;CACnD,OAAO,KAAK,MAAM,KAAK,UAAU,KAAK,KAAK,QAAQ,KAAK,UAAU;AACpE"}
|
package/dist/hit-test.d.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import { Rect } from "./primitives/index.js";
|
|
2
|
+
import { clippingMasks, isPointMaskedOut } from "./hit-test/masks.js";
|
|
3
|
+
import { DropTargetOptions, hitTestDropTarget } from "./hit-test/drop.js";
|
|
4
|
+
import { showsPaint } from "./hit-test/paint.js";
|
|
2
5
|
import { SceneGraph } from "./graph.js";
|
|
3
6
|
import { SceneNode } from "./types.js";
|
|
4
7
|
//#region src/hit-test.d.ts
|
|
@@ -32,20 +35,6 @@ declare function hitTestOpenContainer(graph: SceneGraph, px: number, py: number,
|
|
|
32
35
|
declare function hitTestDeepInRect(graph: SceneGraph, rect: Rect, scopeId: string): SceneNode[];
|
|
33
36
|
/** Whether the point lies inside the node's rotated and flipped bounds. */
|
|
34
37
|
declare function isPointInNode(graph: SceneGraph, nodeId: string, px: number, py: number): boolean;
|
|
35
|
-
interface DropTargetOptions {
|
|
36
|
-
/**
|
|
37
|
-
* Set when the dropped layers are all main components: the component sets they come from.
|
|
38
|
-
* Components then go only into frames, sections, instances, and those sets.
|
|
39
|
-
*/
|
|
40
|
-
componentSetIds?: ReadonlySet<string>;
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* The topmost unlocked frame, section, component, or instance under the point, in its rotated
|
|
44
|
-
* shape and inside its clipping ancestors. Groups and component sets are looked through but never
|
|
45
|
-
* returned, except that a set takes dropped components; locked layers and their contents are
|
|
46
|
-
* skipped.
|
|
47
|
-
*/
|
|
48
|
-
declare function hitTestDropTarget(graph: SceneGraph, px: number, py: number, excludeIds: ReadonlySet<string>, scopeId?: string, options?: DropTargetOptions): SceneNode | null;
|
|
49
38
|
//#endregion
|
|
50
|
-
export { DropTargetOptions, hitTest, hitTestDeep, hitTestDeepInRect, hitTestDropTarget, hitTestOpenContainer, hitTestSelectable, isOpenContainer, isPointInNode };
|
|
39
|
+
export { type DropTargetOptions, clippingMasks, hitTest, hitTestDeep, hitTestDeepInRect, hitTestDropTarget, hitTestOpenContainer, hitTestSelectable, isOpenContainer, isPointInNode, isPointMaskedOut, showsPaint };
|
|
51
40
|
//# sourceMappingURL=hit-test.d.ts.map
|
package/dist/hit-test.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hit-test.d.ts","names":[],"sources":["../src/hit-test.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"hit-test.d.ts","names":[],"sources":["../src/hit-test.ts"],"mappings":";;;;;;;iBA6NgB,QACd,OAAO,YACP,YACA,YACA,mBACC;iBAKa,YACd,OAAO,YACP,YACA,YACA,kBACA,eACC;;;;;;;;;iBA6Ea,kBACd,OAAO,YACP,YACA,YACA,iBACA,aAAa,qBACb,cAEA;;AAAA,mBACC;;iBA6Ba,gBAAgB,OAAO,YAAY;;;;;iBASnC,qBACd,OAAO,YACP,YACA,YACA,kBACC;;;;;;;;iBA0Ea,kBAAkB,OAAO,YAAY,MAAM,MAAM,kBAAkB;;iBASnE,cAAc,OAAO,YAAY,gBAAgB,YAAY"}
|
package/dist/hit-test.js
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import Matrix from "./matrix.js";
|
|
2
2
|
import { getAxisAlignedBoundsAt, getNodeLocalMatrix, getWorldMatrix } from "./coordinate.js";
|
|
3
|
-
import { geometryBlobPolylines } from "./geometry.js";
|
|
4
3
|
import { hitsConnectorLine } from "./connectors/hit.js";
|
|
4
|
+
import { containsPoint } from "./hit-test/geometry.js";
|
|
5
|
+
import { clippingMasks, isMaskedOut, isPointMaskedOut } from "./hit-test/masks.js";
|
|
6
|
+
import { showsPaint } from "./hit-test/paint.js";
|
|
5
7
|
import { drawnChildIds } from "./layout/draw-order.js";
|
|
8
|
+
import { hitTestDropTarget } from "./hit-test/drop.js";
|
|
6
9
|
//#region src/hit-test.ts
|
|
7
10
|
const CONTAINER_TYPES = /* @__PURE__ */ new Set([
|
|
8
11
|
"CANVAS",
|
|
@@ -15,25 +18,6 @@ const CONTAINER_TYPES = /* @__PURE__ */ new Set([
|
|
|
15
18
|
]);
|
|
16
19
|
const OPAQUE_CONTAINER_TYPES = /* @__PURE__ */ new Set(["COMPONENT", "INSTANCE"]);
|
|
17
20
|
/**
|
|
18
|
-
* How far from a zero-width or zero-height layer, in canvas units, a point still lands on it, so
|
|
19
|
-
* a flat line or an empty box stays clickable; matches a connector line's slop (`connectors/hit.ts`).
|
|
20
|
-
*/
|
|
21
|
-
const DEGENERATE_AXIS_SLOP = 4;
|
|
22
|
-
/**
|
|
23
|
-
* Whether a paint shows, so its container's empty area counts when a click reaches past
|
|
24
|
-
* containers: a paint at 0% opacity, or a gradient whose stops are all transparent, does not,
|
|
25
|
-
* while 1% does, and the layer's own opacity does not matter.
|
|
26
|
-
*/
|
|
27
|
-
function isHitPaint(paint) {
|
|
28
|
-
if (!paint.visible || !(paint.opacity > 0)) return false;
|
|
29
|
-
if (paint.type === "SOLID") return paint.color.a > 0;
|
|
30
|
-
if (paint.type === "GRADIENT_LINEAR" || paint.type === "GRADIENT_RADIAL" || paint.type === "GRADIENT_ANGULAR" || paint.type === "GRADIENT_DIAMOND") return (paint.gradientStops ?? []).some((stop) => stop.color.a > 0);
|
|
31
|
-
return true;
|
|
32
|
-
}
|
|
33
|
-
function showsPaint(node) {
|
|
34
|
-
return node.fills.some(isHitPaint) || node.strokes.some(isHitPaint);
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
21
|
* Whether a container's empty area takes the click. Figma counts it only where a paint shows,
|
|
38
22
|
* except a main component's, which always counts, and an instance's when the click is a plain one.
|
|
39
23
|
*/
|
|
@@ -54,105 +38,6 @@ function isTopLevel(graph, node) {
|
|
|
54
38
|
function ownsItsEmptyArea(graph, node) {
|
|
55
39
|
return node.type === "FRAME" && node.layoutMode !== "NONE" && isTopLevel(graph, node);
|
|
56
40
|
}
|
|
57
|
-
function hasTransformedAncestor(node, graph, cache) {
|
|
58
|
-
const cached = cache.get(node.id);
|
|
59
|
-
if (cached !== void 0) return cached;
|
|
60
|
-
const parent = node.parentId ? graph.getNode(node.parentId) : void 0;
|
|
61
|
-
const transformed = node.rotation !== 0 || node.flipX || node.flipY || (parent ? hasTransformedAncestor(parent, graph, cache) : false);
|
|
62
|
-
cache.set(node.id, transformed);
|
|
63
|
-
return transformed;
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* Where the point lies in the node's own coordinates: its offset from the position when nothing
|
|
67
|
-
* above it is turned, else through the inverse world matrix. Null when that cannot be inverted.
|
|
68
|
-
*/
|
|
69
|
-
function localPoint(px, py, node, graph, transformCache) {
|
|
70
|
-
if (!hasTransformedAncestor(node, graph, transformCache)) {
|
|
71
|
-
const absolute = graph.getAbsolutePosition(node.id);
|
|
72
|
-
return {
|
|
73
|
-
x: px - absolute.x,
|
|
74
|
-
y: py - absolute.y
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
const inv = Matrix.invert(getWorldMatrix(node, graph));
|
|
78
|
-
if (!inv) return null;
|
|
79
|
-
const [x, y] = Matrix.mapPoints(inv, [px, py]);
|
|
80
|
-
return {
|
|
81
|
-
x,
|
|
82
|
-
y
|
|
83
|
-
};
|
|
84
|
-
}
|
|
85
|
-
/** Whether the point lies in the node's box, grown by `pad` canvas units on every side. */
|
|
86
|
-
function containsPoint(px, py, node, graph, transformCache, pad = 0) {
|
|
87
|
-
const local = localPoint(px, py, node, graph, transformCache);
|
|
88
|
-
if (!local) return false;
|
|
89
|
-
const padX = node.width > 0 ? pad : Math.max(pad, DEGENERATE_AXIS_SLOP);
|
|
90
|
-
const padY = node.height > 0 ? pad : Math.max(pad, DEGENERATE_AXIS_SLOP);
|
|
91
|
-
return local.x >= -padX && local.x <= node.width + padX && local.y >= -padY && local.y <= node.height + padY;
|
|
92
|
-
}
|
|
93
|
-
/** Whether a rounded rectangle of `width` × `height` holds the point. */
|
|
94
|
-
function roundedRectContains(point, width, height, radius) {
|
|
95
|
-
if (point.x < 0 || point.y < 0 || point.x > width || point.y > height) return false;
|
|
96
|
-
const r = Math.max(0, Math.min(radius, width / 2, height / 2));
|
|
97
|
-
const cx = Math.min(Math.max(point.x, r), width - r);
|
|
98
|
-
const cy = Math.min(Math.max(point.y, r), height - r);
|
|
99
|
-
return Math.hypot(point.x - cx, point.y - cy) <= r;
|
|
100
|
-
}
|
|
101
|
-
/** Whether the point is inside a closed polyline, by the even-odd rule. */
|
|
102
|
-
function insidePolyline(polyline, x, y) {
|
|
103
|
-
let inside = false;
|
|
104
|
-
for (let i = 0, j = polyline.length - 1; i < polyline.length; j = i++) {
|
|
105
|
-
const a = polyline[i];
|
|
106
|
-
const b = polyline[j];
|
|
107
|
-
if (a.y > y === b.y > y) continue;
|
|
108
|
-
if (x < (b.x - a.x) * (y - a.y) / (b.y - a.y) + a.x) inside = !inside;
|
|
109
|
-
}
|
|
110
|
-
return inside;
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
|
-
* Whether a mask lets the point through, by the outline it draws: an ellipse's curve, a rounded
|
|
114
|
-
* rectangle's corners, or a vector's filled path. Other shapes clip by their box.
|
|
115
|
-
*/
|
|
116
|
-
function maskContainsPoint(px, py, mask, graph, transformCache) {
|
|
117
|
-
if (!(mask.type === "ELLIPSE" && mask.width > 0 && mask.height > 0 || mask.type === "RECTANGLE" && mask.cornerRadius > 0 || mask.type === "VECTOR" && mask.fillGeometry.length > 0)) return containsPoint(px, py, mask, graph, transformCache);
|
|
118
|
-
const local = localPoint(px, py, mask, graph, transformCache);
|
|
119
|
-
if (!local) return false;
|
|
120
|
-
if (mask.type === "ELLIPSE") {
|
|
121
|
-
const rx = mask.width / 2;
|
|
122
|
-
const ry = mask.height / 2;
|
|
123
|
-
const dx = (local.x - rx) / rx;
|
|
124
|
-
const dy = (local.y - ry) / ry;
|
|
125
|
-
return dx * dx + dy * dy <= 1;
|
|
126
|
-
}
|
|
127
|
-
if (mask.type === "RECTANGLE") return roundedRectContains(local, mask.width, mask.height, mask.cornerRadius);
|
|
128
|
-
return geometryBlobPolylines(mask.fillGeometry).some((points) => insidePolyline(points, local.x, local.y));
|
|
129
|
-
}
|
|
130
|
-
function isVisibleMask(node) {
|
|
131
|
-
return node !== void 0 && node.visible && node.isMask;
|
|
132
|
-
}
|
|
133
|
-
/**
|
|
134
|
-
* The masks that clip each child, by index, as the renderer groups them (`core/canvas/masks.ts`):
|
|
135
|
-
* a run of visible masks clips the layers after it up to the next visible mask. Empty when the
|
|
136
|
-
* container has no visible mask.
|
|
137
|
-
*/
|
|
138
|
-
function clippingMasks(graph, childIds) {
|
|
139
|
-
const children = childIds.map((childId) => graph.nodes.get(childId));
|
|
140
|
-
if (!children.some(isVisibleMask)) return [];
|
|
141
|
-
const result = [];
|
|
142
|
-
let run = [];
|
|
143
|
-
let previousWasMask = false;
|
|
144
|
-
for (const child of children) {
|
|
145
|
-
if (isVisibleMask(child)) {
|
|
146
|
-
run = previousWasMask ? [...run, child] : [child];
|
|
147
|
-
previousWasMask = true;
|
|
148
|
-
result.push([]);
|
|
149
|
-
continue;
|
|
150
|
-
}
|
|
151
|
-
previousWasMask = false;
|
|
152
|
-
result.push(run);
|
|
153
|
-
}
|
|
154
|
-
return result;
|
|
155
|
-
}
|
|
156
41
|
/**
|
|
157
42
|
* What a click inside a container reports. `opaque` is an instance or component whose body takes
|
|
158
43
|
* the click without looking through it; every other container looks through to its children first.
|
|
@@ -182,11 +67,6 @@ function hitTestContainer(graph, px, py, child, childId, deep, transformCache, p
|
|
|
182
67
|
function standIn(container, hit, deep) {
|
|
183
68
|
return container.locked || !deep ? container : hit;
|
|
184
69
|
}
|
|
185
|
-
/** Whether the masks that clip a layer leave the point outside every one of their outlines. */
|
|
186
|
-
function isMaskedOut(px, py, masks, graph, transformCache) {
|
|
187
|
-
if (!masks) return false;
|
|
188
|
-
return masks.length > 0 && !masks.some((mask) => maskContainsPoint(px, py, mask, graph, transformCache));
|
|
189
|
-
}
|
|
190
70
|
/**
|
|
191
71
|
* The topmost layer under the point among a container's children. `pad`, in canvas units, grows
|
|
192
72
|
* each leaf layer's box, as Figma takes a click a few screen pixels outside a shape.
|
|
@@ -391,55 +271,7 @@ function isPointInNode(graph, nodeId, px, py) {
|
|
|
391
271
|
const node = graph.nodes.get(nodeId);
|
|
392
272
|
return node !== void 0 && containsPoint(px, py, node, graph, /* @__PURE__ */ new Map());
|
|
393
273
|
}
|
|
394
|
-
const LAYER_TARGETS = /* @__PURE__ */ new Set([
|
|
395
|
-
"FRAME",
|
|
396
|
-
"SECTION",
|
|
397
|
-
"COMPONENT",
|
|
398
|
-
"INSTANCE"
|
|
399
|
-
]);
|
|
400
|
-
const COMPONENT_TARGETS = /* @__PURE__ */ new Set([
|
|
401
|
-
"FRAME",
|
|
402
|
-
"SECTION",
|
|
403
|
-
"INSTANCE"
|
|
404
|
-
]);
|
|
405
|
-
function dropRules(options) {
|
|
406
|
-
const { componentSetIds } = options;
|
|
407
|
-
if (!componentSetIds) return {
|
|
408
|
-
takes: (node) => LAYER_TARGETS.has(node.type),
|
|
409
|
-
passThrough: /* @__PURE__ */ new Set(["GROUP", "COMPONENT_SET"])
|
|
410
|
-
};
|
|
411
|
-
return {
|
|
412
|
-
takes: (node) => COMPONENT_TARGETS.has(node.type) || node.type === "COMPONENT_SET" && componentSetIds.has(node.id),
|
|
413
|
-
passThrough: /* @__PURE__ */ new Set(["GROUP"])
|
|
414
|
-
};
|
|
415
|
-
}
|
|
416
|
-
function dropTargetIn(graph, px, py, parent, excludeIds, rules, transformCache) {
|
|
417
|
-
if (parent.type !== "CANVAS" && parent.clipsContent && !containsPoint(px, py, parent, graph, transformCache)) return null;
|
|
418
|
-
for (let i = parent.childIds.length - 1; i >= 0; i--) {
|
|
419
|
-
const childId = parent.childIds[i];
|
|
420
|
-
if (excludeIds.has(childId)) continue;
|
|
421
|
-
const child = graph.nodes.get(childId);
|
|
422
|
-
if (!child || child.internalOnly || !child.visible || child.locked) continue;
|
|
423
|
-
const target = rules.takes(child);
|
|
424
|
-
if (!target && !rules.passThrough.has(child.type)) continue;
|
|
425
|
-
const deeper = dropTargetIn(graph, px, py, child, excludeIds, rules, transformCache);
|
|
426
|
-
if (deeper) return deeper;
|
|
427
|
-
if (target && containsPoint(px, py, child, graph, transformCache)) return child;
|
|
428
|
-
}
|
|
429
|
-
return null;
|
|
430
|
-
}
|
|
431
|
-
/**
|
|
432
|
-
* The topmost unlocked frame, section, component, or instance under the point, in its rotated
|
|
433
|
-
* shape and inside its clipping ancestors. Groups and component sets are looked through but never
|
|
434
|
-
* returned, except that a set takes dropped components; locked layers and their contents are
|
|
435
|
-
* skipped.
|
|
436
|
-
*/
|
|
437
|
-
function hitTestDropTarget(graph, px, py, excludeIds, scopeId, options = {}) {
|
|
438
|
-
const scope = graph.nodes.get(scopeId ?? graph.rootId);
|
|
439
|
-
if (!scope) return null;
|
|
440
|
-
return dropTargetIn(graph, px, py, scope, excludeIds, dropRules(options), /* @__PURE__ */ new Map());
|
|
441
|
-
}
|
|
442
274
|
//#endregion
|
|
443
|
-
export { hitTest, hitTestDeep, hitTestDeepInRect, hitTestDropTarget, hitTestOpenContainer, hitTestSelectable, isOpenContainer, isPointInNode };
|
|
275
|
+
export { clippingMasks, hitTest, hitTestDeep, hitTestDeepInRect, hitTestDropTarget, hitTestOpenContainer, hitTestSelectable, isOpenContainer, isPointInNode, isPointMaskedOut, showsPaint };
|
|
444
276
|
|
|
445
277
|
//# sourceMappingURL=hit-test.js.map
|
package/dist/hit-test.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hit-test.js","names":[],"sources":["../src/hit-test.ts"],"sourcesContent":["import { hitsConnectorLine } from './connectors/hit'\nimport { getAxisAlignedBoundsAt, getNodeLocalMatrix, getWorldMatrix } from './coordinate'\nimport { geometryBlobPolylines } from './geometry'\nimport type { SceneGraph } from './graph'\nimport { drawnChildIds } from './layout/draw-order'\nimport Matrix, { type Mat3 } from './matrix'\nimport type { Rect, Vector } from './primitives'\nimport type { SceneNode, NodeType } from './types'\n\nconst CONTAINER_TYPES = new Set<NodeType>([\n 'CANVAS',\n 'FRAME',\n 'GROUP',\n 'SECTION',\n 'COMPONENT',\n 'COMPONENT_SET',\n 'INSTANCE'\n])\nconst OPAQUE_CONTAINER_TYPES = new Set<NodeType>(['COMPONENT', 'INSTANCE'])\n/**\n * How far from a zero-width or zero-height layer, in canvas units, a point still lands on it, so\n * a flat line or an empty box stays clickable; matches a connector line's slop (`connectors/hit.ts`).\n */\nconst DEGENERATE_AXIS_SLOP = 4\n\n/**\n * Whether a paint shows, so its container's empty area counts when a click reaches past\n * containers: a paint at 0% opacity, or a gradient whose stops are all transparent, does not,\n * while 1% does, and the layer's own opacity does not matter.\n */\nfunction isHitPaint(paint: SceneNode['fills'][number]): boolean {\n if (!paint.visible || !(paint.opacity > 0)) return false\n if (paint.type === 'SOLID') return paint.color.a > 0\n if (\n paint.type === 'GRADIENT_LINEAR' ||\n paint.type === 'GRADIENT_RADIAL' ||\n paint.type === 'GRADIENT_ANGULAR' ||\n paint.type === 'GRADIENT_DIAMOND'\n )\n return (paint.gradientStops ?? []).some((stop) => stop.color.a > 0)\n return true\n}\n\nfunction showsPaint(node: SceneNode): boolean {\n return node.fills.some(isHitPaint) || node.strokes.some(isHitPaint)\n}\n\n/**\n * Whether a container's empty area takes the click. Figma counts it only where a paint shows,\n * except a main component's, which always counts, and an instance's when the click is a plain one.\n */\nfunction takesEmptyArea(graph: SceneGraph, node: SceneNode, instanceBodies: boolean): boolean {\n if (node.type === 'COMPONENT' || showsPaint(node) || ownsItsEmptyArea(graph, node)) return true\n return instanceBodies && node.type === 'INSTANCE'\n}\n\nfunction isTopLevel(graph: SceneGraph, node: SceneNode): boolean {\n const parent = node.parentId ? graph.nodes.get(node.parentId) : undefined\n return parent?.type === 'CANVAS' || parent?.type === 'SECTION'\n}\n\n/**\n * An open container whose empty area still belongs to it, as in Figma: a top-level frame with auto\n * layout is selected, hovered, and dragged by its gaps and padding, while its children stay one\n * click away, with or without a fill. A plain top-level frame's empty area is background, and a\n * nested frame's empty area belongs to it only as a click target (see `frameChildAt`).\n */\nfunction ownsItsEmptyArea(graph: SceneGraph, node: SceneNode): boolean {\n return node.type === 'FRAME' && node.layoutMode !== 'NONE' && isTopLevel(graph, node)\n}\n\nfunction hasTransformedAncestor(\n node: SceneNode,\n graph: SceneGraph,\n cache: Map<string, boolean>\n): boolean {\n const cached = cache.get(node.id)\n if (cached !== undefined) return cached\n const parent = node.parentId ? graph.getNode(node.parentId) : undefined\n const transformed =\n node.rotation !== 0 ||\n node.flipX ||\n node.flipY ||\n (parent ? hasTransformedAncestor(parent, graph, cache) : false)\n cache.set(node.id, transformed)\n return transformed\n}\n\n/**\n * Where the point lies in the node's own coordinates: its offset from the position when nothing\n * above it is turned, else through the inverse world matrix. Null when that cannot be inverted.\n */\nfunction localPoint(\n px: number,\n py: number,\n node: SceneNode,\n graph: SceneGraph,\n transformCache: Map<string, boolean>\n): Vector | null {\n if (!hasTransformedAncestor(node, graph, transformCache)) {\n const absolute = graph.getAbsolutePosition(node.id)\n return { x: px - absolute.x, y: py - absolute.y }\n }\n const inv = Matrix.invert(getWorldMatrix(node, graph))\n if (!inv) return null\n const [x, y] = Matrix.mapPoints(inv, [px, py])\n return { x, y }\n}\n\n/** Whether the point lies in the node's box, grown by `pad` canvas units on every side. */\nfunction containsPoint(\n px: number,\n py: number,\n node: SceneNode,\n graph: SceneGraph,\n transformCache: Map<string, boolean>,\n pad = 0\n): boolean {\n const local = localPoint(px, py, node, graph, transformCache)\n if (!local) return false\n const padX = node.width > 0 ? pad : Math.max(pad, DEGENERATE_AXIS_SLOP)\n const padY = node.height > 0 ? pad : Math.max(pad, DEGENERATE_AXIS_SLOP)\n return (\n local.x >= -padX &&\n local.x <= node.width + padX &&\n local.y >= -padY &&\n local.y <= node.height + padY\n )\n}\n\n/** Whether a rounded rectangle of `width` × `height` holds the point. */\nfunction roundedRectContains(\n point: Vector,\n width: number,\n height: number,\n radius: number\n): boolean {\n if (point.x < 0 || point.y < 0 || point.x > width || point.y > height) return false\n const r = Math.max(0, Math.min(radius, width / 2, height / 2))\n const cx = Math.min(Math.max(point.x, r), width - r)\n const cy = Math.min(Math.max(point.y, r), height - r)\n return Math.hypot(point.x - cx, point.y - cy) <= r\n}\n\n/** Whether the point is inside a closed polyline, by the even-odd rule. */\nfunction insidePolyline(polyline: readonly Vector[], x: number, y: number): boolean {\n let inside = false\n for (let i = 0, j = polyline.length - 1; i < polyline.length; j = i++) {\n const a = polyline[i]\n const b = polyline[j]\n if (a.y > y === b.y > y) continue\n if (x < ((b.x - a.x) * (y - a.y)) / (b.y - a.y) + a.x) inside = !inside\n }\n return inside\n}\n\n/**\n * Whether a mask lets the point through, by the outline it draws: an ellipse's curve, a rounded\n * rectangle's corners, or a vector's filled path. Other shapes clip by their box.\n */\nfunction maskContainsPoint(\n px: number,\n py: number,\n mask: SceneNode,\n graph: SceneGraph,\n transformCache: Map<string, boolean>\n): boolean {\n const outlined =\n (mask.type === 'ELLIPSE' && mask.width > 0 && mask.height > 0) ||\n (mask.type === 'RECTANGLE' && mask.cornerRadius > 0) ||\n (mask.type === 'VECTOR' && mask.fillGeometry.length > 0)\n if (!outlined) return containsPoint(px, py, mask, graph, transformCache)\n const local = localPoint(px, py, mask, graph, transformCache)\n if (!local) return false\n if (mask.type === 'ELLIPSE') {\n const rx = mask.width / 2\n const ry = mask.height / 2\n const dx = (local.x - rx) / rx\n const dy = (local.y - ry) / ry\n return dx * dx + dy * dy <= 1\n }\n if (mask.type === 'RECTANGLE') {\n return roundedRectContains(local, mask.width, mask.height, mask.cornerRadius)\n }\n return geometryBlobPolylines(mask.fillGeometry).some((points) =>\n insidePolyline(points, local.x, local.y)\n )\n}\n\nfunction isVisibleMask(node: SceneNode | undefined): node is SceneNode {\n return node !== undefined && node.visible && node.isMask\n}\n\n/**\n * The masks that clip each child, by index, as the renderer groups them (`core/canvas/masks.ts`):\n * a run of visible masks clips the layers after it up to the next visible mask. Empty when the\n * container has no visible mask.\n */\nfunction clippingMasks(graph: SceneGraph, childIds: readonly string[]): SceneNode[][] {\n const children = childIds.map((childId) => graph.nodes.get(childId))\n if (!children.some(isVisibleMask)) return []\n const result: SceneNode[][] = []\n let run: SceneNode[] = []\n let previousWasMask = false\n for (const child of children) {\n if (isVisibleMask(child)) {\n run = previousWasMask ? [...run, child] : [child]\n previousWasMask = true\n result.push([])\n continue\n }\n previousWasMask = false\n result.push(run)\n }\n return result\n}\n\n/**\n * What a click inside a container reports. `opaque` is an instance or component whose body takes\n * the click without looking through it; every other container looks through to its children first.\n */\nfunction hitTestContainer(\n graph: SceneGraph,\n px: number,\n py: number,\n child: SceneNode,\n childId: string,\n deep: boolean,\n transformCache: Map<string, boolean>,\n pad: number,\n locked = false,\n instanceBodies = false,\n opaque = false\n): SceneNode | null {\n if (opaque) {\n if (!containsPoint(px, py, child, graph, transformCache)) return null\n const childHit = hitTestChildren(\n graph,\n px,\n py,\n childId,\n deep,\n transformCache,\n pad,\n locked,\n instanceBodies\n )\n if (childHit) return child\n if (takesEmptyArea(graph, child, instanceBodies)) return child\n return null\n }\n if (child.type === 'GROUP') {\n // A group fits its layers, so a padded click on one at its edge falls just outside it.\n if (!containsPoint(px, py, child, graph, transformCache, pad)) return null\n const childHit = hitTestChildren(\n graph,\n px,\n py,\n childId,\n deep,\n transformCache,\n pad,\n locked,\n instanceBodies\n )\n if (childHit) return standIn(child, childHit, deep)\n return containsPoint(px, py, child, graph, transformCache) ? child : null\n }\n\n const childHit = hitTestChildren(\n graph,\n px,\n py,\n childId,\n deep,\n transformCache,\n pad,\n locked,\n instanceBodies\n )\n if (childHit) return standIn(child, childHit, true)\n\n if (\n containsPoint(px, py, child, graph, transformCache) &&\n takesEmptyArea(graph, child, instanceBodies)\n )\n return child\n return null\n}\n/**\n * What a hit inside a container reports: the container itself when it is locked (a locked layer,\n * where one is hit at all, stands in for everything inside it) or the walk stops at containers.\n */\nfunction standIn(container: SceneNode, hit: SceneNode, deep: boolean): SceneNode {\n return container.locked || !deep ? container : hit\n}\n\n/** Whether the masks that clip a layer leave the point outside every one of their outlines. */\nfunction isMaskedOut(\n px: number,\n py: number,\n masks: readonly SceneNode[] | undefined,\n graph: SceneGraph,\n transformCache: Map<string, boolean>\n): boolean {\n if (!masks) return false\n return (\n masks.length > 0 &&\n !masks.some((mask) => maskContainsPoint(px, py, mask, graph, transformCache))\n )\n}\n\n/**\n * The topmost layer under the point among a container's children. `pad`, in canvas units, grows\n * each leaf layer's box, as Figma takes a click a few screen pixels outside a shape.\n */\n/**\n * Whether a layer under the point can take the click. A locked layer and everything in it are\n * invisible to a press and to hover, as in Figma: a click falls through to what lies under it, and\n * a drag from it draws a marquee.\n */\nfunction takesClick(\n graph: SceneGraph,\n px: number,\n py: number,\n child: SceneNode | undefined,\n mask: readonly SceneNode[] | undefined,\n locked: boolean,\n transformCache: Map<string, boolean>\n): child is SceneNode {\n if (!child || child.internalOnly || !child.visible || (child.locked && !locked)) return false\n return !isMaskedOut(px, py, mask, graph, transformCache)\n}\n\nfunction hitTestChildren(\n graph: SceneGraph,\n px: number,\n py: number,\n parentId: string,\n deep = false,\n transformCache = new Map<string, boolean>(),\n pad = 0,\n locked = false,\n instanceBodies = false\n): SceneNode | null {\n const parent = graph.nodes.get(parentId)\n if (!parent) return null\n\n if (parent.clipsContent) {\n if (!containsPoint(px, py, parent, graph, transformCache)) return null\n }\n\n // Front to back, as the layers are drawn: a reversed auto layout puts the first child on top.\n const drawn = drawnChildIds(parent)\n const masks = clippingMasks(graph, drawn)\n for (let i = drawn.length - 1; i >= 0; i--) {\n const childId = drawn[i]\n const child = graph.nodes.get(childId)\n if (!takesClick(graph, px, py, child, masks[i], locked, transformCache)) continue\n if (CONTAINER_TYPES.has(child.type)) {\n if (OPAQUE_CONTAINER_TYPES.has(child.type) && !deep) {\n const hit = hitTestContainer(\n graph,\n px,\n py,\n child,\n childId,\n deep,\n transformCache,\n pad,\n locked,\n instanceBodies,\n true\n )\n if (hit) return hit\n continue\n }\n\n const hit = hitTestContainer(\n graph,\n px,\n py,\n child,\n childId,\n deep,\n transformCache,\n pad,\n locked,\n instanceBodies\n )\n if (hit) return hit\n continue\n }\n\n if (child.type === 'CONNECTOR') {\n if (hitsConnectorLine(graph, child, px, py, pad)) return child\n continue\n }\n if (containsPoint(px, py, child, graph, transformCache, pad)) return child\n }\n\n return null\n}\n\nexport function hitTest(\n graph: SceneGraph,\n px: number,\n py: number,\n scopeId?: string\n): SceneNode | null {\n const scope = scopeId ?? graph.rootId\n return hitTestChildren(graph, px, py, scope, false, new Map(), 0, false, true)\n}\n\nexport function hitTestDeep(\n graph: SceneGraph,\n px: number,\n py: number,\n scopeId?: string,\n pad = 0\n): SceneNode | null {\n const scope = scopeId ?? graph.rootId\n return hitTestChildren(graph, px, py, scope, true, new Map(), pad)\n}\n\n/**\n * A container a click looks into by itself, as in Figma: a top-level frame (on the page or in a\n * section) or a section that holds layers, and a component set.\n */\nfunction opensByItself(graph: SceneGraph, node: SceneNode): boolean {\n if (node.type === 'COMPONENT_SET') return true\n if (node.childIds.length === 0) return false\n if (node.type === 'SECTION') return true\n return node.type === 'FRAME' && isTopLevel(graph, node)\n}\n\n/**\n * The topmost frame among a container's children whose bounds hold the point, filled or not: in\n * Figma a click on the empty area of a frame inside an open container, or of an empty frame on the\n * page or in a section, selects that frame, while a deep (⌘) click looks through it.\n */\nfunction frameChildAt(\n graph: SceneGraph,\n container: SceneNode,\n px: number,\n py: number,\n locked = false\n): SceneNode | null {\n const transformCache = new Map<string, boolean>()\n for (let i = container.childIds.length - 1; i >= 0; i--) {\n const child = graph.nodes.get(container.childIds[i])\n if (!child || child.internalOnly || !child.visible || child.type !== 'FRAME') continue\n if (child.locked && !locked) continue\n // A board, such as a top-level frame in a section, keeps its empty area as background.\n if (opensByItself(graph, child) && !ownsItsEmptyArea(graph, child)) continue\n if (containsPoint(px, py, child, graph, transformCache)) return child\n }\n return null\n}\n\n/**\n * The containers the selection opens: every ancestor of a selected layer inside the scope, and a\n * selected container itself, so a click inside it selects the layer under the point, as in Figma.\n * A selected group or boolean stays closed: clicking inside it keeps it selected.\n */\nfunction openedBySelection(\n graph: SceneGraph,\n selectedIds: ReadonlySet<string>,\n scopeId: string\n): Set<string> {\n const ancestors = new Set<string>()\n for (const id of selectedIds) {\n const node = graph.nodes.get(id)\n if (\n node &&\n node.childIds.length > 0 &&\n node.type !== 'GROUP' &&\n node.type !== 'BOOLEAN_OPERATION'\n )\n ancestors.add(id)\n let parentId = node?.parentId\n while (parentId && parentId !== scopeId && !ancestors.has(parentId)) {\n ancestors.add(parentId)\n parentId = graph.nodes.get(parentId)?.parentId\n }\n }\n return ancestors\n}\n\n/**\n * The layer a click selects, as in Figma. It walks from the scope down to the deepest layer under\n * the point and stops at the first layer that is not open. Top-level frames and sections with\n * layers and component sets are open, and so is every ancestor of the selection, so clicks reach\n * the siblings of selected layers. Where every layer under the point is open, a container opened\n * by the selection is selected, so is a top-level frame with auto layout, and a plain top-level\n * frame or a section is not.\n */\nexport function hitTestSelectable(\n graph: SceneGraph,\n px: number,\n py: number,\n scopeId: string,\n selectedIds: ReadonlySet<string>,\n pad = 0,\n /** Whether locked layers are hit, as a right-click hits them; see `hitTestAtPoint`. */\n locked = false\n): SceneNode | null {\n const deepest = hitTestChildren(graph, px, py, scopeId, true, new Map(), pad, locked, true)\n if (!deepest) {\n const scope = graph.nodes.get(scopeId)\n return scope ? frameChildAt(graph, scope, px, py, locked) : null\n }\n\n const chain: SceneNode[] = []\n for (let node: SceneNode | undefined = deepest; node && node.id !== scopeId;) {\n chain.unshift(node)\n node = node.parentId ? graph.nodes.get(node.parentId) : undefined\n }\n\n const opened = openedBySelection(graph, selectedIds, scopeId)\n\n for (const node of chain) {\n // A locked layer, hit only with `locked`, stands in for everything inside it.\n if (node.locked) return node\n if (!opened.has(node.id) && !opensByItself(graph, node)) return node\n }\n const last = chain.at(-1)\n if (!last) return null\n const frame = frameChildAt(graph, last, px, py, locked)\n if (frame) return frame\n if (ownsItsEmptyArea(graph, last)) return last\n return opened.has(last.id) && !opensByItself(graph, last) ? last : null\n}\n\n/** Whether a click looks into this container rather than selecting it; see `hitTestSelectable`. */\nexport function isOpenContainer(graph: SceneGraph, nodeId: string): boolean {\n const node = graph.nodes.get(nodeId)\n return node !== undefined && opensByItself(graph, node)\n}\n\n/**\n * The innermost open container under the point when every layer there is open: a marquee starting\n * here selects that container's layers, as in Figma.\n */\nexport function hitTestOpenContainer(\n graph: SceneGraph,\n px: number,\n py: number,\n scopeId: string\n): SceneNode | null {\n const deepest = hitTestChildren(graph, px, py, scopeId, true, new Map(), 0, false, false)\n for (let node: SceneNode | undefined = deepest ?? undefined; node && node.id !== scopeId;) {\n if (!opensByItself(graph, node) || ownsItsEmptyArea(graph, node)) return null\n node = node.parentId ? graph.nodes.get(node.parentId) : undefined\n }\n return deepest\n}\n\nfunction rectsTouch(a: Rect, b: Rect): boolean {\n return a.x + a.width > b.x && a.x < b.x + b.width && a.y + a.height > b.y && a.y < b.y + b.height\n}\n\nfunction rectOverlap(a: Rect, b: Rect): Rect | null {\n if (!rectsTouch(a, b)) return null\n const x = Math.max(a.x, b.x)\n const y = Math.max(a.y, b.y)\n return {\n x,\n y,\n width: Math.min(a.x + a.width, b.x + b.width) - x,\n height: Math.min(a.y + a.height, b.y + b.height) - y\n }\n}\n\n/**\n * Whether a deep marquee touching a container, but none of its layers, selects it: where a deep\n * click there would select it (a group's box, a painted container, the empty area a top-level frame\n * with auto layout owns), except a board or section whose empty area a press treats as background.\n */\nfunction selectsByOwnArea(graph: SceneGraph, node: SceneNode): boolean {\n if (node.type === 'GROUP' || ownsItsEmptyArea(graph, node)) return true\n return showsPaint(node) && !opensByItself(graph, node)\n}\n\n/**\n * Collects the deepest layers among `parent`'s children that touch `rect`. Each child's world\n * matrix is composed from its parent's, so a walk over a large page climbs no ancestor chains.\n */\nfunction collectDeepestInRect(\n graph: SceneGraph,\n parent: SceneNode,\n parentWorld: Mat3,\n rect: Rect,\n hits: SceneNode[]\n) {\n // A clipping container draws nothing outside itself, so its layers meet only that part of the rect.\n const area =\n parent.clipsContent && parent.type !== 'CANVAS'\n ? rectOverlap(rect, getAxisAlignedBoundsAt(parent, parentWorld))\n : rect\n if (!area) return\n for (const childId of parent.childIds) {\n const child = graph.nodes.get(childId)\n if (!child || child.internalOnly || !child.visible || child.locked) continue\n const world = Matrix.multiply(parentWorld, getNodeLocalMatrix(child))\n const touches = rectsTouch(area, getAxisAlignedBoundsAt(child, world))\n if (!CONTAINER_TYPES.has(child.type) || child.childIds.length === 0) {\n if (touches) hits.push(child)\n continue\n }\n const before = hits.length\n collectDeepestInRect(graph, child, world, area, hits)\n if (hits.length === before && touches && selectsByOwnArea(graph, child)) hits.push(child)\n }\n}\n\n/**\n * The deepest layers a deep (⌘) marquee over `rect` selects, as in Figma, by the rules a deep click\n * follows: it looks into frames, groups, sections, components, component sets, and instances, keeps\n * a boolean's operands inside it, and skips hidden, locked, and internal layers with their contents.\n * A container is selected itself only where the rect touches its own area and none of its layers.\n * Layers are compared by their axis-aligned canvas bounds, as a plain marquee compares them.\n */\nexport function hitTestDeepInRect(graph: SceneGraph, rect: Rect, scopeId: string): SceneNode[] {\n const scope = graph.nodes.get(scopeId)\n if (!scope) return []\n const hits: SceneNode[] = []\n collectDeepestInRect(graph, scope, getWorldMatrix(scope, graph), rect, hits)\n return hits\n}\n\n/** Whether the point lies inside the node's rotated and flipped bounds. */\nexport function isPointInNode(graph: SceneGraph, nodeId: string, px: number, py: number): boolean {\n const node = graph.nodes.get(nodeId)\n return node !== undefined && containsPoint(px, py, node, graph, new Map())\n}\n\n/** Which layers take a drop, and which are only looked through, as in Figma. */\ninterface DropRules {\n takes: (node: SceneNode) => boolean\n passThrough: ReadonlySet<NodeType>\n}\n\nconst LAYER_TARGETS = new Set<NodeType>(['FRAME', 'SECTION', 'COMPONENT', 'INSTANCE'])\nconst COMPONENT_TARGETS = new Set<NodeType>(['FRAME', 'SECTION', 'INSTANCE'])\n\nfunction dropRules(options: DropTargetOptions): DropRules {\n const { componentSetIds } = options\n if (!componentSetIds) {\n return {\n takes: (node) => LAYER_TARGETS.has(node.type),\n passThrough: new Set(['GROUP', 'COMPONENT_SET'])\n }\n }\n // Components never go into other components, and a set only takes back its own variants.\n return {\n takes: (node) =>\n COMPONENT_TARGETS.has(node.type) ||\n (node.type === 'COMPONENT_SET' && componentSetIds.has(node.id)),\n passThrough: new Set(['GROUP'])\n }\n}\n\nfunction dropTargetIn(\n graph: SceneGraph,\n px: number,\n py: number,\n parent: SceneNode,\n excludeIds: ReadonlySet<string>,\n rules: DropRules,\n transformCache: Map<string, boolean>\n): SceneNode | null {\n // A clipped-away part of a child is not under the cursor.\n if (\n parent.type !== 'CANVAS' &&\n parent.clipsContent &&\n !containsPoint(px, py, parent, graph, transformCache)\n )\n return null\n\n for (let i = parent.childIds.length - 1; i >= 0; i--) {\n const childId = parent.childIds[i]\n if (excludeIds.has(childId)) continue\n const child = graph.nodes.get(childId)\n if (!child || child.internalOnly || !child.visible || child.locked) continue\n const target = rules.takes(child)\n if (!target && !rules.passThrough.has(child.type)) continue\n\n const deeper = dropTargetIn(graph, px, py, child, excludeIds, rules, transformCache)\n if (deeper) return deeper\n if (target && containsPoint(px, py, child, graph, transformCache)) return child\n }\n\n return null\n}\n\nexport interface DropTargetOptions {\n /**\n * Set when the dropped layers are all main components: the component sets they come from.\n * Components then go only into frames, sections, instances, and those sets.\n */\n componentSetIds?: ReadonlySet<string>\n}\n\n/**\n * The topmost unlocked frame, section, component, or instance under the point, in its rotated\n * shape and inside its clipping ancestors. Groups and component sets are looked through but never\n * returned, except that a set takes dropped components; locked layers and their contents are\n * skipped.\n */\nexport function hitTestDropTarget(\n graph: SceneGraph,\n px: number,\n py: number,\n excludeIds: ReadonlySet<string>,\n scopeId?: string,\n options: DropTargetOptions = {}\n): SceneNode | null {\n const scope = graph.nodes.get(scopeId ?? graph.rootId)\n if (!scope) return null\n return dropTargetIn(graph, px, py, scope, excludeIds, dropRules(options), new Map())\n}\n"],"mappings":";;;;;;AASA,MAAM,kCAAkB,IAAI,IAAc;CACxC;CACA;CACA;CACA;CACA;CACA;CACA;AACF,CAAC;AACD,MAAM,yCAAyB,IAAI,IAAc,CAAC,aAAa,UAAU,CAAC;;;;;AAK1E,MAAM,uBAAuB;;;;;;AAO7B,SAAS,WAAW,OAA4C;CAC9D,IAAI,CAAC,MAAM,WAAW,EAAE,MAAM,UAAU,IAAI,OAAO;CACnD,IAAI,MAAM,SAAS,SAAS,OAAO,MAAM,MAAM,IAAI;CACnD,IACE,MAAM,SAAS,qBACf,MAAM,SAAS,qBACf,MAAM,SAAS,sBACf,MAAM,SAAS,oBAEf,QAAQ,MAAM,iBAAiB,CAAC,EAAA,CAAG,MAAM,SAAS,KAAK,MAAM,IAAI,CAAC;CACpE,OAAO;AACT;AAEA,SAAS,WAAW,MAA0B;CAC5C,OAAO,KAAK,MAAM,KAAK,UAAU,KAAK,KAAK,QAAQ,KAAK,UAAU;AACpE;;;;;AAMA,SAAS,eAAe,OAAmB,MAAiB,gBAAkC;CAC5F,IAAI,KAAK,SAAS,eAAe,WAAW,IAAI,KAAK,iBAAiB,OAAO,IAAI,GAAG,OAAO;CAC3F,OAAO,kBAAkB,KAAK,SAAS;AACzC;AAEA,SAAS,WAAW,OAAmB,MAA0B;CAC/D,MAAM,SAAS,KAAK,WAAW,MAAM,MAAM,IAAI,KAAK,QAAQ,IAAI,KAAA;CAChE,OAAO,QAAQ,SAAS,YAAY,QAAQ,SAAS;AACvD;;;;;;;AAQA,SAAS,iBAAiB,OAAmB,MAA0B;CACrE,OAAO,KAAK,S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G,KAAK;EACpD,MAAM,UAAU,OAAO,SAAS;EAChC,IAAI,WAAW,IAAI,OAAO,GAAG;EAC7B,MAAM,QAAQ,MAAM,MAAM,IAAI,OAAO;EACrC,IAAI,CAAC,SAAS,MAAM,gBAAgB,CAAC,MAAM,WAAW,MAAM,QAAQ;EACpE,MAAM,SAAS,MAAM,MAAM,KAAK;EAChC,IAAI,CAAC,UAAU,CAAC,MAAM,YAAY,IAAI,MAAM,IAAI,GAAG;EAEnD,MAAM,SAAS,aAAa,OAAO,IAAI,IAAI,OAAO,YAAY,OAAO,cAAc;EACnF,IAAI,QAAQ,OAAO;EACnB,IAAI,UAAU,cAAc,IAAI,IAAI,OAAO,OAAO,cAAc,GAAG,OAAO;CAC5E;CAEA,OAAO;AACT;;;;;;;AAgBA,SAAgB,kBACd,OACA,IACA,IACA,YACA,SACA,UAA6B,CAAC,GACZ;CAClB,MAAM,QAAQ,MAAM,MAAM,IAAI,WAAW,MAAM,MAAM;CACrD,IAAI,CAAC,OAAO,OAAO;CACnB,OAAO,aAAa,OAAO,IAAI,IAAI,OAAO,YAAY,UAAU,OAAO,mBAAG,IAAI,IAAI,CAAC;AACrF"}
|
|
1
|
+
{"version":3,"file":"hit-test.js","names":[],"sources":["../src/hit-test.ts"],"sourcesContent":["import { hitsConnectorLine } from './connectors/hit'\nimport { getAxisAlignedBoundsAt, getNodeLocalMatrix, getWorldMatrix } from './coordinate'\nimport type { SceneGraph } from './graph'\nimport { containsPoint } from './hit-test/geometry'\nimport { clippingMasks, isMaskedOut } from './hit-test/masks'\nimport { showsPaint } from './hit-test/paint'\nimport { drawnChildIds } from './layout/draw-order'\nimport Matrix, { type Mat3 } from './matrix'\nimport type { Rect } from './primitives'\nimport type { SceneNode, NodeType } from './types'\n\nexport { clippingMasks, isPointMaskedOut } from './hit-test/masks'\nexport { hitTestDropTarget, type DropTargetOptions } from './hit-test/drop'\nexport { showsPaint } from './hit-test/paint'\n\nconst CONTAINER_TYPES = new Set<NodeType>([\n 'CANVAS',\n 'FRAME',\n 'GROUP',\n 'SECTION',\n 'COMPONENT',\n 'COMPONENT_SET',\n 'INSTANCE'\n])\nconst OPAQUE_CONTAINER_TYPES = new Set<NodeType>(['COMPONENT', 'INSTANCE'])\n/**\n * Whether a container's empty area takes the click. Figma counts it only where a paint shows,\n * except a main component's, which always counts, and an instance's when the click is a plain one.\n */\nfunction takesEmptyArea(graph: SceneGraph, node: SceneNode, instanceBodies: boolean): boolean {\n if (node.type === 'COMPONENT' || showsPaint(node) || ownsItsEmptyArea(graph, node)) return true\n return instanceBodies && node.type === 'INSTANCE'\n}\n\nfunction isTopLevel(graph: SceneGraph, node: SceneNode): boolean {\n const parent = node.parentId ? graph.nodes.get(node.parentId) : undefined\n return parent?.type === 'CANVAS' || parent?.type === 'SECTION'\n}\n\n/**\n * An open container whose empty area still belongs to it, as in Figma: a top-level frame with auto\n * layout is selected, hovered, and dragged by its gaps and padding, while its children stay one\n * click away, with or without a fill. A plain top-level frame's empty area is background, and a\n * nested frame's empty area belongs to it only as a click target (see `frameChildAt`).\n */\nfunction ownsItsEmptyArea(graph: SceneGraph, node: SceneNode): boolean {\n return node.type === 'FRAME' && node.layoutMode !== 'NONE' && isTopLevel(graph, node)\n}\n\n/**\n * What a click inside a container reports. `opaque` is an instance or component whose body takes\n * the click without looking through it; every other container looks through to its children first.\n */\nfunction hitTestContainer(\n graph: SceneGraph,\n px: number,\n py: number,\n child: SceneNode,\n childId: string,\n deep: boolean,\n transformCache: Map<string, boolean>,\n pad: number,\n locked = false,\n instanceBodies = false,\n opaque = false\n): SceneNode | null {\n if (opaque) {\n if (!containsPoint(px, py, child, graph, transformCache)) return null\n const childHit = hitTestChildren(\n graph,\n px,\n py,\n childId,\n deep,\n transformCache,\n pad,\n locked,\n instanceBodies\n )\n if (childHit) return child\n if (takesEmptyArea(graph, child, instanceBodies)) return child\n return null\n }\n if (child.type === 'GROUP') {\n // A group fits its layers, so a padded click on one at its edge falls just outside it.\n if (!containsPoint(px, py, child, graph, transformCache, pad)) return null\n const childHit = hitTestChildren(\n graph,\n px,\n py,\n childId,\n deep,\n transformCache,\n pad,\n locked,\n instanceBodies\n )\n if (childHit) return standIn(child, childHit, deep)\n return containsPoint(px, py, child, graph, transformCache) ? child : null\n }\n\n const childHit = hitTestChildren(\n graph,\n px,\n py,\n childId,\n deep,\n transformCache,\n pad,\n locked,\n instanceBodies\n )\n if (childHit) return standIn(child, childHit, true)\n\n if (\n containsPoint(px, py, child, graph, transformCache) &&\n takesEmptyArea(graph, child, instanceBodies)\n )\n return child\n return null\n}\n/**\n * What a hit inside a container reports: the container itself when it is locked (a locked layer,\n * where one is hit at all, stands in for everything inside it) or the walk stops at containers.\n */\nfunction standIn(container: SceneNode, hit: SceneNode, deep: boolean): SceneNode {\n return container.locked || !deep ? container : hit\n}\n\n/**\n * The topmost layer under the point among a container's children. `pad`, in canvas units, grows\n * each leaf layer's box, as Figma takes a click a few screen pixels outside a shape.\n */\n/**\n * Whether a layer under the point can take the click. A locked layer and everything in it are\n * invisible to a press and to hover, as in Figma: a click falls through to what lies under it, and\n * a drag from it draws a marquee.\n */\nfunction takesClick(\n graph: SceneGraph,\n px: number,\n py: number,\n child: SceneNode | undefined,\n mask: readonly SceneNode[] | undefined,\n locked: boolean,\n transformCache: Map<string, boolean>\n): child is SceneNode {\n if (!child || child.internalOnly || !child.visible || (child.locked && !locked)) return false\n return !isMaskedOut(px, py, mask, graph, transformCache)\n}\n\nfunction hitTestChildren(\n graph: SceneGraph,\n px: number,\n py: number,\n parentId: string,\n deep = false,\n transformCache = new Map<string, boolean>(),\n pad = 0,\n locked = false,\n instanceBodies = false\n): SceneNode | null {\n const parent = graph.nodes.get(parentId)\n if (!parent) return null\n\n if (parent.clipsContent) {\n if (!containsPoint(px, py, parent, graph, transformCache)) return null\n }\n\n // Front to back, as the layers are drawn: a reversed auto layout puts the first child on top.\n const drawn = drawnChildIds(parent)\n const masks = clippingMasks(graph, drawn)\n for (let i = drawn.length - 1; i >= 0; i--) {\n const childId = drawn[i]\n const child = graph.nodes.get(childId)\n if (!takesClick(graph, px, py, child, masks[i], locked, transformCache)) continue\n if (CONTAINER_TYPES.has(child.type)) {\n if (OPAQUE_CONTAINER_TYPES.has(child.type) && !deep) {\n const hit = hitTestContainer(\n graph,\n px,\n py,\n child,\n childId,\n deep,\n transformCache,\n pad,\n locked,\n instanceBodies,\n true\n )\n if (hit) return hit\n continue\n }\n\n const hit = hitTestContainer(\n graph,\n px,\n py,\n child,\n childId,\n deep,\n transformCache,\n pad,\n locked,\n instanceBodies\n )\n if (hit) return hit\n continue\n }\n\n if (child.type === 'CONNECTOR') {\n if (hitsConnectorLine(graph, child, px, py, pad)) return child\n continue\n }\n if (containsPoint(px, py, child, graph, transformCache, pad)) return child\n }\n\n return null\n}\n\nexport function hitTest(\n graph: SceneGraph,\n px: number,\n py: number,\n scopeId?: string\n): SceneNode | null {\n const scope = scopeId ?? graph.rootId\n return hitTestChildren(graph, px, py, scope, false, new Map(), 0, false, true)\n}\n\nexport function hitTestDeep(\n graph: SceneGraph,\n px: number,\n py: number,\n scopeId?: string,\n pad = 0\n): SceneNode | null {\n const scope = scopeId ?? graph.rootId\n return hitTestChildren(graph, px, py, scope, true, new Map(), pad)\n}\n\n/**\n * A container a click looks into by itself, as in Figma: a top-level frame (on the page or in a\n * section) or a section that holds layers, and a component set.\n */\nfunction opensByItself(graph: SceneGraph, node: SceneNode): boolean {\n if (node.type === 'COMPONENT_SET') return true\n if (node.childIds.length === 0) return false\n if (node.type === 'SECTION') return true\n return node.type === 'FRAME' && isTopLevel(graph, node)\n}\n\n/**\n * The topmost frame among a container's children whose bounds hold the point, filled or not: in\n * Figma a click on the empty area of a frame inside an open container, or of an empty frame on the\n * page or in a section, selects that frame, while a deep (⌘) click looks through it.\n */\nfunction frameChildAt(\n graph: SceneGraph,\n container: SceneNode,\n px: number,\n py: number,\n locked = false\n): SceneNode | null {\n const transformCache = new Map<string, boolean>()\n for (let i = container.childIds.length - 1; i >= 0; i--) {\n const child = graph.nodes.get(container.childIds[i])\n if (!child || child.internalOnly || !child.visible || child.type !== 'FRAME') continue\n if (child.locked && !locked) continue\n // A board, such as a top-level frame in a section, keeps its empty area as background.\n if (opensByItself(graph, child) && !ownsItsEmptyArea(graph, child)) continue\n if (containsPoint(px, py, child, graph, transformCache)) return child\n }\n return null\n}\n\n/**\n * The containers the selection opens: every ancestor of a selected layer inside the scope, and a\n * selected container itself, so a click inside it selects the layer under the point, as in Figma.\n * A selected group or boolean stays closed: clicking inside it keeps it selected.\n */\nfunction openedBySelection(\n graph: SceneGraph,\n selectedIds: ReadonlySet<string>,\n scopeId: string\n): Set<string> {\n const ancestors = new Set<string>()\n for (const id of selectedIds) {\n const node = graph.nodes.get(id)\n if (\n node &&\n node.childIds.length > 0 &&\n node.type !== 'GROUP' &&\n node.type !== 'BOOLEAN_OPERATION'\n )\n ancestors.add(id)\n let parentId = node?.parentId\n while (parentId && parentId !== scopeId && !ancestors.has(parentId)) {\n ancestors.add(parentId)\n parentId = graph.nodes.get(parentId)?.parentId\n }\n }\n return ancestors\n}\n\n/**\n * The layer a click selects, as in Figma. It walks from the scope down to the deepest layer under\n * the point and stops at the first layer that is not open. Top-level frames and sections with\n * layers and component sets are open, and so is every ancestor of the selection, so clicks reach\n * the siblings of selected layers. Where every layer under the point is open, a container opened\n * by the selection is selected, so is a top-level frame with auto layout, and a plain top-level\n * frame or a section is not.\n */\nexport function hitTestSelectable(\n graph: SceneGraph,\n px: number,\n py: number,\n scopeId: string,\n selectedIds: ReadonlySet<string>,\n pad = 0,\n /** Whether locked layers are hit, as a right-click hits them; see `hitTestAtPoint`. */\n locked = false\n): SceneNode | null {\n const deepest = hitTestChildren(graph, px, py, scopeId, true, new Map(), pad, locked, true)\n if (!deepest) {\n const scope = graph.nodes.get(scopeId)\n return scope ? frameChildAt(graph, scope, px, py, locked) : null\n }\n\n const chain: SceneNode[] = []\n for (let node: SceneNode | undefined = deepest; node && node.id !== scopeId;) {\n chain.unshift(node)\n node = node.parentId ? graph.nodes.get(node.parentId) : undefined\n }\n\n const opened = openedBySelection(graph, selectedIds, scopeId)\n\n for (const node of chain) {\n // A locked layer, hit only with `locked`, stands in for everything inside it.\n if (node.locked) return node\n if (!opened.has(node.id) && !opensByItself(graph, node)) return node\n }\n const last = chain.at(-1)\n if (!last) return null\n const frame = frameChildAt(graph, last, px, py, locked)\n if (frame) return frame\n if (ownsItsEmptyArea(graph, last)) return last\n return opened.has(last.id) && !opensByItself(graph, last) ? last : null\n}\n\n/** Whether a click looks into this container rather than selecting it; see `hitTestSelectable`. */\nexport function isOpenContainer(graph: SceneGraph, nodeId: string): boolean {\n const node = graph.nodes.get(nodeId)\n return node !== undefined && opensByItself(graph, node)\n}\n\n/**\n * The innermost open container under the point when every layer there is open: a marquee starting\n * here selects that container's layers, as in Figma.\n */\nexport function hitTestOpenContainer(\n graph: SceneGraph,\n px: number,\n py: number,\n scopeId: string\n): SceneNode | null {\n const deepest = hitTestChildren(graph, px, py, scopeId, true, new Map(), 0, false, false)\n for (let node: SceneNode | undefined = deepest ?? undefined; node && node.id !== scopeId;) {\n if (!opensByItself(graph, node) || ownsItsEmptyArea(graph, node)) return null\n node = node.parentId ? graph.nodes.get(node.parentId) : undefined\n }\n return deepest\n}\n\nfunction rectsTouch(a: Rect, b: Rect): boolean {\n return a.x + a.width > b.x && a.x < b.x + b.width && a.y + a.height > b.y && a.y < b.y + b.height\n}\n\nfunction rectOverlap(a: Rect, b: Rect): Rect | null {\n if (!rectsTouch(a, b)) return null\n const x = Math.max(a.x, b.x)\n const y = Math.max(a.y, b.y)\n return {\n x,\n y,\n width: Math.min(a.x + a.width, b.x + b.width) - x,\n height: Math.min(a.y + a.height, b.y + b.height) - y\n }\n}\n\n/**\n * Whether a deep marquee touching a container, but none of its layers, selects it: where a deep\n * click there would select it (a group's box, a painted container, the empty area a top-level frame\n * with auto layout owns), except a board or section whose empty area a press treats as background.\n */\nfunction selectsByOwnArea(graph: SceneGraph, node: SceneNode): boolean {\n if (node.type === 'GROUP' || ownsItsEmptyArea(graph, node)) return true\n return showsPaint(node) && !opensByItself(graph, node)\n}\n\n/**\n * Collects the deepest layers among `parent`'s children that touch `rect`. Each child's world\n * matrix is composed from its parent's, so a walk over a large page climbs no ancestor chains.\n */\nfunction collectDeepestInRect(\n graph: SceneGraph,\n parent: SceneNode,\n parentWorld: Mat3,\n rect: Rect,\n hits: SceneNode[]\n) {\n // A clipping container draws nothing outside itself, so its layers meet only that part of the rect.\n const area =\n parent.clipsContent && parent.type !== 'CANVAS'\n ? rectOverlap(rect, getAxisAlignedBoundsAt(parent, parentWorld))\n : rect\n if (!area) return\n for (const childId of parent.childIds) {\n const child = graph.nodes.get(childId)\n if (!child || child.internalOnly || !child.visible || child.locked) continue\n const world = Matrix.multiply(parentWorld, getNodeLocalMatrix(child))\n const touches = rectsTouch(area, getAxisAlignedBoundsAt(child, world))\n if (!CONTAINER_TYPES.has(child.type) || child.childIds.length === 0) {\n if (touches) hits.push(child)\n continue\n }\n const before = hits.length\n collectDeepestInRect(graph, child, world, area, hits)\n if (hits.length === before && touches && selectsByOwnArea(graph, child)) hits.push(child)\n }\n}\n\n/**\n * The deepest layers a deep (⌘) marquee over `rect` selects, as in Figma, by the rules a deep click\n * follows: it looks into frames, groups, sections, components, component sets, and instances, keeps\n * a boolean's operands inside it, and skips hidden, locked, and internal layers with their contents.\n * A container is selected itself only where the rect touches its own area and none of its layers.\n * Layers are compared by their axis-aligned canvas bounds, as a plain marquee compares them.\n */\nexport function hitTestDeepInRect(graph: SceneGraph, rect: Rect, scopeId: string): SceneNode[] {\n const scope = graph.nodes.get(scopeId)\n if (!scope) return []\n const hits: SceneNode[] = []\n collectDeepestInRect(graph, scope, getWorldMatrix(scope, graph), rect, hits)\n return hits\n}\n\n/** Whether the point lies inside the node's rotated and flipped bounds. */\nexport function isPointInNode(graph: SceneGraph, nodeId: string, px: number, py: number): boolean {\n const node = graph.nodes.get(nodeId)\n return node !== undefined && containsPoint(px, py, node, graph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B,QAAgB,IAAY,IAAqB;CAChG,MAAM,OAAO,MAAM,MAAM,IAAI,MAAM;CACnC,OAAO,SAAS,KAAA,KAAa,cAAc,IAAI,IAAI,MAAM,uBAAO,IAAI,IAAI,CAAC;AAC3E"}
|
package/dist/images.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ import { Matrix, Size } from "./primitives/index.js";
|
|
|
6
6
|
* hold images under other names; anything that hands images to Figma still checks.
|
|
7
7
|
*/
|
|
8
8
|
declare function computeImageHash(data: Uint8Array): string;
|
|
9
|
-
type ImageMimeType = 'image/png' | 'image/jpeg' | 'image/gif' | 'image/webp';
|
|
9
|
+
type ImageMimeType = 'image/png' | 'image/jpeg' | 'image/gif' | 'image/webp' | 'image/avif';
|
|
10
10
|
/** The raster types a dropped or pasted file can be imported as an image fill from. */
|
|
11
11
|
declare const IMPORTABLE_RASTER_TYPES: ReadonlySet<string>;
|
|
12
12
|
/** Whether a file is an SVG image, which some systems report without a media type. */
|
|
@@ -24,7 +24,8 @@ interface ImageHeader extends Size {
|
|
|
24
24
|
declare function imageHeader(data: Uint8Array): ImageHeader | null;
|
|
25
25
|
/**
|
|
26
26
|
* The media type of encoded image bytes, read from their header, for writing them out as a data
|
|
27
|
-
* URL or file.
|
|
27
|
+
* URL or file. AVIF is read from its ISO media brand, which `image-size` does not decode. Bytes it
|
|
28
|
+
* cannot read are reported as PNG, as image fills began.
|
|
28
29
|
*/
|
|
29
30
|
declare function imageMimeType(data: Uint8Array): ImageMimeType;
|
|
30
31
|
/** An image's size in pixels, read from its header; null for bytes it cannot read. */
|
package/dist/images.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"images.d.ts","names":[],"sources":["../src/images.ts"],"mappings":";;;;;;;iBAWgB,iBAAiB,MAAM;KAI3B;;cAWC,yBAAyB;;iBAStB,UAAU;EAAQ;EAAc;;UAM/B,oBAAoB;EACnC,MAAM;;;;;;iBAOQ,YAAY,MAAM,aAAa
|
|
1
|
+
{"version":3,"file":"images.d.ts","names":[],"sources":["../src/images.ts"],"mappings":";;;;;;;iBAWgB,iBAAiB,MAAM;KAI3B;;cAWC,yBAAyB;;iBAStB,UAAU;EAAQ;EAAc;;UAM/B,oBAAoB;EACnC,MAAM;;;;;;iBAOQ,YAAY,MAAM,aAAa;;;;;;iBAuC/B,cAAc,MAAM,aAAa;;iBAKjC,UAAU,MAAM,aAAa;;;;;iBAS7B,oBACd,eACA,gBACA,oBACA,sBACC"}
|