-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday-32.js
More file actions
45 lines (39 loc) · 1.45 KB
/
day-32.js
File metadata and controls
45 lines (39 loc) · 1.45 KB
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
const toggle = document.querySelector('[data-js="toggle"]');
const workstation = document.querySelector('[data-js="workstation"]');
const lights = document.querySelectorAll('[data-js="light"]');
toggle.addEventListener('click', () => {
if (toggle.checked) {
[...lights].forEach((light) => {
light.classList.add('on');
});
workstation.classList.add('on');
} else {
[...lights].forEach((light) => {
light.classList.remove('on');
});
workstation.classList.remove('on');
}
});
const frames = document.querySelectorAll('[data-js="frame"]');
let mouseDown = false;
const mousemove = frame => evt => {
if (mouseDown) {
const offset = frame.getBoundingClientRect();
const centerX = (offset.left) + (offset.width / 2);
const centerY = (offset.top) + (offset.height / 2);
// The atan2() method returns the arctangent of the quotient of its arguments, as a numeric value between PI and -PI radians.
// https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/Math/atan2
const radians = Math.atan2(evt.pageX - centerX, evt.pageY - centerY);
const angle = (radians * (180 / Math.PI) * -1) - 90;
frame.style.transform = `rotate(${angle}deg)`;
}
}
[...frames].forEach((frame) => {
frame.addEventListener("mousedown", () => {
mouseDown = true;
frame.addEventListener("mousemove", mousemove(frame));
});
frame.addEventListener("mouseup", () => {
mouseDown = false;
});
});