@hozo/canvas 0.1.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/LICENSE +21 -0
- package/README.md +152 -0
- package/dist/accessibility.d.ts +20 -0
- package/dist/accessibility.d.ts.map +1 -0
- package/dist/accessibility.js +9 -0
- package/dist/accessibility.js.map +1 -0
- package/dist/hit-test.d.ts +96 -0
- package/dist/hit-test.d.ts.map +1 -0
- package/dist/hit-test.js +354 -0
- package/dist/hit-test.js.map +1 -0
- package/dist/index.d.ts +80 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +317 -0
- package/dist/index.js.map +1 -0
- package/dist/index.native.d.ts +83 -0
- package/dist/index.native.d.ts.map +1 -0
- package/dist/index.native.js +435 -0
- package/dist/index.native.js.map +1 -0
- package/dist/render-canvas-2d.d.ts +6 -0
- package/dist/render-canvas-2d.d.ts.map +1 -0
- package/dist/render-canvas-2d.js +213 -0
- package/dist/render-canvas-2d.js.map +1 -0
- package/dist/scene.d.ts +495 -0
- package/dist/scene.d.ts.map +1 -0
- package/dist/scene.js +591 -0
- package/dist/scene.js.map +1 -0
- package/dist/viewport.d.ts +9 -0
- package/dist/viewport.d.ts.map +1 -0
- package/dist/viewport.js +2 -0
- package/dist/viewport.js.map +1 -0
- package/dist/wrap-text.d.ts +28 -0
- package/dist/wrap-text.d.ts.map +1 -0
- package/dist/wrap-text.js +148 -0
- package/dist/wrap-text.js.map +1 -0
- package/package.json +79 -0
- package/src/accessibility.ts +28 -0
- package/src/hit-test.ts +499 -0
- package/src/index.native.tsx +733 -0
- package/src/index.tsx +510 -0
- package/src/render-canvas-2d.ts +259 -0
- package/src/scene.tsx +1037 -0
- package/src/viewport.ts +8 -0
- package/src/wrap-text.ts +157 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 iray-tno
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
# @hozo/canvas
|
|
2
|
+
|
|
3
|
+
Declarative retained-mode Canvas scenes for Hozo. The same scene is drawn with
|
|
4
|
+
the browser's built-in Canvas 2D API on Web and `@shopify/react-native-skia` on
|
|
5
|
+
React Native.
|
|
6
|
+
|
|
7
|
+
```tsx
|
|
8
|
+
import { Canvas } from '@hozo/canvas'
|
|
9
|
+
|
|
10
|
+
export function Sparkline() {
|
|
11
|
+
return (
|
|
12
|
+
<Canvas
|
|
13
|
+
width={320}
|
|
14
|
+
height={120}
|
|
15
|
+
viewBox={[0, 0, 100, 40]}
|
|
16
|
+
accessibilityLabel="Revenue increased over the last six months"
|
|
17
|
+
>
|
|
18
|
+
<Canvas.Path
|
|
19
|
+
path="M 0 36 L 20 30 L 40 32 L 60 18 L 80 20 L 100 4"
|
|
20
|
+
stroke="#2563eb"
|
|
21
|
+
strokeWidth={2}
|
|
22
|
+
fill="none"
|
|
23
|
+
/>
|
|
24
|
+
</Canvas>
|
|
25
|
+
)
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Interaction
|
|
30
|
+
|
|
31
|
+
Every Canvas leaf supports portable `onPress` and destination-bearing `href`.
|
|
32
|
+
The same topmost shape must contain both the pointer/touch start and release;
|
|
33
|
+
dragging away cancels activation. Non-interactive drawing above a target does
|
|
34
|
+
not block it.
|
|
35
|
+
|
|
36
|
+
```tsx
|
|
37
|
+
<Canvas.Rect
|
|
38
|
+
x={40}
|
|
39
|
+
y={10}
|
|
40
|
+
width={20}
|
|
41
|
+
height={30}
|
|
42
|
+
onPress={({ point, surfacePoint }) => {
|
|
43
|
+
// point uses viewBox/scene coordinates; surfacePoint uses CSS pixels or
|
|
44
|
+
// React Native layout points and is useful for positioning a tooltip.
|
|
45
|
+
selectBar(point.x, surfacePoint.x)
|
|
46
|
+
}}
|
|
47
|
+
/>
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
An `href` uses the same installed `@hozo/navigation` adapter as `Link`,
|
|
51
|
+
`Button href`, and `Pressable href`. On Web, Ctrl/Command-click, Shift-click,
|
|
52
|
+
and a middle-button press open a new browsing context without entering the
|
|
53
|
+
client router. A named destination also produces a real anchor in the hidden
|
|
54
|
+
accessibility layer; an action produces a button. Native exposes the matching
|
|
55
|
+
link or button role.
|
|
56
|
+
|
|
57
|
+
```tsx
|
|
58
|
+
<Canvas.Rect
|
|
59
|
+
width={40}
|
|
60
|
+
height={20}
|
|
61
|
+
href="/orders/42"
|
|
62
|
+
replace
|
|
63
|
+
accessibilityLabel="Open order 42"
|
|
64
|
+
/>
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
`onPress` runs before navigation. It receives the familiar `ctrlKey`,
|
|
68
|
+
`metaKey`, `shiftKey`, `altKey`, and `button` fields. Call `preventDefault()`
|
|
69
|
+
to retain an activation for local interaction—for example, Ctrl/Command-click
|
|
70
|
+
or Shift-click selection in a Canvas-drawn table—without following `href`.
|
|
71
|
+
|
|
72
|
+
```tsx
|
|
73
|
+
<Canvas.Rect
|
|
74
|
+
width={40}
|
|
75
|
+
height={20}
|
|
76
|
+
href="/orders/42"
|
|
77
|
+
accessibilityLabel="Open order 42"
|
|
78
|
+
onPress={(event) => {
|
|
79
|
+
if (event.ctrlKey || event.metaKey || event.shiftKey) {
|
|
80
|
+
event.preventDefault()
|
|
81
|
+
toggleSelectedOrder(42)
|
|
82
|
+
}
|
|
83
|
+
}}
|
|
84
|
+
/>
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
Hit testing follows reverse paint order, `viewBox`, `fit`, nested group
|
|
88
|
+
translation/rotation/scale/origin, and rectangle clips. It uses the closed
|
|
89
|
+
shape's geometry rather than inspecting painted pixels. Changing only an event
|
|
90
|
+
handler or `disabled` updates a separate registry and does not redraw the
|
|
91
|
+
scene.
|
|
92
|
+
|
|
93
|
+
Line hit testing follows its painted stroke and cap. Path containment and text
|
|
94
|
+
measurement are answered by each platform's renderer so they can participate
|
|
95
|
+
without a second geometry or font implementation.
|
|
96
|
+
|
|
97
|
+
Canvas pixels cannot themselves provide keyboard or screen-reader interaction.
|
|
98
|
+
A named interactive shape therefore gets a semantic control in a visually
|
|
99
|
+
hidden sibling layer. An unnamed interactive shape remains pointer-only and
|
|
100
|
+
reports a warning; give it an `accessibilityLabel` or provide equivalent
|
|
101
|
+
visible controls outside the Canvas.
|
|
102
|
+
|
|
103
|
+
Canvas pixels are not semantic content. Every root must therefore either have
|
|
104
|
+
an `accessibilityLabel`, provide an `accessibleFallback` (for example a data
|
|
105
|
+
table), or opt out explicitly with `decorative`.
|
|
106
|
+
|
|
107
|
+
These are distinct accessibility modes. A label-only Canvas is exposed as one
|
|
108
|
+
named image. When `accessibleFallback` is present, the pixel surface is hidden
|
|
109
|
+
from assistive technology and the fallback is rendered as a visually hidden
|
|
110
|
+
semantic sibling; it is not nested under an image role that would erase the
|
|
111
|
+
fallback's table, list, or heading semantics. A decorative Canvas and its
|
|
112
|
+
drawing surface are hidden completely.
|
|
113
|
+
|
|
114
|
+
Build a fallback from the source data rather than trying to reconstruct it
|
|
115
|
+
from rectangles and paths. It should be informational rather than interactive:
|
|
116
|
+
a visually hidden button or link would create an invisible keyboard or screen
|
|
117
|
+
reader focus target.
|
|
118
|
+
|
|
119
|
+
```tsx
|
|
120
|
+
<Canvas
|
|
121
|
+
width={320}
|
|
122
|
+
height={120}
|
|
123
|
+
accessibilityLabel="Revenue by quarter"
|
|
124
|
+
accessibleFallback={
|
|
125
|
+
<table>
|
|
126
|
+
<caption>Revenue by quarter</caption>
|
|
127
|
+
<tbody>{/* rows made from the same series as the chart */}</tbody>
|
|
128
|
+
</table>
|
|
129
|
+
}
|
|
130
|
+
>
|
|
131
|
+
{/* visual marks */}
|
|
132
|
+
</Canvas>
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
Skia is not installed by this package, so a Web-only installation does not
|
|
136
|
+
download its native binaries. Add it only to a React Native app:
|
|
137
|
+
|
|
138
|
+
```sh
|
|
139
|
+
npx expo install @shopify/react-native-skia
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
The Native entry imports Skia directly and will fail at bundle time with a
|
|
143
|
+
missing-module error until it is installed. It remains an app-level dependency
|
|
144
|
+
so Web-only Hozo installations stay small.
|
|
145
|
+
|
|
146
|
+
<!-- generated: package-footer -->
|
|
147
|
+
|
|
148
|
+
---
|
|
149
|
+
|
|
150
|
+
Part of [Hozo](https://iray-tno.github.io/hozo/), a Rust-powered universal UI compiler and accessibility-first layer for React Native. Most applications install [`@hozo/core`](https://www.npmjs.com/package/@hozo/core) and one build integration; see [Getting started](https://github.com/iray-tno/hozo#getting-started). Source and issues: [github.com/iray-tno/hozo](https://github.com/iray-tno/hozo).
|
|
151
|
+
|
|
152
|
+
<!-- /generated: package-footer -->
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
|
+
/** A fallback must render something; nullish and boolean React nodes do not. */
|
|
3
|
+
export type CanvasAccessibleFallback = Exclude<ReactNode, boolean | null | undefined>;
|
|
4
|
+
export type CanvasAccessibilityProps = {
|
|
5
|
+
decorative: true;
|
|
6
|
+
accessibilityLabel?: never;
|
|
7
|
+
accessibleFallback?: never;
|
|
8
|
+
} | {
|
|
9
|
+
decorative?: false;
|
|
10
|
+
accessibilityLabel: string;
|
|
11
|
+
accessibleFallback?: CanvasAccessibleFallback;
|
|
12
|
+
} | {
|
|
13
|
+
decorative?: false;
|
|
14
|
+
accessibilityLabel?: string;
|
|
15
|
+
accessibleFallback: CanvasAccessibleFallback;
|
|
16
|
+
};
|
|
17
|
+
export type CanvasAccessibilityMode = 'decorative' | 'label' | 'fallback';
|
|
18
|
+
/** Shared policy; each platform decides how that mode maps to its accessibility tree. */
|
|
19
|
+
export declare function canvasAccessibilityMode(props: Pick<CanvasAccessibilityProps, 'decorative' | 'accessibilityLabel' | 'accessibleFallback'>): CanvasAccessibilityMode;
|
|
20
|
+
//# sourceMappingURL=accessibility.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"accessibility.d.ts","sourceRoot":"","sources":["../src/accessibility.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,OAAO,CAAA;AAEtC,gFAAgF;AAChF,MAAM,MAAM,wBAAwB,GAAG,OAAO,CAAC,SAAS,EAAE,OAAO,GAAG,IAAI,GAAG,SAAS,CAAC,CAAA;AAErF,MAAM,MAAM,wBAAwB,GAChC;IAAE,UAAU,EAAE,IAAI,CAAC;IAAC,kBAAkB,CAAC,EAAE,KAAK,CAAC;IAAC,kBAAkB,CAAC,EAAE,KAAK,CAAA;CAAE,GAC5E;IACE,UAAU,CAAC,EAAE,KAAK,CAAA;IAClB,kBAAkB,EAAE,MAAM,CAAA;IAC1B,kBAAkB,CAAC,EAAE,wBAAwB,CAAA;CAC9C,GACD;IACE,UAAU,CAAC,EAAE,KAAK,CAAA;IAClB,kBAAkB,CAAC,EAAE,MAAM,CAAA;IAC3B,kBAAkB,EAAE,wBAAwB,CAAA;CAC7C,CAAA;AAEL,MAAM,MAAM,uBAAuB,GAAG,YAAY,GAAG,OAAO,GAAG,UAAU,CAAA;AAEzE,yFAAyF;AACzF,wBAAgB,uBAAuB,CACrC,KAAK,EAAE,IAAI,CAAC,wBAAwB,EAAE,YAAY,GAAG,oBAAoB,GAAG,oBAAoB,CAAC,GAChG,uBAAuB,CAIzB"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** Shared policy; each platform decides how that mode maps to its accessibility tree. */
|
|
2
|
+
export function canvasAccessibilityMode(props) {
|
|
3
|
+
if (props.decorative)
|
|
4
|
+
return 'decorative';
|
|
5
|
+
if (props.accessibleFallback !== undefined)
|
|
6
|
+
return 'fallback';
|
|
7
|
+
return 'label';
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=accessibility.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"accessibility.js","sourceRoot":"","sources":["../src/accessibility.ts"],"names":[],"mappings":"AAoBA,yFAAyF;AACzF,MAAM,UAAU,uBAAuB,CACrC,KAAiG;IAEjG,IAAI,KAAK,CAAC,UAAU;QAAE,OAAO,YAAY,CAAA;IACzC,IAAI,KAAK,CAAC,kBAAkB,KAAK,SAAS;QAAE,OAAO,UAAU,CAAA;IAC7D,OAAO,OAAO,CAAA;AAChB,CAAC"}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { type CanvasScene, type TextProps } from './scene.tsx';
|
|
2
|
+
export interface CanvasPoint {
|
|
3
|
+
x: number;
|
|
4
|
+
y: number;
|
|
5
|
+
}
|
|
6
|
+
export interface CanvasHitTestViewport {
|
|
7
|
+
/** Logical surface width in CSS pixels or React Native points. */
|
|
8
|
+
width: number;
|
|
9
|
+
/** Logical surface height in CSS pixels or React Native points. */
|
|
10
|
+
height: number;
|
|
11
|
+
viewBox?: readonly [x: number, y: number, width: number, height: number];
|
|
12
|
+
fit?: 'contain' | 'stretch';
|
|
13
|
+
}
|
|
14
|
+
export interface CanvasHitTestResult {
|
|
15
|
+
id: string;
|
|
16
|
+
/** Point in the root scene coordinate system, after the viewBox transform. */
|
|
17
|
+
point: CanvasPoint;
|
|
18
|
+
/** Point after the target's ancestor transforms have also been inverted. */
|
|
19
|
+
localPoint: CanvasPoint;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Whether a point is inside an SVG path, answered by the renderer.
|
|
23
|
+
*
|
|
24
|
+
* Supplied by each surface rather than implemented here, which is the
|
|
25
|
+
* opposite of how every other shape works and is deliberate. A rectangle
|
|
26
|
+
* or a circle is the same geometry under Canvas2D and under Skia, so one
|
|
27
|
+
* shared predicate agrees with both. An arbitrary path is not: béziers,
|
|
28
|
+
* arcs and fill rules are where rasterisers differ, and a third
|
|
29
|
+
* implementation in TypeScript would agree with neither of the two that
|
|
30
|
+
* actually draw. Canvas2D's `isPointInPath` and Skia's `Path.contains`
|
|
31
|
+
* each agree with their own.
|
|
32
|
+
*
|
|
33
|
+
* The same reasoning `TextProps` gives for alignment: each renderer's
|
|
34
|
+
* measurement is its own, and only its own can match what it draws.
|
|
35
|
+
*
|
|
36
|
+
* Optional, and paths refuse without it. A caller that has no renderer to
|
|
37
|
+
* ask -- a test, a server -- gets the behaviour paths had before this
|
|
38
|
+
* existed rather than a guess.
|
|
39
|
+
*/
|
|
40
|
+
export type CanvasPathHitTest = (path: string, fillRule: 'nonzero' | 'evenodd', point: CanvasPoint) => boolean;
|
|
41
|
+
/**
|
|
42
|
+
* A run of text as the renderer that drew it measures it.
|
|
43
|
+
*
|
|
44
|
+
* Supplied per surface for the reason `CanvasPathHitTest` is: the only
|
|
45
|
+
* measurement that can agree with what is on screen is the one taken by
|
|
46
|
+
* the thing that put it there. Canvas2D answers with
|
|
47
|
+
* `actualBoundingBoxAscent` and `Descent`; Skia answers with a rect whose
|
|
48
|
+
* origin is the baseline. Both report the *ink*, not the font box.
|
|
49
|
+
*
|
|
50
|
+
* That last part is why this exists at all. Refusing to hit test text
|
|
51
|
+
* was argued from "a box is not where the ink is" -- true of a font box
|
|
52
|
+
* and false of this one, which both platforms have had all along. The
|
|
53
|
+
* refusal left a label a person can see and cannot press, with nothing
|
|
54
|
+
* said, which is the failure this package spends its time avoiding.
|
|
55
|
+
*/
|
|
56
|
+
export interface CanvasTextMetrics {
|
|
57
|
+
width: number;
|
|
58
|
+
/** Ink above the baseline, positive. */
|
|
59
|
+
ascent: number;
|
|
60
|
+
/** Ink below the baseline, positive. */
|
|
61
|
+
descent: number;
|
|
62
|
+
}
|
|
63
|
+
export type CanvasTextMeasure = (props: TextProps) => CanvasTextMetrics;
|
|
64
|
+
/**
|
|
65
|
+
* What a surface can answer that the shared hit test cannot.
|
|
66
|
+
*
|
|
67
|
+
* One object rather than a growing tail of optional callbacks. Each is
|
|
68
|
+
* a question only a renderer can answer, and each is absent for a caller
|
|
69
|
+
* that has no renderer to ask -- a test, a server -- which refuses the
|
|
70
|
+
* shapes that need it rather than guessing at them.
|
|
71
|
+
*/
|
|
72
|
+
export interface CanvasRendererQueries {
|
|
73
|
+
pathContains?: CanvasPathHitTest;
|
|
74
|
+
measureText?: CanvasTextMeasure;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Where a shape is, for something that arrives without a pointer.
|
|
78
|
+
*
|
|
79
|
+
* A keyboard reaching a shape has no cursor, and `CanvasPressEvent` still
|
|
80
|
+
* has to say where. Its middle is the honest answer, and it is computed
|
|
81
|
+
* through the same matrices the hit test walks so that focusing a shape
|
|
82
|
+
* and clicking it report the same place.
|
|
83
|
+
*/
|
|
84
|
+
export declare function canvasNodePoint(scene: CanvasScene, id: string, viewport: CanvasHitTestViewport): {
|
|
85
|
+
point: CanvasPoint;
|
|
86
|
+
surfacePoint: CanvasPoint;
|
|
87
|
+
} | undefined;
|
|
88
|
+
/**
|
|
89
|
+
* Finds the topmost interactive geometry at a logical surface point.
|
|
90
|
+
*
|
|
91
|
+
* Rectangle clips, the four closed primitive shapes, and lines are
|
|
92
|
+
* portable today. Paths and path clips deliberately refuse hits until both
|
|
93
|
+
* renderers can implement the same contract.
|
|
94
|
+
*/
|
|
95
|
+
export declare function hitTestCanvas(scene: CanvasScene, surfacePoint: CanvasPoint, viewport: CanvasHitTestViewport, isInteractive: (id: string) => boolean, queries?: CanvasRendererQueries): CanvasHitTestResult | undefined;
|
|
96
|
+
//# sourceMappingURL=hit-test.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hit-test.d.ts","sourceRoot":"","sources":["../src/hit-test.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,WAAW,EAMhB,KAAK,SAAS,EAEf,MAAM,aAAa,CAAA;AAGpB,MAAM,WAAW,WAAW;IAC1B,CAAC,EAAE,MAAM,CAAA;IACT,CAAC,EAAE,MAAM,CAAA;CACV;AAED,MAAM,WAAW,qBAAqB;IACpC,kEAAkE;IAClE,KAAK,EAAE,MAAM,CAAA;IACb,mEAAmE;IACnE,MAAM,EAAE,MAAM,CAAA;IACd,OAAO,CAAC,EAAE,SAAS,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAA;IACxE,GAAG,CAAC,EAAE,SAAS,GAAG,SAAS,CAAA;CAC5B;AAED,MAAM,WAAW,mBAAmB;IAClC,EAAE,EAAE,MAAM,CAAA;IACV,8EAA8E;IAC9E,KAAK,EAAE,WAAW,CAAA;IAClB,4EAA4E;IAC5E,UAAU,EAAE,WAAW,CAAA;CACxB;AAqPD;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,MAAM,iBAAiB,GAAG,CAC9B,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,SAAS,GAAG,SAAS,EAC/B,KAAK,EAAE,WAAW,KACf,OAAO,CAAA;AAEZ;;;;;;;;;;;;;;GAcG;AACH,MAAM,WAAW,iBAAiB;IAChC,KAAK,EAAE,MAAM,CAAA;IACb,wCAAwC;IACxC,MAAM,EAAE,MAAM,CAAA;IACd,wCAAwC;IACxC,OAAO,EAAE,MAAM,CAAA;CAChB;AAED,MAAM,MAAM,iBAAiB,GAAG,CAAC,KAAK,EAAE,SAAS,KAAK,iBAAiB,CAAA;AAEvE;;;;;;;GAOG;AACH,MAAM,WAAW,qBAAqB;IACpC,YAAY,CAAC,EAAE,iBAAiB,CAAA;IAChC,WAAW,CAAC,EAAE,iBAAiB,CAAA;CAChC;AAgED;;;;;;;GAOG;AACH,wBAAgB,eAAe,CAC7B,KAAK,EAAE,WAAW,EAClB,EAAE,EAAE,MAAM,EACV,QAAQ,EAAE,qBAAqB,GAC9B;IAAE,KAAK,EAAE,WAAW,CAAC;IAAC,YAAY,EAAE,WAAW,CAAA;CAAE,GAAG,SAAS,CAkC/D;AAED;;;;;;GAMG;AACH,wBAAgB,aAAa,CAC3B,KAAK,EAAE,WAAW,EAClB,YAAY,EAAE,WAAW,EACzB,QAAQ,EAAE,qBAAqB,EAC/B,aAAa,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,OAAO,EACtC,OAAO,CAAC,EAAE,qBAAqB,GAC9B,mBAAmB,GAAG,SAAS,CAmCjC"}
|
package/dist/hit-test.js
ADDED
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
import { paintStrokes, unhandledShape, } from './scene.js';
|
|
2
|
+
import { textLines } from './wrap-text.js';
|
|
3
|
+
const identity = [1, 0, 0, 1, 0, 0];
|
|
4
|
+
function multiply(left, right) {
|
|
5
|
+
return [
|
|
6
|
+
left[0] * right[0] + left[2] * right[1],
|
|
7
|
+
left[1] * right[0] + left[3] * right[1],
|
|
8
|
+
left[0] * right[2] + left[2] * right[3],
|
|
9
|
+
left[1] * right[2] + left[3] * right[3],
|
|
10
|
+
left[0] * right[4] + left[2] * right[5] + left[4],
|
|
11
|
+
left[1] * right[4] + left[3] * right[5] + left[5],
|
|
12
|
+
];
|
|
13
|
+
}
|
|
14
|
+
function translate(x, y) {
|
|
15
|
+
return [1, 0, 0, 1, x, y];
|
|
16
|
+
}
|
|
17
|
+
function scale(x, y) {
|
|
18
|
+
return [x, 0, 0, y, 0, 0];
|
|
19
|
+
}
|
|
20
|
+
function rotate(degrees) {
|
|
21
|
+
const radians = (degrees * Math.PI) / 180;
|
|
22
|
+
const cosine = Math.cos(radians);
|
|
23
|
+
const sine = Math.sin(radians);
|
|
24
|
+
return [cosine, sine, -sine, cosine, 0, 0];
|
|
25
|
+
}
|
|
26
|
+
function invert(matrix) {
|
|
27
|
+
const determinant = matrix[0] * matrix[3] - matrix[1] * matrix[2];
|
|
28
|
+
if (!Number.isFinite(determinant) || Math.abs(determinant) < Number.EPSILON)
|
|
29
|
+
return undefined;
|
|
30
|
+
return [
|
|
31
|
+
matrix[3] / determinant,
|
|
32
|
+
-matrix[1] / determinant,
|
|
33
|
+
-matrix[2] / determinant,
|
|
34
|
+
matrix[0] / determinant,
|
|
35
|
+
(matrix[2] * matrix[5] - matrix[3] * matrix[4]) / determinant,
|
|
36
|
+
(matrix[1] * matrix[4] - matrix[0] * matrix[5]) / determinant,
|
|
37
|
+
];
|
|
38
|
+
}
|
|
39
|
+
function apply(matrix, point) {
|
|
40
|
+
return {
|
|
41
|
+
x: matrix[0] * point.x + matrix[2] * point.y + matrix[4],
|
|
42
|
+
y: matrix[1] * point.x + matrix[3] * point.y + matrix[5],
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
function groupMatrix(transform) {
|
|
46
|
+
if (!transform)
|
|
47
|
+
return identity;
|
|
48
|
+
const originX = transform.originX ?? 0;
|
|
49
|
+
const originY = transform.originY ?? 0;
|
|
50
|
+
let result = translate(transform.translateX ?? 0, transform.translateY ?? 0);
|
|
51
|
+
if (originX !== 0 || originY !== 0)
|
|
52
|
+
result = multiply(result, translate(originX, originY));
|
|
53
|
+
if (transform.rotate)
|
|
54
|
+
result = multiply(result, rotate(transform.rotate));
|
|
55
|
+
result = multiply(result, scale(transform.scaleX ?? 1, transform.scaleY ?? 1));
|
|
56
|
+
if (originX !== 0 || originY !== 0)
|
|
57
|
+
result = multiply(result, translate(-originX, -originY));
|
|
58
|
+
return result;
|
|
59
|
+
}
|
|
60
|
+
function viewportMatrix(viewport) {
|
|
61
|
+
if (viewport.width <= 0 || viewport.height <= 0)
|
|
62
|
+
return undefined;
|
|
63
|
+
const viewBox = viewport.viewBox;
|
|
64
|
+
if (!viewBox)
|
|
65
|
+
return identity;
|
|
66
|
+
if (viewBox[2] <= 0 || viewBox[3] <= 0)
|
|
67
|
+
return undefined;
|
|
68
|
+
const scaleX = viewport.width / viewBox[2];
|
|
69
|
+
const scaleY = viewport.height / viewBox[3];
|
|
70
|
+
if ((viewport.fit ?? 'contain') === 'stretch') {
|
|
71
|
+
return multiply(scale(scaleX, scaleY), translate(-viewBox[0], -viewBox[1]));
|
|
72
|
+
}
|
|
73
|
+
const uniformScale = Math.min(scaleX, scaleY);
|
|
74
|
+
const offsetX = (viewport.width - viewBox[2] * uniformScale) / 2;
|
|
75
|
+
const offsetY = (viewport.height - viewBox[3] * uniformScale) / 2;
|
|
76
|
+
return multiply(multiply(translate(offsetX, offsetY), scale(uniformScale, uniformScale)), translate(-viewBox[0], -viewBox[1]));
|
|
77
|
+
}
|
|
78
|
+
function normalRect(x, y, width, height) {
|
|
79
|
+
return {
|
|
80
|
+
x: width < 0 ? x + width : x,
|
|
81
|
+
y: height < 0 ? y + height : y,
|
|
82
|
+
width: Math.abs(width),
|
|
83
|
+
height: Math.abs(height),
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
function pointInRect(point, x, y, width, height) {
|
|
87
|
+
const rect = normalRect(x, y, width, height);
|
|
88
|
+
return (rect.width > 0 &&
|
|
89
|
+
rect.height > 0 &&
|
|
90
|
+
point.x >= rect.x &&
|
|
91
|
+
point.x <= rect.x + rect.width &&
|
|
92
|
+
point.y >= rect.y &&
|
|
93
|
+
point.y <= rect.y + rect.height);
|
|
94
|
+
}
|
|
95
|
+
function pointInRoundedRect(point, x, y, width, height, radius) {
|
|
96
|
+
const rect = normalRect(x, y, width, height);
|
|
97
|
+
if (!pointInRect(point, rect.x, rect.y, rect.width, rect.height))
|
|
98
|
+
return false;
|
|
99
|
+
const cornerRadius = Math.max(0, Math.min(Math.abs(radius), rect.width / 2, rect.height / 2));
|
|
100
|
+
if (cornerRadius === 0)
|
|
101
|
+
return true;
|
|
102
|
+
const centerX = Math.max(rect.x + cornerRadius, Math.min(point.x, rect.x + rect.width - cornerRadius));
|
|
103
|
+
const centerY = Math.max(rect.y + cornerRadius, Math.min(point.y, rect.y + rect.height - cornerRadius));
|
|
104
|
+
const dx = point.x - centerX;
|
|
105
|
+
const dy = point.y - centerY;
|
|
106
|
+
return dx * dx + dy * dy <= cornerRadius * cornerRadius;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Whether a point lands on a line's stroke, as the stroke is painted.
|
|
110
|
+
*
|
|
111
|
+
* A line has no area, so the only honest region is the one the renderer
|
|
112
|
+
* draws: the band of `strokeWidth` around the segment, ended the way
|
|
113
|
+
* `lineCap` ends it. Anything wider would be a control nobody can see,
|
|
114
|
+
* which is the failure this issue's own wording warns about -- so a chart
|
|
115
|
+
* that wants a finger-sized target draws a wider transparent line, and
|
|
116
|
+
* says so in the scene rather than relying on a tolerance it cannot
|
|
117
|
+
* inspect.
|
|
118
|
+
*
|
|
119
|
+
* The three caps are three different shapes and are treated as such:
|
|
120
|
+
*
|
|
121
|
+
* - `butt` (the default in Canvas2D and Skia both) stops flat at the
|
|
122
|
+
* endpoints, so the region is exactly the rectangle;
|
|
123
|
+
* - `round` adds a half-disc at each end, which is what makes plain
|
|
124
|
+
* distance-to-segment the right test;
|
|
125
|
+
* - `square` extends the rectangle by half the width at each end.
|
|
126
|
+
*/
|
|
127
|
+
function pointInLine(point, props) {
|
|
128
|
+
if (!paintStrokes(props))
|
|
129
|
+
return false;
|
|
130
|
+
const half = (props.strokeWidth ?? 1) / 2;
|
|
131
|
+
const dx = props.x2 - props.x1;
|
|
132
|
+
const dy = props.y2 - props.y1;
|
|
133
|
+
const lengthSquared = dx * dx + dy * dy;
|
|
134
|
+
// A zero-length line, which the platforms disagree about the least when
|
|
135
|
+
// stated explicitly: `butt` paints nothing at all, and the other two
|
|
136
|
+
// paint the cap on its own -- a disc or a square of the stroke width.
|
|
137
|
+
if (lengthSquared === 0) {
|
|
138
|
+
const px = point.x - props.x1;
|
|
139
|
+
const py = point.y - props.y1;
|
|
140
|
+
if (props.lineCap === 'round')
|
|
141
|
+
return px * px + py * py <= half * half;
|
|
142
|
+
if (props.lineCap === 'square')
|
|
143
|
+
return Math.abs(px) <= half && Math.abs(py) <= half;
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
146
|
+
// Where the point falls along the segment, 0 at one end and 1 at the
|
|
147
|
+
// other. The caps decide how far outside that range still counts.
|
|
148
|
+
const t = ((point.x - props.x1) * dx + (point.y - props.y1) * dy) / lengthSquared;
|
|
149
|
+
if (props.lineCap === 'round') {
|
|
150
|
+
const clamped = Math.max(0, Math.min(1, t));
|
|
151
|
+
const nearestX = props.x1 + clamped * dx;
|
|
152
|
+
const nearestY = props.y1 + clamped * dy;
|
|
153
|
+
const offX = point.x - nearestX;
|
|
154
|
+
const offY = point.y - nearestY;
|
|
155
|
+
return offX * offX + offY * offY <= half * half;
|
|
156
|
+
}
|
|
157
|
+
const overhang = props.lineCap === 'square' ? half / Math.sqrt(lengthSquared) : 0;
|
|
158
|
+
if (t < -overhang || t > 1 + overhang)
|
|
159
|
+
return false;
|
|
160
|
+
// Distance to the infinite line, which inside that range is the
|
|
161
|
+
// distance to the stroke's centre.
|
|
162
|
+
const perpendicular = Math.abs((point.x - props.x1) * dy - (point.y - props.y1) * dx);
|
|
163
|
+
return perpendicular <= half * Math.sqrt(lengthSquared);
|
|
164
|
+
}
|
|
165
|
+
function pointInNode(node, point, queries) {
|
|
166
|
+
switch (node.kind) {
|
|
167
|
+
case 'rect':
|
|
168
|
+
return pointInRect(point, node.props.x ?? 0, node.props.y ?? 0, node.props.width, node.props.height);
|
|
169
|
+
case 'rounded-rect':
|
|
170
|
+
return pointInRoundedRect(point, node.props.x ?? 0, node.props.y ?? 0, node.props.width, node.props.height, node.props.radius);
|
|
171
|
+
case 'circle': {
|
|
172
|
+
if (node.props.radius <= 0)
|
|
173
|
+
return false;
|
|
174
|
+
const dx = point.x - node.props.cx;
|
|
175
|
+
const dy = point.y - node.props.cy;
|
|
176
|
+
return dx * dx + dy * dy <= node.props.radius * node.props.radius;
|
|
177
|
+
}
|
|
178
|
+
case 'ellipse': {
|
|
179
|
+
if (node.props.radiusX <= 0 || node.props.radiusY <= 0)
|
|
180
|
+
return false;
|
|
181
|
+
const dx = (point.x - node.props.cx) / node.props.radiusX;
|
|
182
|
+
const dy = (point.y - node.props.cy) / node.props.radiusY;
|
|
183
|
+
return dx * dx + dy * dy <= 1;
|
|
184
|
+
}
|
|
185
|
+
case 'line':
|
|
186
|
+
return pointInLine(point, node.props);
|
|
187
|
+
case 'path':
|
|
188
|
+
// Asked of the renderer, for the reason `CanvasPathHitTest` gives.
|
|
189
|
+
return (queries?.pathContains?.(node.props.path, node.props.fillRule ?? 'nonzero', point) ?? false);
|
|
190
|
+
case 'text': {
|
|
191
|
+
const measure = queries?.measureText;
|
|
192
|
+
if (!measure)
|
|
193
|
+
return false;
|
|
194
|
+
// Each line boxed on its own rather than one box around all of
|
|
195
|
+
// them: a short last line leaves a corner that looks pressable and
|
|
196
|
+
// is not, and a person who presses there gets nothing for a reason
|
|
197
|
+
// they cannot see.
|
|
198
|
+
const width = (run) => measure({ ...node.props, text: run }).width;
|
|
199
|
+
return textLines(node.props, width).some((line) => {
|
|
200
|
+
const props = { ...node.props, text: line.text, y: line.y };
|
|
201
|
+
const box = textBox(props, measure(props));
|
|
202
|
+
return pointInRect(point, box.x, box.y, box.width, box.height);
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
case 'group':
|
|
206
|
+
case 'clip':
|
|
207
|
+
return false;
|
|
208
|
+
default:
|
|
209
|
+
return unhandledShape(node, 'the hit test') ?? false;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* The box a run of text occupies, in the text's own coordinates.
|
|
214
|
+
*
|
|
215
|
+
* `x` is the left edge and `y` the baseline, as `TextProps` says, so the
|
|
216
|
+
* box runs from the baseline up by the ascent and down by the descent.
|
|
217
|
+
* `textAlign` moves the run rather than the anchor, which is what it
|
|
218
|
+
* does on the screen.
|
|
219
|
+
*/
|
|
220
|
+
function textBox(props, metrics) {
|
|
221
|
+
const align = props.textAlign ?? 'left';
|
|
222
|
+
const left = align === 'left'
|
|
223
|
+
? props.x
|
|
224
|
+
: align === 'center'
|
|
225
|
+
? props.x - metrics.width / 2
|
|
226
|
+
: props.x - metrics.width;
|
|
227
|
+
return {
|
|
228
|
+
x: left,
|
|
229
|
+
y: props.y - metrics.ascent,
|
|
230
|
+
width: metrics.width,
|
|
231
|
+
height: metrics.ascent + metrics.descent,
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
function pointInClip(point, props, queries) {
|
|
235
|
+
if (props.path !== undefined) {
|
|
236
|
+
// A clip has no fill rule of its own; both renderers clip a path by
|
|
237
|
+
// its default, which is nonzero on each.
|
|
238
|
+
return queries?.pathContains?.(props.path, 'nonzero', point) ?? false;
|
|
239
|
+
}
|
|
240
|
+
return pointInRect(point, props.x ?? 0, props.y ?? 0, props.width ?? 0, props.height ?? 0);
|
|
241
|
+
}
|
|
242
|
+
/** A shape's own middle, in its own coordinates. */
|
|
243
|
+
function localCentre(node) {
|
|
244
|
+
switch (node.kind) {
|
|
245
|
+
case 'rect':
|
|
246
|
+
case 'rounded-rect': {
|
|
247
|
+
const rect = normalRect(node.props.x ?? 0, node.props.y ?? 0, node.props.width, node.props.height);
|
|
248
|
+
return { x: rect.x + rect.width / 2, y: rect.y + rect.height / 2 };
|
|
249
|
+
}
|
|
250
|
+
case 'circle':
|
|
251
|
+
case 'ellipse':
|
|
252
|
+
return { x: node.props.cx, y: node.props.cy };
|
|
253
|
+
case 'line':
|
|
254
|
+
return { x: (node.props.x1 + node.props.x2) / 2, y: (node.props.y1 + node.props.y2) / 2 };
|
|
255
|
+
case 'text':
|
|
256
|
+
return { x: node.props.x, y: node.props.y };
|
|
257
|
+
default:
|
|
258
|
+
return undefined;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Where a shape is, for something that arrives without a pointer.
|
|
263
|
+
*
|
|
264
|
+
* A keyboard reaching a shape has no cursor, and `CanvasPressEvent` still
|
|
265
|
+
* has to say where. Its middle is the honest answer, and it is computed
|
|
266
|
+
* through the same matrices the hit test walks so that focusing a shape
|
|
267
|
+
* and clicking it report the same place.
|
|
268
|
+
*/
|
|
269
|
+
export function canvasNodePoint(scene, id, viewport) {
|
|
270
|
+
const rootMatrix = viewportMatrix(viewport);
|
|
271
|
+
if (!rootMatrix)
|
|
272
|
+
return undefined;
|
|
273
|
+
const visit = (nodes, matrix) => {
|
|
274
|
+
for (const node of nodes) {
|
|
275
|
+
if (node.kind === 'group') {
|
|
276
|
+
const found = visit(node.children, multiply(matrix, groupMatrix(node.props.transform)));
|
|
277
|
+
if (found)
|
|
278
|
+
return found;
|
|
279
|
+
continue;
|
|
280
|
+
}
|
|
281
|
+
if (node.kind === 'clip') {
|
|
282
|
+
const found = visit(node.children, matrix);
|
|
283
|
+
if (found)
|
|
284
|
+
return found;
|
|
285
|
+
continue;
|
|
286
|
+
}
|
|
287
|
+
if (node.id !== id)
|
|
288
|
+
continue;
|
|
289
|
+
const centre = localCentre(node);
|
|
290
|
+
if (!centre)
|
|
291
|
+
return undefined;
|
|
292
|
+
// `matrix` already carries the viewport, so the ancestor transforms
|
|
293
|
+
// give the scene point and the whole thing gives the surface one.
|
|
294
|
+
const sceneMatrix = invert(rootMatrix);
|
|
295
|
+
const surfacePoint = apply(matrix, centre);
|
|
296
|
+
return {
|
|
297
|
+
point: sceneMatrix ? apply(sceneMatrix, surfacePoint) : centre,
|
|
298
|
+
surfacePoint,
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
return undefined;
|
|
302
|
+
};
|
|
303
|
+
return visit(scene, rootMatrix);
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Finds the topmost interactive geometry at a logical surface point.
|
|
307
|
+
*
|
|
308
|
+
* Rectangle clips, the four closed primitive shapes, and lines are
|
|
309
|
+
* portable today. Paths and path clips deliberately refuse hits until both
|
|
310
|
+
* renderers can implement the same contract.
|
|
311
|
+
*/
|
|
312
|
+
export function hitTestCanvas(scene, surfacePoint, viewport, isInteractive, queries) {
|
|
313
|
+
const rootMatrix = viewportMatrix(viewport);
|
|
314
|
+
if (!rootMatrix)
|
|
315
|
+
return undefined;
|
|
316
|
+
const rootInverse = invert(rootMatrix);
|
|
317
|
+
if (!rootInverse)
|
|
318
|
+
return undefined;
|
|
319
|
+
const scenePoint = apply(rootInverse, surfacePoint);
|
|
320
|
+
const visit = (nodes, matrix) => {
|
|
321
|
+
for (let index = nodes.length - 1; index >= 0; index -= 1) {
|
|
322
|
+
const node = nodes[index];
|
|
323
|
+
if (!node)
|
|
324
|
+
continue;
|
|
325
|
+
if (node.kind === 'group') {
|
|
326
|
+
const hit = visit(node.children, multiply(matrix, groupMatrix(node.props.transform)));
|
|
327
|
+
if (hit)
|
|
328
|
+
return hit;
|
|
329
|
+
continue;
|
|
330
|
+
}
|
|
331
|
+
if (node.kind === 'clip') {
|
|
332
|
+
const inverse = invert(matrix);
|
|
333
|
+
if (!inverse || !pointInClip(apply(inverse, surfacePoint), node.props, queries))
|
|
334
|
+
continue;
|
|
335
|
+
const hit = visit(node.children, matrix);
|
|
336
|
+
if (hit)
|
|
337
|
+
return hit;
|
|
338
|
+
continue;
|
|
339
|
+
}
|
|
340
|
+
if (!node.id || !isInteractive(node.id))
|
|
341
|
+
continue;
|
|
342
|
+
const inverse = invert(matrix);
|
|
343
|
+
if (!inverse)
|
|
344
|
+
continue;
|
|
345
|
+
const localPoint = apply(inverse, surfacePoint);
|
|
346
|
+
if (pointInNode(node, localPoint, queries)) {
|
|
347
|
+
return { id: node.id, point: scenePoint, localPoint };
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
return undefined;
|
|
351
|
+
};
|
|
352
|
+
return visit(scene, rootMatrix);
|
|
353
|
+
}
|
|
354
|
+
//# sourceMappingURL=hit-test.js.map
|