-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
131 lines (110 loc) · 3.42 KB
/
main.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
let artSize = 0;
let colorcount = Math.floor(document.querySelector('.color').clientWidth / 20) * 2
let artheight = 0
if (document.documentElement.clientWidth <= 500) {
artSize = 20;
artheight = 30
}
else if (document.documentElement.clientWidth <= 768) {
artSize = 40;
artheight = 40
}
else if (document.documentElement.clientWidth <= 1024) {
artSize = 60;
artheight = 50
}
else if (document.documentElement.clientWidth > 1024) {
artSize = 80;
artheight = 26
}
const fetchColors = async () => {
const response = await fetch(`https://x-colors.yurace.pro/api/random?number=${colorcount}`)
const data = await response.json();
codes(data);
}
fetchColors();
let tools = document.querySelector('.toolbar .color');
var container = document.querySelector('.art');
var squareSize = 100 / artSize;
const createPixelArt = () => {
container.innerHTML = '';
for (var i = 0; i < artSize; i++) {
for (var j = 0; j < 26; j++) {
var square = document.createElement('div');
square.style.width = squareSize + '%';
container.appendChild(square);
}
}
changeColor();
}
const changeColor = (color = 'rgb(0,0,0)') => {
[...document.querySelectorAll('.art div')].map(square => {
square.addEventListener('click', () => {
square.style.backgroundColor = color
});
});
scroll(color);
}
const codes = (codes) => {
tools.innerHTML = '';
codes.map(color => {
let div = document.createElement('div');
div.addEventListener('click', () => {
document.querySelectorAll('.toolbar .color div').forEach(div => {
div.style.border = '1px solid #000'
div.style.borderRadius = '0'
})
changeColor(color.rgb)
div.style.border = '2px solid #fff'
div.style.borderRadius = '50%'
})
div.style.backgroundColor = color.rgb;
tools.appendChild(div);
})
}
const selectColor = () => {
[...document.querySelectorAll('.toolbar .color div')].map(div => {
div.addEventListener('click', () => {
changeColor(div.style.backgroundColor)
})
})
}
selectColor();
const scroll = (color) => {
var divs = [...document.querySelectorAll('.art div')];
let mouseDown = false;
divs.map(div => div.onmousedown = () => {
mouseDown = true;
})
divs.map(div => div.onmouseup = () => {
mouseDown = false;
})
divs.map(div => div.onmouseover = () => {
if (mouseDown) {
div.style.backgroundColor = color;
}
})
}
const reset = () => {
document.querySelectorAll('.art div').forEach(div => {
div.style.backgroundColor = 'rgb(255,255,255)'
})
changeColor();
}
const save = () => {
html2canvas(container).then(function (canvas) {
var link = document.createElement("a");
document.body.appendChild(link);
link.download = 'pixel-art.png';
link.href = canvas.toDataURL();
link.target = '_blank';
link.click();
});
}
const random = async () => {
const response = await fetch('https://x-colors.yurace.pro/api/random')
const data = await response.json();
changeColor(data.rgb);
}
createPixelArt();
window.addEventListener('resize', createPixelArt);