-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
349 lines (246 loc) · 8.09 KB
/
index.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
window.onload = function () {
var mobile = (/iphone|ipad|ipod|android|blackberry|mini|windows\sce|palm/i.test(navigator.userAgent.toLowerCase()));
if (mobile) {
alert("Visit this on a Computer for Better View and Using Features");
}
}
// importing required components
const toggleBtn = document.querySelector(".nav-toggle");
const links = document.querySelector(".links");
const sideBarfetch = document.querySelector(".sideBar-fetch")
const toolsBar = document.querySelector("aside")
const tools = document.querySelectorAll(".tools")
const cancel = document.querySelector(".cancel")
const canvasSection = document.querySelector("#canvas")
const bgColorDiv = document.querySelectorAll(".container-4 div")
const colorInput = document.querySelector(".bg")
const penColorInput = document.querySelector(".penColors");
toggleBtn.addEventListener("click", () => {
links.classList.toggle('show-links');
})
sideBarfetch.addEventListener("click", () => {
console.log("clicked")
sideBarfetch.classList.add("hide")
toolsBar.classList.add("show")
})
// Slide ToolBar
cancel.addEventListener("click", slideInTools)
function slideInTools() {
sideBarfetch.classList.remove("hide")
tools.forEach((newTool) => {
if (newTool.classList.contains("acitiveOption")) {
newTool.classList.add("active")
}
else {
newTool.classList.remove("activeOption")
}
})
toolsBar.classList.remove("show")
}
tools.forEach((tool) => {
tool.addEventListener("click", () => {
tools.forEach((newTool) => {
newTool.classList.remove("activeOption")
newTool.classList.remove("active")
})
if (!tool.classList.contains("activeOption")) {
tool.classList.add("activeOption")
tool.classList.add("active")
}
})
})
// Canvas Code
const canvas = document.getElementById('canvas');
canvas.style.width = '100%';
canvas.style.height = '100%';
canvas.width = canvas.offsetWidth;
canvas.height = canvas.offsetHeight;
const ctx = canvas.getContext('2d');
// variable
let drawing = false; // Drawing or not
let history = [];
let index = -1;
var pencolor = "black" // Pen Color
var mode = "pen" //Mode pen or eraser
var penwidth; // Pem Width
var eraserWidth; //Eraser Width
var penColor = "black"; //Pen Color Default is Black
var eraserColor; // Easer Color
var canvasColor = "white";
var lineStart = undefined; // starting point for straight line tool
var pos2 = { x: undefined, y: undefined }; // starting point for shapes
// Changing Canvas Backgroung Color;
bgColorDiv.forEach((color) => {
color.addEventListener("click", () => {
canvasColor = color.getAttribute("class")
console.log(canvasColor)
canvasSection.style.backgroundColor = canvasColor;
})
})
colorInput.addEventListener("blur", () => {
console.log("clicked")
console.log(colorInput.value)
canvasColor = colorInput.value;
canvasSection.style.backgroundColor = canvasColor;
})
// Pencolor Change
penColorInput.addEventListener("blur", () => {
console.log("clicked")
console.log(penColorInput.value);
penColor = penColorInput.value;
})
// Changing Width
function getWidth() {
penwidth = document.getElementById("penwidth").value;
eraserWidth = document.getElementById("eraserWidth").value;
}
//Changing Mode
function changeMode(mod) {
mode = mod;
}
// Mouse Down
function startPaint(e) {
slideInTools();
drawing = true;
draw(e);
pos2 = getMousePos(canvas, e);
}
// MOuse UP
function endPaint() {
drawing = false;
ctx.beginPath();
lineStart = undefined;
pos2 = undefined;
history.push(ctx.getImageData(0, 0, canvas.width, canvas.height));
index++;
}
// Gives Position Of Mouse
function getMousePos(canvas, e) {
var rect = canvas.getBoundingClientRect();
return {
x: (e.clientX - rect.left) / (rect.right - rect.left) * canvas.width,
y: (e.clientY - rect.top) / (rect.bottom - rect.top) * canvas.height
};
}
// Move
function draw(e) {
if (!drawing) return;
var pos = getMousePos(canvas, e);
ctx.lineWidth = penwidth; //Line Width
ctx.lineCap = "round"; //Line side cap
getWidth(); //Update Width
switch (mode) {
case "pen":
ctx.strokeStyle = penColor; // Earser Color Same as BAckgroung
ctx.lineWidth = penwidth;
ctx.globalCompositeOperation = "source-over";
ctx.lineTo(pos.x, pos.y);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(pos.x, pos.y);
break;
case "eraser": //Earse
console.log("Using eraser");
ctx.lineWidth = eraserWidth;
ctx.strokeStyle = canvasColor; // Earser Color Same as BAckgroung
ctx.globalCompositeOperation = "destination-out";
ctx.lineTo(pos.x, pos.y);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(pos.x, pos.y);
break;
case "line":
if (lineStart == undefined) lineStart = pos;
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.putImageData(history[index], 0, 0);
ctx.strokeStyle = penColor; // Earser Color Same as BAckgroung
ctx.lineWidth = penwidth;
ctx.globalCompositeOperation = "source-over";
ctx.lineTo(lineStart.x, lineStart.y);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(pos.x, pos.y);
break;
case "rectangle":
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.putImageData(history[index], 0, 0);
if (pos2 == undefined) pos2 = pos;
ctx.strokeRect(pos2.x, pos2.y, pos.x - pos2.x, pos.y - pos2.y);
ctx.stroke();
ctx.beginPath();
break;
case "ellipse":
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.putImageData(history[index], 0, 0);
if (pos2 == undefined) pos2 = pos;
ctx.ellipse(pos2.x, pos2.y , Math.abs( pos.x - pos2.x), Math.abs(pos.y - pos2.y), 0, 0, Math.PI * 2);
ctx.stroke();
ctx.beginPath();
break;
}
}
//Download Board
var button = document.getElementById("save")
button.addEventListener('click', function (e) {
console.log("Download");
//Eddge (PG Only)
if (window.navigator.msSaveBlob) {
window.navigator.msSaveBlob(canvas.msToBlob(), "whiteBoard.png");
} else { //Chrome
const a = document.createElement("a");
document.body.appendChild(a);
a.href = canvas.toDataURL();
a.download = "whiteBoard.png"
a.click();
document.body.removeChild(a);
}
});
//Clear Page
const clearBoard = document.getElementById('clearAll')
clearBoard.addEventListener("click", clearCanvas);
function clearCanvas() {
var txt;
var r = confirm("Do you really want to clear the board?");
if (r == true) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
history = [];
index = -1;
}
}
// Reload,Tab Close or Browser Alert
window.addEventListener('beforeunload', function (e) {
e.preventDefault();
e.returnValue = '';
});
//Events
canvas.addEventListener("mousedown", startPaint);
canvas.addEventListener("mouseup", endPaint);
canvas.addEventListener("mousemove", draw);
// Undo
const undo = document.getElementById("undo");
undo.addEventListener("click", undoLastPoint);
function reset() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
index = -1;
history=[];
}
function undoLastPoint() {
console.log(index);
if (index <= 0) {
reset();
} else {
index--;
history.pop(); //
ctx.putImageData(history[index], 0, 0);
}
}
// // Redo
// const redo = document.getElementById("redo");
// redo.addEventListener("click", redoLastPoint);
// function redoLastPoint() {
// console.log(index);
// if (index < history.length - 1) {
// index++;
// ctx.putImageData(history[index], 0, 0);
// }
// }