Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
99 lines (83 loc) · 2.77 KB
/
Copy pathapp.js
File metadata and controls
99 lines (83 loc) · 2.77 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
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
const grid = document.querySelector('.grid');
const colorPicker = document.querySelector('#favcolor');
let mouseDown = false;
document.body.onmousedown = () => mouseDown = true;
document.body.onmouseup = () => mouseDown = false;
let pixels = [];
let border = true;
let rainbow = false;
let n = 8;
function createGrid() {
let size = 800 / n - 2;
pixels = [];
grid.replaceChildren();
for (let i = 0; i < n * n; ++i) {
const pixel = document.createElement('div');
pixel.classList.add('pixel')
pixel.style.flex = `0 1 ${size}px`;
pixel.style.border = "solid 1px rgba(100, 100, 100, 0.3)";
pixels.push(pixel);
}
pixels.forEach(pixel => pixel.addEventListener('mousedown', fill));
pixels.forEach(pixel => pixel.addEventListener('mouseover', fill));
pixels.forEach(pixel => grid.appendChild(pixel));
}
createGrid();
function toggleBorder() {
border = !border;
if (border) {
let size = 800 / n - 2;
pixels.forEach(pixel => {
pixel.style.flex = `0 1 ${size}px`;
pixel.style.border = "solid 1px rgba(100, 100, 100, 0.3)";
});
} else {
let size = 800 / n;
pixels.forEach(pixel => {
pixel.style.flex = `0 1 ${size}px`;
pixel.style.border = "none";
});
}
}
function fill(e) {
if (e.type === 'mouseover' && !mouseDown) return;
if (rainbow) {
let r = Math.floor(Math.random() * 255) + 1;
let g = Math.floor(Math.random() * 255) + 1;
let b = Math.floor(Math.random() * 255) + 1;
e.target.style.background = `rgb(${r}, ${g}, ${b})`
} else {
const color = colorPicker.value;
e.target.style.background = color;
}
}
const clear_btn = document.querySelector('.clear');
clear_btn.addEventListener('click', () => {
pixels.forEach(pixel => pixel.style.background = "white");
});
const sz_btn = document.querySelector('.size');
sz_btn.addEventListener('click', () => {
let x = prompt("Enter the side of each side");
if (x <= 64 && x > 0)
n = x;
createGrid();
});
const border_btn = document.querySelector('.border');
border_btn.addEventListener('click', toggleBorder);
const toggle_rainbow = document.querySelector('.rainbow');
toggle_rainbow.addEventListener('click', () => {
rainbow = !rainbow;
});
function downloadimage() {
var container = grid;
html2canvas(container, { allowTaint: true }).then(function(canvas) {
var link = document.createElement("a");
document.body.appendChild(link);
link.download = "pixel_art.jpg";
link.href = canvas.toDataURL();
link.target = '_blank';
link.click();
});
}
const down_btn = document.querySelector('.download');
down_btn.addEventListener('click', downloadimage);