canvu-react 0.3.32 → 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,24 +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 approximateEllipsePerimeter(rx, ry) {
352
- if (rx <= 0 || ry <= 0) return 0;
353
- return Math.PI * (3 * (rx + ry) - Math.sqrt((3 * rx + ry) * (rx + 3 * ry)));
354
- }
355
- function architecturalCloudScallopCount(rx, ry, amplitude) {
356
- const perimeter = approximateEllipsePerimeter(rx, ry);
357
- const targetScallopLength = Math.max(10, amplitude * 2);
358
- let count = Math.max(12, Math.round(perimeter / targetScallopLength));
359
- if (count % 2 === 1) count += 1;
360
- return count;
361
- }
362
- function pointOnSuperellipse(centerX, centerY, radiusX, radiusY, theta, exponent) {
363
- const cosTheta = Math.cos(theta);
364
- const sinTheta = Math.sin(theta);
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)
361
+ );
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
+ return {
374
+ length,
375
+ pointAt: (t) => [start[0] + dx * t, start[1] + dy * t]
376
+ };
377
+ }
378
+ function ellipsePoint(centerX, centerY, radiusX, radiusY, angle) {
379
+ return [centerX + Math.cos(angle) * radiusX, centerY + Math.sin(angle) * radiusY];
380
+ }
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;
395
+ }
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
+ )
406
+ };
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;
423
+ }
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;
365
440
  return [
366
- centerX + Math.sign(cosTheta) * radiusX * Math.abs(cosTheta) ** (2 / exponent),
367
- centerY + Math.sign(sinTheta) * radiusY * Math.abs(sinTheta) ** (2 / exponent)
441
+ lineCloudPathSegment([leftCenterX, top], [rightCenterX, top]),
442
+ ellipseCloudPathSegment(
443
+ rightCenterX,
444
+ topCenterY,
445
+ radius,
446
+ radius,
447
+ -Math.PI / 2,
448
+ 0
449
+ ),
450
+ lineCloudPathSegment([right, topCenterY], [right, bottomCenterY]),
451
+ ellipseCloudPathSegment(
452
+ rightCenterX,
453
+ bottomCenterY,
454
+ radius,
455
+ radius,
456
+ 0,
457
+ Math.PI / 2
458
+ ),
459
+ lineCloudPathSegment([rightCenterX, bottom], [leftCenterX, bottom]),
460
+ ellipseCloudPathSegment(
461
+ leftCenterX,
462
+ bottomCenterY,
463
+ radius,
464
+ radius,
465
+ Math.PI / 2,
466
+ Math.PI
467
+ ),
468
+ lineCloudPathSegment([left, bottomCenterY], [left, topCenterY]),
469
+ ellipseCloudPathSegment(
470
+ leftCenterX,
471
+ topCenterY,
472
+ radius,
473
+ radius,
474
+ Math.PI,
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)}`
509
+ );
510
+ }
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
368
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];
369
548
  }
370
549
  function buildRectSvg(width, height, style = DEFAULT_STROKE_STYLE) {
371
550
  return `<rect width="${width}" height="${height}" fill="none" stroke="${style.stroke}" stroke-width="${style.strokeWidth}" rx="4"${strokeOpacityAttr(style)} />`;
@@ -379,40 +558,63 @@ function buildArchitecturalCloudPathD(width, height, strokeWidth = DEFAULT_STROK
379
558
  const w = Math.max(0, width);
380
559
  const h = Math.max(0, height);
381
560
  if (w <= 0 || h <= 0) return "";
382
- const inset = Math.max(0.5, strokeWidth / 2);
383
- const outerRx = Math.max(0, w / 2 - inset);
384
- const outerRy = Math.max(0, h / 2 - inset);
385
- if (outerRx <= 0 || outerRy <= 0) return "";
386
- const baseExponent = 3.6;
387
- const amplitude = Math.min(
388
- outerRx * 0.45,
389
- outerRy * 0.45,
390
- Math.max(2, Math.min(7, Math.min(w, h) * 0.035))
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
391
568
  );
392
- const innerRx = Math.max(0, outerRx - amplitude);
393
- const innerRy = Math.max(0, outerRy - amplitude);
394
- const scallopCount = architecturalCloudScallopCount(innerRx, innerRy, amplitude);
395
- const angleStep = Math.PI * 2 / scallopCount;
396
- const cx = w / 2;
397
- const cy = h / 2;
398
- const startAngle = -Math.PI / 2;
399
- const pointOnArchitecturalCloud = (theta, radiusX, radiusY) => pointOnSuperellipse(cx, cy, radiusX, radiusY, theta, baseExponent);
400
- const [startX, startY] = pointOnArchitecturalCloud(startAngle, innerRx, innerRy);
401
- const segments = [`M${svgNumber(startX)} ${svgNumber(startY)}`];
402
- for (let index = 0; index < scallopCount; index += 1) {
403
- const segmentStart = startAngle + index * angleStep;
404
- const segmentMid = segmentStart + angleStep / 2;
405
- const segmentEnd = segmentStart + angleStep;
406
- const [controlX, controlY] = pointOnArchitecturalCloud(
407
- segmentMid,
408
- outerRx,
409
- outerRy
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)
583
+ );
584
+ const verticalCenters = distributeRange(
585
+ topCenterY,
586
+ bottomCenterY,
587
+ architecturalCloudCenterCount(Math.max(0, bottomCenterY - topCenterY), radiusY)
588
+ );
589
+ if (horizontalCenters.length > 3 && verticalCenters.length > 3) {
590
+ const roundedArcCloudPath = buildRoundedArcCloudPathD(
591
+ cloudWidth,
592
+ cloudHeight,
593
+ center
410
594
  );
411
- const [endX, endY] = pointOnArchitecturalCloud(segmentEnd, innerRx, innerRy);
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];
609
+ const segments = [`M${svgNumber(startX)} ${svgNumber(startY)}`];
610
+ for (const [endX, endY] of points.slice(1)) {
412
611
  segments.push(
413
- `Q${svgNumber(controlX)} ${svgNumber(controlY)} ${svgNumber(endX)} ${svgNumber(endY)}`
612
+ `A ${svgNumber(radiusX)} ${svgNumber(radiusY)} 0 0 1 ${svgNumber(endX)} ${svgNumber(endY)}`
414
613
  );
415
614
  }
615
+ segments.push(
616
+ `A ${svgNumber(radiusX)} ${svgNumber(radiusY)} 0 0 1 ${svgNumber(startX)} ${svgNumber(startY)}`
617
+ );
416
618
  segments.push("Z");
417
619
  return segments.join(" ");
418
620
  }