@meonode/canvas 1.0.0-beta.1

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 (65) hide show
  1. package/CONTRIBUTING.md +75 -0
  2. package/LICENSE +21 -0
  3. package/Readme.md +382 -0
  4. package/dist/cjs/canvas/canvas.helper.d.ts +57 -0
  5. package/dist/cjs/canvas/canvas.helper.d.ts.map +1 -0
  6. package/dist/cjs/canvas/canvas.helper.js +239 -0
  7. package/dist/cjs/canvas/canvas.helper.js.map +1 -0
  8. package/dist/cjs/canvas/canvas.type.d.ts +657 -0
  9. package/dist/cjs/canvas/canvas.type.d.ts.map +1 -0
  10. package/dist/cjs/canvas/grid.canvas.util.d.ts +39 -0
  11. package/dist/cjs/canvas/grid.canvas.util.d.ts.map +1 -0
  12. package/dist/cjs/canvas/grid.canvas.util.js +263 -0
  13. package/dist/cjs/canvas/grid.canvas.util.js.map +1 -0
  14. package/dist/cjs/canvas/image.canvas.util.d.ts +34 -0
  15. package/dist/cjs/canvas/image.canvas.util.d.ts.map +1 -0
  16. package/dist/cjs/canvas/image.canvas.util.js +310 -0
  17. package/dist/cjs/canvas/image.canvas.util.js.map +1 -0
  18. package/dist/cjs/canvas/layout.canvas.util.d.ts +123 -0
  19. package/dist/cjs/canvas/layout.canvas.util.d.ts.map +1 -0
  20. package/dist/cjs/canvas/layout.canvas.util.js +785 -0
  21. package/dist/cjs/canvas/layout.canvas.util.js.map +1 -0
  22. package/dist/cjs/canvas/root.canvas.util.d.ts +42 -0
  23. package/dist/cjs/canvas/root.canvas.util.d.ts.map +1 -0
  24. package/dist/cjs/canvas/root.canvas.util.js +140 -0
  25. package/dist/cjs/canvas/root.canvas.util.js.map +1 -0
  26. package/dist/cjs/canvas/text.canvas.util.d.ts +148 -0
  27. package/dist/cjs/canvas/text.canvas.util.d.ts.map +1 -0
  28. package/dist/cjs/canvas/text.canvas.util.js +1112 -0
  29. package/dist/cjs/canvas/text.canvas.util.js.map +1 -0
  30. package/dist/cjs/constant/common.const.d.ts +37 -0
  31. package/dist/cjs/constant/common.const.d.ts.map +1 -0
  32. package/dist/cjs/constant/common.const.js +51 -0
  33. package/dist/cjs/constant/common.const.js.map +1 -0
  34. package/dist/cjs/index.d.ts +7 -0
  35. package/dist/cjs/index.d.ts.map +1 -0
  36. package/dist/cjs/index.js +31 -0
  37. package/dist/cjs/index.js.map +1 -0
  38. package/dist/esm/canvas/canvas.helper.d.ts +57 -0
  39. package/dist/esm/canvas/canvas.helper.d.ts.map +1 -0
  40. package/dist/esm/canvas/canvas.helper.js +214 -0
  41. package/dist/esm/canvas/canvas.type.d.ts +657 -0
  42. package/dist/esm/canvas/canvas.type.d.ts.map +1 -0
  43. package/dist/esm/canvas/grid.canvas.util.d.ts +39 -0
  44. package/dist/esm/canvas/grid.canvas.util.d.ts.map +1 -0
  45. package/dist/esm/canvas/grid.canvas.util.js +259 -0
  46. package/dist/esm/canvas/image.canvas.util.d.ts +34 -0
  47. package/dist/esm/canvas/image.canvas.util.d.ts.map +1 -0
  48. package/dist/esm/canvas/image.canvas.util.js +306 -0
  49. package/dist/esm/canvas/layout.canvas.util.d.ts +123 -0
  50. package/dist/esm/canvas/layout.canvas.util.d.ts.map +1 -0
  51. package/dist/esm/canvas/layout.canvas.util.js +777 -0
  52. package/dist/esm/canvas/root.canvas.util.d.ts +42 -0
  53. package/dist/esm/canvas/root.canvas.util.d.ts.map +1 -0
  54. package/dist/esm/canvas/root.canvas.util.js +116 -0
  55. package/dist/esm/canvas/text.canvas.util.d.ts +148 -0
  56. package/dist/esm/canvas/text.canvas.util.d.ts.map +1 -0
  57. package/dist/esm/canvas/text.canvas.util.js +1108 -0
  58. package/dist/esm/constant/common.const.d.ts +37 -0
  59. package/dist/esm/constant/common.const.d.ts.map +1 -0
  60. package/dist/esm/constant/common.const.js +23 -0
  61. package/dist/esm/index.d.ts +7 -0
  62. package/dist/esm/index.d.ts.map +1 -0
  63. package/dist/esm/index.js +7 -0
  64. package/dist/meonode-canvas-1.0.0-beta.1.tgz +0 -0
  65. package/package.json +79 -0
@@ -0,0 +1,777 @@
1
+ import { Canvas } from 'skia-canvas';
2
+ import { parsePercentage, parseBorderRadius, drawRoundedRectPath, drawBorders } from './canvas.helper.js';
3
+ import { omit } from 'lodash-es';
4
+ import tinycolor from 'tinycolor2';
5
+ import { Style } from '../constant/common.const.js';
6
+ import YogaTypes__default from 'yoga-layout';
7
+
8
+ /**
9
+ * @class BoxNode
10
+ * @classdesc Base node class for rendering rectangular boxes with layout, styling, and children.
11
+ * It uses the Yoga layout engine for positioning and sizing.
12
+ */
13
+ class BoxNode {
14
+ /**
15
+ * @property {Partial<BoxProps>} initialProps - Original props passed to the constructor before any modifications.
16
+ */
17
+ initialProps;
18
+ /**
19
+ * @property {Node} node - The Yoga layout engine node.
20
+ */
21
+ node;
22
+ /**
23
+ * @property {BoxNode[]} children - Child nodes.
24
+ */
25
+ children;
26
+ /**
27
+ * @property {BoxProps & BaseProps} props - Current props including defaults and inherited values.
28
+ */
29
+ props;
30
+ /**
31
+ * @property {string} name - Node type name.
32
+ */
33
+ name;
34
+ /**
35
+ * @property {string} key - Unique node identifier.
36
+ */
37
+ key;
38
+ /**
39
+ * Creates a new BoxNode instance
40
+ * @param props - Initial box properties and styling
41
+ */
42
+ constructor(props = {}) {
43
+ const children = (Array.isArray(props?.children) ? props.children : [props.children]).filter(child => child);
44
+ this.initialProps = { ...props, children };
45
+ this.node = YogaTypes__default.Node.create();
46
+ this.children = [];
47
+ this.props = {
48
+ key: this.key,
49
+ borderColor: 'black',
50
+ borderStyle: Style.Border.Solid,
51
+ boxSizing: Style.BoxSizing.BorderBox,
52
+ opacity: 1,
53
+ flexShrink: 1,
54
+ ...this.initialProps,
55
+ };
56
+ this.name = this.props.name || 'Box';
57
+ this.key = this.props.key || `${this.name}-0`;
58
+ this.setLayout(this.props);
59
+ }
60
+ /**
61
+ * Processes and appends any children passed in the initial props.
62
+ */
63
+ processInitialChildren() {
64
+ if (this.props.children) {
65
+ const childrenToAdd = Array.isArray(this.props.children) ? this.props.children : [this.props.children];
66
+ childrenToAdd.forEach((child, index) => {
67
+ if (child) {
68
+ this.appendChild(child, index);
69
+ }
70
+ });
71
+ }
72
+ }
73
+ /**
74
+ * Inherits styles from the parent node.
75
+ * @param {BoxProps & BaseProps} parentProps - Parent node properties to inherit from.
76
+ */
77
+ resolveInheritedStyles(parentProps) {
78
+ if (parentProps.key) {
79
+ this.key = `${parentProps.key}-${this.key}`;
80
+ this.props.key = this.key;
81
+ }
82
+ const inheritableKeys = [
83
+ 'fontSize',
84
+ 'fontFamily',
85
+ 'fontWeight',
86
+ 'fontStyle',
87
+ 'color',
88
+ 'textAlign',
89
+ 'verticalAlign',
90
+ 'lineHeight',
91
+ 'lineGap',
92
+ 'letterSpacing',
93
+ 'wordSpacing',
94
+ 'textDecoration',
95
+ 'maxLines',
96
+ 'fontVariant',
97
+ ];
98
+ for (const key of inheritableKeys) {
99
+ if (this.initialProps[key] === undefined && parentProps[key] !== undefined) {
100
+ this.props[key] = parentProps[key];
101
+ }
102
+ }
103
+ if (!this.node.isDirty()) {
104
+ this.node.markDirty();
105
+ }
106
+ }
107
+ /**
108
+ * Applies node type-specific default values after inheritance.
109
+ */
110
+ applyDefaults() {
111
+ // Base implementation does nothing; subclasses can override.
112
+ }
113
+ /**
114
+ * Appends a child node at the specified index.
115
+ * @param {BoxNode} child - Child node to append.
116
+ * @param index - Index to insert child at
117
+ */
118
+ appendChild(child, index) {
119
+ if (!child || !child.node) {
120
+ console.warn('Attempted to append an invalid child node.', child);
121
+ return;
122
+ }
123
+ child.resolveInheritedStyles(omit(this.props, 'children'));
124
+ child.applyDefaults();
125
+ this.children.push(child);
126
+ this.node.insertChild(child.node, index);
127
+ child.processInitialChildren();
128
+ }
129
+ /**
130
+ * Performs final layout adjustments recursively after the main layout calculation.
131
+ * @returns {boolean} Whether any node was marked as dirty during finalization.
132
+ */
133
+ finalizeLayout() {
134
+ let wasDirty = false;
135
+ this.updateLayoutBasedOnComputedSize();
136
+ if (this.node.isDirty()) {
137
+ wasDirty = true;
138
+ }
139
+ for (const child of this.children) {
140
+ child.finalizeLayout();
141
+ if (child.node.isDirty()) {
142
+ wasDirty = true;
143
+ }
144
+ }
145
+ return wasDirty;
146
+ }
147
+ /**
148
+ * Hook for subclasses to update layout based on computed size.
149
+ */
150
+ updateLayoutBasedOnComputedSize() {
151
+ // Base implementation does nothing; subclasses can override.
152
+ }
153
+ /**
154
+ * Applies layout properties to the Yoga node.
155
+ * @param props - Box properties containing layout values
156
+ */
157
+ setLayout(props) {
158
+ // --- Yoga layout property application ---
159
+ // (This entire block remains unchanged as it interacts with Yoga, not the canvas library)
160
+ const { width, height, minWidth, minHeight, maxWidth, maxHeight, flexDirection, justifyContent, alignItems, alignSelf, alignContent, flexGrow, flexShrink, flexBasis, positionType, position, gap, margin, padding, border, aspectRatio, overflow, display, boxSizing = Style.BoxSizing.BorderBox, direction = Style.Direction.LTR, flexWrap, } = props;
161
+ if (width !== undefined)
162
+ this.node.setWidth(width);
163
+ if (height !== undefined)
164
+ this.node.setHeight(height);
165
+ if (minWidth !== undefined)
166
+ this.node.setMinWidth(minWidth);
167
+ if (minHeight !== undefined)
168
+ this.node.setMinHeight(minHeight);
169
+ if (maxWidth !== undefined)
170
+ this.node.setMaxWidth(maxWidth);
171
+ if (maxHeight !== undefined)
172
+ this.node.setMaxHeight(maxHeight);
173
+ if (flexDirection !== undefined)
174
+ this.node.setFlexDirection(flexDirection);
175
+ if (justifyContent !== undefined)
176
+ this.node.setJustifyContent(justifyContent);
177
+ if (alignItems !== undefined)
178
+ this.node.setAlignItems(alignItems);
179
+ if (alignSelf !== undefined)
180
+ this.node.setAlignSelf(alignSelf);
181
+ if (alignContent !== undefined)
182
+ this.node.setAlignContent(alignContent);
183
+ if (flexGrow !== undefined)
184
+ this.node.setFlexGrow(flexGrow);
185
+ if (flexShrink !== undefined)
186
+ this.node.setFlexShrink(flexShrink);
187
+ if (positionType !== undefined)
188
+ this.node.setPositionType(positionType);
189
+ if (flexBasis !== undefined)
190
+ this.node.setFlexBasis(flexBasis);
191
+ if (position) {
192
+ if (typeof position === 'number') {
193
+ this.node.setPosition(Style.Edge.All, position);
194
+ }
195
+ else if (typeof position === 'string' && position.endsWith('%')) {
196
+ this.node.setPositionPercent(Style.Edge.All, parseFloat(position));
197
+ }
198
+ else {
199
+ for (const [edge, value] of Object.entries(position)) {
200
+ if (edge in Style.Edge) {
201
+ if (typeof value === 'string' && value.endsWith('%')) {
202
+ this.node.setPositionPercent(Style.Edge[edge], parseFloat(value));
203
+ }
204
+ else {
205
+ this.node.setPosition(Style.Edge[edge], value);
206
+ }
207
+ }
208
+ }
209
+ }
210
+ }
211
+ if (gap) {
212
+ if (typeof gap === 'number') {
213
+ this.node.setGap(Style.Gutter.All, gap);
214
+ }
215
+ else if (typeof gap === 'string' && gap.endsWith('%')) {
216
+ this.node.setGapPercent(Style.Gutter.All, parseFloat(gap));
217
+ }
218
+ else {
219
+ for (const [gutter, value] of Object.entries(gap)) {
220
+ if (gutter in Style.Gutter) {
221
+ if (typeof value === 'string' && value.endsWith('%')) {
222
+ this.node.setGapPercent(Style.Gutter[gutter], parseFloat(value));
223
+ }
224
+ else {
225
+ this.node.setGap(Style.Gutter[gutter], value);
226
+ }
227
+ }
228
+ }
229
+ }
230
+ }
231
+ if (margin) {
232
+ if (typeof margin === 'number' || margin === 'auto') {
233
+ this.node.setMargin(Style.Edge.All, margin);
234
+ }
235
+ else if (typeof margin === 'string' && margin.endsWith('%')) {
236
+ this.node.setMarginPercent(Style.Edge.All, parseFloat(margin));
237
+ }
238
+ else {
239
+ for (const [edge, value] of Object.entries(margin)) {
240
+ if (edge in Style.Edge) {
241
+ if (typeof value === 'string' && value.endsWith('%')) {
242
+ this.node.setMarginPercent(Style.Edge[edge], parseFloat(value));
243
+ }
244
+ else {
245
+ this.node.setMargin(Style.Edge[edge], value);
246
+ }
247
+ }
248
+ }
249
+ }
250
+ }
251
+ if (padding) {
252
+ if (typeof padding === 'number') {
253
+ this.node.setPadding(Style.Edge.All, padding);
254
+ }
255
+ else if (typeof padding === 'string' && padding.endsWith('%')) {
256
+ this.node.setPaddingPercent(Style.Edge.All, parseFloat(padding));
257
+ }
258
+ else {
259
+ for (const [edge, value] of Object.entries(padding)) {
260
+ if (edge in Style.Edge) {
261
+ if (typeof value === 'string' && value.endsWith('%')) {
262
+ this.node.setPaddingPercent(Style.Edge[edge], parseFloat(value));
263
+ }
264
+ else {
265
+ this.node.setPadding(Style.Edge[edge], value);
266
+ }
267
+ }
268
+ }
269
+ }
270
+ }
271
+ if (border) {
272
+ if (typeof border === 'number') {
273
+ this.node.setBorder(Style.Edge.All, border);
274
+ }
275
+ else {
276
+ for (const [edge, value] of Object.entries(border)) {
277
+ if (edge in Style.Edge)
278
+ this.node.setBorder(Style.Edge[edge], value);
279
+ }
280
+ }
281
+ }
282
+ if (aspectRatio !== undefined)
283
+ this.node.setAspectRatio(aspectRatio);
284
+ if (overflow !== undefined)
285
+ this.node.setOverflow(overflow);
286
+ if (display !== undefined)
287
+ this.node.setDisplay(display);
288
+ if (boxSizing !== undefined)
289
+ this.node.setBoxSizing(boxSizing);
290
+ if (direction !== undefined)
291
+ this.node.setDirection(direction);
292
+ if (flexWrap !== undefined)
293
+ this.node.setFlexWrap(flexWrap);
294
+ // --- End Yoga layout property application ---
295
+ }
296
+ /**
297
+ * Renders the node and its children to the canvas.
298
+ * @param {CanvasRenderingContext2D} ctx - Canvas rendering context (from skia-canvas).
299
+ * @param {number} offsetX - X offset for rendering.
300
+ * @param {number} offsetY - Y offset for rendering.
301
+ */
302
+ render(ctx, offsetX = 0, offsetY = 0) {
303
+ const layout = this.node.getComputedLayout();
304
+ const x = layout.left + offsetX;
305
+ const y = layout.top + offsetY;
306
+ const width = layout.width;
307
+ const height = layout.height;
308
+ // Exit early if the node is invisible or has no dimensions.
309
+ if (width <= 0 || height <= 0 || this.props.display === Style.Display.None) {
310
+ return;
311
+ }
312
+ // --- Opacity Setup ---
313
+ const desiredOpacity = Math.max(0, Math.min(1, this.props.opacity ?? 1));
314
+ let originalAlpha = undefined;
315
+ let appliedOpacity = false;
316
+ if (desiredOpacity < 1) {
317
+ originalAlpha = ctx.globalAlpha;
318
+ ctx.globalAlpha = originalAlpha * desiredOpacity;
319
+ appliedOpacity = true;
320
+ }
321
+ // --- End Opacity Setup ---
322
+ try {
323
+ // --- Transformation Setup ---
324
+ const transform = this.props.transform;
325
+ const needsTransform = transform &&
326
+ (transform.translateX ||
327
+ transform.translateY ||
328
+ transform.rotate ||
329
+ transform.scale ||
330
+ transform.scaleX ||
331
+ transform.scaleY);
332
+ let savedContextForTransform = false;
333
+ if (needsTransform) {
334
+ ctx.save();
335
+ savedContextForTransform = true;
336
+ const originXRaw = transform.originX ?? '50%';
337
+ const originYRaw = transform.originY ?? '50%';
338
+ const originOffsetX = parsePercentage(originXRaw, width);
339
+ const originOffsetY = parsePercentage(originYRaw, height);
340
+ const originAbsX = x + originOffsetX;
341
+ const originAbsY = y + originOffsetY;
342
+ ctx.translate(originAbsX, originAbsY);
343
+ if (transform.translateX || transform.translateY) {
344
+ const tx = parsePercentage(transform.translateX, width);
345
+ const ty = parsePercentage(transform.translateY, height);
346
+ if (tx !== 0 || ty !== 0)
347
+ ctx.translate(tx, ty);
348
+ }
349
+ if (transform.rotate) {
350
+ ctx.rotate((transform.rotate * Math.PI) / 180);
351
+ }
352
+ if (transform.scale || transform.scaleX || transform.scaleY) {
353
+ const scaleX = transform.scaleX ?? transform.scale ?? 1;
354
+ const scaleY = transform.scaleY ?? transform.scale ?? 1;
355
+ if (scaleX !== 1 || scaleY !== 1)
356
+ ctx.scale(scaleX, scaleY);
357
+ }
358
+ ctx.translate(-originAbsX, -originAbsY);
359
+ }
360
+ // --- End Transformation Setup ---
361
+ // --- Step 1: Render Parent Background/Borders/Content ---
362
+ // This renders the current node's own visual appearance first.
363
+ this._renderContent(ctx, x, y, width, height);
364
+ // --- Step 2: Prepare Children for Stacking ---
365
+ const positionedChildren = [];
366
+ const inFlowChildren = [];
367
+ this.children.forEach((child, index) => {
368
+ // Check if child participates in zIndex stacking
369
+ if (child.props.positionType === Style.PositionType.Absolute && child.props.zIndex !== undefined) {
370
+ positionedChildren.push({
371
+ node: child,
372
+ zIndex: child.props.zIndex,
373
+ originalIndex: index, // Keep original order for tie-breaking
374
+ });
375
+ }
376
+ else {
377
+ inFlowChildren.push(child);
378
+ }
379
+ });
380
+ // Sort positioned children by zIndex, then by original order
381
+ positionedChildren.sort((a, b) => {
382
+ return a.zIndex - b.zIndex || a.originalIndex - b.originalIndex;
383
+ });
384
+ // --- Step 3: Handle Clipping (Applies before drawing children) ---
385
+ let savedContextForClip = false;
386
+ if (this.props.overflow === Style.Overflow.Hidden && (width > 0 || height > 0)) {
387
+ ctx.save();
388
+ savedContextForClip = true;
389
+ const borderLeft = this.node.getComputedBorder(Style.Edge.Left);
390
+ const borderTop = this.node.getComputedBorder(Style.Edge.Top);
391
+ const borderRight = this.node.getComputedBorder(Style.Edge.Right);
392
+ const borderBottom = this.node.getComputedBorder(Style.Edge.Bottom);
393
+ const innerX = x + borderLeft;
394
+ const innerY = y + borderTop;
395
+ const innerWidth = Math.max(0, width - borderLeft - borderRight);
396
+ const innerHeight = Math.max(0, height - borderTop - borderBottom);
397
+ const outerRadii = parseBorderRadius(this.props.borderRadius);
398
+ const innerRadii = {
399
+ TopLeft: Math.max(0, outerRadii.TopLeft - Math.max(borderLeft, borderTop)),
400
+ TopRight: Math.max(0, outerRadii.TopRight - Math.max(borderRight, borderTop)),
401
+ BottomRight: Math.max(0, outerRadii.BottomRight - Math.max(borderRight, borderBottom)),
402
+ BottomLeft: Math.max(0, outerRadii.BottomLeft - Math.max(borderLeft, borderBottom)),
403
+ };
404
+ if (innerWidth > 0 && innerHeight > 0) {
405
+ drawRoundedRectPath(ctx, innerX, innerY, innerWidth, innerHeight, innerRadii);
406
+ ctx.clip();
407
+ }
408
+ else {
409
+ ctx.beginPath();
410
+ ctx.rect(innerX, innerY, 0, 0);
411
+ ctx.clip();
412
+ }
413
+ }
414
+ // --- End Clipping Setup ---
415
+ // --- Step 4: Render Children in Stacking Order ---
416
+ // 4a: Render positioned children with negative zIndex
417
+ for (const item of positionedChildren) {
418
+ if (item.zIndex < 0) {
419
+ // Pass parent's layout origin (x, y) as offset
420
+ item.node.render(ctx, x, y);
421
+ }
422
+ }
423
+ // 4b: Render in-flow children (recursively)
424
+ for (const child of inFlowChildren) {
425
+ // Pass parent's layout origin (x, y) as offset
426
+ child.render(ctx, x, y);
427
+ }
428
+ // 4c: Render positioned children with zero or positive zIndex
429
+ for (const item of positionedChildren) {
430
+ if (item.zIndex >= 0) {
431
+ // Pass parent's layout origin (x, y) as offset
432
+ item.node.render(ctx, x, y);
433
+ }
434
+ }
435
+ // --- End Child Rendering ---
436
+ // --- Step 5: Restore Clipping Context ---
437
+ if (savedContextForClip) {
438
+ ctx.restore();
439
+ }
440
+ // --- End Clipping Restoration ---
441
+ // --- Step 6: Restore Transformation Context ---
442
+ if (savedContextForTransform) {
443
+ ctx.restore();
444
+ }
445
+ // --- End Transformation Restoration ---
446
+ }
447
+ finally {
448
+ // --- Opacity Restoration ---
449
+ if (appliedOpacity && originalAlpha !== undefined) {
450
+ ctx.globalAlpha = originalAlpha;
451
+ }
452
+ // --- End Opacity Restoration ---
453
+ }
454
+ }
455
+ /**
456
+ * Renders the node's visual content including background fills, shadows, and borders.
457
+ * This is an internal method used by the render() pipeline.
458
+ *
459
+ * @param ctx - The skia-canvas 2D rendering context to draw into
460
+ * @param x - The absolute x-coordinate where drawing should begin
461
+ * @param y - The absolute y-coordinate where drawing should begin
462
+ * @param width - The width of the content area to render
463
+ * @param height - The height of the content area to render
464
+ */
465
+ _renderContent(ctx, x, y, width, height) {
466
+ // Calculate border radius values for all corners
467
+ const radii = { TopLeft: 0, TopRight: 0, BottomRight: 0, BottomLeft: 0 };
468
+ if (this.props.borderRadius) {
469
+ // Handle both number and object border radius specifications
470
+ if (typeof this.props.borderRadius === 'number') {
471
+ radii.TopLeft = radii.TopRight = radii.BottomRight = radii.BottomLeft = this.props.borderRadius;
472
+ }
473
+ else {
474
+ // Extract individual corner radii, defaulting to 0 if not specified
475
+ radii.TopLeft = this.props.borderRadius.TopLeft ?? 0;
476
+ radii.TopRight = this.props.borderRadius.TopRight ?? 0;
477
+ radii.BottomRight = this.props.borderRadius.BottomRight ?? 0;
478
+ radii.BottomLeft = this.props.borderRadius.BottomLeft ?? 0;
479
+ }
480
+ // Ensure all radii are non-negative
481
+ radii.TopLeft = Math.max(0, radii.TopLeft);
482
+ radii.TopRight = Math.max(0, radii.TopRight);
483
+ radii.BottomRight = Math.max(0, radii.BottomRight);
484
+ radii.BottomLeft = Math.max(0, radii.BottomLeft);
485
+ }
486
+ // Process shadow configurations
487
+ let shadows = [];
488
+ if (this.props.boxShadow) {
489
+ shadows = Array.isArray(this.props.boxShadow) ? this.props.boxShadow : [this.props.boxShadow];
490
+ }
491
+ // Split shadows into outset (normal) and inset types
492
+ const outsetShadows = shadows.filter(s => !s.inset);
493
+ const insetShadows = shadows.filter(s => s.inset);
494
+ // Determine if background is fully opaque for shadow optimization
495
+ const backgroundColor = this.props.backgroundColor;
496
+ let isOpaque = false;
497
+ if (backgroundColor && !this.props.gradient) {
498
+ const rgba = tinycolor(backgroundColor).toRgb();
499
+ isOpaque = rgba && rgba.a === 1;
500
+ }
501
+ // Render outset shadows if present
502
+ if (outsetShadows.length > 0) {
503
+ const subtractOffset = 0.75;
504
+ if (isOpaque) {
505
+ // Optimized rendering path for opaque backgrounds
506
+ ctx.save();
507
+ ctx.fillStyle = 'black'; // Shadow source color
508
+ for (const shadow of outsetShadows) {
509
+ ctx.shadowColor = shadow.color ?? 'black';
510
+ ctx.shadowOffsetX = shadow.offsetX ?? 0;
511
+ ctx.shadowOffsetY = shadow.offsetY ?? 0;
512
+ ctx.shadowBlur = shadow.blur ?? Math.max(shadow.offsetX ?? 0, shadow.offsetY ?? 0);
513
+ drawRoundedRectPath(ctx, x + subtractOffset / 2, y + subtractOffset / 2, width - subtractOffset, height - subtractOffset, radii);
514
+ ctx.fill();
515
+ }
516
+ ctx.restore();
517
+ }
518
+ else {
519
+ // Complex shadow rendering for transparent/gradient backgrounds
520
+ let maxBlur = 0;
521
+ let maxOffsetX = 0;
522
+ let maxOffsetY = 0;
523
+ // Calculate maximum shadow extents
524
+ for (const shadow of outsetShadows) {
525
+ const currentOffsetX = shadow.offsetX ?? 0;
526
+ const currentOffsetY = shadow.offsetY ?? 0;
527
+ const currentBlur = shadow.blur ?? Math.max(currentOffsetX, currentOffsetY);
528
+ maxBlur = Math.max(maxBlur, currentBlur);
529
+ maxOffsetX = Math.max(maxOffsetX, Math.abs(currentOffsetX));
530
+ maxOffsetY = Math.max(maxOffsetY, Math.abs(currentOffsetY));
531
+ }
532
+ // Calculate offscreen canvas size with padding for shadows
533
+ const blurPaddingMultiplier = 2;
534
+ const shadowPadding = Math.ceil(maxBlur * blurPaddingMultiplier + Math.max(maxOffsetX, maxOffsetY));
535
+ const offscreenWidth = Math.ceil(width + shadowPadding * 2);
536
+ const offscreenHeight = Math.ceil(height + shadowPadding * 2);
537
+ if (offscreenWidth > 0 && offscreenHeight > 0) {
538
+ // Create temporary canvas for shadow composition
539
+ const offscreenCanvas = new Canvas(offscreenWidth, offscreenHeight);
540
+ const offCtx = offscreenCanvas.getContext('2d');
541
+ offCtx.imageSmoothingEnabled = true;
542
+ offCtx.imageSmoothingQuality = 'high';
543
+ const shapeOffsetX = shadowPadding;
544
+ const shapeOffsetY = shadowPadding;
545
+ // Render each shadow individually onto offscreen canvas
546
+ for (const shadow of outsetShadows) {
547
+ offCtx.save();
548
+ const shadowOffsetX = shadow.offsetX ?? 0;
549
+ const shadowOffsetY = shadow.offsetY ?? 0;
550
+ const blur = shadow.blur ?? Math.max(shadowOffsetX, shadowOffsetY);
551
+ offCtx.shadowColor = shadow.color ?? 'black';
552
+ offCtx.shadowOffsetX = shadowOffsetX;
553
+ offCtx.shadowOffsetY = shadowOffsetY;
554
+ offCtx.shadowBlur = Math.max(0, blur);
555
+ drawRoundedRectPath(offCtx, shapeOffsetX + subtractOffset / 2, shapeOffsetY + subtractOffset / 2, width - subtractOffset, height - subtractOffset, radii);
556
+ offCtx.fillStyle = 'rgba(0,0,0,1)';
557
+ offCtx.fill();
558
+ offCtx.restore();
559
+ }
560
+ // Cut out the shape from accumulated shadows
561
+ offCtx.save();
562
+ offCtx.globalCompositeOperation = 'destination-out';
563
+ drawRoundedRectPath(offCtx, shapeOffsetX, shapeOffsetY, width, height, radii);
564
+ offCtx.fillStyle = 'rgba(0,0,0,1)';
565
+ offCtx.fill();
566
+ offCtx.restore();
567
+ // Composite shadow result onto main canvas
568
+ ctx.drawImage(offscreenCanvas, x - shadowPadding, y - shadowPadding);
569
+ }
570
+ }
571
+ }
572
+ // Render background fill (solid color or gradient)
573
+ // This logic uses standard context methods and remains unchanged.
574
+ const hasFill = this.props.gradient || this.props.backgroundColor;
575
+ if (hasFill) {
576
+ let fillStyle = this.props.backgroundColor || 'transparent';
577
+ if (this.props.gradient) {
578
+ const { type = 'linear', colors, direction = 'to-bottom' } = this.props.gradient;
579
+ let grad = null;
580
+ if (colors && colors.length > 0 && width > 0 && height > 0) {
581
+ if (type === 'linear') {
582
+ let x0 = 0, y0 = 0, x1 = 0, y1 = 0;
583
+ let directionIsValid = false;
584
+ if (Array.isArray(direction) && direction.length === 4) {
585
+ [x0, y0, x1, y1] = direction;
586
+ directionIsValid = true;
587
+ }
588
+ else if (typeof direction === 'string') {
589
+ switch (direction.toLowerCase()) {
590
+ case 'to-right':
591
+ x0 = 0;
592
+ y0 = 0;
593
+ x1 = width;
594
+ y1 = 0;
595
+ directionIsValid = true;
596
+ break;
597
+ case 'to-left':
598
+ x0 = width;
599
+ y0 = 0;
600
+ x1 = 0;
601
+ y1 = 0;
602
+ directionIsValid = true;
603
+ break;
604
+ case 'to-bottom':
605
+ x0 = 0;
606
+ y0 = 0;
607
+ x1 = 0;
608
+ y1 = height;
609
+ directionIsValid = true;
610
+ break;
611
+ case 'to-top':
612
+ x0 = 0;
613
+ y0 = height;
614
+ x1 = 0;
615
+ y1 = 0;
616
+ directionIsValid = true;
617
+ break;
618
+ case 'to-top-right':
619
+ x0 = 0;
620
+ y0 = height;
621
+ x1 = width;
622
+ y1 = 0;
623
+ directionIsValid = true;
624
+ break;
625
+ case 'to-top-left':
626
+ x0 = width;
627
+ y0 = height;
628
+ x1 = 0;
629
+ y1 = 0;
630
+ directionIsValid = true;
631
+ break;
632
+ case 'to-bottom-right':
633
+ x0 = 0;
634
+ y0 = 0;
635
+ x1 = width;
636
+ y1 = height;
637
+ directionIsValid = true;
638
+ break;
639
+ case 'to-bottom-left':
640
+ x0 = width;
641
+ y0 = 0;
642
+ x1 = 0;
643
+ y1 = height;
644
+ directionIsValid = true;
645
+ break;
646
+ }
647
+ }
648
+ if (directionIsValid) {
649
+ grad = ctx.createLinearGradient(x + x0, y + y0, x + x1, y + y1);
650
+ }
651
+ else {
652
+ console.warn(`[BoxNode ${this.key}] Invalid linear gradient direction:`, direction);
653
+ }
654
+ }
655
+ else if (type === 'radial') {
656
+ const centerX = x + width / 2;
657
+ const centerY = y + height / 2;
658
+ const r0 = 0;
659
+ const r1 = 0.5 * Math.sqrt(width * width + height * height);
660
+ if (r1 > 0) {
661
+ grad = ctx.createRadialGradient(centerX, centerY, r0, centerX, centerY, r1);
662
+ }
663
+ }
664
+ if (grad) {
665
+ colors.forEach((color, i) => {
666
+ const stop = colors.length > 1 ? Math.max(0, Math.min(1, i / (colors.length - 1))) : 0.5;
667
+ grad.addColorStop(stop, color);
668
+ });
669
+ fillStyle = grad;
670
+ }
671
+ else {
672
+ console.warn(`[BoxNode ${this.key}] Could not create ${type} gradient. Falling back to backgroundColor.`);
673
+ }
674
+ }
675
+ else {
676
+ if (!colors?.length) {
677
+ console.warn(`[BoxNode ${this.key}] Gradient specified but no colors provided. Falling back to backgroundColor.`);
678
+ }
679
+ else {
680
+ console.warn(`[BoxNode ${this.key}] Cannot draw gradient with zero width/height.`);
681
+ }
682
+ }
683
+ }
684
+ if (fillStyle && fillStyle !== 'transparent') {
685
+ ctx.fillStyle = fillStyle;
686
+ drawRoundedRectPath(ctx, x, y, width, height, radii);
687
+ ctx.fill();
688
+ }
689
+ }
690
+ // Render inset shadows
691
+ // This logic uses standard context methods and remains unchanged.
692
+ if (insetShadows.length > 0) {
693
+ for (const shadow of insetShadows) {
694
+ ctx.save();
695
+ const color = shadow.color ?? 'black';
696
+ const shadowOffsetX = shadow.offsetX ?? 0;
697
+ const shadowOffsetY = shadow.offsetY ?? 0;
698
+ const blur = shadow.blur ?? Math.max(shadowOffsetX, shadowOffsetY);
699
+ drawRoundedRectPath(ctx, x, y, width, height, radii);
700
+ ctx.clip();
701
+ ctx.shadowColor = color;
702
+ ctx.shadowOffsetX = shadowOffsetX;
703
+ ctx.shadowOffsetY = shadowOffsetY;
704
+ ctx.shadowBlur = blur;
705
+ ctx.lineWidth = 1; // Minimal line width for the stroke.
706
+ ctx.strokeStyle = 'transparent'; // Stroke color doesn't matter; only the shadow does.
707
+ // Draw a slightly offset path *inside* the clip to generate the inset shadow.
708
+ drawRoundedRectPath(ctx, x - shadowOffsetX, y - shadowOffsetY, width, height, radii);
709
+ ctx.stroke(); // The stroke generates the shadow inside the clipped area.
710
+ ctx.restore();
711
+ }
712
+ }
713
+ // Render border strokes
714
+ // (This logic uses standard context methods via drawBorders helper and remains unchanged)
715
+ drawBorders({
716
+ ctx,
717
+ node: this.node,
718
+ x,
719
+ y,
720
+ width,
721
+ height,
722
+ radii,
723
+ borderColor: this.props.borderColor,
724
+ borderStyle: this.props.borderStyle,
725
+ });
726
+ }
727
+ }
728
+ /**
729
+ * Creates a new BoxNode instance.
730
+ * @param {BoxProps} props - Box properties and configuration.
731
+ * @returns {BoxNode} New BoxNode instance.
732
+ */
733
+ const Box = (props) => new BoxNode(props);
734
+ /**
735
+ * @class ColumnNode
736
+ * Node class for vertical column layout
737
+ */
738
+ class ColumnNode extends BoxNode {
739
+ constructor(props = {}) {
740
+ super({
741
+ display: Style.Display.Flex,
742
+ flexDirection: Style.FlexDirection.Column,
743
+ flexShrink: 1,
744
+ flexBasis: props.flexGrow === 1 ? 0 : undefined,
745
+ ...props,
746
+ });
747
+ }
748
+ }
749
+ /**
750
+ * Creates a new ColumnNode instance.
751
+ * @param {BoxProps} props - Column properties and configuration.
752
+ * @returns {ColumnNode} New ColumnNode instance.
753
+ */
754
+ const Column = (props) => new ColumnNode(props);
755
+ /**
756
+ * @class RowNode
757
+ * @classdesc Node class for horizontal row layout.
758
+ */
759
+ class RowNode extends BoxNode {
760
+ constructor(props = {}) {
761
+ super({
762
+ name: 'Row',
763
+ display: Style.Display.Flex,
764
+ flexDirection: Style.FlexDirection.Row,
765
+ flexShrink: 1, // Default shrink for rows
766
+ ...props,
767
+ });
768
+ }
769
+ }
770
+ /**
771
+ * Creates a new RowNode instance.
772
+ * @param {BoxProps} props - Row properties and configuration.
773
+ * @returns {RowNode} New RowNode instance.
774
+ */
775
+ const Row = (props) => new RowNode(props);
776
+
777
+ export { Box, BoxNode, Column, ColumnNode, Row, RowNode };