canvu-react 0.3.33 → 0.3.35

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