-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
67 lines (58 loc) · 2.09 KB
/
main.js
File metadata and controls
67 lines (58 loc) · 2.09 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
import './js/image.js';
import './js/imageData.js';
import './js/imageOptions.js'
import ImageGrid from './js/imageGrid.js';
import { createPath, options } from './js/imageOptions.js';
export let drawCellOptionsCount = false;
const pixelSize = 20;
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const tileCanvas = document.getElementById('tileCanvas');
const tileCtx = tileCanvas.getContext('2d');
canvas.width = canvas.height = 700;
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
function setTileCanvas() {
tileCanvas.width = window.innerWidth - 100;
tileCanvas.height = window.innerHeight - 110;
tileCtx.textAlign = 'center'
tileCtx.textBaseline = 'middle'
}
setTileCanvas();
window.addEventListener('resize', setTileCanvas);
// Image Selector
const select = document.getElementById('selectImage');
let lastOption;
select.addEventListener('change', () => {
select.blur();
lastOption?.imageGrid.abort();
const option = options.get(select.value);
if(option.imageGrid) {
option.imageGrid.load();
} else {
option.imageGrid = new ImageGrid(ctx, option.src, select.value);
}
option.imageGrid.generate(pixelSize);
lastOption = option;
});
const totalOptions = select.options.length;
const randomIndex = Math.floor(Math.random() * totalOptions);
select.selectedIndex = randomIndex;
select.dispatchEvent(new Event('change'));
const saveBtn = document.getElementById('save');
saveBtn.addEventListener('click', () => {
const { activeCanvas } = lastOption.imageGrid;
const { width, height } = activeCanvas;
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
canvas.width = width;
canvas.height = height;
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, width, height);
ctx.drawImage(activeCanvas, 0, 0);
const a = document.createElement('a');
const fileName = select.value;
a.download = fileName.substring(fileName.indexOf('/', 2) + 1);
a.href = canvas.toDataURL();
a.click();
});