Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
101 lines (83 loc) · 2.9 KB
/
Copy pathscript.js
File metadata and controls
101 lines (83 loc) · 2.9 KB
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
var canvas = document.querySelector('#draw-board'),
context = canvas.getContext('2d'),
color = document.querySelector('#color'),
width = document.querySelector('#width'),
eraser = document.querySelector('#eraser'),
pen = document.querySelector('#pen'),
downloadBtn = document.querySelector('#download'),
clear = document.querySelector('#clear')
widthText = document.querySelector('#width-text');
canvas.width = window.innerWidth * 0.9;
canvas.height = window.innerHeight * 0.8;
context.strokeStyle = '#fa6167';
context.lineJoin = 'round';
context.lineCap = 'round';
context.lineWidth = 1;
context.fillStyle = "#202124";
context.fillRect(0, 0, canvas.width, canvas.height);
let isDrawing = false;
var coordinateX = 0;
var coordinateY = 0;
var selected = 'pen'
canvas.addEventListener('mousemove', draw);
canvas.addEventListener('mousedown', changeCoordinate);
canvas.addEventListener('mouseup', () => isDrawing = false);
canvas.addEventListener('mouseout', () => isDrawing = false);
color.addEventListener('change', () => {
selected == 'eraser' ? context.strokeStyle = `#202124` : context.strokeStyle = `${color.value}`;
});
width.addEventListener('change', () => context.lineWidth = `${width.value}`);
eraser.addEventListener('click', selectedEraser);
pen.addEventListener('click', selectedPen);
downloadBtn.addEventListener('click', download);
clear.addEventListener('click', clearCanvas);
function draw(e) {
if (!isDrawing) return;
context.beginPath();
context.moveTo(coordinateX, coordinateY);
context.lineTo(e.offsetX, e.offsetY);
context.stroke();
[coordinateX, coordinateY] = [e.offsetX, e.offsetY];
}
function changeCoordinate(e) {
if (!isDrawing) [coordinateX, coordinateY] = [e.offsetX, e.offsetY];
isDrawing = true;
}
function selectedEraser(e) {
e.preventDefault();
if (selected == 'eraser') return
this.classList.add('active');
pen.classList.remove('active');
selected = 'eraser';
context.strokeStyle = '#202124';
color.setAttribute('disabled', '');
}
function selectedPen(e) {
e.preventDefault();
if (selected == 'pen') return
this.classList.add('active');
eraser.classList.remove('active');
selected = 'pen';
context.strokeStyle = `${color.value}`;
color.enabled = true;
color.removeAttribute('disabled');
}
function download(e) {
e.preventDefault();
var a = document.createElement('a'), f;
a.href = canvas.toDataURL("image/png;base64");
a.download = "YourCanvasDrawing.png";
if (document.createEvent) {
f = document.createEvent("MouseEvents");
f.initMouseEvent("click", true, true, window,
0, 0, 0, 0, 0, false, false, false,
false, 0, null);
a.dispatchEvent(f);
} else if (a.fireEvent) {
a.fireEvent("onclick");
}
}
function clearCanvas(e) {
e.preventDefault();
context.fillRect(0, 0, canvas.width, canvas.height);
}