-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimagemaker.js
96 lines (90 loc) · 2.78 KB
/
imagemaker.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
function range(n) {
let _newArr = [];
for (let i = 0; i < n; i++) _newArr.push(i);
return _newArr;
}
window.onload = function () {
if (localStorage.getItem('customPuzzles') == null) localStorage.setItem('customPuzzles', '[]');
let custom = JSON.parse(localStorage.getItem('customPuzzles'));
document.getElementById('color').value = '#000';
let game = document.getElementById('pixels');
let ctx = game.getContext('2d');
let matrix = [];
let colors = ['#000000'];
for (let i = 0; i < matrix.length; i++) {
for (let j = 0; j < matrix[0].length; j++) {
if (colors.indexOf(matrix[i][j]) === -1) colors.push(matrix[i][j]);
}
}
let cols = 5;
let rows = 5;
let one = game.width / cols;
let w = game.width;
let h = game.height = one * rows;
setMatrix(5, 5, false);
render();
renColors();
function setMatrix(c, r, x=true) {
if (x) if (!confirm('All data will be lost!')) return;
cols = c;
rows = r;
one = game.width / cols;
h = game.height = one * rows;
for (let i = 0; i < r; i++) {
matrix[i] = [];
for (let j = 0; j < c; j++) {
matrix[i][j] = '#ffffff';
}
}
}
function renColors() {
for (let i = 0; i < colors.length; i++) {
console.log('x');
if (colors.indexOf(document.getElementById('color').value) == -1) colors.push(document.getElementById('color').value);
}
document.getElementById('recent').innerHTML = '';
for (let i = 0; i < colors.length; i++) {
let _li = document.createElement('li');
_li.innerHTML = colors[i] + ' (Click to set)';
_li.style.color = colors[i];
_li.style.textShadow = '1px 1px 2px black';
_li.onclick = () => document.getElementById('color').value = colors[i];
document.getElementById('recent').append(_li);
}
}
function render() {
ctx.clearRect(0, 0, w, h);
for (let i of range(rows)) {
for (let j of range(cols)) {
ctx.fillStyle = matrix[i][j];
ctx.fillRect(j * one, i * one, one, one);
}
}
}
document.getElementById('apply').onclick = function () {
setMatrix(
Number(document.getElementById('width').value),
Number(document.getElementById('height').value)
);
}
document.getElementById('ok').onclick = function () {
custom.push(matrix);
localStorage.setItem('customPuzzles', JSON.stringify(custom));
location.href = 'index.html';
//document.getElementById('output').innerHTML = JSON.stringify(matrix);
}
game.onclick = function (evt) {
let _x = Math.floor(evt.offsetX / one);
let _y = Math.floor(evt.offsetY / one);
matrix[_y][_x] = document.getElementById('color').value;
render();
renColors();
}
game.oncontextmenu = function (evt) {
evt.preventDefault();
let _x = Math.floor(evt.offsetX / one);
let _y = Math.floor(evt.offsetY / one);
matrix[_y][_x] = '#ffffff';
render();
}
}