-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathscript.js
More file actions
144 lines (117 loc) · 3.35 KB
/
Copy pathscript.js
File metadata and controls
144 lines (117 loc) · 3.35 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
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
const canvas = document.querySelector("#canvas");
const ctx = canvas.getContext("2d");
const penSize = document.querySelector('#pen-size')
const eraserSize = document.querySelector('#eraser-size')
const penColor = document.querySelector('#pen-color')
const resetBtn = document.querySelector('#reset-canvas')
const saveBtn = document.querySelector('#save-canvas')
const backgroundColors = document.querySelectorAll('.color-field')
const radioBtns = document.querySelectorAll('input[name="pen-type"]')
let lineWidth = 10
const pencilWidth = 5
let eraserWidth = 10
let lineColor = 'black'
let eraserColor = 'white'
penSize.value = lineWidth
eraserSize.value = lineWidth
penColor.value = 'black'
// canvas sizing
canvas.height = window.innerHeight;
canvas.width = window.innerWidth;
// variables
let painting = false;
function startPosition() {
painting = true
}
function finishedPosition() {
painting = false
ctx.beginPath();
}
function draw(e) {
if (!painting) return;
ctx.lineWidth = lineWidth;
ctx.lineCap = "round";
let canvasX = e.pageX - canvas.offsetLeft;
let canvasY = e.pageY - canvas.offsetTop;
ctx.strokeStyle = lineColor;
ctx.lineTo(canvasX, canvasY);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(canvasX, canvasY);
}
const handleWidthChange = evt => {
if (evt.target.id === 'eraser-size') {
eraserWidth = parseInt(evt.target.value)
} else if (evt.target.id === 'pen-size') {
lineWidth = parseInt(evt.target.value)
}
}
const handleColorChange = () => {
lineColor = penColor.value
}
const handleEraserWidth = (evt) => {
lineWidth = eraserSize.value
}
const changeTool = (btn) => {
switch (btn.value) {
case 'pen':
lineWidth = penSize.value
lineColor = penColor.value
eraserWidth = eraserSize.value
break
case 'pencil':
lineWidth = pencilWidth
eraserWidth = eraserSize.value
lineColor = penColor.value
break
case 'eraser':
lineWidth = pencilWidth
eraserWidth = eraserSize.value
lineColor = eraserColor
break
}
}
const handleRadioInput = () => {
for (const radioBtn of radioBtns) {
if (radioBtn.checked) {
changeTool(radioBtn)
}
}
}
const changeBackground = (evt) => {
ctx.fillStyle = evt.target.style.backgroundColor
ctx.fillRect(0,0, canvas.width, canvas.height)
eraserColor = evt.target.style.backgroundColor
}
const clearScreen = () => {
ctx.clearRect(0,0, canvas.width, canvas.height)
eraserColor = 'white'
}
// EventListeners to draw a line
canvas.addEventListener('mousedown', startPosition);
canvas.addEventListener('mouseup', finishedPosition);
canvas.addEventListener('mousemove', draw);
// background color
for (const backgroundColor of backgroundColors) {
backgroundColor.addEventListener('click', changeBackground)
}
// radio btn
for (const btn of radioBtns) {
btn.addEventListener('input', handleRadioInput)
}
// reset button
resetBtn.addEventListener('click', clearScreen)
// respond to user input
penSize.addEventListener('input', handleWidthChange)
eraserSize.addEventListener('input', handleWidthChange)
penColor.addEventListener("input", handleColorChange)
eraserSize.addEventListener('input', handleEraserWidth)
// Resizing when screen length changes
window.addEventListener('resize', () => {
canvas.height = window.innerHeight;
canvas.width = window.innerWidth;
});
saveBtn.addEventListener("click", function(ev) {
saveBtn.href = canvas.toDataURL();
saveBtn.download = "mypainting.png";
}, false);