-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrender.js
More file actions
79 lines (62 loc) · 2.42 KB
/
render.js
File metadata and controls
79 lines (62 loc) · 2.42 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
const fs = require('fs');
const config = require('./config.json');
const rows = require('./temp/sequence.json');
function getPixelPosition(pixelID) {
const y = Math.floor(pixelID / config.general.width);
const x = pixelID % config.general.width;
return { x, y };
}
function getHexColor(value) {
const hex = value.toString(16).padStart(2, '0');
return '#' + hex + hex + hex;
}
const animations= {};
let lastFrame = 0;
async function generateAnimation(pixelID) {
const position = getPixelPosition(pixelID);
const pixel = rows[position.y][position.x];
let frames = Object.keys(pixel);
frames.splice(frames.length-1, 1);
if (lastFrame === 0) {
lastFrame = frames[frames.length - 1];
}
let animation = `@keyframes pixel_${pixelID} {\n`;
let firstColor;
for (let i = 0; i < frames.length; i++) {
const framePos = (frames[i] / lastFrame) * 100;
const color = getHexColor(pixel[frames[i]]);
if (i === 0) {
firstColor = color;
}
animation += `\t${framePos}%\t{background-color:${color};}\n`;
}
animation += `}\n#${config.render.pixelContainer} #row_${position.y} #pixel_${pixelID} {animation-name: pixel_${pixelID}}`;
animations[pixelID] = animation;
}
console.log('Preparing Environment');
if (fs.existsSync('./out')) {
fs.rmdirSync('./out', { recursive: true });
}
fs.mkdirSync('./out');
console.log('Generate Pixel Animation CSS');
const pixelCount = config.general.width * config.general.height;
const generators = [];
for (let i = 0; i < pixelCount; i++) {
generators.push(generateAnimation(i));
}
Promise.all(generators).then(() => {
let finalCSS = '';
for (let i = 0; i < pixelCount; i++) {
finalCSS += '\n' + animations[i];
}
fs.writeFileSync(`./out/rendered.css`, finalCSS);
console.log('Generate display HTML');
let html = fs.readFileSync(`./template.html`, { encoding: 'utf8' });
html = html.split('###PIXELSIZE###').join(config.render.pixelSize);
html = html.split('###ROWWIDTH###').join(config.render.pixelSize * config.general.width);
html = html.split('###DURATION###').join(lastFrame / config.render.framerate);
html = html.split('###CONTAINER###').join(config.render.pixelContainer);
html = html.split('###WIDTH###').join(config.general.width);
html = html.split('###HEIGHT###').join(config.general.height);
fs.writeFileSync(`./out/index.html`, html);
})