-
Notifications
You must be signed in to change notification settings - Fork 0
/
drawing.ts
149 lines (140 loc) · 3.41 KB
/
drawing.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
* Class for a drawing in JavaScript.
*/
class Drawing
{
public title: string;
protected width: number;
protected height: number;
protected margin: number;
protected parent: HTMLElement;
protected canvas: HTMLCanvasElement;
protected ctx: CanvasRenderingContext2D;
protected maxAnimationStep: number;
/**
* @param {Element} parent DOM elemnt to append this drawing to
* @param {number} width width of the canvas
* @param {number} height height of the canvas
* @param {number} margin margin around image content
*/
constructor(
parent: HTMLElement = document.getElementsByTagName("body")[0],
width: number = 500,
height: number = 500,
margin: number = 10,
title: string = "Unnamed Drawing"
)
{
this.width = width;
this.height = height;
this.margin = margin;
this.parent = parent;
this.title = title;
}
/**
* Sets the size of this drawing.
*
* @param {number} width width
* @param {number} height height
* @returns {Drawing} this
* @memberof Drawing
*/
public setSize(width: number, height: number): Drawing
{
this.width = width;
this.height = height;
return this;
}
/**
* Sets the width of this drawing.
*
* @param {number} width width
* @returns {Drawing} this
* @memberof Drawing
*/
public setWidth(width: number): Drawing
{
this.width = width;
return this;
}
/**
* Sets the height of this drawing.
*
* @param {number} height height
* @returns {Drawing} this
* @memberof Drawing
*/
public setHeight(height: number): Drawing
{
this.height = height;
return this;
}
/**
* Creates the drawing container, title and canvas and draws the image.
* @return {Drawing} this
*/
public init(draw: boolean = true): Drawing
{
this.clear();
this.createCanvas();
if (draw)
{
this.draw();
}
return this;
}
/**
* Clears the parent element.
* @return {Drawing} this
*/
public clear(): Drawing
{
if (this.canvas)
{
this.parent.removeChild(this.canvas);
}
return this;
}
/**
* Draws the image.
* @return {Drawing} this
*/
public draw(): Drawing
{
let step = 0;
const interval = setInterval(() =>
{
this.ctx.clearRect(0, 0, this.width, this.height);
this.drawStep(step++);
if (step > this.maxAnimationStep)
{
clearInterval(interval);
}
}, 500);
return this;
}
/**
* Draws one animation step of the image.
* @param step current step
*/
public drawStep(step: number): void
{
console.warn("draw(): Not implemented yet!");
}
/**
* Creates the canvas.
* @return {Drawing} this
*/
private createCanvas(): Drawing
{
if (!this.canvas)
{
this.canvas = document.createElement("canvas");
this.canvas.width = this.width;
this.canvas.height = this.height;
this.parent.appendChild(this.canvas);
}
this.ctx = this.canvas.getContext("2d") as CanvasRenderingContext2D;
return this;
}
}