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/index.cjs CHANGED
@@ -1050,153 +1050,203 @@ function svgNumber(value) {
1050
1050
  const rounded = Math.round(value * 100) / 100;
1051
1051
  return Number.isInteger(rounded) ? String(rounded) : String(rounded);
1052
1052
  }
1053
- function architecturalCloudScallopCount(perimeter, amplitude) {
1054
- const targetScallopLength = Math.max(18, amplitude * 2.45);
1055
- let count = Math.max(12, Math.round(perimeter / targetScallopLength));
1056
- if (count % 2 === 1) count += 1;
1057
- return count;
1058
- }
1059
- function roundedRectMetrics(width, height, inset, radius) {
1060
- const left = inset;
1061
- const top = inset;
1062
- const right = width - inset;
1063
- const bottom = height - inset;
1064
- const rectWidth = Math.max(0, right - left);
1065
- const rectHeight = Math.max(0, bottom - top);
1066
- const normalizedRadius = Math.max(
1067
- 0,
1068
- Math.min(radius, rectWidth / 2, rectHeight / 2)
1053
+ var ARCHITECTURAL_CLOUD_RADIUS = 38;
1054
+ var ARCHITECTURAL_CLOUD_TARGET_SPACING = 50;
1055
+ var ARCHITECTURAL_CLOUD_FLAT_RADIUS_RATIO = 0.38;
1056
+ var ARCHITECTURAL_CLOUD_ROUNDED_RADIUS = 72;
1057
+ var ARCHITECTURAL_CLOUD_ROUNDED_MIN_RADIUS = 44;
1058
+ var ARCHITECTURAL_CLOUD_ROUNDED_TARGET_SPACING = 98;
1059
+ function architecturalCloudCenterCount(edgeLength, radius) {
1060
+ const targetSpacing = Math.min(
1061
+ ARCHITECTURAL_CLOUD_TARGET_SPACING,
1062
+ Math.max(1, radius * 1.3)
1069
1063
  );
1070
- const centerX = width / 2;
1071
- const topHalfLength = Math.max(0, right - normalizedRadius - centerX);
1072
- const horizontalLength = Math.max(0, rectWidth - normalizedRadius * 2);
1073
- const verticalLength = Math.max(0, rectHeight - normalizedRadius * 2);
1074
- const arcLength = normalizedRadius * (Math.PI / 2);
1064
+ return Math.max(2, Math.round(edgeLength / targetSpacing) + 1);
1065
+ }
1066
+ function distributeRange(start, end, count) {
1067
+ if (count <= 1) return [start];
1068
+ const step = (end - start) / (count - 1);
1069
+ return Array.from({ length: count }, (_, index) => start + step * index);
1070
+ }
1071
+ function lineCloudPathSegment(start, end) {
1072
+ const dx = end[0] - start[0];
1073
+ const dy = end[1] - start[1];
1074
+ const length = Math.hypot(dx, dy);
1075
1075
  return {
1076
- left,
1077
- top,
1078
- right,
1079
- bottom,
1080
- radius: normalizedRadius,
1081
- centerX,
1082
- topHalfLength,
1083
- horizontalLength,
1084
- verticalLength,
1085
- arcLength,
1086
- perimeter: horizontalLength * 2 + verticalLength * 2 + Math.PI * 2 * normalizedRadius
1076
+ length,
1077
+ pointAt: (t) => [start[0] + dx * t, start[1] + dy * t]
1087
1078
  };
1088
1079
  }
1089
- function pointOnLine(startX, startY, endX, endY, t) {
1090
- return [startX + (endX - startX) * t, startY + (endY - startY) * t];
1080
+ function ellipsePoint(centerX, centerY, radiusX, radiusY, angle) {
1081
+ return [centerX + Math.cos(angle) * radiusX, centerY + Math.sin(angle) * radiusY];
1091
1082
  }
1092
- function pointOnArc(centerX, centerY, radius, startAngle, endAngle, t) {
1093
- const theta = startAngle + (endAngle - startAngle) * t;
1094
- return [centerX + Math.cos(theta) * radius, centerY + Math.sin(theta) * radius];
1083
+ function approximateEllipseArcLength(radiusX, radiusY, startAngle, endAngle) {
1084
+ const steps = Math.max(
1085
+ 4,
1086
+ Math.ceil(Math.abs(endAngle - startAngle) / (Math.PI / 16))
1087
+ );
1088
+ let length = 0;
1089
+ let previous = ellipsePoint(0, 0, radiusX, radiusY, startAngle);
1090
+ for (let index = 1; index <= steps; index += 1) {
1091
+ const angle = startAngle + (endAngle - startAngle) * index / steps;
1092
+ const next = ellipsePoint(0, 0, radiusX, radiusY, angle);
1093
+ length += Math.hypot(next[0] - previous[0], next[1] - previous[1]);
1094
+ previous = next;
1095
+ }
1096
+ return length;
1095
1097
  }
1096
- function pointOnRoundedRectPath(metrics, distance) {
1097
- if (metrics.perimeter <= 0) return [metrics.centerX, metrics.top];
1098
- let remaining = (distance % metrics.perimeter + metrics.perimeter) % metrics.perimeter;
1099
- const consume = (length) => {
1100
- if (length <= 1e-9) return null;
1101
- if (remaining <= length) return remaining / length;
1102
- remaining -= length;
1103
- return null;
1098
+ function ellipseCloudPathSegment(centerX, centerY, radiusX, radiusY, startAngle, endAngle) {
1099
+ return {
1100
+ length: approximateEllipseArcLength(radiusX, radiusY, startAngle, endAngle),
1101
+ pointAt: (t) => ellipsePoint(
1102
+ centerX,
1103
+ centerY,
1104
+ radiusX,
1105
+ radiusY,
1106
+ startAngle + (endAngle - startAngle) * t
1107
+ )
1104
1108
  };
1105
- let t = consume(metrics.topHalfLength);
1106
- if (t != null) {
1107
- return pointOnLine(
1108
- metrics.centerX,
1109
- metrics.top,
1110
- metrics.right - metrics.radius,
1111
- metrics.top,
1112
- t
1113
- );
1109
+ }
1110
+ function cloudPathPerimeter(segments) {
1111
+ const usableSegments = segments.filter((segment) => segment.length > 1e-9);
1112
+ return usableSegments.reduce((sum, segment) => sum + segment.length, 0);
1113
+ }
1114
+ function pointOnCloudPath(segments, distance) {
1115
+ const perimeter = cloudPathPerimeter(segments);
1116
+ if (perimeter <= 0) return [0, 0];
1117
+ let remaining = (distance % perimeter + perimeter) % perimeter;
1118
+ for (const segment of segments) {
1119
+ if (segment.length <= 1e-9) continue;
1120
+ if (remaining <= segment.length) {
1121
+ const t = remaining / segment.length;
1122
+ return segment.pointAt(t);
1123
+ }
1124
+ remaining -= segment.length;
1114
1125
  }
1115
- t = consume(metrics.arcLength);
1116
- if (t != null) {
1117
- return pointOnArc(
1118
- metrics.right - metrics.radius,
1119
- metrics.top + metrics.radius,
1120
- metrics.radius,
1126
+ const fallback = segments.find((segment) => segment.length > 1e-9);
1127
+ return fallback?.pointAt(0) ?? [0, 0];
1128
+ }
1129
+ function buildRoundedCapsulePathSegments(width, height, inset) {
1130
+ const left = inset;
1131
+ const top = inset;
1132
+ const right = width - inset;
1133
+ const bottom = height - inset;
1134
+ const capsuleWidth = Math.max(0, right - left);
1135
+ const capsuleHeight = Math.max(0, bottom - top);
1136
+ const radius = Math.min(capsuleWidth, capsuleHeight) / 2;
1137
+ if (radius <= 0) return [];
1138
+ const leftCenterX = left + radius;
1139
+ const rightCenterX = right - radius;
1140
+ const topCenterY = top + radius;
1141
+ const bottomCenterY = bottom - radius;
1142
+ return [
1143
+ lineCloudPathSegment([leftCenterX, top], [rightCenterX, top]),
1144
+ ellipseCloudPathSegment(
1145
+ rightCenterX,
1146
+ topCenterY,
1147
+ radius,
1148
+ radius,
1121
1149
  -Math.PI / 2,
1150
+ 0
1151
+ ),
1152
+ lineCloudPathSegment([right, topCenterY], [right, bottomCenterY]),
1153
+ ellipseCloudPathSegment(
1154
+ rightCenterX,
1155
+ bottomCenterY,
1156
+ radius,
1157
+ radius,
1122
1158
  0,
1123
- t
1124
- );
1125
- }
1126
- t = consume(metrics.verticalLength);
1127
- if (t != null) {
1128
- return pointOnLine(
1129
- metrics.right,
1130
- metrics.top + metrics.radius,
1131
- metrics.right,
1132
- metrics.bottom - metrics.radius,
1133
- t
1134
- );
1135
- }
1136
- t = consume(metrics.arcLength);
1137
- if (t != null) {
1138
- return pointOnArc(
1139
- metrics.right - metrics.radius,
1140
- metrics.bottom - metrics.radius,
1141
- metrics.radius,
1142
- 0,
1159
+ Math.PI / 2
1160
+ ),
1161
+ lineCloudPathSegment([rightCenterX, bottom], [leftCenterX, bottom]),
1162
+ ellipseCloudPathSegment(
1163
+ leftCenterX,
1164
+ bottomCenterY,
1165
+ radius,
1166
+ radius,
1143
1167
  Math.PI / 2,
1144
- t
1145
- );
1146
- }
1147
- t = consume(metrics.horizontalLength);
1148
- if (t != null) {
1149
- return pointOnLine(
1150
- metrics.right - metrics.radius,
1151
- metrics.bottom,
1152
- metrics.left + metrics.radius,
1153
- metrics.bottom,
1154
- t
1155
- );
1156
- }
1157
- t = consume(metrics.arcLength);
1158
- if (t != null) {
1159
- return pointOnArc(
1160
- metrics.left + metrics.radius,
1161
- metrics.bottom - metrics.radius,
1162
- metrics.radius,
1163
- Math.PI / 2,
1164
- Math.PI,
1165
- t
1166
- );
1167
- }
1168
- t = consume(metrics.verticalLength);
1169
- if (t != null) {
1170
- return pointOnLine(
1171
- metrics.left,
1172
- metrics.bottom - metrics.radius,
1173
- metrics.left,
1174
- metrics.top + metrics.radius,
1175
- t
1176
- );
1177
- }
1178
- t = consume(metrics.arcLength);
1179
- if (t != null) {
1180
- return pointOnArc(
1181
- metrics.left + metrics.radius,
1182
- metrics.top + metrics.radius,
1183
- metrics.radius,
1168
+ Math.PI
1169
+ ),
1170
+ lineCloudPathSegment([left, bottomCenterY], [left, topCenterY]),
1171
+ ellipseCloudPathSegment(
1172
+ leftCenterX,
1173
+ topCenterY,
1174
+ radius,
1175
+ radius,
1184
1176
  Math.PI,
1185
- Math.PI * 1.5,
1186
- t
1187
- );
1188
- }
1189
- t = consume(metrics.topHalfLength);
1190
- if (t != null) {
1191
- return pointOnLine(
1192
- metrics.left + metrics.radius,
1193
- metrics.top,
1194
- metrics.centerX,
1195
- metrics.top,
1196
- t
1177
+ Math.PI * 1.5
1178
+ )
1179
+ ];
1180
+ }
1181
+ function buildRoundedArcCloudPathD(cloudWidth, cloudHeight, center) {
1182
+ const minDimension = Math.min(cloudWidth, cloudHeight);
1183
+ const radius = Math.min(
1184
+ ARCHITECTURAL_CLOUD_ROUNDED_RADIUS,
1185
+ Math.max(ARCHITECTURAL_CLOUD_ROUNDED_MIN_RADIUS, minDimension * 0.16)
1186
+ );
1187
+ const centerPath = buildRoundedCapsulePathSegments(
1188
+ cloudWidth,
1189
+ cloudHeight,
1190
+ radius
1191
+ );
1192
+ const centerPerimeter = cloudPathPerimeter(centerPath);
1193
+ if (centerPerimeter <= 0) return "";
1194
+ const lobeCount = Math.max(
1195
+ 8,
1196
+ Math.round(centerPerimeter / ARCHITECTURAL_CLOUD_ROUNDED_TARGET_SPACING)
1197
+ );
1198
+ const centers = Array.from(
1199
+ { length: lobeCount },
1200
+ (_, index) => pointOnCloudPath(centerPath, centerPerimeter * index / lobeCount)
1201
+ );
1202
+ const points = centers.map((point, index) => {
1203
+ const previous = centers[(index - 1 + centers.length) % centers.length] ?? point;
1204
+ return cloudCircleIntersection(previous, point, radius, center);
1205
+ });
1206
+ const [startX, startY] = points[0] ?? [0, 0];
1207
+ const segments = [`M${svgNumber(startX)} ${svgNumber(startY)}`];
1208
+ for (const [endX, endY] of points.slice(1)) {
1209
+ segments.push(
1210
+ `A ${svgNumber(radius)} ${svgNumber(radius)} 0 0 1 ${svgNumber(endX)} ${svgNumber(endY)}`
1197
1211
  );
1198
1212
  }
1199
- return [metrics.centerX, metrics.top];
1213
+ segments.push(
1214
+ `A ${svgNumber(radius)} ${svgNumber(radius)} 0 0 1 ${svgNumber(startX)} ${svgNumber(startY)}`
1215
+ );
1216
+ segments.push("Z");
1217
+ return segments.join(" ");
1218
+ }
1219
+ function cloudCircleIntersection(a, b, radius, center) {
1220
+ const midX = (a[0] + b[0]) / 2;
1221
+ const midY = (a[1] + b[1]) / 2;
1222
+ const dx = b[0] - a[0];
1223
+ const dy = b[1] - a[1];
1224
+ const distance = Math.hypot(dx, dy);
1225
+ if (distance <= 1e-9) return [midX, midY];
1226
+ const halfDistance = distance / 2;
1227
+ const offset = Math.sqrt(
1228
+ Math.max(0, radius * radius - halfDistance * halfDistance)
1229
+ );
1230
+ const normalX = -dy / distance;
1231
+ const normalY = dx / distance;
1232
+ const first = [midX + normalX * offset, midY + normalY * offset];
1233
+ const second = [
1234
+ midX - normalX * offset,
1235
+ midY - normalY * offset
1236
+ ];
1237
+ const firstDistance = (first[0] - center[0]) * (first[0] - center[0]) + (first[1] - center[1]) * (first[1] - center[1]);
1238
+ const secondDistance = (second[0] - center[0]) * (second[0] - center[0]) + (second[1] - center[1]) * (second[1] - center[1]);
1239
+ return firstDistance >= secondDistance ? first : second;
1240
+ }
1241
+ function cloudEllipseIntersection(a, b, radiusX, radiusY, center) {
1242
+ const scaleY = radiusX / radiusY;
1243
+ const [x, y] = cloudCircleIntersection(
1244
+ [a[0], a[1] * scaleY],
1245
+ [b[0], b[1] * scaleY],
1246
+ radiusX,
1247
+ [center[0], center[1] * scaleY]
1248
+ );
1249
+ return [x, y / scaleY];
1200
1250
  }
1201
1251
  function buildRectSvg(width, height, style = DEFAULT_STROKE_STYLE) {
1202
1252
  return `<rect width="${width}" height="${height}" fill="none" stroke="${style.stroke}" stroke-width="${style.strokeWidth}" rx="4"${strokeOpacityAttr(style)} />`;
@@ -1210,39 +1260,63 @@ function buildArchitecturalCloudPathD(width, height, strokeWidth = DEFAULT_STROK
1210
1260
  const w = Math.max(0, width);
1211
1261
  const h = Math.max(0, height);
1212
1262
  if (w <= 0 || h <= 0) return "";
1213
- const inset = Math.max(0.5, strokeWidth / 2);
1214
- const outerWidth = Math.max(0, w - inset * 2);
1215
- const outerHeight = Math.max(0, h - inset * 2);
1216
- if (outerWidth <= 0 || outerHeight <= 0) return "";
1217
- const amplitude = Math.min(
1218
- outerWidth * 0.12,
1219
- outerHeight * 0.12,
1220
- Math.max(5, Math.min(14, Math.min(w, h) * 0.07))
1263
+ const padding = Math.max(0, strokeWidth * 2);
1264
+ const cloudWidth = Math.max(0, w - padding);
1265
+ const cloudHeight = Math.max(0, h - padding);
1266
+ if (cloudWidth <= 0 || cloudHeight <= 0) return "";
1267
+ const radiusX = Math.min(
1268
+ ARCHITECTURAL_CLOUD_RADIUS,
1269
+ cloudWidth * ARCHITECTURAL_CLOUD_FLAT_RADIUS_RATIO
1221
1270
  );
1222
- const radius = Math.min(
1223
- outerWidth / 2,
1224
- outerHeight / 2,
1225
- Math.max(amplitude * 2.5, outerHeight * 0.43)
1271
+ const radiusY = Math.min(
1272
+ ARCHITECTURAL_CLOUD_RADIUS,
1273
+ cloudHeight * ARCHITECTURAL_CLOUD_FLAT_RADIUS_RATIO
1226
1274
  );
1227
- const outer = roundedRectMetrics(w, h, inset, radius);
1228
- const inner = roundedRectMetrics(
1229
- w,
1230
- h,
1231
- inset + amplitude,
1232
- Math.max(0, radius - amplitude)
1275
+ if (radiusX <= 0 || radiusY <= 0) return "";
1276
+ const center = [cloudWidth / 2, cloudHeight / 2];
1277
+ const leftCenterX = radiusX;
1278
+ const rightCenterX = cloudWidth - radiusX;
1279
+ const topCenterY = radiusY;
1280
+ const bottomCenterY = cloudHeight - radiusY;
1281
+ const horizontalCenters = distributeRange(
1282
+ leftCenterX,
1283
+ rightCenterX,
1284
+ architecturalCloudCenterCount(Math.max(0, rightCenterX - leftCenterX), radiusX)
1233
1285
  );
1234
- const scallopCount = architecturalCloudScallopCount(outer.perimeter, amplitude);
1235
- const [startX, startY] = pointOnRoundedRectPath(inner, 0);
1286
+ const verticalCenters = distributeRange(
1287
+ topCenterY,
1288
+ bottomCenterY,
1289
+ architecturalCloudCenterCount(Math.max(0, bottomCenterY - topCenterY), radiusY)
1290
+ );
1291
+ if (horizontalCenters.length > 3 && verticalCenters.length > 3) {
1292
+ const roundedArcCloudPath = buildRoundedArcCloudPathD(
1293
+ cloudWidth,
1294
+ cloudHeight,
1295
+ center
1296
+ );
1297
+ if (roundedArcCloudPath !== "") return roundedArcCloudPath;
1298
+ }
1299
+ const rectangularCenters = [
1300
+ ...horizontalCenters.map((x) => [x, topCenterY]),
1301
+ ...verticalCenters.slice(1).map((y) => [rightCenterX, y]),
1302
+ ...horizontalCenters.slice(0, -1).reverse().map((x) => [x, bottomCenterY]),
1303
+ ...verticalCenters.slice(1, -1).reverse().map((y) => [leftCenterX, y])
1304
+ ];
1305
+ const centers = rectangularCenters;
1306
+ const points = centers.map((point, index) => {
1307
+ const previous = centers[(index - 1 + centers.length) % centers.length] ?? point;
1308
+ return cloudEllipseIntersection(previous, point, radiusX, radiusY, center);
1309
+ });
1310
+ const [startX, startY] = points[0] ?? [0, 0];
1236
1311
  const segments = [`M${svgNumber(startX)} ${svgNumber(startY)}`];
1237
- for (let index = 0; index < scallopCount; index += 1) {
1238
- const controlDistance = (index + 0.5) / scallopCount * outer.perimeter;
1239
- const endDistance = (index + 1) / scallopCount * inner.perimeter;
1240
- const [controlX, controlY] = pointOnRoundedRectPath(outer, controlDistance);
1241
- const [endX, endY] = pointOnRoundedRectPath(inner, endDistance);
1312
+ for (const [endX, endY] of points.slice(1)) {
1242
1313
  segments.push(
1243
- `Q${svgNumber(controlX)} ${svgNumber(controlY)} ${svgNumber(endX)} ${svgNumber(endY)}`
1314
+ `A ${svgNumber(radiusX)} ${svgNumber(radiusY)} 0 0 1 ${svgNumber(endX)} ${svgNumber(endY)}`
1244
1315
  );
1245
1316
  }
1317
+ segments.push(
1318
+ `A ${svgNumber(radiusX)} ${svgNumber(radiusY)} 0 0 1 ${svgNumber(startX)} ${svgNumber(startY)}`
1319
+ );
1246
1320
  segments.push("Z");
1247
1321
  return segments.join(" ");
1248
1322
  }
@@ -2532,20 +2606,25 @@ function markImageAsManaged(item) {
2532
2606
  };
2533
2607
  }
2534
2608
  function restackManagedImages(items) {
2535
- let anchor;
2609
+ let anchorAabbY = Infinity;
2610
+ let anchorCenterX = 0;
2536
2611
  for (const item of items) {
2537
2612
  if (!isManagedImage(item)) continue;
2538
- if (!anchor || item.bounds.y < anchor.bounds.y) anchor = item;
2613
+ const aabb = boundsAabbForRotatedItem(item);
2614
+ if (aabb.y < anchorAabbY) {
2615
+ anchorAabbY = aabb.y;
2616
+ anchorCenterX = aabb.x + aabb.width / 2;
2617
+ }
2539
2618
  }
2540
- if (!anchor) return [...items];
2541
- const anchorCenterX = anchor.bounds.x + anchor.bounds.width / 2;
2542
- const anchorTopY = anchor.bounds.y;
2543
- let cursorY = anchorTopY;
2619
+ if (!Number.isFinite(anchorAabbY)) return [...items];
2620
+ let cursorY = anchorAabbY;
2544
2621
  return items.map((item) => {
2545
2622
  if (!isManagedImage(item)) return item;
2623
+ const aabb = boundsAabbForRotatedItem(item);
2624
+ const centerY = cursorY + aabb.height / 2;
2546
2625
  const newX = anchorCenterX - item.bounds.width / 2;
2547
- const newY = cursorY;
2548
- cursorY = newY + item.bounds.height + STACK_GAP_WORLD;
2626
+ const newY = centerY - item.bounds.height / 2;
2627
+ cursorY += aabb.height + STACK_GAP_WORLD;
2549
2628
  if (item.bounds.x === newX && item.bounds.y === newY) return item;
2550
2629
  return {
2551
2630
  ...item,
@@ -2568,8 +2647,10 @@ function copyManagedImage(items, id) {
2568
2647
  return restackManagedImages(inserted);
2569
2648
  }
2570
2649
  function rotateManagedImage(items, id) {
2571
- return items.map(
2572
- (i) => i.id === id ? { ...i, rotation: ((i.rotation ?? 0) + Math.PI / 2) % (Math.PI * 2) } : i
2650
+ return restackManagedImages(
2651
+ items.map(
2652
+ (i) => i.id === id ? { ...i, rotation: ((i.rotation ?? 0) + Math.PI / 2) % (Math.PI * 2) } : i
2653
+ )
2573
2654
  );
2574
2655
  }
2575
2656
  function deleteManagedImage(items, id) {