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