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/index.cjs CHANGED
@@ -1050,24 +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 approximateEllipsePerimeter(rx, ry) {
1054
- if (rx <= 0 || ry <= 0) return 0;
1055
- return Math.PI * (3 * (rx + ry) - Math.sqrt((3 * rx + ry) * (rx + 3 * ry)));
1056
- }
1057
- function architecturalCloudScallopCount(rx, ry, amplitude) {
1058
- const perimeter = approximateEllipsePerimeter(rx, ry);
1059
- const targetScallopLength = Math.max(10, amplitude * 2);
1060
- let count = Math.max(12, Math.round(perimeter / targetScallopLength));
1061
- if (count % 2 === 1) count += 1;
1062
- return count;
1063
- }
1064
- function pointOnSuperellipse(centerX, centerY, radiusX, radiusY, theta, exponent) {
1065
- const cosTheta = Math.cos(theta);
1066
- const sinTheta = Math.sin(theta);
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)
1063
+ );
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
+ return {
1076
+ length,
1077
+ pointAt: (t) => [start[0] + dx * t, start[1] + dy * t]
1078
+ };
1079
+ }
1080
+ function ellipsePoint(centerX, centerY, radiusX, radiusY, angle) {
1081
+ return [centerX + Math.cos(angle) * radiusX, centerY + Math.sin(angle) * radiusY];
1082
+ }
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;
1097
+ }
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
+ )
1108
+ };
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;
1125
+ }
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;
1067
1142
  return [
1068
- centerX + Math.sign(cosTheta) * radiusX * Math.abs(cosTheta) ** (2 / exponent),
1069
- centerY + Math.sign(sinTheta) * radiusY * Math.abs(sinTheta) ** (2 / exponent)
1143
+ lineCloudPathSegment([leftCenterX, top], [rightCenterX, top]),
1144
+ ellipseCloudPathSegment(
1145
+ rightCenterX,
1146
+ topCenterY,
1147
+ radius,
1148
+ radius,
1149
+ -Math.PI / 2,
1150
+ 0
1151
+ ),
1152
+ lineCloudPathSegment([right, topCenterY], [right, bottomCenterY]),
1153
+ ellipseCloudPathSegment(
1154
+ rightCenterX,
1155
+ bottomCenterY,
1156
+ radius,
1157
+ radius,
1158
+ 0,
1159
+ Math.PI / 2
1160
+ ),
1161
+ lineCloudPathSegment([rightCenterX, bottom], [leftCenterX, bottom]),
1162
+ ellipseCloudPathSegment(
1163
+ leftCenterX,
1164
+ bottomCenterY,
1165
+ radius,
1166
+ radius,
1167
+ Math.PI / 2,
1168
+ Math.PI
1169
+ ),
1170
+ lineCloudPathSegment([left, bottomCenterY], [left, topCenterY]),
1171
+ ellipseCloudPathSegment(
1172
+ leftCenterX,
1173
+ topCenterY,
1174
+ radius,
1175
+ radius,
1176
+ Math.PI,
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)}`
1211
+ );
1212
+ }
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
1070
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];
1071
1250
  }
1072
1251
  function buildRectSvg(width, height, style = DEFAULT_STROKE_STYLE) {
1073
1252
  return `<rect width="${width}" height="${height}" fill="none" stroke="${style.stroke}" stroke-width="${style.strokeWidth}" rx="4"${strokeOpacityAttr(style)} />`;
@@ -1081,40 +1260,63 @@ function buildArchitecturalCloudPathD(width, height, strokeWidth = DEFAULT_STROK
1081
1260
  const w = Math.max(0, width);
1082
1261
  const h = Math.max(0, height);
1083
1262
  if (w <= 0 || h <= 0) return "";
1084
- const inset = Math.max(0.5, strokeWidth / 2);
1085
- const outerRx = Math.max(0, w / 2 - inset);
1086
- const outerRy = Math.max(0, h / 2 - inset);
1087
- if (outerRx <= 0 || outerRy <= 0) return "";
1088
- const baseExponent = 3.6;
1089
- const amplitude = Math.min(
1090
- outerRx * 0.45,
1091
- outerRy * 0.45,
1092
- Math.max(2, Math.min(7, Math.min(w, h) * 0.035))
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
1093
1270
  );
1094
- const innerRx = Math.max(0, outerRx - amplitude);
1095
- const innerRy = Math.max(0, outerRy - amplitude);
1096
- const scallopCount = architecturalCloudScallopCount(innerRx, innerRy, amplitude);
1097
- const angleStep = Math.PI * 2 / scallopCount;
1098
- const cx = w / 2;
1099
- const cy = h / 2;
1100
- const startAngle = -Math.PI / 2;
1101
- const pointOnArchitecturalCloud = (theta, radiusX, radiusY) => pointOnSuperellipse(cx, cy, radiusX, radiusY, theta, baseExponent);
1102
- const [startX, startY] = pointOnArchitecturalCloud(startAngle, innerRx, innerRy);
1103
- const segments = [`M${svgNumber(startX)} ${svgNumber(startY)}`];
1104
- for (let index = 0; index < scallopCount; index += 1) {
1105
- const segmentStart = startAngle + index * angleStep;
1106
- const segmentMid = segmentStart + angleStep / 2;
1107
- const segmentEnd = segmentStart + angleStep;
1108
- const [controlX, controlY] = pointOnArchitecturalCloud(
1109
- segmentMid,
1110
- outerRx,
1111
- outerRy
1271
+ const radiusY = Math.min(
1272
+ ARCHITECTURAL_CLOUD_RADIUS,
1273
+ cloudHeight * ARCHITECTURAL_CLOUD_FLAT_RADIUS_RATIO
1274
+ );
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)
1285
+ );
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
1112
1296
  );
1113
- const [endX, endY] = pointOnArchitecturalCloud(segmentEnd, innerRx, innerRy);
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];
1311
+ const segments = [`M${svgNumber(startX)} ${svgNumber(startY)}`];
1312
+ for (const [endX, endY] of points.slice(1)) {
1114
1313
  segments.push(
1115
- `Q${svgNumber(controlX)} ${svgNumber(controlY)} ${svgNumber(endX)} ${svgNumber(endY)}`
1314
+ `A ${svgNumber(radiusX)} ${svgNumber(radiusY)} 0 0 1 ${svgNumber(endX)} ${svgNumber(endY)}`
1116
1315
  );
1117
1316
  }
1317
+ segments.push(
1318
+ `A ${svgNumber(radiusX)} ${svgNumber(radiusY)} 0 0 1 ${svgNumber(startX)} ${svgNumber(startY)}`
1319
+ );
1118
1320
  segments.push("Z");
1119
1321
  return segments.join(" ");
1120
1322
  }