-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
157 lines (138 loc) · 4.82 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
/* slider value display script */
var gridSlider = document.getElementById("grid-slider");
var gridDisplay = document.getElementById("grid-display");
var gridValue = 16; // default initial value
let gridElements = makeGrid(gridValue); // initializes first artboard grid
let rainbowMode = false;
let shadingMode = false;
gridSlider.oninput = function() {
gridDisplay.textContent = `Board Grid: ${gridSlider.value}x${gridSlider.value}`
gridValue = gridSlider.value;
gridElements = makeGrid(gridValue);
addListenerForSketch();
}
/* creates new grid for artboard */
/* returns new collection of grid items */
function makeGrid(gridValue){
const sizeOfElements = (480/gridValue); // assuming artboard width and height fixed 480px
const numberOfElements = Math.pow(gridValue, 2);
clearGrid();
setArtboardProperties(numberOfElements, sizeOfElements);
addGridElement(numberOfElements);
return document.querySelectorAll(".grid-item");
}
function clearGrid() {
const artboard = document.getElementById("artboard");
var gridElement = artboard.lastElementChild;
while (gridElement) {
artboard.removeChild(gridElement);
gridElement = artboard.lastElementChild;
}
}
function addGridElement(numberOfElements){
const artboard = document.getElementById("artboard");
for(let i = 0; i<numberOfElements; i++){
const gridElement = document.createElement("div");
gridElement.classList.add("grid-item");
artboard.appendChild(gridElement);
}
}
function setArtboardProperties(numberOfElements, sizeOfElements){
const artboard = document.getElementById("artboard");
artboard.style.gridTemplateColumns = `repeat(${gridValue}, ${sizeOfElements}px)`;
artboard.style.gridAutoRows = `${sizeOfElements}px`;
}
/* sketches with chosen color when hovered over artboard */
function addListenerForSketch(){
gridElements.forEach(element => {
element.addEventListener("mouseover", doSketch);
});
}
addListenerForSketch();
function getCurrentColor(){
const colorPicker = document.getElementById("color-picker");
const colorValue = colorPicker.value;
return colorValue;
}
function doSketch(e){
let colorValue;
if(rainbowMode){
colorValue = getRandomColor();
this.style.backgroundColor = colorValue;
this.style.opacity = 1;
} else if(shadingMode){
colorValue = getCurrentColor();
if (rgbToHex(this.style.backgroundColor)!=colorValue){
this.style.backgroundColor = colorValue;
this.style.opacity = 0.25;
} else {
if(this.style.opacity<1){
this.style.opacity = parseFloat(this.style.opacity) + 0.25;
}
}
} else {
colorValue = getCurrentColor();
this.style.backgroundColor = colorValue;
this.style.opacity = 1;
}
}
/* clears the artboard when clear button is clicked */
const clearButton = document.querySelector(".clear-button");
clearButton.addEventListener("click", clear);
function clear(e){
gridElements.forEach(element => {
element.style.backgroundColor = "#ffffff";
element.style.opacity = 1;
});
}
/* sets current color to white when eraser button is clicked */
const eraserButton = document.querySelector(".eraser-button");
eraserButton.addEventListener("click", setEraser);
function setEraser(e){
shadingMode = false;
rainbowMode = false;
const colorPicker = document.getElementById("color-picker");
colorPicker.value = "#ffffff";
}
/* rainbow mode: changes current color randomly when rainbow mode button is clicked */
const rainbowButton = document.querySelector(".rainbow-button");
rainbowButton.addEventListener("click", () => {
rainbowMode = true;
shadingMode = false;
});
function getRandomColor(){
const letters = "0123456789ABCDEF".split("");
let color = "#";
for (let i = 0; i < 6; i++){
color += letters[Math.round(Math.random()*15)];
}
return color;
}
/* shading mode: reduces sketching opacity */
const shadingButton = document.querySelector(".shading-button");
shadingButton.addEventListener("click", () => {
rainbowMode = false;
shadingMode = true;
});
function rgbToHex(rgbString) {
rgbString = rgbString.replace("rgb(", "");
rgbString = rgbString.replace(")", "");
const rgbValues = rgbString.split(", ");
let r = toHex(parseInt(rgbValues[0]));
let g = toHex(parseInt(rgbValues[1]));
let b = toHex(parseInt(rgbValues[2]));
return "#" + r + g + b;
}
function toHex(n) {
let hex = n.toString(16);
while (hex.length < 2){
hex = "0" + hex;
}
return hex;
}
/* default mode: sets rainbow and shading modes to false */
const defaultButton = document.querySelector(".default-button");
defaultButton.addEventListener("click", () => {
rainbowMode = false;
shadingMode = false;
});