forked from DhanushNehru/board
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
312 lines (252 loc) · 8.22 KB
/
script.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
const buttonDownload = document.getElementById("download");
buttonDownload.addEventListener("click", downloadOptions);
function download(name, format) {
// get width and height and background color - original draw
const canvas = document.getElementById("black-board");
const width = canvas.width;
const height = canvas.height;
let boardBackground;
const background = canvas.style.backgroundColor;
boardBackground = background;
if (!background) {
boardBackground = "rgba(255,255,255)";
}
// generate a new canvas with background
const backgroundCanvasGenerate = document.createElement("canvas");
backgroundCanvasGenerate.width = width;
backgroundCanvasGenerate.height = height;
const ctx2 = backgroundCanvasGenerate.getContext("2d");
ctx2.fillStyle = boardBackground;
ctx2.fillRect(0, 0, width, height);
// merge background with draw
const canvas3 = document.createElement("canvas");
canvas3.width = width;
canvas3.height = height;
const ctx3 = canvas3.getContext("2d");
ctx3.drawImage(backgroundCanvasGenerate, 0, 0);
ctx3.drawImage(canvas, 0, 0);
const newCanvas = canvas3.toDataURL();
fetch(newCanvas)
.then((resp) => resp.blob())
.then((blobobject) => {
const blob = window.URL.createObjectURL(blobobject);
const anchor = document.createElement("a");
anchor.style.display = "none";
anchor.href = blob;
anchor.download = `${name}.${format}`;
document.body.appendChild(anchor);
anchor.click();
window.URL.revokeObjectURL(blob);
document.getElementById("modal").remove();
});
}
function downloadOptions() {
const modal = document.createElement("div");
modal.classList.add("modal");
modal.setAttribute("id", "modal");
const content = document.createElement("div");
content.classList.add("modal-content");
modal.appendChild(content);
const labelInput = document.createElement("div");
labelInput.classList.add("label-input");
const label = document.createElement("label");
label.appendChild(document.createTextNode("File Name"));
labelInput.appendChild(label);
const inputName = document.createElement("input");
inputName.addEventListener("keydown", () => {
const error = inputName.classList.contains("error");
if (error) {
inputName.classList.remove("error");
}
});
inputName.classList.add("input-name");
labelInput.appendChild(inputName);
content.appendChild(labelInput);
const formats = ["png", "jpeg"];
const listFormats = document.createElement("div");
listFormats.classList.add("download-list-formats");
content.appendChild(listFormats);
formats.map((format) => {
const label = document.createElement("label");
label.classList.add("download-format-control");
const input = document.createElement("input");
if (format == "png") {
input.checked = true;
}
input.setAttribute("type", "radio");
input.setAttribute("name", "radio");
input.setAttribute("value", format);
label.appendChild(document.createTextNode(format));
label.appendChild(input);
listFormats.appendChild(label);
});
const downloadCancel = document.createElement("div");
downloadCancel.classList.add("download-cancel");
const btnDownload = document.createElement("button");
btnDownload.addEventListener("click", () => {
let nameFile = inputName.value;
if (!nameFile) {
inputName.classList.add("error");
return;
}
nameFile = nameFile.replace(/\s+/g, "-").toLowerCase();
const format = document.querySelector('input[name="radio"]:checked').value;
download(nameFile, format);
});
btnDownload.classList.add("btn-download");
btnDownload.appendChild(document.createTextNode("Download"));
downloadCancel.appendChild(btnDownload);
const btnCancelClose = document.createElement("button");
btnCancelClose.classList.add("btn-cancel");
btnCancelClose.addEventListener("click", () => {
modal.remove();
});
btnCancelClose.appendChild(document.createTextNode("Cancel"));
downloadCancel.appendChild(btnCancelClose);
content.appendChild(downloadCancel);
document.body.appendChild(modal);
}
function blackBoard() {
const canvas = document.getElementById("black-board");
const ctx = canvas.getContext("2d");
const strokeInp = document.getElementById("stroke");
//resizing
canvas.width = canvas.getBoundingClientRect().width;
canvas.height = canvas.getBoundingClientRect().height;
ctx.lineJoin = "round";
ctx.lineCap = "round";
ctx.lineWidth = 2;
ctx.strokeStyle = "#000000";
//variables
let painting = false;
let lastX = 0;
let lastY = 0;
//functions
function startPosition(e) {
painting = true;
draw(e);
}
function endPosition() {
painting = false;
ctx.beginPath();
}
function draw(e) {
if (!painting) {
return;
}
e.preventDefault();
ctx.beginPath();
ctx.moveTo(lastX, lastY);
if (e.type == "touchmove") {
console.log(e.touches[0]);
ctx.lineTo(e.touches[0].clientX, e.touches[0].clientY);
} else {
ctx.lineTo(e.offsetX, e.offsetY);
}
if (e.type == "touchmove") {
lastX = e.touches[0].clientX;
lastY = e.touches[0].clientY;
} else {
lastX = e.offsetX;
lastY = e.offsetY;
}
ctx.stroke();
saveCanvas();
}
function saveCanvas() {
localStorage.setItem("myCanvas", canvas.toDataURL());
}
//event listeners
canvas.addEventListener("mousedown", (event) => {
ctx.lineWidth = strokeInp.value
painting = true;
lastX = event.offsetX;
lastY = event.offsetY;
});
canvas.addEventListener("touchstart", (event) => {
ctx.lineWidth = strokeInp.value
painting = true;
lastX = event.touches[0].clientX;
lastY = event.touches[0].clientY;
});
canvas.addEventListener("mouseup", endPosition);
canvas.addEventListener("touchend", endPosition);
canvas.addEventListener("mousemove", draw);
canvas.addEventListener("touchmove", draw);
}
function circle() {
const canvas = document.getElementById("black-board");
const penColor = document.getElementById("pen-color");
const strokeInp = document.getElementById("stroke");
const ctx = canvas.getContext("2d", {
willReadFrequently: true,
});
let snapshot;
ctx.lineJoin = "round";
ctx.lineCap = "round";
ctx.lineWidth = 2;
ctx.strokeStyle = "#FFFFFF";
let painting = false;
let lastX = 0;
let lastY = 0;
function endPosition() {
painting = false;
ctx.beginPath();
canvas.removeEventListener("mousemove", draw);
canvas.removeEventListener("touchmove", draw);
}
function get_radius(x1, y1, x2, y2) {
return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
}
function draw(e) {
if (!painting) {
return;
}
e.preventDefault();
ctx.beginPath();
ctx.putImageData(snapshot, 0, 0)
ctx.arc(lastX, lastY, get_radius(lastX, lastY, e.offsetX, e.offsetY), 0, Math.PI * 2);
ctx.stroke();
}
//event listeners
canvas.addEventListener("mousedown", (event) => {
ctx.strokeStyle = penColor.value
ctx.lineWidth = strokeInp.value
painting = true;
snapshot = ctx.getImageData(0, 0, canvas.width, canvas.height);
lastX = event.offsetX;
lastY = event.offsetY;
});
canvas.addEventListener("touchstart", (event) => {
ctx.strokeStyle = penColor.value
ctx.lineWidth = strokeInp.value
painting = true;
snapshot = ctx.getImageData(0, 0, canvas.width, canvas.height);
lastX = event.touches[0].clientX;
lastY = event.touches[0].clientY;
});
canvas.addEventListener("mouseup", endPosition);
canvas.addEventListener("touchend", endPosition);
canvas.addEventListener("mousemove", draw);
canvas.addEventListener("touchmove", draw);
}
function onClear() {
const canvas = document.getElementById("black-board");
const context = canvas.getContext("2d");
context.clearRect(0, 0, canvas.width, canvas.height);
localStorage.setItem("myCanvas", null);
}
function loadCanvas() {
if (localStorage.getItem("myCanvas") !== "null") {
const canvas = document.getElementById("black-board");
const ctx = canvas.getContext("2d");
const dataURL = localStorage.getItem("myCanvas");
let img = new Image();
img.src = dataURL;
img.onload = function () {
ctx.drawImage(img, 0, 0);
};
}
}
loadCanvas();
blackBoard();