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/native.cjs CHANGED
@@ -354,153 +354,203 @@ function svgNumber(value) {
354
354
  const rounded = Math.round(value * 100) / 100;
355
355
  return Number.isInteger(rounded) ? String(rounded) : String(rounded);
356
356
  }
357
- function architecturalCloudScallopCount(perimeter, amplitude) {
358
- const targetScallopLength = Math.max(18, amplitude * 2.45);
359
- let count = Math.max(12, Math.round(perimeter / targetScallopLength));
360
- if (count % 2 === 1) count += 1;
361
- return count;
362
- }
363
- function roundedRectMetrics(width, height, inset, radius) {
364
- const left = inset;
365
- const top = inset;
366
- const right = width - inset;
367
- const bottom = height - inset;
368
- const rectWidth = Math.max(0, right - left);
369
- const rectHeight = Math.max(0, bottom - top);
370
- const normalizedRadius = Math.max(
371
- 0,
372
- Math.min(radius, rectWidth / 2, rectHeight / 2)
357
+ var ARCHITECTURAL_CLOUD_RADIUS = 38;
358
+ var ARCHITECTURAL_CLOUD_TARGET_SPACING = 50;
359
+ var ARCHITECTURAL_CLOUD_FLAT_RADIUS_RATIO = 0.38;
360
+ var ARCHITECTURAL_CLOUD_ROUNDED_RADIUS = 72;
361
+ var ARCHITECTURAL_CLOUD_ROUNDED_MIN_RADIUS = 44;
362
+ var ARCHITECTURAL_CLOUD_ROUNDED_TARGET_SPACING = 98;
363
+ function architecturalCloudCenterCount(edgeLength, radius) {
364
+ const targetSpacing = Math.min(
365
+ ARCHITECTURAL_CLOUD_TARGET_SPACING,
366
+ Math.max(1, radius * 1.3)
373
367
  );
374
- const centerX = width / 2;
375
- const topHalfLength = Math.max(0, right - normalizedRadius - centerX);
376
- const horizontalLength = Math.max(0, rectWidth - normalizedRadius * 2);
377
- const verticalLength = Math.max(0, rectHeight - normalizedRadius * 2);
378
- const arcLength = normalizedRadius * (Math.PI / 2);
368
+ return Math.max(2, Math.round(edgeLength / targetSpacing) + 1);
369
+ }
370
+ function distributeRange(start, end, count) {
371
+ if (count <= 1) return [start];
372
+ const step = (end - start) / (count - 1);
373
+ return Array.from({ length: count }, (_, index) => start + step * index);
374
+ }
375
+ function lineCloudPathSegment(start, end) {
376
+ const dx = end[0] - start[0];
377
+ const dy = end[1] - start[1];
378
+ const length = Math.hypot(dx, dy);
379
379
  return {
380
- left,
381
- top,
382
- right,
383
- bottom,
384
- radius: normalizedRadius,
385
- centerX,
386
- topHalfLength,
387
- horizontalLength,
388
- verticalLength,
389
- arcLength,
390
- perimeter: horizontalLength * 2 + verticalLength * 2 + Math.PI * 2 * normalizedRadius
380
+ length,
381
+ pointAt: (t) => [start[0] + dx * t, start[1] + dy * t]
391
382
  };
392
383
  }
393
- function pointOnLine(startX, startY, endX, endY, t) {
394
- return [startX + (endX - startX) * t, startY + (endY - startY) * t];
384
+ function ellipsePoint(centerX, centerY, radiusX, radiusY, angle) {
385
+ return [centerX + Math.cos(angle) * radiusX, centerY + Math.sin(angle) * radiusY];
395
386
  }
396
- function pointOnArc(centerX, centerY, radius, startAngle, endAngle, t) {
397
- const theta = startAngle + (endAngle - startAngle) * t;
398
- return [centerX + Math.cos(theta) * radius, centerY + Math.sin(theta) * radius];
387
+ function approximateEllipseArcLength(radiusX, radiusY, startAngle, endAngle) {
388
+ const steps = Math.max(
389
+ 4,
390
+ Math.ceil(Math.abs(endAngle - startAngle) / (Math.PI / 16))
391
+ );
392
+ let length = 0;
393
+ let previous = ellipsePoint(0, 0, radiusX, radiusY, startAngle);
394
+ for (let index = 1; index <= steps; index += 1) {
395
+ const angle = startAngle + (endAngle - startAngle) * index / steps;
396
+ const next = ellipsePoint(0, 0, radiusX, radiusY, angle);
397
+ length += Math.hypot(next[0] - previous[0], next[1] - previous[1]);
398
+ previous = next;
399
+ }
400
+ return length;
399
401
  }
400
- function pointOnRoundedRectPath(metrics, distance) {
401
- if (metrics.perimeter <= 0) return [metrics.centerX, metrics.top];
402
- let remaining = (distance % metrics.perimeter + metrics.perimeter) % metrics.perimeter;
403
- const consume = (length) => {
404
- if (length <= 1e-9) return null;
405
- if (remaining <= length) return remaining / length;
406
- remaining -= length;
407
- return null;
402
+ function ellipseCloudPathSegment(centerX, centerY, radiusX, radiusY, startAngle, endAngle) {
403
+ return {
404
+ length: approximateEllipseArcLength(radiusX, radiusY, startAngle, endAngle),
405
+ pointAt: (t) => ellipsePoint(
406
+ centerX,
407
+ centerY,
408
+ radiusX,
409
+ radiusY,
410
+ startAngle + (endAngle - startAngle) * t
411
+ )
408
412
  };
409
- let t = consume(metrics.topHalfLength);
410
- if (t != null) {
411
- return pointOnLine(
412
- metrics.centerX,
413
- metrics.top,
414
- metrics.right - metrics.radius,
415
- metrics.top,
416
- t
417
- );
413
+ }
414
+ function cloudPathPerimeter(segments) {
415
+ const usableSegments = segments.filter((segment) => segment.length > 1e-9);
416
+ return usableSegments.reduce((sum, segment) => sum + segment.length, 0);
417
+ }
418
+ function pointOnCloudPath(segments, distance) {
419
+ const perimeter = cloudPathPerimeter(segments);
420
+ if (perimeter <= 0) return [0, 0];
421
+ let remaining = (distance % perimeter + perimeter) % perimeter;
422
+ for (const segment of segments) {
423
+ if (segment.length <= 1e-9) continue;
424
+ if (remaining <= segment.length) {
425
+ const t = remaining / segment.length;
426
+ return segment.pointAt(t);
427
+ }
428
+ remaining -= segment.length;
418
429
  }
419
- t = consume(metrics.arcLength);
420
- if (t != null) {
421
- return pointOnArc(
422
- metrics.right - metrics.radius,
423
- metrics.top + metrics.radius,
424
- metrics.radius,
430
+ const fallback = segments.find((segment) => segment.length > 1e-9);
431
+ return fallback?.pointAt(0) ?? [0, 0];
432
+ }
433
+ function buildRoundedCapsulePathSegments(width, height, inset) {
434
+ const left = inset;
435
+ const top = inset;
436
+ const right = width - inset;
437
+ const bottom = height - inset;
438
+ const capsuleWidth = Math.max(0, right - left);
439
+ const capsuleHeight = Math.max(0, bottom - top);
440
+ const radius = Math.min(capsuleWidth, capsuleHeight) / 2;
441
+ if (radius <= 0) return [];
442
+ const leftCenterX = left + radius;
443
+ const rightCenterX = right - radius;
444
+ const topCenterY = top + radius;
445
+ const bottomCenterY = bottom - radius;
446
+ return [
447
+ lineCloudPathSegment([leftCenterX, top], [rightCenterX, top]),
448
+ ellipseCloudPathSegment(
449
+ rightCenterX,
450
+ topCenterY,
451
+ radius,
452
+ radius,
425
453
  -Math.PI / 2,
454
+ 0
455
+ ),
456
+ lineCloudPathSegment([right, topCenterY], [right, bottomCenterY]),
457
+ ellipseCloudPathSegment(
458
+ rightCenterX,
459
+ bottomCenterY,
460
+ radius,
461
+ radius,
426
462
  0,
427
- t
428
- );
429
- }
430
- t = consume(metrics.verticalLength);
431
- if (t != null) {
432
- return pointOnLine(
433
- metrics.right,
434
- metrics.top + metrics.radius,
435
- metrics.right,
436
- metrics.bottom - metrics.radius,
437
- t
438
- );
439
- }
440
- t = consume(metrics.arcLength);
441
- if (t != null) {
442
- return pointOnArc(
443
- metrics.right - metrics.radius,
444
- metrics.bottom - metrics.radius,
445
- metrics.radius,
446
- 0,
463
+ Math.PI / 2
464
+ ),
465
+ lineCloudPathSegment([rightCenterX, bottom], [leftCenterX, bottom]),
466
+ ellipseCloudPathSegment(
467
+ leftCenterX,
468
+ bottomCenterY,
469
+ radius,
470
+ radius,
447
471
  Math.PI / 2,
448
- t
449
- );
450
- }
451
- t = consume(metrics.horizontalLength);
452
- if (t != null) {
453
- return pointOnLine(
454
- metrics.right - metrics.radius,
455
- metrics.bottom,
456
- metrics.left + metrics.radius,
457
- metrics.bottom,
458
- t
459
- );
460
- }
461
- t = consume(metrics.arcLength);
462
- if (t != null) {
463
- return pointOnArc(
464
- metrics.left + metrics.radius,
465
- metrics.bottom - metrics.radius,
466
- metrics.radius,
467
- Math.PI / 2,
468
- Math.PI,
469
- t
470
- );
471
- }
472
- t = consume(metrics.verticalLength);
473
- if (t != null) {
474
- return pointOnLine(
475
- metrics.left,
476
- metrics.bottom - metrics.radius,
477
- metrics.left,
478
- metrics.top + metrics.radius,
479
- t
480
- );
481
- }
482
- t = consume(metrics.arcLength);
483
- if (t != null) {
484
- return pointOnArc(
485
- metrics.left + metrics.radius,
486
- metrics.top + metrics.radius,
487
- metrics.radius,
472
+ Math.PI
473
+ ),
474
+ lineCloudPathSegment([left, bottomCenterY], [left, topCenterY]),
475
+ ellipseCloudPathSegment(
476
+ leftCenterX,
477
+ topCenterY,
478
+ radius,
479
+ radius,
488
480
  Math.PI,
489
- Math.PI * 1.5,
490
- t
491
- );
492
- }
493
- t = consume(metrics.topHalfLength);
494
- if (t != null) {
495
- return pointOnLine(
496
- metrics.left + metrics.radius,
497
- metrics.top,
498
- metrics.centerX,
499
- metrics.top,
500
- t
481
+ Math.PI * 1.5
482
+ )
483
+ ];
484
+ }
485
+ function buildRoundedArcCloudPathD(cloudWidth, cloudHeight, center) {
486
+ const minDimension = Math.min(cloudWidth, cloudHeight);
487
+ const radius = Math.min(
488
+ ARCHITECTURAL_CLOUD_ROUNDED_RADIUS,
489
+ Math.max(ARCHITECTURAL_CLOUD_ROUNDED_MIN_RADIUS, minDimension * 0.16)
490
+ );
491
+ const centerPath = buildRoundedCapsulePathSegments(
492
+ cloudWidth,
493
+ cloudHeight,
494
+ radius
495
+ );
496
+ const centerPerimeter = cloudPathPerimeter(centerPath);
497
+ if (centerPerimeter <= 0) return "";
498
+ const lobeCount = Math.max(
499
+ 8,
500
+ Math.round(centerPerimeter / ARCHITECTURAL_CLOUD_ROUNDED_TARGET_SPACING)
501
+ );
502
+ const centers = Array.from(
503
+ { length: lobeCount },
504
+ (_, index) => pointOnCloudPath(centerPath, centerPerimeter * index / lobeCount)
505
+ );
506
+ const points = centers.map((point, index) => {
507
+ const previous = centers[(index - 1 + centers.length) % centers.length] ?? point;
508
+ return cloudCircleIntersection(previous, point, radius, center);
509
+ });
510
+ const [startX, startY] = points[0] ?? [0, 0];
511
+ const segments = [`M${svgNumber(startX)} ${svgNumber(startY)}`];
512
+ for (const [endX, endY] of points.slice(1)) {
513
+ segments.push(
514
+ `A ${svgNumber(radius)} ${svgNumber(radius)} 0 0 1 ${svgNumber(endX)} ${svgNumber(endY)}`
501
515
  );
502
516
  }
503
- return [metrics.centerX, metrics.top];
517
+ segments.push(
518
+ `A ${svgNumber(radius)} ${svgNumber(radius)} 0 0 1 ${svgNumber(startX)} ${svgNumber(startY)}`
519
+ );
520
+ segments.push("Z");
521
+ return segments.join(" ");
522
+ }
523
+ function cloudCircleIntersection(a, b, radius, center) {
524
+ const midX = (a[0] + b[0]) / 2;
525
+ const midY = (a[1] + b[1]) / 2;
526
+ const dx = b[0] - a[0];
527
+ const dy = b[1] - a[1];
528
+ const distance = Math.hypot(dx, dy);
529
+ if (distance <= 1e-9) return [midX, midY];
530
+ const halfDistance = distance / 2;
531
+ const offset = Math.sqrt(
532
+ Math.max(0, radius * radius - halfDistance * halfDistance)
533
+ );
534
+ const normalX = -dy / distance;
535
+ const normalY = dx / distance;
536
+ const first = [midX + normalX * offset, midY + normalY * offset];
537
+ const second = [
538
+ midX - normalX * offset,
539
+ midY - normalY * offset
540
+ ];
541
+ const firstDistance = (first[0] - center[0]) * (first[0] - center[0]) + (first[1] - center[1]) * (first[1] - center[1]);
542
+ const secondDistance = (second[0] - center[0]) * (second[0] - center[0]) + (second[1] - center[1]) * (second[1] - center[1]);
543
+ return firstDistance >= secondDistance ? first : second;
544
+ }
545
+ function cloudEllipseIntersection(a, b, radiusX, radiusY, center) {
546
+ const scaleY = radiusX / radiusY;
547
+ const [x, y] = cloudCircleIntersection(
548
+ [a[0], a[1] * scaleY],
549
+ [b[0], b[1] * scaleY],
550
+ radiusX,
551
+ [center[0], center[1] * scaleY]
552
+ );
553
+ return [x, y / scaleY];
504
554
  }
505
555
  function buildRectSvg(width, height, style = DEFAULT_STROKE_STYLE) {
506
556
  return `<rect width="${width}" height="${height}" fill="none" stroke="${style.stroke}" stroke-width="${style.strokeWidth}" rx="4"${strokeOpacityAttr(style)} />`;
@@ -514,39 +564,63 @@ function buildArchitecturalCloudPathD(width, height, strokeWidth = DEFAULT_STROK
514
564
  const w = Math.max(0, width);
515
565
  const h = Math.max(0, height);
516
566
  if (w <= 0 || h <= 0) return "";
517
- const inset = Math.max(0.5, strokeWidth / 2);
518
- const outerWidth = Math.max(0, w - inset * 2);
519
- const outerHeight = Math.max(0, h - inset * 2);
520
- if (outerWidth <= 0 || outerHeight <= 0) return "";
521
- const amplitude = Math.min(
522
- outerWidth * 0.12,
523
- outerHeight * 0.12,
524
- Math.max(5, Math.min(14, Math.min(w, h) * 0.07))
567
+ const padding = Math.max(0, strokeWidth * 2);
568
+ const cloudWidth = Math.max(0, w - padding);
569
+ const cloudHeight = Math.max(0, h - padding);
570
+ if (cloudWidth <= 0 || cloudHeight <= 0) return "";
571
+ const radiusX = Math.min(
572
+ ARCHITECTURAL_CLOUD_RADIUS,
573
+ cloudWidth * ARCHITECTURAL_CLOUD_FLAT_RADIUS_RATIO
525
574
  );
526
- const radius = Math.min(
527
- outerWidth / 2,
528
- outerHeight / 2,
529
- Math.max(amplitude * 2.5, outerHeight * 0.43)
575
+ const radiusY = Math.min(
576
+ ARCHITECTURAL_CLOUD_RADIUS,
577
+ cloudHeight * ARCHITECTURAL_CLOUD_FLAT_RADIUS_RATIO
578
+ );
579
+ if (radiusX <= 0 || radiusY <= 0) return "";
580
+ const center = [cloudWidth / 2, cloudHeight / 2];
581
+ const leftCenterX = radiusX;
582
+ const rightCenterX = cloudWidth - radiusX;
583
+ const topCenterY = radiusY;
584
+ const bottomCenterY = cloudHeight - radiusY;
585
+ const horizontalCenters = distributeRange(
586
+ leftCenterX,
587
+ rightCenterX,
588
+ architecturalCloudCenterCount(Math.max(0, rightCenterX - leftCenterX), radiusX)
530
589
  );
531
- const outer = roundedRectMetrics(w, h, inset, radius);
532
- const inner = roundedRectMetrics(
533
- w,
534
- h,
535
- inset + amplitude,
536
- Math.max(0, radius - amplitude)
590
+ const verticalCenters = distributeRange(
591
+ topCenterY,
592
+ bottomCenterY,
593
+ architecturalCloudCenterCount(Math.max(0, bottomCenterY - topCenterY), radiusY)
537
594
  );
538
- const scallopCount = architecturalCloudScallopCount(outer.perimeter, amplitude);
539
- const [startX, startY] = pointOnRoundedRectPath(inner, 0);
595
+ if (horizontalCenters.length > 3 && verticalCenters.length > 3) {
596
+ const roundedArcCloudPath = buildRoundedArcCloudPathD(
597
+ cloudWidth,
598
+ cloudHeight,
599
+ center
600
+ );
601
+ if (roundedArcCloudPath !== "") return roundedArcCloudPath;
602
+ }
603
+ const rectangularCenters = [
604
+ ...horizontalCenters.map((x) => [x, topCenterY]),
605
+ ...verticalCenters.slice(1).map((y) => [rightCenterX, y]),
606
+ ...horizontalCenters.slice(0, -1).reverse().map((x) => [x, bottomCenterY]),
607
+ ...verticalCenters.slice(1, -1).reverse().map((y) => [leftCenterX, y])
608
+ ];
609
+ const centers = rectangularCenters;
610
+ const points = centers.map((point, index) => {
611
+ const previous = centers[(index - 1 + centers.length) % centers.length] ?? point;
612
+ return cloudEllipseIntersection(previous, point, radiusX, radiusY, center);
613
+ });
614
+ const [startX, startY] = points[0] ?? [0, 0];
540
615
  const segments = [`M${svgNumber(startX)} ${svgNumber(startY)}`];
541
- for (let index = 0; index < scallopCount; index += 1) {
542
- const controlDistance = (index + 0.5) / scallopCount * outer.perimeter;
543
- const endDistance = (index + 1) / scallopCount * inner.perimeter;
544
- const [controlX, controlY] = pointOnRoundedRectPath(outer, controlDistance);
545
- const [endX, endY] = pointOnRoundedRectPath(inner, endDistance);
616
+ for (const [endX, endY] of points.slice(1)) {
546
617
  segments.push(
547
- `Q${svgNumber(controlX)} ${svgNumber(controlY)} ${svgNumber(endX)} ${svgNumber(endY)}`
618
+ `A ${svgNumber(radiusX)} ${svgNumber(radiusY)} 0 0 1 ${svgNumber(endX)} ${svgNumber(endY)}`
548
619
  );
549
620
  }
621
+ segments.push(
622
+ `A ${svgNumber(radiusX)} ${svgNumber(radiusY)} 0 0 1 ${svgNumber(startX)} ${svgNumber(startY)}`
623
+ );
550
624
  segments.push("Z");
551
625
  return segments.join(" ");
552
626
  }