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.js CHANGED
@@ -222,25 +222,204 @@ 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 approximateEllipsePerimeter(rx, ry) {
226
- if (rx <= 0 || ry <= 0) return 0;
227
- return Math.PI * (3 * (rx + ry) - Math.sqrt((3 * rx + ry) * (rx + 3 * ry)));
228
- }
229
- function architecturalCloudScallopCount(rx, ry, amplitude) {
230
- const perimeter = approximateEllipsePerimeter(rx, ry);
231
- const targetScallopLength = Math.max(10, amplitude * 2);
232
- let count = Math.max(12, Math.round(perimeter / targetScallopLength));
233
- if (count % 2 === 1) count += 1;
234
- return count;
235
- }
236
- function pointOnSuperellipse(centerX, centerY, radiusX, radiusY, theta, exponent) {
237
- const cosTheta = Math.cos(theta);
238
- const sinTheta = Math.sin(theta);
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)
235
+ );
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
+ return {
248
+ length,
249
+ pointAt: (t) => [start[0] + dx * t, start[1] + dy * t]
250
+ };
251
+ }
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
+ )
280
+ };
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;
297
+ }
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;
239
314
  return [
240
- centerX + Math.sign(cosTheta) * radiusX * Math.abs(cosTheta) ** (2 / exponent),
241
- centerY + Math.sign(sinTheta) * radiusY * Math.abs(sinTheta) ** (2 / exponent)
315
+ lineCloudPathSegment([leftCenterX, top], [rightCenterX, top]),
316
+ ellipseCloudPathSegment(
317
+ rightCenterX,
318
+ topCenterY,
319
+ radius,
320
+ radius,
321
+ -Math.PI / 2,
322
+ 0
323
+ ),
324
+ lineCloudPathSegment([right, topCenterY], [right, bottomCenterY]),
325
+ ellipseCloudPathSegment(
326
+ rightCenterX,
327
+ bottomCenterY,
328
+ radius,
329
+ radius,
330
+ 0,
331
+ Math.PI / 2
332
+ ),
333
+ lineCloudPathSegment([rightCenterX, bottom], [leftCenterX, bottom]),
334
+ ellipseCloudPathSegment(
335
+ leftCenterX,
336
+ bottomCenterY,
337
+ radius,
338
+ radius,
339
+ Math.PI / 2,
340
+ Math.PI
341
+ ),
342
+ lineCloudPathSegment([left, bottomCenterY], [left, topCenterY]),
343
+ ellipseCloudPathSegment(
344
+ leftCenterX,
345
+ topCenterY,
346
+ radius,
347
+ radius,
348
+ Math.PI,
349
+ Math.PI * 1.5
350
+ )
242
351
  ];
243
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)}`
383
+ );
384
+ }
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];
422
+ }
244
423
  function buildRectSvg(width, height, style = DEFAULT_STROKE_STYLE) {
245
424
  return `<rect width="${width}" height="${height}" fill="none" stroke="${style.stroke}" stroke-width="${style.strokeWidth}" rx="4"${strokeOpacityAttr(style)} />`;
246
425
  }
@@ -253,40 +432,63 @@ function buildArchitecturalCloudPathD(width, height, strokeWidth = DEFAULT_STROK
253
432
  const w = Math.max(0, width);
254
433
  const h = Math.max(0, height);
255
434
  if (w <= 0 || h <= 0) return "";
256
- const inset = Math.max(0.5, strokeWidth / 2);
257
- const outerRx = Math.max(0, w / 2 - inset);
258
- const outerRy = Math.max(0, h / 2 - inset);
259
- if (outerRx <= 0 || outerRy <= 0) return "";
260
- const baseExponent = 3.6;
261
- const amplitude = Math.min(
262
- outerRx * 0.45,
263
- outerRy * 0.45,
264
- Math.max(2, Math.min(7, Math.min(w, h) * 0.035))
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
265
442
  );
266
- const innerRx = Math.max(0, outerRx - amplitude);
267
- const innerRy = Math.max(0, outerRy - amplitude);
268
- const scallopCount = architecturalCloudScallopCount(innerRx, innerRy, amplitude);
269
- const angleStep = Math.PI * 2 / scallopCount;
270
- const cx = w / 2;
271
- const cy = h / 2;
272
- const startAngle = -Math.PI / 2;
273
- const pointOnArchitecturalCloud = (theta, radiusX, radiusY) => pointOnSuperellipse(cx, cy, radiusX, radiusY, theta, baseExponent);
274
- const [startX, startY] = pointOnArchitecturalCloud(startAngle, innerRx, innerRy);
275
- const segments = [`M${svgNumber(startX)} ${svgNumber(startY)}`];
276
- for (let index = 0; index < scallopCount; index += 1) {
277
- const segmentStart = startAngle + index * angleStep;
278
- const segmentMid = segmentStart + angleStep / 2;
279
- const segmentEnd = segmentStart + angleStep;
280
- const [controlX, controlY] = pointOnArchitecturalCloud(
281
- segmentMid,
282
- outerRx,
283
- outerRy
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)
457
+ );
458
+ const verticalCenters = distributeRange(
459
+ topCenterY,
460
+ bottomCenterY,
461
+ architecturalCloudCenterCount(Math.max(0, bottomCenterY - topCenterY), radiusY)
462
+ );
463
+ if (horizontalCenters.length > 3 && verticalCenters.length > 3) {
464
+ const roundedArcCloudPath = buildRoundedArcCloudPathD(
465
+ cloudWidth,
466
+ cloudHeight,
467
+ center
284
468
  );
285
- const [endX, endY] = pointOnArchitecturalCloud(segmentEnd, innerRx, innerRy);
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];
483
+ const segments = [`M${svgNumber(startX)} ${svgNumber(startY)}`];
484
+ for (const [endX, endY] of points.slice(1)) {
286
485
  segments.push(
287
- `Q${svgNumber(controlX)} ${svgNumber(controlY)} ${svgNumber(endX)} ${svgNumber(endY)}`
486
+ `A ${svgNumber(radiusX)} ${svgNumber(radiusY)} 0 0 1 ${svgNumber(endX)} ${svgNumber(endY)}`
288
487
  );
289
488
  }
489
+ segments.push(
490
+ `A ${svgNumber(radiusX)} ${svgNumber(radiusY)} 0 0 1 ${svgNumber(startX)} ${svgNumber(startY)}`
491
+ );
290
492
  segments.push("Z");
291
493
  return segments.join(" ");
292
494
  }