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/tldraw.cjs CHANGED
@@ -228,153 +228,203 @@ function svgNumber(value) {
228
228
  const rounded = Math.round(value * 100) / 100;
229
229
  return Number.isInteger(rounded) ? String(rounded) : String(rounded);
230
230
  }
231
- function architecturalCloudScallopCount(perimeter, amplitude) {
232
- const targetScallopLength = Math.max(18, amplitude * 2.45);
233
- let count = Math.max(12, Math.round(perimeter / targetScallopLength));
234
- if (count % 2 === 1) count += 1;
235
- return count;
236
- }
237
- function roundedRectMetrics(width, height, inset, radius) {
238
- const left = inset;
239
- const top = inset;
240
- const right = width - inset;
241
- const bottom = height - inset;
242
- const rectWidth = Math.max(0, right - left);
243
- const rectHeight = Math.max(0, bottom - top);
244
- const normalizedRadius = Math.max(
245
- 0,
246
- Math.min(radius, rectWidth / 2, rectHeight / 2)
231
+ var ARCHITECTURAL_CLOUD_RADIUS = 38;
232
+ var ARCHITECTURAL_CLOUD_TARGET_SPACING = 50;
233
+ var ARCHITECTURAL_CLOUD_FLAT_RADIUS_RATIO = 0.38;
234
+ var ARCHITECTURAL_CLOUD_ROUNDED_RADIUS = 72;
235
+ var ARCHITECTURAL_CLOUD_ROUNDED_MIN_RADIUS = 44;
236
+ var ARCHITECTURAL_CLOUD_ROUNDED_TARGET_SPACING = 98;
237
+ function architecturalCloudCenterCount(edgeLength, radius) {
238
+ const targetSpacing = Math.min(
239
+ ARCHITECTURAL_CLOUD_TARGET_SPACING,
240
+ Math.max(1, radius * 1.3)
247
241
  );
248
- const centerX = width / 2;
249
- const topHalfLength = Math.max(0, right - normalizedRadius - centerX);
250
- const horizontalLength = Math.max(0, rectWidth - normalizedRadius * 2);
251
- const verticalLength = Math.max(0, rectHeight - normalizedRadius * 2);
252
- const arcLength = normalizedRadius * (Math.PI / 2);
242
+ return Math.max(2, Math.round(edgeLength / targetSpacing) + 1);
243
+ }
244
+ function distributeRange(start, end, count) {
245
+ if (count <= 1) return [start];
246
+ const step = (end - start) / (count - 1);
247
+ return Array.from({ length: count }, (_, index) => start + step * index);
248
+ }
249
+ function lineCloudPathSegment(start, end) {
250
+ const dx = end[0] - start[0];
251
+ const dy = end[1] - start[1];
252
+ const length = Math.hypot(dx, dy);
253
253
  return {
254
- left,
255
- top,
256
- right,
257
- bottom,
258
- radius: normalizedRadius,
259
- centerX,
260
- topHalfLength,
261
- horizontalLength,
262
- verticalLength,
263
- arcLength,
264
- perimeter: horizontalLength * 2 + verticalLength * 2 + Math.PI * 2 * normalizedRadius
254
+ length,
255
+ pointAt: (t) => [start[0] + dx * t, start[1] + dy * t]
265
256
  };
266
257
  }
267
- function pointOnLine(startX, startY, endX, endY, t) {
268
- return [startX + (endX - startX) * t, startY + (endY - startY) * t];
269
- }
270
- function pointOnArc(centerX, centerY, radius, startAngle, endAngle, t) {
271
- const theta = startAngle + (endAngle - startAngle) * t;
272
- return [centerX + Math.cos(theta) * radius, centerY + Math.sin(theta) * radius];
273
- }
274
- function pointOnRoundedRectPath(metrics, distance) {
275
- if (metrics.perimeter <= 0) return [metrics.centerX, metrics.top];
276
- let remaining = (distance % metrics.perimeter + metrics.perimeter) % metrics.perimeter;
277
- const consume = (length) => {
278
- if (length <= 1e-9) return null;
279
- if (remaining <= length) return remaining / length;
280
- remaining -= length;
281
- return null;
258
+ function ellipsePoint(centerX, centerY, radiusX, radiusY, angle) {
259
+ return [centerX + Math.cos(angle) * radiusX, centerY + Math.sin(angle) * radiusY];
260
+ }
261
+ function approximateEllipseArcLength(radiusX, radiusY, startAngle, endAngle) {
262
+ const steps = Math.max(
263
+ 4,
264
+ Math.ceil(Math.abs(endAngle - startAngle) / (Math.PI / 16))
265
+ );
266
+ let length = 0;
267
+ let previous = ellipsePoint(0, 0, radiusX, radiusY, startAngle);
268
+ for (let index = 1; index <= steps; index += 1) {
269
+ const angle = startAngle + (endAngle - startAngle) * index / steps;
270
+ const next = ellipsePoint(0, 0, radiusX, radiusY, angle);
271
+ length += Math.hypot(next[0] - previous[0], next[1] - previous[1]);
272
+ previous = next;
273
+ }
274
+ return length;
275
+ }
276
+ function ellipseCloudPathSegment(centerX, centerY, radiusX, radiusY, startAngle, endAngle) {
277
+ return {
278
+ length: approximateEllipseArcLength(radiusX, radiusY, startAngle, endAngle),
279
+ pointAt: (t) => ellipsePoint(
280
+ centerX,
281
+ centerY,
282
+ radiusX,
283
+ radiusY,
284
+ startAngle + (endAngle - startAngle) * t
285
+ )
282
286
  };
283
- let t = consume(metrics.topHalfLength);
284
- if (t != null) {
285
- return pointOnLine(
286
- metrics.centerX,
287
- metrics.top,
288
- metrics.right - metrics.radius,
289
- metrics.top,
290
- t
291
- );
287
+ }
288
+ function cloudPathPerimeter(segments) {
289
+ const usableSegments = segments.filter((segment) => segment.length > 1e-9);
290
+ return usableSegments.reduce((sum, segment) => sum + segment.length, 0);
291
+ }
292
+ function pointOnCloudPath(segments, distance) {
293
+ const perimeter = cloudPathPerimeter(segments);
294
+ if (perimeter <= 0) return [0, 0];
295
+ let remaining = (distance % perimeter + perimeter) % perimeter;
296
+ for (const segment of segments) {
297
+ if (segment.length <= 1e-9) continue;
298
+ if (remaining <= segment.length) {
299
+ const t = remaining / segment.length;
300
+ return segment.pointAt(t);
301
+ }
302
+ remaining -= segment.length;
292
303
  }
293
- t = consume(metrics.arcLength);
294
- if (t != null) {
295
- return pointOnArc(
296
- metrics.right - metrics.radius,
297
- metrics.top + metrics.radius,
298
- metrics.radius,
304
+ const fallback = segments.find((segment) => segment.length > 1e-9);
305
+ return fallback?.pointAt(0) ?? [0, 0];
306
+ }
307
+ function buildRoundedCapsulePathSegments(width, height, inset) {
308
+ const left = inset;
309
+ const top = inset;
310
+ const right = width - inset;
311
+ const bottom = height - inset;
312
+ const capsuleWidth = Math.max(0, right - left);
313
+ const capsuleHeight = Math.max(0, bottom - top);
314
+ const radius = Math.min(capsuleWidth, capsuleHeight) / 2;
315
+ if (radius <= 0) return [];
316
+ const leftCenterX = left + radius;
317
+ const rightCenterX = right - radius;
318
+ const topCenterY = top + radius;
319
+ const bottomCenterY = bottom - radius;
320
+ return [
321
+ lineCloudPathSegment([leftCenterX, top], [rightCenterX, top]),
322
+ ellipseCloudPathSegment(
323
+ rightCenterX,
324
+ topCenterY,
325
+ radius,
326
+ radius,
299
327
  -Math.PI / 2,
328
+ 0
329
+ ),
330
+ lineCloudPathSegment([right, topCenterY], [right, bottomCenterY]),
331
+ ellipseCloudPathSegment(
332
+ rightCenterX,
333
+ bottomCenterY,
334
+ radius,
335
+ radius,
300
336
  0,
301
- t
302
- );
303
- }
304
- t = consume(metrics.verticalLength);
305
- if (t != null) {
306
- return pointOnLine(
307
- metrics.right,
308
- metrics.top + metrics.radius,
309
- metrics.right,
310
- metrics.bottom - metrics.radius,
311
- t
312
- );
313
- }
314
- t = consume(metrics.arcLength);
315
- if (t != null) {
316
- return pointOnArc(
317
- metrics.right - metrics.radius,
318
- metrics.bottom - metrics.radius,
319
- metrics.radius,
320
- 0,
321
- Math.PI / 2,
322
- t
323
- );
324
- }
325
- t = consume(metrics.horizontalLength);
326
- if (t != null) {
327
- return pointOnLine(
328
- metrics.right - metrics.radius,
329
- metrics.bottom,
330
- metrics.left + metrics.radius,
331
- metrics.bottom,
332
- t
333
- );
334
- }
335
- t = consume(metrics.arcLength);
336
- if (t != null) {
337
- return pointOnArc(
338
- metrics.left + metrics.radius,
339
- metrics.bottom - metrics.radius,
340
- metrics.radius,
337
+ Math.PI / 2
338
+ ),
339
+ lineCloudPathSegment([rightCenterX, bottom], [leftCenterX, bottom]),
340
+ ellipseCloudPathSegment(
341
+ leftCenterX,
342
+ bottomCenterY,
343
+ radius,
344
+ radius,
341
345
  Math.PI / 2,
346
+ Math.PI
347
+ ),
348
+ lineCloudPathSegment([left, bottomCenterY], [left, topCenterY]),
349
+ ellipseCloudPathSegment(
350
+ leftCenterX,
351
+ topCenterY,
352
+ radius,
353
+ radius,
342
354
  Math.PI,
343
- t
344
- );
345
- }
346
- t = consume(metrics.verticalLength);
347
- if (t != null) {
348
- return pointOnLine(
349
- metrics.left,
350
- metrics.bottom - metrics.radius,
351
- metrics.left,
352
- metrics.top + metrics.radius,
353
- t
354
- );
355
- }
356
- t = consume(metrics.arcLength);
357
- if (t != null) {
358
- return pointOnArc(
359
- metrics.left + metrics.radius,
360
- metrics.top + metrics.radius,
361
- metrics.radius,
362
- Math.PI,
363
- Math.PI * 1.5,
364
- t
365
- );
366
- }
367
- t = consume(metrics.topHalfLength);
368
- if (t != null) {
369
- return pointOnLine(
370
- metrics.left + metrics.radius,
371
- metrics.top,
372
- metrics.centerX,
373
- metrics.top,
374
- t
355
+ Math.PI * 1.5
356
+ )
357
+ ];
358
+ }
359
+ function buildRoundedArcCloudPathD(cloudWidth, cloudHeight, center) {
360
+ const minDimension = Math.min(cloudWidth, cloudHeight);
361
+ const radius = Math.min(
362
+ ARCHITECTURAL_CLOUD_ROUNDED_RADIUS,
363
+ Math.max(ARCHITECTURAL_CLOUD_ROUNDED_MIN_RADIUS, minDimension * 0.16)
364
+ );
365
+ const centerPath = buildRoundedCapsulePathSegments(
366
+ cloudWidth,
367
+ cloudHeight,
368
+ radius
369
+ );
370
+ const centerPerimeter = cloudPathPerimeter(centerPath);
371
+ if (centerPerimeter <= 0) return "";
372
+ const lobeCount = Math.max(
373
+ 8,
374
+ Math.round(centerPerimeter / ARCHITECTURAL_CLOUD_ROUNDED_TARGET_SPACING)
375
+ );
376
+ const centers = Array.from(
377
+ { length: lobeCount },
378
+ (_, index) => pointOnCloudPath(centerPath, centerPerimeter * index / lobeCount)
379
+ );
380
+ const points = centers.map((point, index) => {
381
+ const previous = centers[(index - 1 + centers.length) % centers.length] ?? point;
382
+ return cloudCircleIntersection(previous, point, radius, center);
383
+ });
384
+ const [startX, startY] = points[0] ?? [0, 0];
385
+ const segments = [`M${svgNumber(startX)} ${svgNumber(startY)}`];
386
+ for (const [endX, endY] of points.slice(1)) {
387
+ segments.push(
388
+ `A ${svgNumber(radius)} ${svgNumber(radius)} 0 0 1 ${svgNumber(endX)} ${svgNumber(endY)}`
375
389
  );
376
390
  }
377
- return [metrics.centerX, metrics.top];
391
+ segments.push(
392
+ `A ${svgNumber(radius)} ${svgNumber(radius)} 0 0 1 ${svgNumber(startX)} ${svgNumber(startY)}`
393
+ );
394
+ segments.push("Z");
395
+ return segments.join(" ");
396
+ }
397
+ function cloudCircleIntersection(a, b, radius, center) {
398
+ const midX = (a[0] + b[0]) / 2;
399
+ const midY = (a[1] + b[1]) / 2;
400
+ const dx = b[0] - a[0];
401
+ const dy = b[1] - a[1];
402
+ const distance = Math.hypot(dx, dy);
403
+ if (distance <= 1e-9) return [midX, midY];
404
+ const halfDistance = distance / 2;
405
+ const offset = Math.sqrt(
406
+ Math.max(0, radius * radius - halfDistance * halfDistance)
407
+ );
408
+ const normalX = -dy / distance;
409
+ const normalY = dx / distance;
410
+ const first = [midX + normalX * offset, midY + normalY * offset];
411
+ const second = [
412
+ midX - normalX * offset,
413
+ midY - normalY * offset
414
+ ];
415
+ const firstDistance = (first[0] - center[0]) * (first[0] - center[0]) + (first[1] - center[1]) * (first[1] - center[1]);
416
+ const secondDistance = (second[0] - center[0]) * (second[0] - center[0]) + (second[1] - center[1]) * (second[1] - center[1]);
417
+ return firstDistance >= secondDistance ? first : second;
418
+ }
419
+ function cloudEllipseIntersection(a, b, radiusX, radiusY, center) {
420
+ const scaleY = radiusX / radiusY;
421
+ const [x, y] = cloudCircleIntersection(
422
+ [a[0], a[1] * scaleY],
423
+ [b[0], b[1] * scaleY],
424
+ radiusX,
425
+ [center[0], center[1] * scaleY]
426
+ );
427
+ return [x, y / scaleY];
378
428
  }
379
429
  function buildRectSvg(width, height, style = DEFAULT_STROKE_STYLE) {
380
430
  return `<rect width="${width}" height="${height}" fill="none" stroke="${style.stroke}" stroke-width="${style.strokeWidth}" rx="4"${strokeOpacityAttr(style)} />`;
@@ -388,39 +438,63 @@ function buildArchitecturalCloudPathD(width, height, strokeWidth = DEFAULT_STROK
388
438
  const w = Math.max(0, width);
389
439
  const h = Math.max(0, height);
390
440
  if (w <= 0 || h <= 0) return "";
391
- const inset = Math.max(0.5, strokeWidth / 2);
392
- const outerWidth = Math.max(0, w - inset * 2);
393
- const outerHeight = Math.max(0, h - inset * 2);
394
- if (outerWidth <= 0 || outerHeight <= 0) return "";
395
- const amplitude = Math.min(
396
- outerWidth * 0.12,
397
- outerHeight * 0.12,
398
- Math.max(5, Math.min(14, Math.min(w, h) * 0.07))
441
+ const padding = Math.max(0, strokeWidth * 2);
442
+ const cloudWidth = Math.max(0, w - padding);
443
+ const cloudHeight = Math.max(0, h - padding);
444
+ if (cloudWidth <= 0 || cloudHeight <= 0) return "";
445
+ const radiusX = Math.min(
446
+ ARCHITECTURAL_CLOUD_RADIUS,
447
+ cloudWidth * ARCHITECTURAL_CLOUD_FLAT_RADIUS_RATIO
399
448
  );
400
- const radius = Math.min(
401
- outerWidth / 2,
402
- outerHeight / 2,
403
- Math.max(amplitude * 2.5, outerHeight * 0.43)
449
+ const radiusY = Math.min(
450
+ ARCHITECTURAL_CLOUD_RADIUS,
451
+ cloudHeight * ARCHITECTURAL_CLOUD_FLAT_RADIUS_RATIO
452
+ );
453
+ if (radiusX <= 0 || radiusY <= 0) return "";
454
+ const center = [cloudWidth / 2, cloudHeight / 2];
455
+ const leftCenterX = radiusX;
456
+ const rightCenterX = cloudWidth - radiusX;
457
+ const topCenterY = radiusY;
458
+ const bottomCenterY = cloudHeight - radiusY;
459
+ const horizontalCenters = distributeRange(
460
+ leftCenterX,
461
+ rightCenterX,
462
+ architecturalCloudCenterCount(Math.max(0, rightCenterX - leftCenterX), radiusX)
404
463
  );
405
- const outer = roundedRectMetrics(w, h, inset, radius);
406
- const inner = roundedRectMetrics(
407
- w,
408
- h,
409
- inset + amplitude,
410
- Math.max(0, radius - amplitude)
464
+ const verticalCenters = distributeRange(
465
+ topCenterY,
466
+ bottomCenterY,
467
+ architecturalCloudCenterCount(Math.max(0, bottomCenterY - topCenterY), radiusY)
411
468
  );
412
- const scallopCount = architecturalCloudScallopCount(outer.perimeter, amplitude);
413
- const [startX, startY] = pointOnRoundedRectPath(inner, 0);
469
+ if (horizontalCenters.length > 3 && verticalCenters.length > 3) {
470
+ const roundedArcCloudPath = buildRoundedArcCloudPathD(
471
+ cloudWidth,
472
+ cloudHeight,
473
+ center
474
+ );
475
+ if (roundedArcCloudPath !== "") return roundedArcCloudPath;
476
+ }
477
+ const rectangularCenters = [
478
+ ...horizontalCenters.map((x) => [x, topCenterY]),
479
+ ...verticalCenters.slice(1).map((y) => [rightCenterX, y]),
480
+ ...horizontalCenters.slice(0, -1).reverse().map((x) => [x, bottomCenterY]),
481
+ ...verticalCenters.slice(1, -1).reverse().map((y) => [leftCenterX, y])
482
+ ];
483
+ const centers = rectangularCenters;
484
+ const points = centers.map((point, index) => {
485
+ const previous = centers[(index - 1 + centers.length) % centers.length] ?? point;
486
+ return cloudEllipseIntersection(previous, point, radiusX, radiusY, center);
487
+ });
488
+ const [startX, startY] = points[0] ?? [0, 0];
414
489
  const segments = [`M${svgNumber(startX)} ${svgNumber(startY)}`];
415
- for (let index = 0; index < scallopCount; index += 1) {
416
- const controlDistance = (index + 0.5) / scallopCount * outer.perimeter;
417
- const endDistance = (index + 1) / scallopCount * inner.perimeter;
418
- const [controlX, controlY] = pointOnRoundedRectPath(outer, controlDistance);
419
- const [endX, endY] = pointOnRoundedRectPath(inner, endDistance);
490
+ for (const [endX, endY] of points.slice(1)) {
420
491
  segments.push(
421
- `Q${svgNumber(controlX)} ${svgNumber(controlY)} ${svgNumber(endX)} ${svgNumber(endY)}`
492
+ `A ${svgNumber(radiusX)} ${svgNumber(radiusY)} 0 0 1 ${svgNumber(endX)} ${svgNumber(endY)}`
422
493
  );
423
494
  }
495
+ segments.push(
496
+ `A ${svgNumber(radiusX)} ${svgNumber(radiusY)} 0 0 1 ${svgNumber(startX)} ${svgNumber(startY)}`
497
+ );
424
498
  segments.push("Z");
425
499
  return segments.join(" ");
426
500
  }