@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.
Files changed (43) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +152 -0
  3. package/dist/accessibility.d.ts +20 -0
  4. package/dist/accessibility.d.ts.map +1 -0
  5. package/dist/accessibility.js +9 -0
  6. package/dist/accessibility.js.map +1 -0
  7. package/dist/hit-test.d.ts +96 -0
  8. package/dist/hit-test.d.ts.map +1 -0
  9. package/dist/hit-test.js +354 -0
  10. package/dist/hit-test.js.map +1 -0
  11. package/dist/index.d.ts +80 -0
  12. package/dist/index.d.ts.map +1 -0
  13. package/dist/index.js +317 -0
  14. package/dist/index.js.map +1 -0
  15. package/dist/index.native.d.ts +83 -0
  16. package/dist/index.native.d.ts.map +1 -0
  17. package/dist/index.native.js +435 -0
  18. package/dist/index.native.js.map +1 -0
  19. package/dist/render-canvas-2d.d.ts +6 -0
  20. package/dist/render-canvas-2d.d.ts.map +1 -0
  21. package/dist/render-canvas-2d.js +213 -0
  22. package/dist/render-canvas-2d.js.map +1 -0
  23. package/dist/scene.d.ts +495 -0
  24. package/dist/scene.d.ts.map +1 -0
  25. package/dist/scene.js +591 -0
  26. package/dist/scene.js.map +1 -0
  27. package/dist/viewport.d.ts +9 -0
  28. package/dist/viewport.d.ts.map +1 -0
  29. package/dist/viewport.js +2 -0
  30. package/dist/viewport.js.map +1 -0
  31. package/dist/wrap-text.d.ts +28 -0
  32. package/dist/wrap-text.d.ts.map +1 -0
  33. package/dist/wrap-text.js +148 -0
  34. package/dist/wrap-text.js.map +1 -0
  35. package/package.json +79 -0
  36. package/src/accessibility.ts +28 -0
  37. package/src/hit-test.ts +499 -0
  38. package/src/index.native.tsx +733 -0
  39. package/src/index.tsx +510 -0
  40. package/src/render-canvas-2d.ts +259 -0
  41. package/src/scene.tsx +1037 -0
  42. package/src/viewport.ts +8 -0
  43. package/src/wrap-text.ts +157 -0
@@ -0,0 +1,259 @@
1
+ import {
2
+ type CanvasPaint,
3
+ type CanvasPaintProps,
4
+ type CanvasScene,
5
+ type CanvasSceneNode,
6
+ type CanvasTransform,
7
+ type ClipProps,
8
+ cssFontShorthand,
9
+ isGradient,
10
+ paintFills,
11
+ paintStrokes,
12
+ unhandledShape,
13
+ } from './scene.tsx'
14
+ import type { CanvasViewport } from './viewport.ts'
15
+ import { textLines } from './wrap-text.ts'
16
+
17
+ export type { CanvasViewport } from './viewport.ts'
18
+
19
+ function applyTransform(context: CanvasRenderingContext2D, transform?: CanvasTransform) {
20
+ if (!transform) return
21
+ const originX = transform.originX ?? 0
22
+ const originY = transform.originY ?? 0
23
+ context.translate(transform.translateX ?? 0, transform.translateY ?? 0)
24
+ if (originX !== 0 || originY !== 0) context.translate(originX, originY)
25
+ if (transform.rotate) context.rotate((transform.rotate * Math.PI) / 180)
26
+ context.scale(transform.scaleX ?? 1, transform.scaleY ?? 1)
27
+ if (originX !== 0 || originY !== 0) context.translate(-originX, -originY)
28
+ }
29
+
30
+ /**
31
+ * A paint as Canvas2D wants it: a colour string, or a gradient object.
32
+ *
33
+ * Built per draw rather than cached. A gradient belongs to the context
34
+ * and to the transform in force when it was made, so one kept across
35
+ * frames would paint against a transform that has moved.
36
+ */
37
+ function paintStyle(
38
+ context: CanvasRenderingContext2D,
39
+ paint: CanvasPaint,
40
+ ): string | CanvasGradient {
41
+ if (!isGradient(paint)) return paint
42
+ const gradient =
43
+ paint.kind === 'linear'
44
+ ? context.createLinearGradient(paint.from.x, paint.from.y, paint.to.x, paint.to.y)
45
+ : // The inner radius is zero and the two centres are the same,
46
+ // which is the single-circle form both platforms share.
47
+ context.createRadialGradient(
48
+ paint.center.x,
49
+ paint.center.y,
50
+ 0,
51
+ paint.center.x,
52
+ paint.center.y,
53
+ paint.radius,
54
+ )
55
+ for (const stop of paint.stops) gradient.addColorStop(stop.offset, stop.color)
56
+ return gradient
57
+ }
58
+
59
+ function applyPaint(context: CanvasRenderingContext2D, paint: CanvasPaintProps) {
60
+ context.globalAlpha *= paint.opacity ?? 1
61
+ if (paint.fill && paint.fill !== 'none') context.fillStyle = paintStyle(context, paint.fill)
62
+ if (paint.stroke && paint.stroke !== 'none') {
63
+ context.strokeStyle = paintStyle(context, paint.stroke)
64
+ }
65
+ if (paint.strokeWidth !== undefined) context.lineWidth = paint.strokeWidth
66
+ if (paint.lineCap) context.lineCap = paint.lineCap
67
+ if (paint.lineJoin) context.lineJoin = paint.lineJoin
68
+ }
69
+
70
+ function paints(
71
+ paint: CanvasPaintProps,
72
+ context: CanvasRenderingContext2D,
73
+ fillRule?: CanvasFillRule,
74
+ ) {
75
+ if (paintFills(paint)) context.fill(fillRule)
76
+ if (paintStrokes(paint)) context.stroke()
77
+ }
78
+
79
+ function pathForClip(context: CanvasRenderingContext2D, props: Omit<ClipProps, 'children'>) {
80
+ if (props.path !== undefined) {
81
+ if (typeof Path2D === 'undefined') {
82
+ throw new Error(
83
+ 'This browser does not support Path2D, which Canvas.Path and path clips require.',
84
+ )
85
+ }
86
+ context.clip(new Path2D(props.path))
87
+ return
88
+ }
89
+ context.beginPath()
90
+ context.rect(props.x ?? 0, props.y ?? 0, props.width ?? 0, props.height ?? 0)
91
+ context.clip()
92
+ }
93
+
94
+ function drawNode(context: CanvasRenderingContext2D, node: CanvasSceneNode) {
95
+ context.save()
96
+ try {
97
+ switch (node.kind) {
98
+ case 'group':
99
+ context.globalAlpha *= node.props.opacity ?? 1
100
+ applyTransform(context, node.props.transform)
101
+ for (const child of node.children) drawNode(context, child)
102
+ return
103
+ case 'clip':
104
+ pathForClip(context, node.props)
105
+ for (const child of node.children) drawNode(context, child)
106
+ return
107
+ case 'rect': {
108
+ applyPaint(context, node.props)
109
+ context.beginPath()
110
+ context.rect(node.props.x ?? 0, node.props.y ?? 0, node.props.width, node.props.height)
111
+ paints(node.props, context)
112
+ return
113
+ }
114
+ case 'rounded-rect': {
115
+ applyPaint(context, node.props)
116
+ context.beginPath()
117
+ const roundRect = (context as Partial<CanvasRenderingContext2D>).roundRect
118
+ if (typeof roundRect === 'function') {
119
+ roundRect.call(
120
+ context,
121
+ node.props.x ?? 0,
122
+ node.props.y ?? 0,
123
+ node.props.width,
124
+ node.props.height,
125
+ node.props.radius,
126
+ )
127
+ } else {
128
+ // Older embedded WebViews get a correct shape, not a hard failure.
129
+ const x = node.props.x ?? 0
130
+ const y = node.props.y ?? 0
131
+ const radius = Math.min(node.props.radius, node.props.width / 2, node.props.height / 2)
132
+ context.moveTo(x + radius, y)
133
+ context.arcTo(
134
+ x + node.props.width,
135
+ y,
136
+ x + node.props.width,
137
+ y + node.props.height,
138
+ radius,
139
+ )
140
+ context.arcTo(
141
+ x + node.props.width,
142
+ y + node.props.height,
143
+ x,
144
+ y + node.props.height,
145
+ radius,
146
+ )
147
+ context.arcTo(x, y + node.props.height, x, y, radius)
148
+ context.arcTo(x, y, x + node.props.width, y, radius)
149
+ context.closePath()
150
+ }
151
+ paints(node.props, context)
152
+ return
153
+ }
154
+ case 'circle':
155
+ applyPaint(context, node.props)
156
+ context.beginPath()
157
+ context.arc(node.props.cx, node.props.cy, node.props.radius, 0, Math.PI * 2)
158
+ paints(node.props, context)
159
+ return
160
+ case 'ellipse':
161
+ applyPaint(context, node.props)
162
+ context.beginPath()
163
+ context.ellipse(
164
+ node.props.cx,
165
+ node.props.cy,
166
+ node.props.radiusX,
167
+ node.props.radiusY,
168
+ 0,
169
+ 0,
170
+ Math.PI * 2,
171
+ )
172
+ paints(node.props, context)
173
+ return
174
+ case 'line':
175
+ applyPaint(context, node.props)
176
+ context.beginPath()
177
+ context.moveTo(node.props.x1, node.props.y1)
178
+ context.lineTo(node.props.x2, node.props.y2)
179
+ // A line is stroke-only; `fill` is accepted as part of the common
180
+ // paint contract but intentionally has no geometrical meaning here.
181
+ if (node.props.stroke !== 'none' && (node.props.strokeWidth ?? 1) > 0) context.stroke()
182
+ return
183
+ case 'text': {
184
+ // `textAlign` is set on the context rather than measured here:
185
+ // that is the renderer aligning against its own metrics, which is
186
+ // the only measurement that can agree with what it rasterises.
187
+ // `textBaseline` is left alone -- alphabetic is its default and
188
+ // is the anchor `TextProps` documents.
189
+ context.font = cssFontShorthand(node.props)
190
+ context.textAlign = node.props.textAlign ?? 'left'
191
+ applyPaint(context, node.props)
192
+ // Wrapped against this context, which is the one about to
193
+ // rasterise the glyphs. The font is set above, so the widths the
194
+ // breaker asks for are the widths it will draw with.
195
+ const lines = textLines(node.props, (run) => context.measureText(run).width)
196
+ for (const line of lines) {
197
+ if (paintFills(node.props)) context.fillText(line.text, node.props.x, line.y)
198
+ if (paintStrokes(node.props)) context.strokeText(line.text, node.props.x, line.y)
199
+ }
200
+ return
201
+ }
202
+ case 'path': {
203
+ if (typeof Path2D === 'undefined') {
204
+ throw new Error('This browser does not support Path2D, which Canvas.Path requires.')
205
+ }
206
+ applyPaint(context, node.props)
207
+ const path = new Path2D(node.props.path)
208
+ const hasFill =
209
+ node.props.fill !== 'none' &&
210
+ (node.props.fill !== undefined || node.props.stroke === undefined)
211
+ const hasStroke = node.props.stroke !== undefined && node.props.stroke !== 'none'
212
+ if (hasFill) context.fill(path, node.props.fillRule)
213
+ if (hasStroke) context.stroke(path)
214
+ return
215
+ }
216
+ default:
217
+ unhandledShape(node, 'the Canvas2D renderer')
218
+ }
219
+ } finally {
220
+ context.restore()
221
+ }
222
+ }
223
+
224
+ /** Draws one immutable scene snapshot. Exported for custom renderers and deterministic tests. */
225
+ export function renderCanvas2D(
226
+ context: CanvasRenderingContext2D,
227
+ scene: CanvasScene,
228
+ viewport: CanvasViewport,
229
+ ) {
230
+ const pixelRatio = Math.max(1, viewport.pixelRatio || 1)
231
+ context.setTransform(1, 0, 0, 1, 0, 0)
232
+ context.clearRect(0, 0, viewport.width * pixelRatio, viewport.height * pixelRatio)
233
+ context.setTransform(pixelRatio, 0, 0, pixelRatio, 0, 0)
234
+
235
+ context.save()
236
+ try {
237
+ const viewBox = viewport.viewBox
238
+ if (viewBox) {
239
+ const [, , viewWidth, viewHeight] = viewBox
240
+ if (viewWidth <= 0 || viewHeight <= 0) return
241
+ const scaleX = viewport.width / viewWidth
242
+ const scaleY = viewport.height / viewHeight
243
+ if ((viewport.fit ?? 'contain') === 'stretch') {
244
+ context.scale(scaleX, scaleY)
245
+ context.translate(-viewBox[0], -viewBox[1])
246
+ } else {
247
+ const scale = Math.min(scaleX, scaleY)
248
+ const offsetX = (viewport.width - viewWidth * scale) / 2
249
+ const offsetY = (viewport.height - viewHeight * scale) / 2
250
+ context.translate(offsetX, offsetY)
251
+ context.scale(scale, scale)
252
+ context.translate(-viewBox[0], -viewBox[1])
253
+ }
254
+ }
255
+ for (const node of scene) drawNode(context, node)
256
+ } finally {
257
+ context.restore()
258
+ }
259
+ }