|
1 | 1 | import {Component, Element, h, Method, Prop, State, Watch} from '@stencil/core'; |
2 | 2 |
|
3 | 3 | import {unifyEvent} from '@deckdeckgo/utils'; |
| 4 | + |
| 5 | +import {Arrow, Circle, Drawable, Pencil} from '@deckdeckgo/remote-utils'; |
| 6 | + |
4 | 7 | // Types |
5 | 8 | import {DeckdeckgoDrawAction, DeckdeckgoEventEmitter, DeckdeckgoEventType} from '@deckdeckgo/types'; |
| 9 | + |
6 | 10 | // Services |
7 | 11 | import {CommunicationService} from '../../services/communication/communication.service'; |
8 | 12 |
|
9 | | -interface Point { |
10 | | - x: number; |
11 | | - y: number; |
12 | | -} |
13 | | - |
14 | | -interface Drawable { |
15 | | - draw(ctx: CanvasRenderingContext2D); |
16 | | -} |
17 | | - |
18 | | -class Circle implements Drawable { |
19 | | - private readonly from: Point; |
20 | | - private readonly to: Point; |
21 | | - private readonly color: string; |
22 | | - |
23 | | - constructor(from: Point, to: Point, color: string) { |
24 | | - this.from = from; |
25 | | - this.to = to; |
26 | | - this.color = color; |
27 | | - } |
28 | | - |
29 | | - draw(ctx: CanvasRenderingContext2D) { |
30 | | - ctx.beginPath(); |
31 | | - |
32 | | - ctx.moveTo(this.from.x, this.from.y + (this.to.y - this.from.y) / 2); |
33 | | - ctx.bezierCurveTo(this.from.x, this.from.y, this.to.x, this.from.y, this.to.x, this.from.y + (this.to.y - this.from.y) / 2); |
34 | | - ctx.bezierCurveTo(this.to.x, this.to.y, this.from.x, this.to.y, this.from.x, this.from.y + (this.to.y - this.from.y) / 2); |
35 | | - |
36 | | - ctx.strokeStyle = this.color; |
37 | | - ctx.lineWidth = 3; |
38 | | - |
39 | | - ctx.stroke(); |
40 | | - ctx.closePath(); |
41 | | - } |
42 | | -} |
43 | | - |
44 | | -class Pencil implements Drawable { |
45 | | - private readonly from: Point; |
46 | | - private readonly to: Point; |
47 | | - private readonly color: string; |
48 | | - |
49 | | - constructor(from: Point, to: Point, color: string) { |
50 | | - this.from = from; |
51 | | - this.to = to; |
52 | | - this.color = color; |
53 | | - } |
54 | | - |
55 | | - draw(ctx: CanvasRenderingContext2D) { |
56 | | - ctx.beginPath(); |
57 | | - |
58 | | - ctx.moveTo(this.from.x, this.from.y); |
59 | | - ctx.lineTo(this.to.x, this.to.y); |
60 | | - ctx.strokeStyle = this.color; |
61 | | - ctx.lineWidth = 3; |
62 | | - |
63 | | - ctx.stroke(); |
64 | | - ctx.closePath(); |
65 | | - } |
66 | | -} |
67 | | - |
68 | | -class Arrow implements Drawable { |
69 | | - private readonly from: Point; |
70 | | - private readonly to: Point; |
71 | | - private readonly color: string; |
72 | | - |
73 | | - constructor(from: Point, to: Point, color: string) { |
74 | | - this.from = from; |
75 | | - this.to = to; |
76 | | - this.color = color; |
77 | | - } |
78 | | - |
79 | | - draw(ctx: CanvasRenderingContext2D) { |
80 | | - ctx.beginPath(); |
81 | | - |
82 | | - ctx.moveTo(this.from.x, this.from.y); |
83 | | - ctx.lineTo(this.to.x, this.to.y); |
84 | | - ctx.strokeStyle = this.color; |
85 | | - ctx.lineWidth = 3; |
86 | | - |
87 | | - ctx.stroke(); |
88 | | - ctx.closePath(); |
89 | | - |
90 | | - this.drawTriangle(ctx); |
91 | | - } |
92 | | - |
93 | | - private drawTriangle(ctx: CanvasRenderingContext2D) { |
94 | | - ctx.beginPath(); |
95 | | - |
96 | | - const length: number = Math.sqrt(Math.pow(this.to.y - this.from.y, 2) + Math.pow(this.to.x - this.from.x, 2)); |
97 | | - const size: number = Math.min(length, 20); |
98 | | - |
99 | | - // https://stackoverflow.com/a/36805543/5404186 |
100 | | - |
101 | | - let angle: number = Math.atan2(this.to.y - this.from.y, this.to.x - this.from.x); |
102 | | - let x: number = size * Math.cos(angle) + this.to.x; |
103 | | - let y: number = size * Math.sin(angle) + this.to.y; |
104 | | - |
105 | | - ctx.moveTo(x, y); |
106 | | - |
107 | | - angle += (1 / 3) * (2 * Math.PI); |
108 | | - x = size * Math.cos(angle) + this.to.x; |
109 | | - y = size * Math.sin(angle) + this.to.y; |
110 | | - |
111 | | - ctx.lineTo(x, y); |
112 | | - |
113 | | - angle += (1 / 3) * (2 * Math.PI); |
114 | | - x = size * Math.cos(angle) + this.to.x; |
115 | | - y = size * Math.sin(angle) + this.to.y; |
116 | | - |
117 | | - ctx.lineTo(x, y); |
118 | | - |
119 | | - ctx.fillStyle = this.color; |
120 | | - ctx.fill(); |
121 | | - |
122 | | - ctx.closePath(); |
123 | | - } |
124 | | -} |
125 | | - |
126 | 13 | @Component({ |
127 | 14 | tag: 'app-draw', |
128 | 15 | styleUrl: 'app-draw.scss' |
|
0 commit comments