@g9tm/lynxcontrols-canvas 2.5.10 → 2.5.12

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.
@@ -0,0 +1,809 @@
1
+ import perfect_freehand from "perfect-freehand";
2
+ import { RoughGenerator } from "roughjs/bin/generator";
3
+ import { DEFAULT_ADAPTIVE_RADIUS, DEFAULT_PROPORTIONAL_RADIUS, FRAME_STYLE, LINE_CONFIRM_THRESHOLD, ROUGHNESS, ROUNDNESS, isLinear, isTransparent, verticalOffset } from "./elements.js";
4
+ import { absoluteCoords, elementBounds } from "./geometry.js";
5
+ const generator = new RoughGenerator();
6
+ const dashed = (strokeWidth)=>[
7
+ 8,
8
+ 8 + strokeWidth
9
+ ];
10
+ const dotted = (strokeWidth)=>[
11
+ 1.5,
12
+ 6 + strokeWidth
13
+ ];
14
+ function canChangeRoundness(type) {
15
+ return 'rectangle' === type || 'iframe' === type || 'embeddable' === type || 'line' === type || 'diamond' === type || 'image' === type;
16
+ }
17
+ function adjustRoughness(element) {
18
+ const roughness = element.roughness;
19
+ const maxSize = Math.max(element.width, element.height);
20
+ const minSize = Math.min(element.width, element.height);
21
+ if (minSize >= 20 && maxSize >= 50 || minSize >= 15 && !!element.roundness && canChangeRoundness(element.type) || isLinear(element) && maxSize >= 50) return roughness;
22
+ return Math.min(roughness / (maxSize < 10 ? 3 : 2), 2.5);
23
+ }
24
+ function isPathALoop(points, zoom = 1) {
25
+ if (points.length >= 3) {
26
+ const [first, last] = [
27
+ points[0],
28
+ points[points.length - 1]
29
+ ];
30
+ return Math.hypot(first[0] - last[0], first[1] - last[1]) <= LINE_CONFIRM_THRESHOLD / zoom;
31
+ }
32
+ return false;
33
+ }
34
+ function roughOptions(element, continuousPath = false) {
35
+ const options = {
36
+ seed: element.seed,
37
+ strokeLineDash: 'dashed' === element.strokeStyle ? dashed(element.strokeWidth) : 'dotted' === element.strokeStyle ? dotted(element.strokeWidth) : void 0,
38
+ disableMultiStroke: 'solid' !== element.strokeStyle,
39
+ strokeWidth: 'solid' !== element.strokeStyle ? element.strokeWidth + 0.5 : element.strokeWidth,
40
+ fillWeight: element.strokeWidth / 2,
41
+ hachureGap: 4 * element.strokeWidth,
42
+ roughness: adjustRoughness(element),
43
+ stroke: element.strokeColor,
44
+ preserveVertices: continuousPath || element.roughness < ROUGHNESS.cartoonist
45
+ };
46
+ switch(element.type){
47
+ case 'rectangle':
48
+ case 'iframe':
49
+ case 'embeddable':
50
+ case 'diamond':
51
+ case 'ellipse':
52
+ options.fillStyle = element.fillStyle;
53
+ options.fill = isTransparent(element.backgroundColor) ? void 0 : element.backgroundColor;
54
+ if ('ellipse' === element.type) options.curveFitting = 1;
55
+ return options;
56
+ case 'line':
57
+ case 'freedraw':
58
+ var _element_points;
59
+ if (isPathALoop(null != (_element_points = element.points) ? _element_points : [])) {
60
+ options.fillStyle = element.fillStyle;
61
+ options.fill = 'transparent' === element.backgroundColor ? void 0 : element.backgroundColor;
62
+ }
63
+ return options;
64
+ default:
65
+ return options;
66
+ }
67
+ }
68
+ function cornerRadius(size, element) {
69
+ var _element_roundness;
70
+ const type = null == (_element_roundness = element.roundness) ? void 0 : _element_roundness.type;
71
+ if (type === ROUNDNESS.PROPORTIONAL_RADIUS || type === ROUNDNESS.LEGACY) return size * DEFAULT_PROPORTIONAL_RADIUS;
72
+ if (type === ROUNDNESS.ADAPTIVE_RADIUS) {
73
+ var _ref;
74
+ var _element_roundness1;
75
+ const fixed = null != (_ref = null == (_element_roundness1 = element.roundness) ? void 0 : _element_roundness1.value) ? _ref : DEFAULT_ADAPTIVE_RADIUS;
76
+ return size <= fixed / DEFAULT_PROPORTIONAL_RADIUS ? size * DEFAULT_PROPORTIONAL_RADIUS : fixed;
77
+ }
78
+ return 0;
79
+ }
80
+ function diamondPoints(element) {
81
+ const topX = Math.floor(element.width / 2) + 1;
82
+ const rightY = Math.floor(element.height / 2) + 1;
83
+ return [
84
+ topX,
85
+ 0,
86
+ element.width,
87
+ rightY,
88
+ topX,
89
+ element.height,
90
+ 0,
91
+ rightY
92
+ ];
93
+ }
94
+ function curvePathOps(shape) {
95
+ var _ref;
96
+ var _shape_sets_;
97
+ for (const set of shape.sets)if ('path' === set.type) return set.ops;
98
+ return null != (_ref = null == (_shape_sets_ = shape.sets[0]) ? void 0 : _shape_sets_.ops) ? _ref : [];
99
+ }
100
+ function arrowheadSize(arrowhead) {
101
+ if ('arrow' === arrowhead) return 25;
102
+ if ('diamond' === arrowhead || 'diamond_outline' === arrowhead) return 12;
103
+ if ('crowfoot_many' === arrowhead || 'crowfoot_one' === arrowhead || 'crowfoot_one_or_many' === arrowhead) return 20;
104
+ return 15;
105
+ }
106
+ function arrowheadAngle(arrowhead) {
107
+ if ('bar' === arrowhead) return 90;
108
+ if ('arrow' === arrowhead) return 20;
109
+ return 25;
110
+ }
111
+ function rotateAround([x, y], [cx, cy], angle) {
112
+ return [
113
+ (x - cx) * Math.cos(angle) - (y - cy) * Math.sin(angle) + cx,
114
+ (x - cx) * Math.sin(angle) + (y - cy) * Math.cos(angle) + cy
115
+ ];
116
+ }
117
+ function arrowheadPoints(element, shape, position, arrowhead) {
118
+ var _element_points;
119
+ var _ops_index;
120
+ if (shape.length < 1) return null;
121
+ const ops = curvePathOps(shape[0]);
122
+ if (ops.length < 1) return null;
123
+ const index = 'start' === position ? 1 : ops.length - 1;
124
+ const data = null == (_ops_index = ops[index]) ? void 0 : _ops_index.data;
125
+ if (!data || 6 !== data.length) return null;
126
+ const p3 = [
127
+ data[4],
128
+ data[5]
129
+ ];
130
+ const p2 = [
131
+ data[2],
132
+ data[3]
133
+ ];
134
+ const p1 = [
135
+ data[0],
136
+ data[1]
137
+ ];
138
+ const previous = ops[index - 1];
139
+ let p0 = [
140
+ 0,
141
+ 0
142
+ ];
143
+ if ('move' === previous.op) p0 = [
144
+ previous.data[0],
145
+ previous.data[1]
146
+ ];
147
+ else if ('bcurveTo' === previous.op) p0 = [
148
+ previous.data[4],
149
+ previous.data[5]
150
+ ];
151
+ const equation = (t, i)=>(1 - t) ** 3 * p3[i] + 3 * t * (1 - t) ** 2 * p2[i] + 3 * t ** 2 * (1 - t) * p1[i] + p0[i] * t ** 3;
152
+ const [x2, y2] = 'start' === position ? p0 : p3;
153
+ const [x1, y1] = [
154
+ equation(0.3, 0),
155
+ equation(0.3, 1)
156
+ ];
157
+ const distance = Math.hypot(x2 - x1, y2 - y1);
158
+ const nx = (x2 - x1) / distance;
159
+ const ny = (y2 - y1) / distance;
160
+ const size = arrowheadSize(arrowhead);
161
+ const points = null != (_element_points = element.points) ? _element_points : [];
162
+ const [cx, cy] = 'end' === position ? points[points.length - 1] : points[0];
163
+ const [px, py] = points.length > 1 ? 'end' === position ? points[points.length - 2] : points[1] : [
164
+ 0,
165
+ 0
166
+ ];
167
+ const length = Math.hypot(cx - px, cy - py);
168
+ const minSize = Math.min(size, length * ('diamond' === arrowhead || 'diamond_outline' === arrowhead ? 0.25 : 0.5));
169
+ const xs = x2 - nx * minSize;
170
+ const ys = y2 - ny * minSize;
171
+ if ('dot' === arrowhead || 'circle' === arrowhead || 'circle_outline' === arrowhead) return [
172
+ x2,
173
+ y2,
174
+ Math.hypot(ys - y2, xs - x2) + element.strokeWidth - 2
175
+ ];
176
+ const angle = arrowheadAngle(arrowhead);
177
+ if ('crowfoot_many' === arrowhead || 'crowfoot_one_or_many' === arrowhead) {
178
+ const [x3, y3] = rotateAround([
179
+ x2,
180
+ y2
181
+ ], [
182
+ xs,
183
+ ys
184
+ ], -angle * Math.PI / 180);
185
+ const [x4, y4] = rotateAround([
186
+ x2,
187
+ y2
188
+ ], [
189
+ xs,
190
+ ys
191
+ ], angle * Math.PI / 180);
192
+ return [
193
+ xs,
194
+ ys,
195
+ x3,
196
+ y3,
197
+ x4,
198
+ y4
199
+ ];
200
+ }
201
+ const [x3, y3] = rotateAround([
202
+ xs,
203
+ ys
204
+ ], [
205
+ x2,
206
+ y2
207
+ ], -angle * Math.PI / 180);
208
+ const [x4, y4] = rotateAround([
209
+ xs,
210
+ ys
211
+ ], [
212
+ x2,
213
+ y2
214
+ ], angle * Math.PI / 180);
215
+ if ('diamond' === arrowhead || 'diamond_outline' === arrowhead) {
216
+ let ox;
217
+ let oy;
218
+ if ('start' === position) {
219
+ const [qx, qy] = points.length > 1 ? points[1] : [
220
+ 0,
221
+ 0
222
+ ];
223
+ [ox, oy] = rotateAround([
224
+ x2 + 2 * minSize,
225
+ y2
226
+ ], [
227
+ x2,
228
+ y2
229
+ ], Math.atan2(qy - y2, qx - x2));
230
+ } else {
231
+ const [qx, qy] = points.length > 1 ? points[points.length - 2] : [
232
+ 0,
233
+ 0
234
+ ];
235
+ [ox, oy] = rotateAround([
236
+ x2 - 2 * minSize,
237
+ y2
238
+ ], [
239
+ x2,
240
+ y2
241
+ ], Math.atan2(y2 - qy, x2 - qx));
242
+ }
243
+ return [
244
+ x2,
245
+ y2,
246
+ x3,
247
+ y3,
248
+ ox,
249
+ oy,
250
+ x4,
251
+ y4
252
+ ];
253
+ }
254
+ return [
255
+ x2,
256
+ y2,
257
+ x3,
258
+ y3,
259
+ x4,
260
+ y4
261
+ ];
262
+ }
263
+ function arrowheadShapes(element, shape, position, arrowhead, options, background) {
264
+ const points = arrowheadPoints(element, shape, position, arrowhead);
265
+ if (null === points) return [];
266
+ const crowfootOne = (head, o)=>null === head ? [] : [
267
+ generator.line(head[2], head[3], head[4], head[5], o)
268
+ ];
269
+ switch(arrowhead){
270
+ case 'dot':
271
+ case 'circle':
272
+ case 'circle_outline':
273
+ {
274
+ const [x, y, diameter] = points;
275
+ delete options.strokeLineDash;
276
+ return [
277
+ generator.circle(x, y, diameter, {
278
+ ...options,
279
+ fill: 'circle_outline' === arrowhead ? background : element.strokeColor,
280
+ fillStyle: 'solid',
281
+ stroke: element.strokeColor,
282
+ roughness: Math.min(0.5, options.roughness || 0)
283
+ })
284
+ ];
285
+ }
286
+ case 'triangle':
287
+ case 'triangle_outline':
288
+ {
289
+ const [x, y, x2, y2, x3, y3] = points;
290
+ delete options.strokeLineDash;
291
+ return [
292
+ generator.polygon([
293
+ [
294
+ x,
295
+ y
296
+ ],
297
+ [
298
+ x2,
299
+ y2
300
+ ],
301
+ [
302
+ x3,
303
+ y3
304
+ ],
305
+ [
306
+ x,
307
+ y
308
+ ]
309
+ ], {
310
+ ...options,
311
+ fill: 'triangle_outline' === arrowhead ? background : element.strokeColor,
312
+ fillStyle: 'solid',
313
+ roughness: Math.min(1, options.roughness || 0)
314
+ })
315
+ ];
316
+ }
317
+ case 'diamond':
318
+ case 'diamond_outline':
319
+ {
320
+ const [x, y, x2, y2, x3, y3, x4, y4] = points;
321
+ delete options.strokeLineDash;
322
+ return [
323
+ generator.polygon([
324
+ [
325
+ x,
326
+ y
327
+ ],
328
+ [
329
+ x2,
330
+ y2
331
+ ],
332
+ [
333
+ x3,
334
+ y3
335
+ ],
336
+ [
337
+ x4,
338
+ y4
339
+ ],
340
+ [
341
+ x,
342
+ y
343
+ ]
344
+ ], {
345
+ ...options,
346
+ fill: 'diamond_outline' === arrowhead ? background : element.strokeColor,
347
+ fillStyle: 'solid',
348
+ roughness: Math.min(1, options.roughness || 0)
349
+ })
350
+ ];
351
+ }
352
+ case 'crowfoot_one':
353
+ return crowfootOne(points, options);
354
+ default:
355
+ {
356
+ const [x2, y2, x3, y3, x4, y4] = points;
357
+ if ('dotted' === element.strokeStyle) {
358
+ const dash = dotted(element.strokeWidth - 1);
359
+ options.strokeLineDash = [
360
+ dash[0],
361
+ dash[1] - 1
362
+ ];
363
+ } else delete options.strokeLineDash;
364
+ options.roughness = Math.min(1, options.roughness || 0);
365
+ return [
366
+ generator.line(x3, y3, x2, y2, options),
367
+ generator.line(x4, y4, x2, y2, options),
368
+ ...'crowfoot_one_or_many' === arrowhead ? crowfootOne(arrowheadPoints(element, shape, position, 'crowfoot_one'), options) : []
369
+ ];
370
+ }
371
+ }
372
+ }
373
+ function round(value) {
374
+ return Math.round(100 * value) / 100;
375
+ }
376
+ function opsToPath(set) {
377
+ let path = '';
378
+ for (const item of set.ops){
379
+ const d = item.data;
380
+ if ('move' === item.op) path += `M${round(d[0])} ${round(d[1])}`;
381
+ else if ('bcurveTo' === item.op) path += `C${round(d[0])} ${round(d[1])} ${round(d[2])} ${round(d[3])} ${round(d[4])} ${round(d[5])}`;
382
+ else if ('lineTo' === item.op) path += `L${round(d[0])} ${round(d[1])}`;
383
+ }
384
+ return path;
385
+ }
386
+ function drawableOps(drawable, out) {
387
+ const o = drawable.options;
388
+ for (const set of drawable.sets){
389
+ const d = opsToPath(set);
390
+ if (d) {
391
+ if ('path' === set.type) {
392
+ if (!o.stroke || 'none' === o.stroke) continue;
393
+ out.push({
394
+ k: 'p',
395
+ d,
396
+ s: o.stroke,
397
+ w: o.strokeWidth,
398
+ ...o.strokeLineDash ? {
399
+ dash: o.strokeLineDash
400
+ } : {}
401
+ });
402
+ } else if ('fillPath' === set.type) {
403
+ if (!o.fill) continue;
404
+ const evenOdd = 'curve' === drawable.shape || 'polygon' === drawable.shape || 'path' === drawable.shape;
405
+ out.push({
406
+ k: 'p',
407
+ d,
408
+ f: o.fill,
409
+ ...evenOdd ? {
410
+ eo: true
411
+ } : {}
412
+ });
413
+ } else if ('fillSketch' === set.type) {
414
+ if (!o.fill) continue;
415
+ const weight = o.fillWeight < 0 ? o.strokeWidth / 2 : o.fillWeight;
416
+ out.push({
417
+ k: 'p',
418
+ d,
419
+ s: o.fill,
420
+ w: weight,
421
+ ...o.fillLineDash ? {
422
+ dash: o.fillLineDash
423
+ } : {}
424
+ });
425
+ }
426
+ }
427
+ }
428
+ }
429
+ function med(a, b) {
430
+ return [
431
+ (a[0] + b[0]) / 2,
432
+ (a[1] + b[1]) / 2
433
+ ];
434
+ }
435
+ function svgPathFromStroke(points) {
436
+ if (!points.length) return '';
437
+ const max = points.length - 1;
438
+ let d = `M${round(points[0][0])} ${round(points[0][1])}Q`;
439
+ for(let i = 0; i < points.length; i++){
440
+ const point = points[i];
441
+ if (i === max) {
442
+ const m = med(point, points[0]);
443
+ d += `${round(point[0])} ${round(point[1])} ${round(m[0])} ${round(m[1])}L${round(points[0][0])} ${round(points[0][1])}Z`;
444
+ } else {
445
+ const m = med(point, points[i + 1]);
446
+ d += `${round(point[0])} ${round(point[1])} ${round(m[0])} ${round(m[1])} `;
447
+ }
448
+ }
449
+ return d;
450
+ }
451
+ function freeDrawPath(element) {
452
+ var _element_points;
453
+ const points = null != (_element_points = element.points) ? _element_points : [];
454
+ const input = element.simulatePressure ? points : points.length ? points.map(([x, y], i)=>{
455
+ var _ref;
456
+ var _element_pressures;
457
+ return [
458
+ x,
459
+ y,
460
+ null != (_ref = null == (_element_pressures = element.pressures) ? void 0 : _element_pressures[i]) ? _ref : 0.5
461
+ ];
462
+ }) : [
463
+ [
464
+ 0,
465
+ 0,
466
+ 0.5
467
+ ]
468
+ ];
469
+ return svgPathFromStroke(perfect_freehand(input, {
470
+ simulatePressure: element.simulatePressure,
471
+ size: 4.25 * element.strokeWidth,
472
+ thinning: 0.6,
473
+ smoothing: 0.5,
474
+ streamline: 0.5,
475
+ easing: (t)=>Math.sin(t * Math.PI / 2),
476
+ last: !!element.lastCommittedPoint
477
+ }));
478
+ }
479
+ function textLines(text) {
480
+ return text.replace(/\r\n?/g, '\n').split('\n');
481
+ }
482
+ const LTR_RANGES = [
483
+ [
484
+ 0x41,
485
+ 0x5a
486
+ ],
487
+ [
488
+ 0x61,
489
+ 0x7a
490
+ ],
491
+ [
492
+ 0xc0,
493
+ 0xd6
494
+ ],
495
+ [
496
+ 0xd8,
497
+ 0xf6
498
+ ],
499
+ [
500
+ 0xf8,
501
+ 0x2b8
502
+ ],
503
+ [
504
+ 0x300,
505
+ 0x590
506
+ ],
507
+ [
508
+ 0x800,
509
+ 0x1fff
510
+ ],
511
+ [
512
+ 0x2c00,
513
+ 0xfb1c
514
+ ],
515
+ [
516
+ 0xfdfe,
517
+ 0xfe6f
518
+ ],
519
+ [
520
+ 0xfefd,
521
+ 0xffff
522
+ ]
523
+ ];
524
+ const RTL_RANGES = [
525
+ [
526
+ 0x591,
527
+ 0x7ff
528
+ ],
529
+ [
530
+ 0xfb1d,
531
+ 0xfdfd
532
+ ],
533
+ [
534
+ 0xfe70,
535
+ 0xfefc
536
+ ]
537
+ ];
538
+ const inRanges = (code, ranges)=>ranges.some(([a, b])=>code >= a && code <= b);
539
+ function isRtl(text) {
540
+ for(let i = 0; i < text.length; i++){
541
+ const code = text.charCodeAt(i);
542
+ if (inRanges(code, RTL_RANGES)) return true;
543
+ if (inRanges(code, LTR_RANGES)) break;
544
+ }
545
+ return false;
546
+ }
547
+ function elementOps(element, context) {
548
+ const ops = [];
549
+ switch(element.type){
550
+ case 'rectangle':
551
+ case 'iframe':
552
+ case 'embeddable':
553
+ {
554
+ const w = element.width;
555
+ const h = element.height;
556
+ if (element.roundness) {
557
+ const r = cornerRadius(Math.min(w, h), element);
558
+ drawableOps(generator.path(`M ${r} 0 L ${w - r} 0 Q ${w} 0, ${w} ${r} L ${w} ${h - r} Q ${w} ${h}, ${w - r} ${h} L ${r} ${h} Q 0 ${h}, 0 ${h - r} L 0 ${r} Q 0 0, ${r} 0`, roughOptions(element, true)), ops);
559
+ } else drawableOps(generator.rectangle(0, 0, w, h, roughOptions(element, false)), ops);
560
+ break;
561
+ }
562
+ case 'diamond':
563
+ {
564
+ const [topX, topY, rightX, rightY, bottomX, bottomY, leftX, leftY] = diamondPoints(element);
565
+ if (element.roundness) {
566
+ const vr = cornerRadius(Math.abs(topX - leftX), element);
567
+ const hr = cornerRadius(Math.abs(rightY - topY), element);
568
+ drawableOps(generator.path(`M ${topX + vr} ${topY + hr} L ${rightX - vr} ${rightY - hr}
569
+ C ${rightX} ${rightY}, ${rightX} ${rightY}, ${rightX - vr} ${rightY + hr}
570
+ L ${bottomX + vr} ${bottomY - hr}
571
+ C ${bottomX} ${bottomY}, ${bottomX} ${bottomY}, ${bottomX - vr} ${bottomY - hr}
572
+ L ${leftX + vr} ${leftY + hr}
573
+ C ${leftX} ${leftY}, ${leftX} ${leftY}, ${leftX + vr} ${leftY - hr}
574
+ L ${topX - vr} ${topY + hr}
575
+ C ${topX} ${topY}, ${topX} ${topY}, ${topX + vr} ${topY + hr}`, roughOptions(element, true)), ops);
576
+ } else drawableOps(generator.polygon([
577
+ [
578
+ topX,
579
+ topY
580
+ ],
581
+ [
582
+ rightX,
583
+ rightY
584
+ ],
585
+ [
586
+ bottomX,
587
+ bottomY
588
+ ],
589
+ [
590
+ leftX,
591
+ leftY
592
+ ]
593
+ ], roughOptions(element)), ops);
594
+ break;
595
+ }
596
+ case 'ellipse':
597
+ drawableOps(generator.ellipse(element.width / 2, element.height / 2, element.width, element.height, roughOptions(element)), ops);
598
+ break;
599
+ case 'line':
600
+ case 'arrow':
601
+ {
602
+ var _element_points;
603
+ const options = roughOptions(element);
604
+ const points = (null == (_element_points = element.points) ? void 0 : _element_points.length) ? element.points : [
605
+ [
606
+ 0,
607
+ 0
608
+ ]
609
+ ];
610
+ let shape;
611
+ shape = element.elbowed ? [
612
+ generator.path(elbowPath(points, 16), roughOptions(element, true))
613
+ ] : element.roundness ? [
614
+ generator.curve(points, options)
615
+ ] : [
616
+ options.fill ? generator.polygon(points, options) : generator.linearPath(points, options)
617
+ ];
618
+ if ('arrow' === element.type) {
619
+ var _element_startArrowhead;
620
+ const start = null != (_element_startArrowhead = element.startArrowhead) ? _element_startArrowhead : null;
621
+ const end = void 0 === element.endArrowhead ? 'arrow' : element.endArrowhead;
622
+ if (null !== start) shape.push(...arrowheadShapes(element, shape, 'start', start, {
623
+ ...options
624
+ }, context.background));
625
+ if (null !== end) shape.push(...arrowheadShapes(element, shape, 'end', end, {
626
+ ...options
627
+ }, context.background));
628
+ }
629
+ for (const drawable of shape)drawableOps(drawable, ops);
630
+ break;
631
+ }
632
+ case 'freedraw':
633
+ {
634
+ var _element_points1;
635
+ const points = null != (_element_points1 = element.points) ? _element_points1 : [];
636
+ if (isPathALoop(points) && !isTransparent(element.backgroundColor)) drawableOps(generator.curve(points, {
637
+ ...roughOptions(element),
638
+ stroke: 'none'
639
+ }), ops);
640
+ ops.push({
641
+ k: 'p',
642
+ d: freeDrawPath(element),
643
+ f: element.strokeColor
644
+ });
645
+ break;
646
+ }
647
+ case 'text':
648
+ {
649
+ var _element_fontSize, _element_fontFamily, _element_lineHeight, _element_textAlign, _element_text;
650
+ const fontSize = null != (_element_fontSize = element.fontSize) ? _element_fontSize : 20;
651
+ const fontFamily = null != (_element_fontFamily = element.fontFamily) ? _element_fontFamily : 5;
652
+ const lineHeightPx = fontSize * (null != (_element_lineHeight = element.lineHeight) ? _element_lineHeight : 1.25);
653
+ const align = null != (_element_textAlign = element.textAlign) ? _element_textAlign : 'left';
654
+ const text = null != (_element_text = element.text) ? _element_text : '';
655
+ ops.push({
656
+ k: 't',
657
+ t: textLines(text),
658
+ x: 'center' === align ? element.width / 2 : 'right' === align ? element.width : 0,
659
+ y: verticalOffset(fontFamily, fontSize, lineHeightPx),
660
+ lh: lineHeightPx,
661
+ size: fontSize,
662
+ font: fontFamily,
663
+ c: element.strokeColor,
664
+ al: align,
665
+ w: element.width,
666
+ ...isRtl(text) ? {
667
+ rtl: true
668
+ } : {}
669
+ });
670
+ break;
671
+ }
672
+ case 'image':
673
+ {
674
+ var _element_fileId;
675
+ const crop = element.crop;
676
+ ops.push({
677
+ k: 'i',
678
+ file: null != (_element_fileId = element.fileId) ? _element_fileId : '',
679
+ w: element.width,
680
+ h: element.height,
681
+ ...element.roundness ? {
682
+ r: cornerRadius(Math.min(element.width, element.height), element)
683
+ } : {},
684
+ ...crop ? {
685
+ crop: [
686
+ crop.x,
687
+ crop.y,
688
+ crop.width,
689
+ crop.height
690
+ ]
691
+ } : {}
692
+ });
693
+ break;
694
+ }
695
+ case 'frame':
696
+ case 'magicframe':
697
+ {
698
+ var _element_name;
699
+ const w = element.width;
700
+ const h = element.height;
701
+ const r = Math.min(FRAME_STYLE.radius, w / 2, h / 2);
702
+ ops.push({
703
+ k: 'p',
704
+ d: `M${r} 0L${w - r} 0Q${w} 0 ${w} ${r}L${w} ${h - r}Q${w} ${h} ${w - r} ${h}L${r} ${h}Q0 ${h} 0 ${h - r}L0 ${r}Q0 0 ${r} 0Z`,
705
+ s: FRAME_STYLE.strokeColor,
706
+ w: FRAME_STYLE.strokeWidth,
707
+ f: 'rgba(0, 0, 200, 0.04)'
708
+ });
709
+ const name = null != (_element_name = element.name) ? _element_name : 'frame' === element.type ? 'Frame' : 'AI Frame';
710
+ ops.push({
711
+ k: 't',
712
+ t: [
713
+ name
714
+ ],
715
+ x: 0,
716
+ y: -FRAME_STYLE.nameOffsetY - 3,
717
+ lh: FRAME_STYLE.nameFontSize * FRAME_STYLE.nameLineHeight,
718
+ size: FRAME_STYLE.nameFontSize,
719
+ font: 2,
720
+ c: context.dark ? FRAME_STYLE.nameColorDarkTheme : FRAME_STYLE.nameColorLightTheme,
721
+ al: 'left',
722
+ w,
723
+ ...isRtl(name) ? {
724
+ rtl: true
725
+ } : {}
726
+ });
727
+ break;
728
+ }
729
+ default:
730
+ break;
731
+ }
732
+ return ops;
733
+ }
734
+ function elbowPath(points, radius) {
735
+ const horizontal = (a, b)=>Math.abs(a[0] - b[0]) >= Math.abs(a[1] - b[1]);
736
+ const sub = [];
737
+ for(let i = 1; i < points.length - 1; i++){
738
+ const prev = points[i - 1];
739
+ const next = points[i + 1];
740
+ const point = points[i];
741
+ const corner = Math.min(radius, Math.hypot(point[0] - next[0], point[1] - next[1]) / 2, Math.hypot(point[0] - prev[0], point[1] - prev[1]) / 2);
742
+ if (horizontal(point, prev)) sub.push(prev[0] < point[0] ? [
743
+ point[0] - corner,
744
+ point[1]
745
+ ] : [
746
+ point[0] + corner,
747
+ point[1]
748
+ ]);
749
+ else sub.push(prev[1] < point[1] ? [
750
+ point[0],
751
+ point[1] - corner
752
+ ] : [
753
+ point[0],
754
+ point[1] + corner
755
+ ]);
756
+ sub.push(point);
757
+ if (horizontal(next, point)) sub.push(next[0] < point[0] ? [
758
+ point[0] - corner,
759
+ point[1]
760
+ ] : [
761
+ point[0] + corner,
762
+ point[1]
763
+ ]);
764
+ else sub.push(next[1] < point[1] ? [
765
+ point[0],
766
+ point[1] - corner
767
+ ] : [
768
+ point[0],
769
+ point[1] + corner
770
+ ]);
771
+ }
772
+ const d = [
773
+ `M ${points[0][0]} ${points[0][1]}`
774
+ ];
775
+ for(let i = 0; i < sub.length; i += 3){
776
+ d.push(`L ${sub[i][0]} ${sub[i][1]}`);
777
+ d.push(`Q ${sub[i + 1][0]} ${sub[i + 1][1]}, ${sub[i + 2][0]} ${sub[i + 2][1]}`);
778
+ }
779
+ d.push(`L ${points[points.length - 1][0]} ${points[points.length - 1][1]}`);
780
+ return d.join(' ');
781
+ }
782
+ function toItem(element, context) {
783
+ var _element_opacity;
784
+ const [, , , , cx, cy] = absoluteCoords(element);
785
+ return {
786
+ id: element.id,
787
+ v: element.version,
788
+ x: element.x,
789
+ y: element.y,
790
+ a: element.angle || 0,
791
+ cx,
792
+ cy,
793
+ o: Math.max(0, Math.min(100, null != (_element_opacity = element.opacity) ? _element_opacity : 100)) / 100,
794
+ b: padBounds(elementBounds(element), element),
795
+ ops: elementOps(element, context)
796
+ };
797
+ }
798
+ function padBounds(bounds, element) {
799
+ const pad = 2 * (element.strokeWidth || 1) + 4 * (element.roughness || 0) + ('arrow' === element.type ? 20 : 0);
800
+ return [
801
+ bounds[0] - pad,
802
+ bounds[1] - pad - ('frame' === element.type || 'magicframe' === element.type ? 24 : 0),
803
+ bounds[2] + pad,
804
+ bounds[3] + pad
805
+ ];
806
+ }
807
+ export { cornerRadius, diamondPoints, elementOps, freeDrawPath, isPathALoop, isRtl, toItem };
808
+
809
+ //# sourceMappingURL=shapes.js.map