@pairdraw/scene-graph 0.19.1 → 0.20.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/behaviours/index.d.ts +2 -2
- package/dist/behaviours/index.js +2 -2
- package/dist/behaviours/spec.d.ts +6 -1
- package/dist/behaviours/spec.d.ts.map +1 -1
- package/dist/behaviours/spec.js +62 -5
- package/dist/behaviours/spec.js.map +1 -1
- package/dist/canvas-layout.js +66 -0
- package/dist/canvas-layout.js.map +1 -0
- package/dist/components/index.d.ts +2 -2
- package/dist/components/index.js +2 -2
- 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/coordinate.js +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/text.d.ts +22 -1
- package/dist/css/text.d.ts.map +1 -1
- package/dist/css/text.js +81 -1
- package/dist/css/text.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 +93 -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/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/graph.d.ts +8 -1
- package/dist/graph.d.ts.map +1 -1
- package/dist/graph.js +36 -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/group-bounds.d.ts +4 -1
- package/dist/group-bounds.d.ts.map +1 -1
- package/dist/group-bounds.js +11 -1
- package/dist/group-bounds.js.map +1 -1
- 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 +33 -178
- package/dist/hit-test.js.map +1 -1
- package/dist/index.d.ts +2 -2
- 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 +1 -1
- package/dist/instances/index.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/node-defaults.d.ts +21 -1
- package/dist/node-defaults.d.ts.map +1 -1
- package/dist/node-defaults.js +63 -50
- package/dist/node-defaults.js.map +1 -1
- package/dist/plugin-data/fields.d.ts +75 -0
- package/dist/plugin-data/fields.d.ts.map +1 -1
- package/dist/plugin-data/fields.js +18 -1
- package/dist/plugin-data/fields.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/resize.d.ts.map +1 -1
- package/dist/resize.js +3 -6
- package/dist/resize.js.map +1 -1
- package/dist/scaling/node.d.ts.map +1 -1
- package/dist/scaling/node.js +28 -4
- package/dist/scaling/node.js.map +1 -1
- package/dist/shared-styles.d.ts +3 -1
- package/dist/shared-styles.d.ts.map +1 -1
- package/dist/shared-styles.js +7 -1
- package/dist/shared-styles.js.map +1 -1
- package/dist/text/path.js +11 -0
- package/dist/text/path.js.map +1 -0
- package/dist/text/picture.d.ts.map +1 -1
- package/dist/text/picture.js +5 -5
- package/dist/text/picture.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/copied.d.ts +14 -0
- package/dist/transfer/copied.d.ts.map +1 -0
- package/dist/transfer/copied.js +52 -0
- package/dist/transfer/copied.js.map +1 -0
- package/dist/transfer/index.d.ts +2 -1
- package/dist/transfer/index.js +2 -1
- package/dist/transfer/nodes.js +7 -0
- package/dist/transfer/nodes.js.map +1 -1
- package/dist/types.d.ts +104 -15
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/undo.d.ts +6 -0
- package/dist/undo.d.ts.map +1 -1
- package/dist/undo.js +13 -0
- package/dist/undo.js.map +1 -1
- package/dist/variables/index.d.ts.map +1 -1
- package/dist/variables/index.js.map +1 -1
- 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/behaviours/spec.ts +94 -4
- package/src/canvas-layout.ts +76 -0
- package/src/components/variants.ts +39 -0
- package/src/constants.ts +75 -0
- package/src/copy.ts +20 -1
- package/src/css/index.ts +12 -1
- package/src/css/text.ts +86 -0
- 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 +101 -0
- package/src/fields/components.ts +21 -0
- package/src/fields/index.ts +1 -0
- package/src/fields/layout.ts +10 -0
- package/src/generated/shape-with-text.ts +28 -2
- package/src/graph.ts +48 -6
- package/src/grid-values.ts +47 -0
- package/src/group-bounds.ts +13 -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 +78 -291
- package/src/instances/fields.ts +2 -0
- package/src/instances/index.ts +1 -0
- package/src/node-defaults.ts +71 -52
- package/src/plugin-data/fields.ts +18 -1
- package/src/preview.ts +2 -0
- package/src/resize.ts +3 -14
- package/src/scaling/node.ts +34 -5
- package/src/shared-styles.ts +8 -0
- package/src/text/path.ts +14 -0
- package/src/text/picture.ts +5 -2
- 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/copied.ts +72 -0
- package/src/transfer/index.ts +1 -0
- package/src/transfer/nodes.ts +10 -0
- package/src/types.ts +117 -14
- package/src/undo.ts +11 -0
- package/src/variables/index.ts +1 -4
- 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,90 @@
|
|
|
1
|
+
// Where dragged layers land: the frame, section, component, or instance under the point.
|
|
2
|
+
import type { SceneGraph } from '../graph'
|
|
3
|
+
import type { NodeType, SceneNode } from '../types'
|
|
4
|
+
import { containsPoint } from './geometry'
|
|
5
|
+
|
|
6
|
+
/** Which layers take a drop, and which are only looked through, as in Figma. */
|
|
7
|
+
interface DropRules {
|
|
8
|
+
takes: (node: SceneNode) => boolean
|
|
9
|
+
passThrough: ReadonlySet<NodeType>
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const LAYER_TARGETS = new Set<NodeType>(['FRAME', 'SECTION', 'COMPONENT', 'INSTANCE'])
|
|
13
|
+
const COMPONENT_TARGETS = new Set<NodeType>(['FRAME', 'SECTION', 'INSTANCE'])
|
|
14
|
+
|
|
15
|
+
function dropRules(options: DropTargetOptions): DropRules {
|
|
16
|
+
const { componentSetIds } = options
|
|
17
|
+
if (!componentSetIds) {
|
|
18
|
+
return {
|
|
19
|
+
takes: (node) => LAYER_TARGETS.has(node.type),
|
|
20
|
+
passThrough: new Set(['GROUP', 'COMPONENT_SET'])
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
// Components never go into other components, and a set only takes back its own variants.
|
|
24
|
+
return {
|
|
25
|
+
takes: (node) =>
|
|
26
|
+
COMPONENT_TARGETS.has(node.type) ||
|
|
27
|
+
(node.type === 'COMPONENT_SET' && componentSetIds.has(node.id)),
|
|
28
|
+
passThrough: new Set(['GROUP'])
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function dropTargetIn(
|
|
33
|
+
graph: SceneGraph,
|
|
34
|
+
px: number,
|
|
35
|
+
py: number,
|
|
36
|
+
parent: SceneNode,
|
|
37
|
+
excludeIds: ReadonlySet<string>,
|
|
38
|
+
rules: DropRules,
|
|
39
|
+
transformCache: Map<string, boolean>
|
|
40
|
+
): SceneNode | null {
|
|
41
|
+
// A clipped-away part of a child is not under the cursor.
|
|
42
|
+
if (
|
|
43
|
+
parent.type !== 'CANVAS' &&
|
|
44
|
+
parent.clipsContent &&
|
|
45
|
+
!containsPoint(px, py, parent, graph, transformCache)
|
|
46
|
+
)
|
|
47
|
+
return null
|
|
48
|
+
|
|
49
|
+
for (let i = parent.childIds.length - 1; i >= 0; i--) {
|
|
50
|
+
const childId = parent.childIds[i]
|
|
51
|
+
if (excludeIds.has(childId)) continue
|
|
52
|
+
const child = graph.nodes.get(childId)
|
|
53
|
+
if (!child || child.internalOnly || !child.visible || child.locked) continue
|
|
54
|
+
const target = rules.takes(child)
|
|
55
|
+
if (!target && !rules.passThrough.has(child.type)) continue
|
|
56
|
+
|
|
57
|
+
const deeper = dropTargetIn(graph, px, py, child, excludeIds, rules, transformCache)
|
|
58
|
+
if (deeper) return deeper
|
|
59
|
+
if (target && containsPoint(px, py, child, graph, transformCache)) return child
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return null
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface DropTargetOptions {
|
|
66
|
+
/**
|
|
67
|
+
* Set when the dropped layers are all main components: the component sets they come from.
|
|
68
|
+
* Components then go only into frames, sections, instances, and those sets.
|
|
69
|
+
*/
|
|
70
|
+
componentSetIds?: ReadonlySet<string>
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* The topmost unlocked frame, section, component, or instance under the point, in its rotated
|
|
75
|
+
* shape and inside its clipping ancestors. Groups and component sets are looked through but never
|
|
76
|
+
* returned, except that a set takes dropped components; locked layers and their contents are
|
|
77
|
+
* skipped.
|
|
78
|
+
*/
|
|
79
|
+
export function hitTestDropTarget(
|
|
80
|
+
graph: SceneGraph,
|
|
81
|
+
px: number,
|
|
82
|
+
py: number,
|
|
83
|
+
excludeIds: ReadonlySet<string>,
|
|
84
|
+
scopeId?: string,
|
|
85
|
+
options: DropTargetOptions = {}
|
|
86
|
+
): SceneNode | null {
|
|
87
|
+
const scope = graph.nodes.get(scopeId ?? graph.rootId)
|
|
88
|
+
if (!scope) return null
|
|
89
|
+
return dropTargetIn(graph, px, py, scope, excludeIds, dropRules(options), new Map())
|
|
90
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
// Where a point lies against a layer's box and a mask's outline, in the layer's own coordinates.
|
|
2
|
+
import { getWorldMatrix } from '../coordinate'
|
|
3
|
+
import { geometryBlobPolylines } from '../geometry'
|
|
4
|
+
import type { SceneGraph } from '../graph'
|
|
5
|
+
import Matrix from '../matrix'
|
|
6
|
+
import type { Vector } from '../primitives'
|
|
7
|
+
import type { SceneNode } from '../types'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* How far from a zero-width or zero-height layer, in canvas units, a point still lands on it, so
|
|
11
|
+
* a flat line or an empty box stays clickable; matches a connector line's slop (`connectors/hit.ts`).
|
|
12
|
+
*/
|
|
13
|
+
const DEGENERATE_AXIS_SLOP = 4
|
|
14
|
+
|
|
15
|
+
function hasTransformedAncestor(
|
|
16
|
+
node: SceneNode,
|
|
17
|
+
graph: SceneGraph,
|
|
18
|
+
cache: Map<string, boolean>
|
|
19
|
+
): boolean {
|
|
20
|
+
const cached = cache.get(node.id)
|
|
21
|
+
if (cached !== undefined) return cached
|
|
22
|
+
const parent = node.parentId ? graph.getNode(node.parentId) : undefined
|
|
23
|
+
const transformed =
|
|
24
|
+
node.rotation !== 0 ||
|
|
25
|
+
node.flipX ||
|
|
26
|
+
node.flipY ||
|
|
27
|
+
(parent ? hasTransformedAncestor(parent, graph, cache) : false)
|
|
28
|
+
cache.set(node.id, transformed)
|
|
29
|
+
return transformed
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Where the point lies in the node's own coordinates: its offset from the position when nothing
|
|
34
|
+
* above it is turned, else through the inverse world matrix. Null when that cannot be inverted.
|
|
35
|
+
*/
|
|
36
|
+
export function localPoint(
|
|
37
|
+
px: number,
|
|
38
|
+
py: number,
|
|
39
|
+
node: SceneNode,
|
|
40
|
+
graph: SceneGraph,
|
|
41
|
+
transformCache: Map<string, boolean>
|
|
42
|
+
): Vector | null {
|
|
43
|
+
if (!hasTransformedAncestor(node, graph, transformCache)) {
|
|
44
|
+
const absolute = graph.getAbsolutePosition(node.id)
|
|
45
|
+
return { x: px - absolute.x, y: py - absolute.y }
|
|
46
|
+
}
|
|
47
|
+
const inv = Matrix.invert(getWorldMatrix(node, graph))
|
|
48
|
+
if (!inv) return null
|
|
49
|
+
const [x, y] = Matrix.mapPoints(inv, [px, py])
|
|
50
|
+
return { x, y }
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Whether the point lies in the node's box, grown by `pad` canvas units on every side. */
|
|
54
|
+
export function containsPoint(
|
|
55
|
+
px: number,
|
|
56
|
+
py: number,
|
|
57
|
+
node: SceneNode,
|
|
58
|
+
graph: SceneGraph,
|
|
59
|
+
transformCache: Map<string, boolean>,
|
|
60
|
+
pad = 0
|
|
61
|
+
): boolean {
|
|
62
|
+
const local = localPoint(px, py, node, graph, transformCache)
|
|
63
|
+
if (!local) return false
|
|
64
|
+
const padX = node.width > 0 ? pad : Math.max(pad, DEGENERATE_AXIS_SLOP)
|
|
65
|
+
const padY = node.height > 0 ? pad : Math.max(pad, DEGENERATE_AXIS_SLOP)
|
|
66
|
+
return (
|
|
67
|
+
local.x >= -padX &&
|
|
68
|
+
local.x <= node.width + padX &&
|
|
69
|
+
local.y >= -padY &&
|
|
70
|
+
local.y <= node.height + padY
|
|
71
|
+
)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Whether a rounded rectangle of `width` × `height` holds the point. */
|
|
75
|
+
function roundedRectContains(
|
|
76
|
+
point: Vector,
|
|
77
|
+
width: number,
|
|
78
|
+
height: number,
|
|
79
|
+
radius: number
|
|
80
|
+
): boolean {
|
|
81
|
+
if (point.x < 0 || point.y < 0 || point.x > width || point.y > height) return false
|
|
82
|
+
const r = Math.max(0, Math.min(radius, width / 2, height / 2))
|
|
83
|
+
const cx = Math.min(Math.max(point.x, r), width - r)
|
|
84
|
+
const cy = Math.min(Math.max(point.y, r), height - r)
|
|
85
|
+
return Math.hypot(point.x - cx, point.y - cy) <= r
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Whether the point is inside a closed polyline, by the even-odd rule. */
|
|
89
|
+
function insidePolyline(polyline: readonly Vector[], x: number, y: number): boolean {
|
|
90
|
+
let inside = false
|
|
91
|
+
for (let i = 0, j = polyline.length - 1; i < polyline.length; j = i++) {
|
|
92
|
+
const a = polyline[i]
|
|
93
|
+
const b = polyline[j]
|
|
94
|
+
if (a.y > y === b.y > y) continue
|
|
95
|
+
if (x < ((b.x - a.x) * (y - a.y)) / (b.y - a.y) + a.x) inside = !inside
|
|
96
|
+
}
|
|
97
|
+
return inside
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Whether a mask lets the point through, by the outline it draws: an ellipse's curve, a rounded
|
|
102
|
+
* rectangle's corners, or a vector's filled path. Other shapes clip by their box.
|
|
103
|
+
*/
|
|
104
|
+
export function maskContainsPoint(
|
|
105
|
+
px: number,
|
|
106
|
+
py: number,
|
|
107
|
+
mask: SceneNode,
|
|
108
|
+
graph: SceneGraph,
|
|
109
|
+
transformCache: Map<string, boolean>
|
|
110
|
+
): boolean {
|
|
111
|
+
const outlined =
|
|
112
|
+
(mask.type === 'ELLIPSE' && mask.width > 0 && mask.height > 0) ||
|
|
113
|
+
(mask.type === 'RECTANGLE' && mask.cornerRadius > 0) ||
|
|
114
|
+
(mask.type === 'VECTOR' && mask.fillGeometry.length > 0)
|
|
115
|
+
if (!outlined) return containsPoint(px, py, mask, graph, transformCache)
|
|
116
|
+
const local = localPoint(px, py, mask, graph, transformCache)
|
|
117
|
+
if (!local) return false
|
|
118
|
+
if (mask.type === 'ELLIPSE') {
|
|
119
|
+
const rx = mask.width / 2
|
|
120
|
+
const ry = mask.height / 2
|
|
121
|
+
const dx = (local.x - rx) / rx
|
|
122
|
+
const dy = (local.y - ry) / ry
|
|
123
|
+
return dx * dx + dy * dy <= 1
|
|
124
|
+
}
|
|
125
|
+
if (mask.type === 'RECTANGLE') {
|
|
126
|
+
return roundedRectContains(local, mask.width, mask.height, mask.cornerRadius)
|
|
127
|
+
}
|
|
128
|
+
return geometryBlobPolylines(mask.fillGeometry).some((points) =>
|
|
129
|
+
insidePolyline(points, local.x, local.y)
|
|
130
|
+
)
|
|
131
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// The masks that clip a container's children, and whether they hide a point.
|
|
2
|
+
import type { SceneGraph } from '../graph'
|
|
3
|
+
import type { SceneNode } from '../types'
|
|
4
|
+
import { maskContainsPoint } from './geometry'
|
|
5
|
+
|
|
6
|
+
function isVisibleMask(node: SceneNode | undefined): node is SceneNode {
|
|
7
|
+
return node !== undefined && node.visible && node.isMask
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The masks that clip each child, by index, as the renderer groups them (`core/canvas/masks.ts`):
|
|
12
|
+
* a run of visible masks clips the layers after it up to the next visible mask. Empty when the
|
|
13
|
+
* container has no visible mask.
|
|
14
|
+
*/
|
|
15
|
+
export function clippingMasks(graph: SceneGraph, childIds: readonly string[]): SceneNode[][] {
|
|
16
|
+
const children = childIds.map((childId) => graph.nodes.get(childId))
|
|
17
|
+
if (!children.some(isVisibleMask)) return []
|
|
18
|
+
const result: SceneNode[][] = []
|
|
19
|
+
let run: SceneNode[] = []
|
|
20
|
+
let previousWasMask = false
|
|
21
|
+
for (const child of children) {
|
|
22
|
+
if (isVisibleMask(child)) {
|
|
23
|
+
run = previousWasMask ? [...run, child] : [child]
|
|
24
|
+
previousWasMask = true
|
|
25
|
+
result.push([])
|
|
26
|
+
continue
|
|
27
|
+
}
|
|
28
|
+
previousWasMask = false
|
|
29
|
+
result.push(run)
|
|
30
|
+
}
|
|
31
|
+
return result
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Whether the masks that clip a layer leave the point outside every one of their outlines. */
|
|
35
|
+
export function isMaskedOut(
|
|
36
|
+
px: number,
|
|
37
|
+
py: number,
|
|
38
|
+
masks: readonly SceneNode[] | undefined,
|
|
39
|
+
graph: SceneGraph,
|
|
40
|
+
transformCache: Map<string, boolean>
|
|
41
|
+
): boolean {
|
|
42
|
+
if (!masks) return false
|
|
43
|
+
return (
|
|
44
|
+
masks.length > 0 &&
|
|
45
|
+
!masks.some((mask) => maskContainsPoint(px, py, mask, graph, transformCache))
|
|
46
|
+
)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Whether a point lies outside every mask of a run that clips a layer, so the layer hides it. */
|
|
50
|
+
export function isPointMaskedOut(
|
|
51
|
+
graph: SceneGraph,
|
|
52
|
+
masks: readonly SceneNode[] | undefined,
|
|
53
|
+
px: number,
|
|
54
|
+
py: number
|
|
55
|
+
): boolean {
|
|
56
|
+
return isMaskedOut(px, py, masks, graph, new Map())
|
|
57
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// Which layers show a paint where a pointer lands, so their area takes it.
|
|
2
|
+
import type { SceneNode } from '../types'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Whether a paint shows, so its container's empty area counts when a click reaches past
|
|
6
|
+
* containers: a paint at 0% opacity, or a gradient whose stops are all transparent, does not,
|
|
7
|
+
* while 1% does, and the layer's own opacity does not matter.
|
|
8
|
+
*/
|
|
9
|
+
function isHitPaint(paint: SceneNode['fills'][number]): boolean {
|
|
10
|
+
if (!paint.visible || !(paint.opacity > 0)) return false
|
|
11
|
+
if (paint.type === 'SOLID') return paint.color.a > 0
|
|
12
|
+
if (
|
|
13
|
+
paint.type === 'GRADIENT_LINEAR' ||
|
|
14
|
+
paint.type === 'GRADIENT_RADIAL' ||
|
|
15
|
+
paint.type === 'GRADIENT_ANGULAR' ||
|
|
16
|
+
paint.type === 'GRADIENT_DIAMOND'
|
|
17
|
+
)
|
|
18
|
+
return (paint.gradientStops ?? []).some((stop) => stop.color.a > 0)
|
|
19
|
+
return true
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function showsPaint(node: SceneNode): boolean {
|
|
23
|
+
return node.fills.some(isHitPaint) || node.strokes.some(isHitPaint)
|
|
24
|
+
}
|