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/tldraw.cjs CHANGED
@@ -228,25 +228,204 @@ 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 approximateEllipsePerimeter(rx, ry) {
232
- if (rx <= 0 || ry <= 0) return 0;
233
- return Math.PI * (3 * (rx + ry) - Math.sqrt((3 * rx + ry) * (rx + 3 * ry)));
234
- }
235
- function architecturalCloudScallopCount(rx, ry, amplitude) {
236
- const perimeter = approximateEllipsePerimeter(rx, ry);
237
- const targetScallopLength = Math.max(10, amplitude * 2);
238
- let count = Math.max(12, Math.round(perimeter / targetScallopLength));
239
- if (count % 2 === 1) count += 1;
240
- return count;
241
- }
242
- function pointOnSuperellipse(centerX, centerY, radiusX, radiusY, theta, exponent) {
243
- const cosTheta = Math.cos(theta);
244
- const sinTheta = Math.sin(theta);
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)
241
+ );
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
+ return {
254
+ length,
255
+ pointAt: (t) => [start[0] + dx * t, start[1] + dy * t]
256
+ };
257
+ }
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
+ )
286
+ };
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;
303
+ }
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;
245
320
  return [
246
- centerX + Math.sign(cosTheta) * radiusX * Math.abs(cosTheta) ** (2 / exponent),
247
- centerY + Math.sign(sinTheta) * radiusY * Math.abs(sinTheta) ** (2 / exponent)
321
+ lineCloudPathSegment([leftCenterX, top], [rightCenterX, top]),
322
+ ellipseCloudPathSegment(
323
+ rightCenterX,
324
+ topCenterY,
325
+ radius,
326
+ radius,
327
+ -Math.PI / 2,
328
+ 0
329
+ ),
330
+ lineCloudPathSegment([right, topCenterY], [right, bottomCenterY]),
331
+ ellipseCloudPathSegment(
332
+ rightCenterX,
333
+ bottomCenterY,
334
+ radius,
335
+ radius,
336
+ 0,
337
+ Math.PI / 2
338
+ ),
339
+ lineCloudPathSegment([rightCenterX, bottom], [leftCenterX, bottom]),
340
+ ellipseCloudPathSegment(
341
+ leftCenterX,
342
+ bottomCenterY,
343
+ radius,
344
+ radius,
345
+ Math.PI / 2,
346
+ Math.PI
347
+ ),
348
+ lineCloudPathSegment([left, bottomCenterY], [left, topCenterY]),
349
+ ellipseCloudPathSegment(
350
+ leftCenterX,
351
+ topCenterY,
352
+ radius,
353
+ radius,
354
+ Math.PI,
355
+ Math.PI * 1.5
356
+ )
248
357
  ];
249
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)}`
389
+ );
390
+ }
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];
428
+ }
250
429
  function buildRectSvg(width, height, style = DEFAULT_STROKE_STYLE) {
251
430
  return `<rect width="${width}" height="${height}" fill="none" stroke="${style.stroke}" stroke-width="${style.strokeWidth}" rx="4"${strokeOpacityAttr(style)} />`;
252
431
  }
@@ -259,40 +438,63 @@ function buildArchitecturalCloudPathD(width, height, strokeWidth = DEFAULT_STROK
259
438
  const w = Math.max(0, width);
260
439
  const h = Math.max(0, height);
261
440
  if (w <= 0 || h <= 0) return "";
262
- const inset = Math.max(0.5, strokeWidth / 2);
263
- const outerRx = Math.max(0, w / 2 - inset);
264
- const outerRy = Math.max(0, h / 2 - inset);
265
- if (outerRx <= 0 || outerRy <= 0) return "";
266
- const baseExponent = 3.6;
267
- const amplitude = Math.min(
268
- outerRx * 0.45,
269
- outerRy * 0.45,
270
- Math.max(2, Math.min(7, Math.min(w, h) * 0.035))
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
271
448
  );
272
- const innerRx = Math.max(0, outerRx - amplitude);
273
- const innerRy = Math.max(0, outerRy - amplitude);
274
- const scallopCount = architecturalCloudScallopCount(innerRx, innerRy, amplitude);
275
- const angleStep = Math.PI * 2 / scallopCount;
276
- const cx = w / 2;
277
- const cy = h / 2;
278
- const startAngle = -Math.PI / 2;
279
- const pointOnArchitecturalCloud = (theta, radiusX, radiusY) => pointOnSuperellipse(cx, cy, radiusX, radiusY, theta, baseExponent);
280
- const [startX, startY] = pointOnArchitecturalCloud(startAngle, innerRx, innerRy);
281
- const segments = [`M${svgNumber(startX)} ${svgNumber(startY)}`];
282
- for (let index = 0; index < scallopCount; index += 1) {
283
- const segmentStart = startAngle + index * angleStep;
284
- const segmentMid = segmentStart + angleStep / 2;
285
- const segmentEnd = segmentStart + angleStep;
286
- const [controlX, controlY] = pointOnArchitecturalCloud(
287
- segmentMid,
288
- outerRx,
289
- outerRy
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)
463
+ );
464
+ const verticalCenters = distributeRange(
465
+ topCenterY,
466
+ bottomCenterY,
467
+ architecturalCloudCenterCount(Math.max(0, bottomCenterY - topCenterY), radiusY)
468
+ );
469
+ if (horizontalCenters.length > 3 && verticalCenters.length > 3) {
470
+ const roundedArcCloudPath = buildRoundedArcCloudPathD(
471
+ cloudWidth,
472
+ cloudHeight,
473
+ center
290
474
  );
291
- const [endX, endY] = pointOnArchitecturalCloud(segmentEnd, innerRx, innerRy);
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];
489
+ const segments = [`M${svgNumber(startX)} ${svgNumber(startY)}`];
490
+ for (const [endX, endY] of points.slice(1)) {
292
491
  segments.push(
293
- `Q${svgNumber(controlX)} ${svgNumber(controlY)} ${svgNumber(endX)} ${svgNumber(endY)}`
492
+ `A ${svgNumber(radiusX)} ${svgNumber(radiusY)} 0 0 1 ${svgNumber(endX)} ${svgNumber(endY)}`
294
493
  );
295
494
  }
495
+ segments.push(
496
+ `A ${svgNumber(radiusX)} ${svgNumber(radiusY)} 0 0 1 ${svgNumber(startX)} ${svgNumber(startY)}`
497
+ );
296
498
  segments.push("Z");
297
499
  return segments.join(" ");
298
500
  }