-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
90 lines (71 loc) · 3.02 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
// CONSTANTS
const DEFAULT_COLOR = '#000000'
// DOM ELEMENTS
const sketchBoard = document.querySelector('.sketch-board');
const sketchContainer = document.querySelector('.sketch-container');
const colorPicker = document.querySelector('#color-picker')
const penTool = document.querySelector('#pen')
const eraserTool = document.querySelector('#eraser')
const clearTool = document.querySelector('#clear')
const sketchSize = document.querySelector('#sketch-size');
const sizeDisplay = document.querySelector('#size-display');
// ink mode to determine which color to apply on sketch-cells
let inkMode = DEFAULT_COLOR;
// attaching required listeners initially
window.onload = () => {
colorPicker.value = DEFAULT_COLOR
sketchSize.value = 16;
}
penTool.onclick = () => setInkMode(colorPicker.value);
colorPicker.onchange = (e) => setInkMode(e.target.value);
eraserTool.onclick = () => setInkMode('#FFFFFF');
clearTool.onclick = () => Array.from(sketchBoard.children).forEach(child => child.style.backgroundColor = null)
// function to generate the grid with the given input (size)
function generateGrid(size){
sketchBoard.style.gridTemplateColumns = `repeat(${size}, 1fr)`
sketchBoard.style.gridTemplateRows = `repeat(${size}, 1fr)`
for (let i = 0; i < size * size; i++){
const cell = document.createElement('cell');
cell.className = 'sketch-cell';
sketchBoard.appendChild(cell);
}
}
// function to handle sketch events
function sketchHandler(){
// an abort controller to abort requests (in this case an event)
let abortController = new AbortController();
// callbacks that can later be referenced to remove respective listeners
const highlightCallback = (e) => addColorToCell(e.target, inkMode)
const clearHighlightCallback = () => sketchBoard.removeEventListener('mouseover', highlightCallback);
// attaching listeners to the sketch-board
sketchBoard.addEventListener('mousedown', (e) => {
e.preventDefault()
addColorToCell(e.target, inkMode)
sketchBoard.addEventListener('mouseover', highlightCallback, {signal: abortController.signal})
window.addEventListener('mouseup', clearHighlightCallback, {signal: abortController.signal});
})
// detaching the mouseover events to prevent unnecessary sketch-cell coloring
// when the mousdown event is released
sketchBoard.addEventListener('mouseup', () => {
abortController.abort()
abortController = new AbortController()
})
}
// helper function to color the target sketch-cell
function addColorToCell(cell, color){
if (!cell.classList.contains('sketch-cell')) return;
cell.style.backgroundColor = color;
}
// helper function to set ink mode
function setInkMode(mode){
inkMode = mode
}
// attach listener to track slider input and update the grid respectively
sketchSize.addEventListener('input', (e) => {
sketchBoard.replaceChildren()
generateGrid(+e.target.value)
sizeDisplay.textContent = `Sketch Area : ${e.target.value} x ${e.target.value}`
})
// initial grid render (16x16) and attach listeners
generateGrid(16);
sketchHandler();