-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
108 lines (92 loc) · 2.92 KB
/
index.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
import config from './config.json';
import {
drawGrid,
fillSquare,
clearCanvas,
targetSquare,
} from './src/canvas';
import {
colorBox,
leftInput,
rightInput,
colorPicker,
downloadBtn,
drawingGrid,
sliderCursor,
} from './src/htmlElements';
import {
pickColor,
sliderPosition,
drawPickerColumn,
} from './src/colorPicker';
import { download } from './src/utils';
let currentSliderPositon = 0;
let color = config.defaults.color;
let squaresNumber = config.defaults.squaresNumber;
const canvasSize = config.defaults.canvasSize;
// Set Grid canvas size
drawingGrid.width = canvasSize;
drawingGrid.height = canvasSize;
// Set color picker canvas size
colorPicker.height = canvasSize;
const context = drawingGrid.getContext('2d');
// Draw the grid the first time
drawGrid(context, squaresNumber, canvasSize);
// Draw the picker bar
drawPickerColumn();
// Send the default values even in the css file
document.documentElement.style.setProperty('--color-box-bg', color);
document.documentElement.style.setProperty('--canvas-size', `${canvasSize}px`);
// Put default values
// into the HTML elements
colorBox.innerText = color;
leftInput.value = squaresNumber;
rightInput.value = squaresNumber;
// Handle inputs change
leftInput.onchange = (event) => {
squaresNumber = event.target.value;
rightInput.value = event.target.value;
clearCanvas(context, canvasSize);
drawGrid(context, parseInt(event.target.value), canvasSize);
}
rightInput.onchange = (event) => {
squaresNumber = event.target.value;
leftInput.value = event.target.value;
clearCanvas(context, canvasSize);
drawGrid(context, parseInt(event.target.value), canvasSize);
}
// Listen for mouse clicks on the canvas
drawingGrid.addEventListener('click', (event) => { // For some reason mousedown won't work as expected wihtin canvas :-(
const mouseCoordinates = targetSquare(drawingGrid, event, squaresNumber);
fillSquare(
color,
context,
canvasSize,
squaresNumber,
mouseCoordinates.x,
mouseCoordinates.y,
);
});
let mouseDragSlider = undefined;
// TODO This keeps the slider works but needs refactoring
sliderCursor.addEventListener('mousedown', () => {
mouseDragSlider = true;
sliderCursor.addEventListener('mousemove', (mouseEvent) => {
// If the mouse is still pressed
if (mouseDragSlider === true) {
// Update slider color and position
currentSliderPositon = sliderPosition(mouseEvent);
const newColor = pickColor(colorPicker, currentSliderPositon);
// Spread the new color and position to the HMTL and CSS
color = newColor;
colorBox.innerText = newColor;
document.documentElement.style.setProperty('--color-box-bg', newColor);
document.documentElement.style.setProperty('--slider-position', currentSliderPositon);
}
});
});
document.addEventListener('mouseup', () => {
mouseDragSlider = false;
});
// Download the PNG
downloadBtn.addEventListener('click', download);