-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
76 lines (60 loc) · 1.98 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
let color = 'black'
const gridSlider = document.getElementById('gridSlider');
const gridValue = document.getElementById('gridValue');
const knob = document.querySelector('.knob')
function boardDivs(size) {
let board = document.querySelector('.board')
let squares = board. querySelectorAll('div')
squares.forEach((div) => div.remove())
board.style.gridTemplateColumns = `repeat(${size}, 1fr)`
board.style.gridTemplateRows = `repeat(${size}, 1fr)`
let amount = size * size
for (let i = 0; i < amount; i++) {
let square = document.createElement('div')
square.addEventListener('mouseover', squareColor)
square.style.backgroundColor = 'white'
board.insertAdjacentElement('beforeend', square)
}
}
function updateSizeText(size){
gridValue.innerHTML = `${size} x ${size}`;}
boardDivs(16)
gridSlider.onchange = (e) => boardDivs(e.target.value);
gridSlider.onmousemove = (e) => updateSizeText(e.target.value);
function squareColor(){
if ((color === "random")){
this.style.backgroundColor = `hsl(${Math.random() * 360}, 80%, 50%)`;
}else{
this.style.backgroundColor = color;
}
}
function changeColor(choice){
color = choice;
}
function resetBoard(){
let board = document.querySelector('.board')
let squares = board. querySelectorAll('div')
squares.forEach((div) => div.style.backgroundColor = 'white')
}
//knob
function calculateDegree(e){
const x1 = window.innerWidth / 2;
const y1 = window.innerHeight / 2;
const x2 = e.clientX;
const y2 = e.clientY;
const deltaX = x1 - x2;
const deltaY = y1 - y2;
const rad = Math.atan2(deltaY, deltaX)
let deg = rad * (180 / Math.PI);
return deg
}
knob.addEventListener('mousedown', ()=>{
knob.addEventListener("mousemove", rotate)
function rotate(e) {
const result = Math.floor(calculateDegree(e) - 90);
knob.style.transform = `rotate(${result}deg)`;
}
knob.addEventListener('mouseup', function () {
knob.removeEventListener( 'mousemove', rotate);
});
});