|
@@ -5,22 +5,27 @@ import { RoughCanvas } from "roughjs/bin/canvas";
|
5
|
5
|
import { TwitterPicker } from "react-color";
|
6
|
6
|
|
7
|
7
|
import { moveOneLeft, moveAllLeft, moveOneRight, moveAllRight } from "./zindex";
|
8
|
|
-import { LCG, randomSeed, withCustomMathRandom } from "./random";
|
9
|
|
-import { distanceBetweenPointAndSegment } from "./math";
|
|
8
|
+import { randomSeed } from "./random";
|
10
|
9
|
import { roundRect } from "./roundRect";
|
|
10
|
+import {
|
|
11
|
+ newElement,
|
|
12
|
+ resizeTest,
|
|
13
|
+ generateDraw,
|
|
14
|
+ getElementAbsoluteX1,
|
|
15
|
+ getElementAbsoluteX2,
|
|
16
|
+ getElementAbsoluteY1,
|
|
17
|
+ getElementAbsoluteY2,
|
|
18
|
+ handlerRectangles,
|
|
19
|
+ hitTest,
|
|
20
|
+ isTextElement
|
|
21
|
+} from "./element";
|
|
22
|
+import { SceneState } from "./scene/types";
|
|
23
|
+import { ExcalidrawElement, ExcalidrawTextElement } from "./element/types";
|
11
|
24
|
|
12
|
25
|
import EditableText from "./components/EditableText";
|
13
|
26
|
|
14
|
27
|
import "./styles.scss";
|
15
|
28
|
|
16
|
|
-type ExcalidrawElement = ReturnType<typeof newElement>;
|
17
|
|
-type ExcalidrawTextElement = ExcalidrawElement & {
|
18
|
|
- type: "text";
|
19
|
|
- font: string;
|
20
|
|
- text: string;
|
21
|
|
- actualBoundingBoxAscent: number;
|
22
|
|
-};
|
23
|
|
-
|
24
|
29
|
const LOCAL_STORAGE_KEY = "excalidraw";
|
25
|
30
|
const LOCAL_STORAGE_KEY_STATE = "excalidraw-state";
|
26
|
31
|
|
|
@@ -58,186 +63,6 @@ function restoreHistoryEntry(entry: string) {
|
58
|
63
|
skipHistory = true;
|
59
|
64
|
}
|
60
|
65
|
|
61
|
|
-function hitTest(element: ExcalidrawElement, x: number, y: number): boolean {
|
62
|
|
- // For shapes that are composed of lines, we only enable point-selection when the distance
|
63
|
|
- // of the click is less than x pixels of any of the lines that the shape is composed of
|
64
|
|
- const lineThreshold = 10;
|
65
|
|
-
|
66
|
|
- if (element.type === "ellipse") {
|
67
|
|
- // https://stackoverflow.com/a/46007540/232122
|
68
|
|
- const px = Math.abs(x - element.x - element.width / 2);
|
69
|
|
- const py = Math.abs(y - element.y - element.height / 2);
|
70
|
|
-
|
71
|
|
- let tx = 0.707;
|
72
|
|
- let ty = 0.707;
|
73
|
|
-
|
74
|
|
- const a = element.width / 2;
|
75
|
|
- const b = element.height / 2;
|
76
|
|
-
|
77
|
|
- [0, 1, 2, 3].forEach(x => {
|
78
|
|
- const xx = a * tx;
|
79
|
|
- const yy = b * ty;
|
80
|
|
-
|
81
|
|
- const ex = ((a * a - b * b) * tx ** 3) / a;
|
82
|
|
- const ey = ((b * b - a * a) * ty ** 3) / b;
|
83
|
|
-
|
84
|
|
- const rx = xx - ex;
|
85
|
|
- const ry = yy - ey;
|
86
|
|
-
|
87
|
|
- const qx = px - ex;
|
88
|
|
- const qy = py - ey;
|
89
|
|
-
|
90
|
|
- const r = Math.hypot(ry, rx);
|
91
|
|
- const q = Math.hypot(qy, qx);
|
92
|
|
-
|
93
|
|
- tx = Math.min(1, Math.max(0, ((qx * r) / q + ex) / a));
|
94
|
|
- ty = Math.min(1, Math.max(0, ((qy * r) / q + ey) / b));
|
95
|
|
- const t = Math.hypot(ty, tx);
|
96
|
|
- tx /= t;
|
97
|
|
- ty /= t;
|
98
|
|
- });
|
99
|
|
-
|
100
|
|
- return Math.hypot(a * tx - px, b * ty - py) < lineThreshold;
|
101
|
|
- } else if (element.type === "rectangle") {
|
102
|
|
- const x1 = getElementAbsoluteX1(element);
|
103
|
|
- const x2 = getElementAbsoluteX2(element);
|
104
|
|
- const y1 = getElementAbsoluteY1(element);
|
105
|
|
- const y2 = getElementAbsoluteY2(element);
|
106
|
|
-
|
107
|
|
- // (x1, y1) --A-- (x2, y1)
|
108
|
|
- // |D |B
|
109
|
|
- // (x1, y2) --C-- (x2, y2)
|
110
|
|
- return (
|
111
|
|
- distanceBetweenPointAndSegment(x, y, x1, y1, x2, y1) < lineThreshold || // A
|
112
|
|
- distanceBetweenPointAndSegment(x, y, x2, y1, x2, y2) < lineThreshold || // B
|
113
|
|
- distanceBetweenPointAndSegment(x, y, x2, y2, x1, y2) < lineThreshold || // C
|
114
|
|
- distanceBetweenPointAndSegment(x, y, x1, y2, x1, y1) < lineThreshold // D
|
115
|
|
- );
|
116
|
|
- } else if (element.type === "diamond") {
|
117
|
|
- x -= element.x;
|
118
|
|
- y -= element.y;
|
119
|
|
-
|
120
|
|
- const [
|
121
|
|
- topX,
|
122
|
|
- topY,
|
123
|
|
- rightX,
|
124
|
|
- rightY,
|
125
|
|
- bottomX,
|
126
|
|
- bottomY,
|
127
|
|
- leftX,
|
128
|
|
- leftY
|
129
|
|
- ] = getDiamondPoints(element);
|
130
|
|
-
|
131
|
|
- return (
|
132
|
|
- distanceBetweenPointAndSegment(x, y, topX, topY, rightX, rightY) <
|
133
|
|
- lineThreshold ||
|
134
|
|
- distanceBetweenPointAndSegment(x, y, rightX, rightY, bottomX, bottomY) <
|
135
|
|
- lineThreshold ||
|
136
|
|
- distanceBetweenPointAndSegment(x, y, bottomX, bottomY, leftX, leftY) <
|
137
|
|
- lineThreshold ||
|
138
|
|
- distanceBetweenPointAndSegment(x, y, leftX, leftY, topX, topY) <
|
139
|
|
- lineThreshold
|
140
|
|
- );
|
141
|
|
- } else if (element.type === "arrow") {
|
142
|
|
- let [x1, y1, x2, y2, x3, y3, x4, y4] = getArrowPoints(element);
|
143
|
|
- // The computation is done at the origin, we need to add a translation
|
144
|
|
- x -= element.x;
|
145
|
|
- y -= element.y;
|
146
|
|
-
|
147
|
|
- return (
|
148
|
|
- // \
|
149
|
|
- distanceBetweenPointAndSegment(x, y, x3, y3, x2, y2) < lineThreshold ||
|
150
|
|
- // -----
|
151
|
|
- distanceBetweenPointAndSegment(x, y, x1, y1, x2, y2) < lineThreshold ||
|
152
|
|
- // /
|
153
|
|
- distanceBetweenPointAndSegment(x, y, x4, y4, x2, y2) < lineThreshold
|
154
|
|
- );
|
155
|
|
- } else if (element.type === "text") {
|
156
|
|
- const x1 = getElementAbsoluteX1(element);
|
157
|
|
- const x2 = getElementAbsoluteX2(element);
|
158
|
|
- const y1 = getElementAbsoluteY1(element);
|
159
|
|
- const y2 = getElementAbsoluteY2(element);
|
160
|
|
-
|
161
|
|
- return x >= x1 && x <= x2 && y >= y1 && y <= y2;
|
162
|
|
- } else if (element.type === "selection") {
|
163
|
|
- console.warn("This should not happen, we need to investigate why it does.");
|
164
|
|
- return false;
|
165
|
|
- } else {
|
166
|
|
- throw new Error("Unimplemented type " + element.type);
|
167
|
|
- }
|
168
|
|
-}
|
169
|
|
-
|
170
|
|
-function resizeTest(
|
171
|
|
- element: ExcalidrawElement,
|
172
|
|
- x: number,
|
173
|
|
- y: number,
|
174
|
|
- sceneState: SceneState
|
175
|
|
-): string | false {
|
176
|
|
- if (element.type === "text") return false;
|
177
|
|
-
|
178
|
|
- const handlers = handlerRectangles(element, sceneState);
|
179
|
|
-
|
180
|
|
- const filter = Object.keys(handlers).filter(key => {
|
181
|
|
- const handler = handlers[key];
|
182
|
|
-
|
183
|
|
- return (
|
184
|
|
- x + sceneState.scrollX >= handler[0] &&
|
185
|
|
- x + sceneState.scrollX <= handler[0] + handler[2] &&
|
186
|
|
- y + sceneState.scrollY >= handler[1] &&
|
187
|
|
- y + sceneState.scrollY <= handler[1] + handler[3]
|
188
|
|
- );
|
189
|
|
- });
|
190
|
|
-
|
191
|
|
- if (filter.length > 0) {
|
192
|
|
- return filter[0];
|
193
|
|
- }
|
194
|
|
-
|
195
|
|
- return false;
|
196
|
|
-}
|
197
|
|
-
|
198
|
|
-function newElement(
|
199
|
|
- type: string,
|
200
|
|
- x: number,
|
201
|
|
- y: number,
|
202
|
|
- strokeColor: string,
|
203
|
|
- backgroundColor: string,
|
204
|
|
- fillStyle: string,
|
205
|
|
- strokeWidth: number,
|
206
|
|
- roughness: number,
|
207
|
|
- opacity: number,
|
208
|
|
- width = 0,
|
209
|
|
- height = 0
|
210
|
|
-) {
|
211
|
|
- const element = {
|
212
|
|
- type: type,
|
213
|
|
- x: x,
|
214
|
|
- y: y,
|
215
|
|
- width: width,
|
216
|
|
- height: height,
|
217
|
|
- isSelected: false,
|
218
|
|
- strokeColor: strokeColor,
|
219
|
|
- backgroundColor: backgroundColor,
|
220
|
|
- fillStyle: fillStyle,
|
221
|
|
- strokeWidth: strokeWidth,
|
222
|
|
- roughness: roughness,
|
223
|
|
- opacity: opacity,
|
224
|
|
- seed: randomSeed(),
|
225
|
|
- draw(
|
226
|
|
- rc: RoughCanvas,
|
227
|
|
- context: CanvasRenderingContext2D,
|
228
|
|
- sceneState: SceneState
|
229
|
|
- ) {}
|
230
|
|
- };
|
231
|
|
- return element;
|
232
|
|
-}
|
233
|
|
-
|
234
|
|
-type SceneState = {
|
235
|
|
- scrollX: number;
|
236
|
|
- scrollY: number;
|
237
|
|
- // null indicates transparent bg
|
238
|
|
- viewBackgroundColor: string | null;
|
239
|
|
-};
|
240
|
|
-
|
241
|
66
|
const SCROLLBAR_WIDTH = 6;
|
242
|
67
|
const SCROLLBAR_MIN_SIZE = 15;
|
243
|
68
|
const SCROLLBAR_MARGIN = 4;
|
|
@@ -340,86 +165,6 @@ function isOverScrollBars(
|
340
|
165
|
};
|
341
|
166
|
}
|
342
|
167
|
|
343
|
|
-function handlerRectangles(element: ExcalidrawElement, sceneState: SceneState) {
|
344
|
|
- const elementX1 = element.x;
|
345
|
|
- const elementX2 = element.x + element.width;
|
346
|
|
- const elementY1 = element.y;
|
347
|
|
- const elementY2 = element.y + element.height;
|
348
|
|
-
|
349
|
|
- const margin = 4;
|
350
|
|
- const minimumSize = 40;
|
351
|
|
- const handlers: { [handler: string]: number[] } = {};
|
352
|
|
-
|
353
|
|
- const marginX = element.width < 0 ? 8 : -8;
|
354
|
|
- const marginY = element.height < 0 ? 8 : -8;
|
355
|
|
-
|
356
|
|
- if (Math.abs(elementX2 - elementX1) > minimumSize) {
|
357
|
|
- handlers["n"] = [
|
358
|
|
- elementX1 + (elementX2 - elementX1) / 2 + sceneState.scrollX - 4,
|
359
|
|
- elementY1 - margin + sceneState.scrollY + marginY,
|
360
|
|
- 8,
|
361
|
|
- 8
|
362
|
|
- ];
|
363
|
|
-
|
364
|
|
- handlers["s"] = [
|
365
|
|
- elementX1 + (elementX2 - elementX1) / 2 + sceneState.scrollX - 4,
|
366
|
|
- elementY2 - margin + sceneState.scrollY - marginY,
|
367
|
|
- 8,
|
368
|
|
- 8
|
369
|
|
- ];
|
370
|
|
- }
|
371
|
|
-
|
372
|
|
- if (Math.abs(elementY2 - elementY1) > minimumSize) {
|
373
|
|
- handlers["w"] = [
|
374
|
|
- elementX1 - margin + sceneState.scrollX + marginX,
|
375
|
|
- elementY1 + (elementY2 - elementY1) / 2 + sceneState.scrollY - 4,
|
376
|
|
- 8,
|
377
|
|
- 8
|
378
|
|
- ];
|
379
|
|
-
|
380
|
|
- handlers["e"] = [
|
381
|
|
- elementX2 - margin + sceneState.scrollX - marginX,
|
382
|
|
- elementY1 + (elementY2 - elementY1) / 2 + sceneState.scrollY - 4,
|
383
|
|
- 8,
|
384
|
|
- 8
|
385
|
|
- ];
|
386
|
|
- }
|
387
|
|
-
|
388
|
|
- handlers["nw"] = [
|
389
|
|
- elementX1 - margin + sceneState.scrollX + marginX,
|
390
|
|
- elementY1 - margin + sceneState.scrollY + marginY,
|
391
|
|
- 8,
|
392
|
|
- 8
|
393
|
|
- ]; // nw
|
394
|
|
- handlers["ne"] = [
|
395
|
|
- elementX2 - margin + sceneState.scrollX - marginX,
|
396
|
|
- elementY1 - margin + sceneState.scrollY + marginY,
|
397
|
|
- 8,
|
398
|
|
- 8
|
399
|
|
- ]; // ne
|
400
|
|
- handlers["sw"] = [
|
401
|
|
- elementX1 - margin + sceneState.scrollX + marginX,
|
402
|
|
- elementY2 - margin + sceneState.scrollY - marginY,
|
403
|
|
- 8,
|
404
|
|
- 8
|
405
|
|
- ]; // sw
|
406
|
|
- handlers["se"] = [
|
407
|
|
- elementX2 - margin + sceneState.scrollX - marginX,
|
408
|
|
- elementY2 - margin + sceneState.scrollY - marginY,
|
409
|
|
- 8,
|
410
|
|
- 8
|
411
|
|
- ]; // se
|
412
|
|
-
|
413
|
|
- if (element.type === "arrow") {
|
414
|
|
- return {
|
415
|
|
- nw: handlers.nw,
|
416
|
|
- se: handlers.se
|
417
|
|
- };
|
418
|
|
- }
|
419
|
|
-
|
420
|
|
- return handlers;
|
421
|
|
-}
|
422
|
|
-
|
423
|
168
|
function renderScene(
|
424
|
169
|
rc: RoughCanvas,
|
425
|
170
|
canvas: HTMLCanvasElement,
|
|
@@ -624,16 +369,6 @@ function saveFile(name: string, data: string) {
|
624
|
369
|
link.remove();
|
625
|
370
|
}
|
626
|
371
|
|
627
|
|
-function rotate(x1: number, y1: number, x2: number, y2: number, angle: number) {
|
628
|
|
- // 𝑎′𝑥=(𝑎𝑥−𝑐𝑥)cos𝜃−(𝑎𝑦−𝑐𝑦)sin𝜃+𝑐𝑥
|
629
|
|
- // 𝑎′𝑦=(𝑎𝑥−𝑐𝑥)sin𝜃+(𝑎𝑦−𝑐𝑦)cos𝜃+𝑐𝑦.
|
630
|
|
- // https://math.stackexchange.com/questions/2204520/how-do-i-rotate-a-line-segment-in-a-specific-point-on-the-line
|
631
|
|
- return [
|
632
|
|
- (x1 - x2) * Math.cos(angle) - (y1 - y2) * Math.sin(angle) + x2,
|
633
|
|
- (x1 - x2) * Math.sin(angle) + (y1 - y2) * Math.cos(angle) + y2
|
634
|
|
- ];
|
635
|
|
-}
|
636
|
|
-
|
637
|
372
|
function getDateTime() {
|
638
|
373
|
const date = new Date();
|
639
|
374
|
const year = date.getFullYear();
|
|
@@ -646,16 +381,6 @@ function getDateTime() {
|
646
|
381
|
return `${year}${month}${day}${hr}${min}${secs}`;
|
647
|
382
|
}
|
648
|
383
|
|
649
|
|
-// Casting second argument (DrawingSurface) to any,
|
650
|
|
-// because it is requred by TS definitions and not required at runtime
|
651
|
|
-const generator = rough.generator(null, null as any);
|
652
|
|
-
|
653
|
|
-function isTextElement(
|
654
|
|
- element: ExcalidrawElement
|
655
|
|
-): element is ExcalidrawTextElement {
|
656
|
|
- return element.type === "text";
|
657
|
|
-}
|
658
|
|
-
|
659
|
384
|
function isInputLike(
|
660
|
385
|
target: Element | EventTarget | null
|
661
|
386
|
): target is HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement {
|
|
@@ -666,190 +391,6 @@ function isInputLike(
|
666
|
391
|
);
|
667
|
392
|
}
|
668
|
393
|
|
669
|
|
-function getArrowPoints(element: ExcalidrawElement) {
|
670
|
|
- const x1 = 0;
|
671
|
|
- const y1 = 0;
|
672
|
|
- const x2 = element.width;
|
673
|
|
- const y2 = element.height;
|
674
|
|
-
|
675
|
|
- const size = 30; // pixels
|
676
|
|
- const distance = Math.hypot(x2 - x1, y2 - y1);
|
677
|
|
- // Scale down the arrow until we hit a certain size so that it doesn't look weird
|
678
|
|
- const minSize = Math.min(size, distance / 2);
|
679
|
|
- const xs = x2 - ((x2 - x1) / distance) * minSize;
|
680
|
|
- const ys = y2 - ((y2 - y1) / distance) * minSize;
|
681
|
|
-
|
682
|
|
- const angle = 20; // degrees
|
683
|
|
- const [x3, y3] = rotate(xs, ys, x2, y2, (-angle * Math.PI) / 180);
|
684
|
|
- const [x4, y4] = rotate(xs, ys, x2, y2, (angle * Math.PI) / 180);
|
685
|
|
-
|
686
|
|
- return [x1, y1, x2, y2, x3, y3, x4, y4];
|
687
|
|
-}
|
688
|
|
-
|
689
|
|
-function getDiamondPoints(element: ExcalidrawElement) {
|
690
|
|
- const topX = Math.floor(element.width / 2) + 1;
|
691
|
|
- const topY = 0;
|
692
|
|
- const rightX = element.width;
|
693
|
|
- const rightY = Math.floor(element.height / 2) + 1;
|
694
|
|
- const bottomX = topX;
|
695
|
|
- const bottomY = element.height;
|
696
|
|
- const leftX = topY;
|
697
|
|
- const leftY = rightY;
|
698
|
|
-
|
699
|
|
- return [topX, topY, rightX, rightY, bottomX, bottomY, leftX, leftY];
|
700
|
|
-}
|
701
|
|
-
|
702
|
|
-function generateDraw(element: ExcalidrawElement) {
|
703
|
|
- if (element.type === "selection") {
|
704
|
|
- element.draw = (rc, context, { scrollX, scrollY }) => {
|
705
|
|
- const fillStyle = context.fillStyle;
|
706
|
|
- context.fillStyle = "rgba(0, 0, 255, 0.10)";
|
707
|
|
- context.fillRect(
|
708
|
|
- element.x + scrollX,
|
709
|
|
- element.y + scrollY,
|
710
|
|
- element.width,
|
711
|
|
- element.height
|
712
|
|
- );
|
713
|
|
- context.fillStyle = fillStyle;
|
714
|
|
- };
|
715
|
|
- } else if (element.type === "rectangle") {
|
716
|
|
- const shape = withCustomMathRandom(element.seed, () => {
|
717
|
|
- return generator.rectangle(0, 0, element.width, element.height, {
|
718
|
|
- stroke: element.strokeColor,
|
719
|
|
- fill: element.backgroundColor,
|
720
|
|
- fillStyle: element.fillStyle,
|
721
|
|
- strokeWidth: element.strokeWidth,
|
722
|
|
- roughness: element.roughness
|
723
|
|
- });
|
724
|
|
- });
|
725
|
|
- element.draw = (rc, context, { scrollX, scrollY }) => {
|
726
|
|
- context.globalAlpha = element.opacity / 100;
|
727
|
|
- context.translate(element.x + scrollX, element.y + scrollY);
|
728
|
|
- rc.draw(shape);
|
729
|
|
- context.translate(-element.x - scrollX, -element.y - scrollY);
|
730
|
|
- context.globalAlpha = 1;
|
731
|
|
- };
|
732
|
|
- } else if (element.type === "diamond") {
|
733
|
|
- const shape = withCustomMathRandom(element.seed, () => {
|
734
|
|
- const [
|
735
|
|
- topX,
|
736
|
|
- topY,
|
737
|
|
- rightX,
|
738
|
|
- rightY,
|
739
|
|
- bottomX,
|
740
|
|
- bottomY,
|
741
|
|
- leftX,
|
742
|
|
- leftY
|
743
|
|
- ] = getDiamondPoints(element);
|
744
|
|
- return generator.polygon(
|
745
|
|
- [
|
746
|
|
- [topX, topY],
|
747
|
|
- [rightX, rightY],
|
748
|
|
- [bottomX, bottomY],
|
749
|
|
- [leftX, leftY]
|
750
|
|
- ],
|
751
|
|
- {
|
752
|
|
- stroke: element.strokeColor,
|
753
|
|
- fill: element.backgroundColor,
|
754
|
|
- fillStyle: element.fillStyle,
|
755
|
|
- strokeWidth: element.strokeWidth,
|
756
|
|
- roughness: element.roughness
|
757
|
|
- }
|
758
|
|
- );
|
759
|
|
- });
|
760
|
|
- element.draw = (rc, context, { scrollX, scrollY }) => {
|
761
|
|
- context.globalAlpha = element.opacity / 100;
|
762
|
|
- context.translate(element.x + scrollX, element.y + scrollY);
|
763
|
|
- rc.draw(shape);
|
764
|
|
- context.translate(-element.x - scrollX, -element.y - scrollY);
|
765
|
|
- context.globalAlpha = 1;
|
766
|
|
- };
|
767
|
|
- } else if (element.type === "ellipse") {
|
768
|
|
- const shape = withCustomMathRandom(element.seed, () =>
|
769
|
|
- generator.ellipse(
|
770
|
|
- element.width / 2,
|
771
|
|
- element.height / 2,
|
772
|
|
- element.width,
|
773
|
|
- element.height,
|
774
|
|
- {
|
775
|
|
- stroke: element.strokeColor,
|
776
|
|
- fill: element.backgroundColor,
|
777
|
|
- fillStyle: element.fillStyle,
|
778
|
|
- strokeWidth: element.strokeWidth,
|
779
|
|
- roughness: element.roughness
|
780
|
|
- }
|
781
|
|
- )
|
782
|
|
- );
|
783
|
|
- element.draw = (rc, context, { scrollX, scrollY }) => {
|
784
|
|
- context.globalAlpha = element.opacity / 100;
|
785
|
|
- context.translate(element.x + scrollX, element.y + scrollY);
|
786
|
|
- rc.draw(shape);
|
787
|
|
- context.translate(-element.x - scrollX, -element.y - scrollY);
|
788
|
|
- context.globalAlpha = 1;
|
789
|
|
- };
|
790
|
|
- } else if (element.type === "arrow") {
|
791
|
|
- const [x1, y1, x2, y2, x3, y3, x4, y4] = getArrowPoints(element);
|
792
|
|
- const options = {
|
793
|
|
- stroke: element.strokeColor,
|
794
|
|
- strokeWidth: element.strokeWidth,
|
795
|
|
- roughness: element.roughness
|
796
|
|
- };
|
797
|
|
-
|
798
|
|
- const shapes = withCustomMathRandom(element.seed, () => [
|
799
|
|
- // \
|
800
|
|
- generator.line(x3, y3, x2, y2, options),
|
801
|
|
- // -----
|
802
|
|
- generator.line(x1, y1, x2, y2, options),
|
803
|
|
- // /
|
804
|
|
- generator.line(x4, y4, x2, y2, options)
|
805
|
|
- ]);
|
806
|
|
-
|
807
|
|
- element.draw = (rc, context, { scrollX, scrollY }) => {
|
808
|
|
- context.globalAlpha = element.opacity / 100;
|
809
|
|
- context.translate(element.x + scrollX, element.y + scrollY);
|
810
|
|
- shapes.forEach(shape => rc.draw(shape));
|
811
|
|
- context.translate(-element.x - scrollX, -element.y - scrollY);
|
812
|
|
- context.globalAlpha = 1;
|
813
|
|
- };
|
814
|
|
- return;
|
815
|
|
- } else if (isTextElement(element)) {
|
816
|
|
- element.draw = (rc, context, { scrollX, scrollY }) => {
|
817
|
|
- context.globalAlpha = element.opacity / 100;
|
818
|
|
- const font = context.font;
|
819
|
|
- context.font = element.font;
|
820
|
|
- const fillStyle = context.fillStyle;
|
821
|
|
- context.fillStyle = element.strokeColor;
|
822
|
|
- context.fillText(
|
823
|
|
- element.text,
|
824
|
|
- element.x + scrollX,
|
825
|
|
- element.y + element.actualBoundingBoxAscent + scrollY
|
826
|
|
- );
|
827
|
|
- context.fillStyle = fillStyle;
|
828
|
|
- context.font = font;
|
829
|
|
- context.globalAlpha = 1;
|
830
|
|
- };
|
831
|
|
- } else {
|
832
|
|
- throw new Error("Unimplemented type " + element.type);
|
833
|
|
- }
|
834
|
|
-}
|
835
|
|
-
|
836
|
|
-// If the element is created from right to left, the width is going to be negative
|
837
|
|
-// This set of functions retrieves the absolute position of the 4 points.
|
838
|
|
-// We can't just always normalize it since we need to remember the fact that an arrow
|
839
|
|
-// is pointing left or right.
|
840
|
|
-function getElementAbsoluteX1(element: ExcalidrawElement) {
|
841
|
|
- return element.width >= 0 ? element.x : element.x + element.width;
|
842
|
|
-}
|
843
|
|
-function getElementAbsoluteX2(element: ExcalidrawElement) {
|
844
|
|
- return element.width >= 0 ? element.x + element.width : element.x;
|
845
|
|
-}
|
846
|
|
-function getElementAbsoluteY1(element: ExcalidrawElement) {
|
847
|
|
- return element.height >= 0 ? element.y : element.y + element.height;
|
848
|
|
-}
|
849
|
|
-function getElementAbsoluteY2(element: ExcalidrawElement) {
|
850
|
|
- return element.height >= 0 ? element.y + element.height : element.y;
|
851
|
|
-}
|
852
|
|
-
|
853
|
394
|
function setSelection(selection: ExcalidrawElement) {
|
854
|
395
|
const selectionX1 = getElementAbsoluteX1(selection);
|
855
|
396
|
const selectionX2 = getElementAbsoluteX2(selection);
|