-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
207 lines (186 loc) · 5.63 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
const activeToolEl = document.getElementById('active-tool');
const brushColorBtn = document.getElementById('brush-color');
const brushIcon = document.getElementById('brush');
const brushSize = document.getElementById('brush-size');
const brushSlider = document.getElementById('brush-slider');
const bucketColorBtn = document.getElementById('bucket-color');
const eraser = document.getElementById('eraser');
const clearCanvasBtn = document.getElementById('clear-canvas');
const saveStorageBtn = document.getElementById('save-storage');
const loadStorageBtn = document.getElementById('load-storage');
const clearStorageBtn = document.getElementById('clear-storage');
const downloadBtn = document.getElementById('download');
const { body } = document;
// Global Variables
const canvas = document.createElement('canvas');
canvas.id = 'canvas';
const context = canvas.getContext('2d');
let currentSize = 10;
let bucketColor = '#FFFFFF';
let currentColor = '#A51DAB';
let isEraser = false;
let isMouseDown = false;
let drawnArray = [];
// Formatting Brush Size
function displayBrushSize() {
if (brushSlider.value < 10)
brushSize.textContent = `0${brushSlider.value}`;
else
brushSize.textContent = brushSlider.value;
}
// Setting Brush Size
brushSlider.addEventListener('change', () => {
currentSize = brushSlider.value;
displayBrushSize();
});
// Setting Brush Color
brushColorBtn.addEventListener('change', () => {
isEraser = false;
currentColor = `#${brushColorBtn.value}`;
});
// Setting Background Color
bucketColorBtn.addEventListener('change', () => {
bucketColor = `#${bucketColorBtn.value}`;
createCanvas();
restoreCanvas();
});
// Eraser
eraser.addEventListener('click', () => {
isEraser = true;
activeToolEl.textContent = 'Eraser';
brushIcon.style.color = 'white';
eraser.style.color = 'black';
currentColor = bucketColor;
currentSize = 50;
brushSlider.value = 50;
displayBrushSize();
});
// Switch back to Brush
function switchToBrush() {
isEraser = false;
activeToolEl.textContent = 'Brush';
brushIcon.style.color = 'black';
eraser.style.color = 'white';
currentColor = `#${brushColorBtn.value}`;
currentSize = 10;
brushSlider.value = 10;
displayBrushSize();
}
// Create Canvas
function createCanvas() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight - 50;
context.fillStyle = bucketColor;
context.fillRect(0, 0, canvas.width, canvas.height);
body.appendChild(canvas);
switchToBrush();
}
// Clear Canvas
clearCanvasBtn.addEventListener('click', () => {
createCanvas();
drawnArray = [];
// Active Tool
activeToolEl.textContent = 'Canvas Cleared';
setTimeout(switchToBrush, 1500);
});
// Draw what is stored in DrawnArray
function restoreCanvas() {
for (let i = 1; i < drawnArray.length; i++) {
context.beginPath();
context.moveTo(drawnArray[i - 1].x, drawnArray[i - 1].y);
context.lineWidth = drawnArray[i].size;
context.lineCap = 'round';
if (drawnArray[i].erase)
context.strokeStyle = bucketColor;
else
context.strokeStyle = drawnArray[i].color;
context.lineTo(drawnArray[i].x, drawnArray[i].y);
context.stroke();
}
}
// Store Drawn Lines in DrawnArray
function storeDrawn(x, y, size, color, erase) {
const line = {
x,
y,
size,
color,
erase,
};
drawnArray.push(line);
}
// Get Mouse Position
function getMousePosition(event) {
const boundaries = canvas.getBoundingClientRect();
return {
x: event.clientX - boundaries.left,
y: event.clientY - boundaries.top,
};
}
// Mouse Down
canvas.addEventListener('mousedown', (event) => {
isMouseDown = true;
const currentPosition = getMousePosition(event);
context.moveTo(currentPosition.x, currentPosition.y);
context.beginPath();
context.lineWidth = currentSize;
context.lineCap = 'round';
context.strokeStyle = currentColor;
});
// Mouse Move
canvas.addEventListener('mousemove', (event) => {
if (isMouseDown) {
const currentPosition = getMousePosition(event);
context.lineTo(currentPosition.x, currentPosition.y);
context.stroke();
storeDrawn(
currentPosition.x,
currentPosition.y,
currentSize,
currentColor,
isEraser,
);
} else
storeDrawn(undefined);
});
// Mouse Up
canvas.addEventListener('mouseup', () => {
isMouseDown = false;
});
// Save to Local Storage
saveStorageBtn.addEventListener('click', () => {
localStorage.setItem('savedCanvas', JSON.stringify(drawnArray));
// Active Tool
activeToolEl.textContent = 'Canvas Saved';
setTimeout(switchToBrush, 1500);
});
// Load from Local Storage
loadStorageBtn.addEventListener('click', () => {
if (localStorage.getItem('savedCanvas')) {
drawnArray = JSON.parse(localStorage.savedCanvas);
restoreCanvas();
// Active Tool
activeToolEl.textContent = 'Canvas Loaded';
} else
activeToolEl.textContent = 'No Canvas Found';
setTimeout(switchToBrush, 1500);
});
// Clear Local Storage
clearStorageBtn.addEventListener('click', () => {
localStorage.removeItem('savedCanvas');
// Active Tool
activeToolEl.textContent = 'Local Storage Cleared';
setTimeout(switchToBrush, 1500);
});
// Download Image
downloadBtn.addEventListener('click', () => {
downloadBtn.href = canvas.toDataURL('image/jpg', 1);
downloadBtn.download = 'Canvas.jpg'
// Active Tool
activeToolEl.textContent = 'Image File Saved';
setTimeout(switchToBrush, 1500);
});
// Event Listener
brushIcon.addEventListener('click', switchToBrush);
// On Load
createCanvas();