canvu-react 0.3.33 → 0.3.34

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/react.js CHANGED
@@ -284,153 +284,197 @@ function svgNumber(value) {
284
284
  const rounded = Math.round(value * 100) / 100;
285
285
  return Number.isInteger(rounded) ? String(rounded) : String(rounded);
286
286
  }
287
- function architecturalCloudScallopCount(perimeter, amplitude) {
288
- const targetScallopLength = Math.max(18, amplitude * 2.45);
289
- let count = Math.max(12, Math.round(perimeter / targetScallopLength));
290
- if (count % 2 === 1) count += 1;
291
- return count;
292
- }
293
- function roundedRectMetrics(width, height, inset, radius) {
294
- const left = inset;
295
- const top = inset;
296
- const right = width - inset;
297
- const bottom = height - inset;
298
- const rectWidth = Math.max(0, right - left);
299
- const rectHeight = Math.max(0, bottom - top);
300
- const normalizedRadius = Math.max(
301
- 0,
302
- Math.min(radius, rectWidth / 2, rectHeight / 2)
287
+ function architecturalCloudCenterCount(edgeLength, radius) {
288
+ const targetSpacing = Math.min(
289
+ ARCHITECTURAL_CLOUD_TARGET_SPACING,
290
+ Math.max(1, radius * 1.3)
303
291
  );
304
- const centerX = width / 2;
305
- const topHalfLength = Math.max(0, right - normalizedRadius - centerX);
306
- const horizontalLength = Math.max(0, rectWidth - normalizedRadius * 2);
307
- const verticalLength = Math.max(0, rectHeight - normalizedRadius * 2);
308
- const arcLength = normalizedRadius * (Math.PI / 2);
292
+ return Math.max(2, Math.round(edgeLength / targetSpacing) + 1);
293
+ }
294
+ function distributeRange(start, end, count) {
295
+ if (count <= 1) return [start];
296
+ const step = (end - start) / (count - 1);
297
+ return Array.from({ length: count }, (_, index) => start + step * index);
298
+ }
299
+ function lineCloudPathSegment(start, end) {
300
+ const dx = end[0] - start[0];
301
+ const dy = end[1] - start[1];
302
+ const length = Math.hypot(dx, dy);
309
303
  return {
310
- left,
311
- top,
312
- right,
313
- bottom,
314
- radius: normalizedRadius,
315
- centerX,
316
- topHalfLength,
317
- horizontalLength,
318
- verticalLength,
319
- arcLength,
320
- perimeter: horizontalLength * 2 + verticalLength * 2 + Math.PI * 2 * normalizedRadius
304
+ length,
305
+ pointAt: (t) => [start[0] + dx * t, start[1] + dy * t]
321
306
  };
322
307
  }
323
- function pointOnLine(startX, startY, endX, endY, t) {
324
- return [startX + (endX - startX) * t, startY + (endY - startY) * t];
308
+ function ellipsePoint(centerX, centerY, radiusX, radiusY, angle) {
309
+ return [centerX + Math.cos(angle) * radiusX, centerY + Math.sin(angle) * radiusY];
325
310
  }
326
- function pointOnArc(centerX, centerY, radius, startAngle, endAngle, t) {
327
- const theta = startAngle + (endAngle - startAngle) * t;
328
- return [centerX + Math.cos(theta) * radius, centerY + Math.sin(theta) * radius];
311
+ function approximateEllipseArcLength(radiusX, radiusY, startAngle, endAngle) {
312
+ const steps = Math.max(
313
+ 4,
314
+ Math.ceil(Math.abs(endAngle - startAngle) / (Math.PI / 16))
315
+ );
316
+ let length = 0;
317
+ let previous = ellipsePoint(0, 0, radiusX, radiusY, startAngle);
318
+ for (let index = 1; index <= steps; index += 1) {
319
+ const angle = startAngle + (endAngle - startAngle) * index / steps;
320
+ const next = ellipsePoint(0, 0, radiusX, radiusY, angle);
321
+ length += Math.hypot(next[0] - previous[0], next[1] - previous[1]);
322
+ previous = next;
323
+ }
324
+ return length;
329
325
  }
330
- function pointOnRoundedRectPath(metrics, distance) {
331
- if (metrics.perimeter <= 0) return [metrics.centerX, metrics.top];
332
- let remaining = (distance % metrics.perimeter + metrics.perimeter) % metrics.perimeter;
333
- const consume = (length) => {
334
- if (length <= 1e-9) return null;
335
- if (remaining <= length) return remaining / length;
336
- remaining -= length;
337
- return null;
326
+ function ellipseCloudPathSegment(centerX, centerY, radiusX, radiusY, startAngle, endAngle) {
327
+ return {
328
+ length: approximateEllipseArcLength(radiusX, radiusY, startAngle, endAngle),
329
+ pointAt: (t) => ellipsePoint(
330
+ centerX,
331
+ centerY,
332
+ radiusX,
333
+ radiusY,
334
+ startAngle + (endAngle - startAngle) * t
335
+ )
338
336
  };
339
- let t = consume(metrics.topHalfLength);
340
- if (t != null) {
341
- return pointOnLine(
342
- metrics.centerX,
343
- metrics.top,
344
- metrics.right - metrics.radius,
345
- metrics.top,
346
- t
347
- );
337
+ }
338
+ function cloudPathPerimeter(segments) {
339
+ const usableSegments = segments.filter((segment) => segment.length > 1e-9);
340
+ return usableSegments.reduce((sum, segment) => sum + segment.length, 0);
341
+ }
342
+ function pointOnCloudPath(segments, distance) {
343
+ const perimeter = cloudPathPerimeter(segments);
344
+ if (perimeter <= 0) return [0, 0];
345
+ let remaining = (distance % perimeter + perimeter) % perimeter;
346
+ for (const segment of segments) {
347
+ if (segment.length <= 1e-9) continue;
348
+ if (remaining <= segment.length) {
349
+ const t = remaining / segment.length;
350
+ return segment.pointAt(t);
351
+ }
352
+ remaining -= segment.length;
348
353
  }
349
- t = consume(metrics.arcLength);
350
- if (t != null) {
351
- return pointOnArc(
352
- metrics.right - metrics.radius,
353
- metrics.top + metrics.radius,
354
- metrics.radius,
354
+ const fallback = segments.find((segment) => segment.length > 1e-9);
355
+ return fallback?.pointAt(0) ?? [0, 0];
356
+ }
357
+ function buildRoundedCapsulePathSegments(width, height, inset) {
358
+ const left = inset;
359
+ const top = inset;
360
+ const right = width - inset;
361
+ const bottom = height - inset;
362
+ const capsuleWidth = Math.max(0, right - left);
363
+ const capsuleHeight = Math.max(0, bottom - top);
364
+ const radius = Math.min(capsuleWidth, capsuleHeight) / 2;
365
+ if (radius <= 0) return [];
366
+ const leftCenterX = left + radius;
367
+ const rightCenterX = right - radius;
368
+ const topCenterY = top + radius;
369
+ const bottomCenterY = bottom - radius;
370
+ return [
371
+ lineCloudPathSegment([leftCenterX, top], [rightCenterX, top]),
372
+ ellipseCloudPathSegment(
373
+ rightCenterX,
374
+ topCenterY,
375
+ radius,
376
+ radius,
355
377
  -Math.PI / 2,
378
+ 0
379
+ ),
380
+ lineCloudPathSegment([right, topCenterY], [right, bottomCenterY]),
381
+ ellipseCloudPathSegment(
382
+ rightCenterX,
383
+ bottomCenterY,
384
+ radius,
385
+ radius,
356
386
  0,
357
- t
358
- );
359
- }
360
- t = consume(metrics.verticalLength);
361
- if (t != null) {
362
- return pointOnLine(
363
- metrics.right,
364
- metrics.top + metrics.radius,
365
- metrics.right,
366
- metrics.bottom - metrics.radius,
367
- t
368
- );
369
- }
370
- t = consume(metrics.arcLength);
371
- if (t != null) {
372
- return pointOnArc(
373
- metrics.right - metrics.radius,
374
- metrics.bottom - metrics.radius,
375
- metrics.radius,
376
- 0,
377
- Math.PI / 2,
378
- t
379
- );
380
- }
381
- t = consume(metrics.horizontalLength);
382
- if (t != null) {
383
- return pointOnLine(
384
- metrics.right - metrics.radius,
385
- metrics.bottom,
386
- metrics.left + metrics.radius,
387
- metrics.bottom,
388
- t
389
- );
390
- }
391
- t = consume(metrics.arcLength);
392
- if (t != null) {
393
- return pointOnArc(
394
- metrics.left + metrics.radius,
395
- metrics.bottom - metrics.radius,
396
- metrics.radius,
387
+ Math.PI / 2
388
+ ),
389
+ lineCloudPathSegment([rightCenterX, bottom], [leftCenterX, bottom]),
390
+ ellipseCloudPathSegment(
391
+ leftCenterX,
392
+ bottomCenterY,
393
+ radius,
394
+ radius,
397
395
  Math.PI / 2,
396
+ Math.PI
397
+ ),
398
+ lineCloudPathSegment([left, bottomCenterY], [left, topCenterY]),
399
+ ellipseCloudPathSegment(
400
+ leftCenterX,
401
+ topCenterY,
402
+ radius,
403
+ radius,
398
404
  Math.PI,
399
- t
400
- );
401
- }
402
- t = consume(metrics.verticalLength);
403
- if (t != null) {
404
- return pointOnLine(
405
- metrics.left,
406
- metrics.bottom - metrics.radius,
407
- metrics.left,
408
- metrics.top + metrics.radius,
409
- t
410
- );
411
- }
412
- t = consume(metrics.arcLength);
413
- if (t != null) {
414
- return pointOnArc(
415
- metrics.left + metrics.radius,
416
- metrics.top + metrics.radius,
417
- metrics.radius,
418
- Math.PI,
419
- Math.PI * 1.5,
420
- t
421
- );
422
- }
423
- t = consume(metrics.topHalfLength);
424
- if (t != null) {
425
- return pointOnLine(
426
- metrics.left + metrics.radius,
427
- metrics.top,
428
- metrics.centerX,
429
- metrics.top,
430
- t
405
+ Math.PI * 1.5
406
+ )
407
+ ];
408
+ }
409
+ function buildRoundedArcCloudPathD(cloudWidth, cloudHeight, center) {
410
+ const minDimension = Math.min(cloudWidth, cloudHeight);
411
+ const radius = Math.min(
412
+ ARCHITECTURAL_CLOUD_ROUNDED_RADIUS,
413
+ Math.max(ARCHITECTURAL_CLOUD_ROUNDED_MIN_RADIUS, minDimension * 0.16)
414
+ );
415
+ const centerPath = buildRoundedCapsulePathSegments(
416
+ cloudWidth,
417
+ cloudHeight,
418
+ radius
419
+ );
420
+ const centerPerimeter = cloudPathPerimeter(centerPath);
421
+ if (centerPerimeter <= 0) return "";
422
+ const lobeCount = Math.max(
423
+ 8,
424
+ Math.round(centerPerimeter / ARCHITECTURAL_CLOUD_ROUNDED_TARGET_SPACING)
425
+ );
426
+ const centers = Array.from(
427
+ { length: lobeCount },
428
+ (_, index) => pointOnCloudPath(centerPath, centerPerimeter * index / lobeCount)
429
+ );
430
+ const points = centers.map((point, index) => {
431
+ const previous = centers[(index - 1 + centers.length) % centers.length] ?? point;
432
+ return cloudCircleIntersection(previous, point, radius, center);
433
+ });
434
+ const [startX, startY] = points[0] ?? [0, 0];
435
+ const segments = [`M${svgNumber(startX)} ${svgNumber(startY)}`];
436
+ for (const [endX, endY] of points.slice(1)) {
437
+ segments.push(
438
+ `A ${svgNumber(radius)} ${svgNumber(radius)} 0 0 1 ${svgNumber(endX)} ${svgNumber(endY)}`
431
439
  );
432
440
  }
433
- return [metrics.centerX, metrics.top];
441
+ segments.push(
442
+ `A ${svgNumber(radius)} ${svgNumber(radius)} 0 0 1 ${svgNumber(startX)} ${svgNumber(startY)}`
443
+ );
444
+ segments.push("Z");
445
+ return segments.join(" ");
446
+ }
447
+ function cloudCircleIntersection(a, b, radius, center) {
448
+ const midX = (a[0] + b[0]) / 2;
449
+ const midY = (a[1] + b[1]) / 2;
450
+ const dx = b[0] - a[0];
451
+ const dy = b[1] - a[1];
452
+ const distance = Math.hypot(dx, dy);
453
+ if (distance <= 1e-9) return [midX, midY];
454
+ const halfDistance = distance / 2;
455
+ const offset = Math.sqrt(
456
+ Math.max(0, radius * radius - halfDistance * halfDistance)
457
+ );
458
+ const normalX = -dy / distance;
459
+ const normalY = dx / distance;
460
+ const first = [midX + normalX * offset, midY + normalY * offset];
461
+ const second = [
462
+ midX - normalX * offset,
463
+ midY - normalY * offset
464
+ ];
465
+ const firstDistance = (first[0] - center[0]) * (first[0] - center[0]) + (first[1] - center[1]) * (first[1] - center[1]);
466
+ const secondDistance = (second[0] - center[0]) * (second[0] - center[0]) + (second[1] - center[1]) * (second[1] - center[1]);
467
+ return firstDistance >= secondDistance ? first : second;
468
+ }
469
+ function cloudEllipseIntersection(a, b, radiusX, radiusY, center) {
470
+ const scaleY = radiusX / radiusY;
471
+ const [x, y] = cloudCircleIntersection(
472
+ [a[0], a[1] * scaleY],
473
+ [b[0], b[1] * scaleY],
474
+ radiusX,
475
+ [center[0], center[1] * scaleY]
476
+ );
477
+ return [x, y / scaleY];
434
478
  }
435
479
  function buildRectSvg(width, height, style = DEFAULT_STROKE_STYLE) {
436
480
  return `<rect width="${width}" height="${height}" fill="none" stroke="${style.stroke}" stroke-width="${style.strokeWidth}" rx="4"${strokeOpacityAttr(style)} />`;
@@ -444,39 +488,63 @@ function buildArchitecturalCloudPathD(width, height, strokeWidth = DEFAULT_STROK
444
488
  const w = Math.max(0, width);
445
489
  const h = Math.max(0, height);
446
490
  if (w <= 0 || h <= 0) return "";
447
- const inset = Math.max(0.5, strokeWidth / 2);
448
- const outerWidth = Math.max(0, w - inset * 2);
449
- const outerHeight = Math.max(0, h - inset * 2);
450
- if (outerWidth <= 0 || outerHeight <= 0) return "";
451
- const amplitude = Math.min(
452
- outerWidth * 0.12,
453
- outerHeight * 0.12,
454
- Math.max(5, Math.min(14, Math.min(w, h) * 0.07))
491
+ const padding = Math.max(0, strokeWidth * 2);
492
+ const cloudWidth = Math.max(0, w - padding);
493
+ const cloudHeight = Math.max(0, h - padding);
494
+ if (cloudWidth <= 0 || cloudHeight <= 0) return "";
495
+ const radiusX = Math.min(
496
+ ARCHITECTURAL_CLOUD_RADIUS,
497
+ cloudWidth * ARCHITECTURAL_CLOUD_FLAT_RADIUS_RATIO
455
498
  );
456
- const radius = Math.min(
457
- outerWidth / 2,
458
- outerHeight / 2,
459
- Math.max(amplitude * 2.5, outerHeight * 0.43)
499
+ const radiusY = Math.min(
500
+ ARCHITECTURAL_CLOUD_RADIUS,
501
+ cloudHeight * ARCHITECTURAL_CLOUD_FLAT_RADIUS_RATIO
460
502
  );
461
- const outer = roundedRectMetrics(w, h, inset, radius);
462
- const inner = roundedRectMetrics(
463
- w,
464
- h,
465
- inset + amplitude,
466
- Math.max(0, radius - amplitude)
503
+ if (radiusX <= 0 || radiusY <= 0) return "";
504
+ const center = [cloudWidth / 2, cloudHeight / 2];
505
+ const leftCenterX = radiusX;
506
+ const rightCenterX = cloudWidth - radiusX;
507
+ const topCenterY = radiusY;
508
+ const bottomCenterY = cloudHeight - radiusY;
509
+ const horizontalCenters = distributeRange(
510
+ leftCenterX,
511
+ rightCenterX,
512
+ architecturalCloudCenterCount(Math.max(0, rightCenterX - leftCenterX), radiusX)
467
513
  );
468
- const scallopCount = architecturalCloudScallopCount(outer.perimeter, amplitude);
469
- const [startX, startY] = pointOnRoundedRectPath(inner, 0);
514
+ const verticalCenters = distributeRange(
515
+ topCenterY,
516
+ bottomCenterY,
517
+ architecturalCloudCenterCount(Math.max(0, bottomCenterY - topCenterY), radiusY)
518
+ );
519
+ if (horizontalCenters.length > 3 && verticalCenters.length > 3) {
520
+ const roundedArcCloudPath = buildRoundedArcCloudPathD(
521
+ cloudWidth,
522
+ cloudHeight,
523
+ center
524
+ );
525
+ if (roundedArcCloudPath !== "") return roundedArcCloudPath;
526
+ }
527
+ const rectangularCenters = [
528
+ ...horizontalCenters.map((x) => [x, topCenterY]),
529
+ ...verticalCenters.slice(1).map((y) => [rightCenterX, y]),
530
+ ...horizontalCenters.slice(0, -1).reverse().map((x) => [x, bottomCenterY]),
531
+ ...verticalCenters.slice(1, -1).reverse().map((y) => [leftCenterX, y])
532
+ ];
533
+ const centers = rectangularCenters;
534
+ const points = centers.map((point, index) => {
535
+ const previous = centers[(index - 1 + centers.length) % centers.length] ?? point;
536
+ return cloudEllipseIntersection(previous, point, radiusX, radiusY, center);
537
+ });
538
+ const [startX, startY] = points[0] ?? [0, 0];
470
539
  const segments = [`M${svgNumber(startX)} ${svgNumber(startY)}`];
471
- for (let index = 0; index < scallopCount; index += 1) {
472
- const controlDistance = (index + 0.5) / scallopCount * outer.perimeter;
473
- const endDistance = (index + 1) / scallopCount * inner.perimeter;
474
- const [controlX, controlY] = pointOnRoundedRectPath(outer, controlDistance);
475
- const [endX, endY] = pointOnRoundedRectPath(inner, endDistance);
540
+ for (const [endX, endY] of points.slice(1)) {
476
541
  segments.push(
477
- `Q${svgNumber(controlX)} ${svgNumber(controlY)} ${svgNumber(endX)} ${svgNumber(endY)}`
542
+ `A ${svgNumber(radiusX)} ${svgNumber(radiusY)} 0 0 1 ${svgNumber(endX)} ${svgNumber(endY)}`
478
543
  );
479
544
  }
545
+ segments.push(
546
+ `A ${svgNumber(radiusX)} ${svgNumber(radiusY)} 0 0 1 ${svgNumber(startX)} ${svgNumber(startY)}`
547
+ );
480
548
  segments.push("Z");
481
549
  return segments.join(" ");
482
550
  }
@@ -914,7 +982,7 @@ function createImageFromVectorTrace(id, bounds, imageVectorInnerSvg, imageVector
914
982
  childrenSvg
915
983
  };
916
984
  }
917
- var DEFAULT_STROKE_STYLE, TOOL_FREEHAND_DEFAULTS;
985
+ var DEFAULT_STROKE_STYLE, TOOL_FREEHAND_DEFAULTS, ARCHITECTURAL_CLOUD_RADIUS, ARCHITECTURAL_CLOUD_TARGET_SPACING, ARCHITECTURAL_CLOUD_FLAT_RADIUS_RATIO, ARCHITECTURAL_CLOUD_ROUNDED_RADIUS, ARCHITECTURAL_CLOUD_ROUNDED_MIN_RADIUS, ARCHITECTURAL_CLOUD_ROUNDED_TARGET_SPACING;
918
986
  var init_shape_builders = __esm({
919
987
  "src/scene/shape-builders.ts"() {
920
988
  init_rect();
@@ -930,6 +998,12 @@ var init_shape_builders = __esm({
930
998
  brush: { strokeWidth: 10 },
931
999
  marker: { stroke: "#fde047", strokeWidth: 16, strokeOpacity: 0.5 }
932
1000
  };
1001
+ ARCHITECTURAL_CLOUD_RADIUS = 38;
1002
+ ARCHITECTURAL_CLOUD_TARGET_SPACING = 50;
1003
+ ARCHITECTURAL_CLOUD_FLAT_RADIUS_RATIO = 0.38;
1004
+ ARCHITECTURAL_CLOUD_ROUNDED_RADIUS = 72;
1005
+ ARCHITECTURAL_CLOUD_ROUNDED_MIN_RADIUS = 44;
1006
+ ARCHITECTURAL_CLOUD_ROUNDED_TARGET_SPACING = 98;
933
1007
  }
934
1008
  });
935
1009
 
@@ -3234,7 +3308,7 @@ function ShapeContextMenu({
3234
3308
  }
3235
3309
  return createPortal(menu, document.body);
3236
3310
  }
3237
- var architecturalCloudIconPath = "M11 2.44 Q13.3 1 14.84 2.44 Q17.84 1.4 18.41 3.47 Q20.8 4.72 19.56 7 Q20.8 9.28 18.41 10.53 Q17.84 12.6 14.84 11.56 Q13.3 13 11 11.56 Q8.7 13 7.16 11.56 Q4.16 12.6 3.59 10.53 Q1.2 9.28 2.44 7 Q1.2 4.72 3.59 3.47 Q4.16 1.4 7.16 2.44 Q8.7 1 11 2.44 Z";
3311
+ var architecturalCloudIconPath = "M1.5 12 A 4.94 5.23 0 0 1 9.07 6 A 4.94 5.23 0 0 1 14.93 6 A 4.94 5.23 0 0 1 22.5 12 A 4.94 5.23 0 0 1 14.93 18 A 4.94 5.23 0 0 1 9.07 18 A 4.94 5.23 0 0 1 1.5 12 Z";
3238
3312
  var base = {
3239
3313
  width: 20,
3240
3314
  height: 20,
@@ -3266,7 +3340,7 @@ function IconEllipse(props) {
3266
3340
  return /* @__PURE__ */ jsx("svg", { ...base, ...props, "aria-hidden": true, children: /* @__PURE__ */ jsx("ellipse", { cx: "12", cy: "12", rx: "9", ry: "6" }) });
3267
3341
  }
3268
3342
  function IconArchitecturalCloud(props) {
3269
- return /* @__PURE__ */ jsx("svg", { ...base, ...props, "aria-hidden": true, children: /* @__PURE__ */ jsx("path", { d: architecturalCloudIconPath, transform: "translate(1 5)" }) });
3343
+ return /* @__PURE__ */ jsx("svg", { ...base, ...props, "aria-hidden": true, children: /* @__PURE__ */ jsx("path", { d: architecturalCloudIconPath }) });
3270
3344
  }
3271
3345
  function IconLine(props) {
3272
3346
  return /* @__PURE__ */ jsx("svg", { ...base, ...props, "aria-hidden": true, children: /* @__PURE__ */ jsx("line", { x1: "5", y1: "19", x2: "19", y2: "5" }) });