-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource.js
83 lines (83 loc) · 2.86 KB
/
source.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
(() => {
const inputElements = document.getElementsByTagName("INPUT");
let isCreating = true;
let dragable = false;
const elements = [];
const W_KEY_CODES = [87, 119,1094,1062];
const tagClickListenteners = {
BODY: addRectangle,
DIV: dragRectangle,
BUTTON: buttonClick
};
const bodyClick = document.body.addEventListener('click', (event) => {
tagClickListenteners[event.target.tagName] && tagClickListenteners[event.target.tagName](event)
});
const fadeAway = document.body.addEventListener('keypress', (event) => {
if (W_KEY_CODES.indexOf(event.keyCode) > -1) {
for (const element of elements) {
element.classList += 'animated fadeOut';
setTimeout(() => {
element.remove()
},3000)
}
}
});
const startDrag = document.body.addEventListener('mousedown', (event) => {
if (event.target.tagName === "DIV") {
dragable = event.target;
if (event.shiftKey) {
event.target.style.zIndex = "100";
}
}
});
const drag = document.body.addEventListener('mousemove',(event) => {
if (dragable) {
dragable.style.top = event.clientY + "px";
dragable.style.left = event.clientX + "px";
}
});
const finishDrag = document.body.addEventListener('mouseup', (event) => {
if (dragable) {
dragable.style.zIndex = 0;
dragable = false;
console.log('1')
}
});
function addRectangle() {
if(isCreating) {
const inputs = getInputsObject();
const newRectangle = document.createElement('div');
applyStyles(newRectangle, inputs);
document.body.appendChild(newRectangle);
elements.push(newRectangle);
}
}
function getInputsObject() {
return {
height: inputElements[0].value || null,
width: inputElements[1].value || null
};
}
function applyStyles(newRectangle, inputs) {
newRectangle.style.background = getRandomColor();
newRectangle.style.width = inputs.width + 'px';
newRectangle.style.position = 'absolute';
newRectangle.style.height = inputs.height + 'px';
newRectangle.style.top = event.clientY + 'px';
newRectangle.style.left = event.clientX + 'px';
}
function buttonClick () {
isCreating = !isCreating;
isCreating ? event.target.innerHTML = "Хватит уже" : event.target.innerHTML = "Давай еще";
}
function dragRectangle() {
}
function getRandomColor() {
let letters = '0123456789ABCDEF';
let color = '#';
for (let i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
})();