-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmini-paint.js
161 lines (146 loc) · 4.42 KB
/
mini-paint.js
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
150
151
152
153
154
155
156
157
158
159
160
161
let canvas = null;
let ctx = null;
let cBounds = null;
let canvasPreview = null;
let ctxPreview = null;
let cBoundsPreview = null;
let tools =["brush", "pen", "eraser", "square","squareFill"]
// Main Functions ********************************************
function _start() {
//Canvas
canvas = document.querySelector("#paintCanvas");
ctx = canvas.getContext("2d");
cBounds = canvas.getBoundingClientRect();
canvasPreview = document.querySelector("#previewCanvas");
ctxPreview = canvasPreview.getContext("2d");
cBoundsPreview = canvasPreview.getBoundingClientRect();
// Make the objects
let program = new Program();
let tool = new Tool()
//Events
drawEvents(tool);
toolsEvent(tool)
programEvents(program)
// Update and Draw
_update(program,tool);
_draw(program,tool);
}
function _update(program,tool) {
requestAnimationFrame(() => _update(program,tool));
cBounds = canvas.getBoundingClientRect();
program.update(tool);
}
function _draw(program,tool) {
requestAnimationFrame(() => _draw(program,tool));
if(program.clearCanvas){
ctx.clearRect(0, 0, canvas.width, canvas.height);
program.clearCanvas=false;
}
tool.draw(program);
}
// Objects ********************************************
class Program {
constructor(){
this.toolChanged = false;
this.clearCanvas = false;
this.selectedTool = "brush"
}
update(tool){
if(this.toolChanged){
switch (this.selectedTool) {
case "brush":
tool.eraser=false;
tool.roundCap = true;
break;
case "pen":
tool.eraser=false;
tool.roundCap = false;
break;
case "eraser":
tool.eraser=true;
tool.roundCap = true;
break;
case "square":
tool.fill = false;
break;
case "squareFill":
tool.fill = true;
break;
default:
break;
}
this.toolChanged = false;
}
//console.log(this.selectedTool)
}
selectTool(newTool){
tools.forEach(tool => {
if(newTool == tool){
this.selectedTool=newTool;
this.toolChanged = true;
return;
}
});
}
downloadCanvas(imageName) {
let img = canvas.toDataURL('image/jpg');
let a = document.createElement('a');
a.href = img;
a.download = imageName || 'draw';
a.click();
}
}
class Tool{
constructor(){
this.x = -99; //Out of canvas
this.y = -99; //Out of canvas
this.startDraw=false;
this.isDrawing = false;
this.endDrawing = false;
this.color = "rgba(0,0,0,1)";
this.stroke = 5;
//Brushes
this.roundCap = true;
this.eraser = false;
//Tools
this.fill=false;
this.startX = null;
this.startY = null;
}
draw(program){
if(this.isDrawing){
if(program.selectedTool == "brush" || program.selectedTool == "pen" || program.selectedTool == "eraser")this.brush();
if(program.selectedTool == "square" || program.selectedTool == "squareFill") this.square();
}
}
brush(){
if(this.startDraw){
ctx.lineWidth = this.stroke;
ctx.strokeStyle = !this.eraser ? this.color : 'rgba(255, 255, 255, 1)'; // Green path
ctx.lineCap = this.roundCap ? "round" : "square"
ctx.beginPath();
ctx.moveTo(this.x, this.y);
this.startDraw = false;
}
ctx.lineTo(this.x, this.y);
ctx.stroke(); // Draw it
if(this.endDrawing) this.isDrawing = false;
}
square(){
if(this.startDraw){
this.startX =this.x;
this.startY= this.y;
this.startDraw = false;
}
if(this.endDrawing){
ctx.beginPath();
ctx.lineWidth = this.stroke;
ctx.strokeStyle = this.color;
ctx.fillStyle = this.color;
ctx.rect(this.startX, this.startY, this.x-this.startX, this.y-this.startY);
ctx.stroke();
if(this.fill) ctx.fill()
this.isDrawing = false;
}
}
}