-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
41 lines (33 loc) · 1.07 KB
/
index.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
const express = require('express');
const { createCanvas } = require('canvas');
const seedrandom = require('seedrandom');
const app = express();
const PORT = process.env.PORT || 8080; // Fleek
app.get('/:id?', (req, res) => {
const { id } = req.params;
const seed = id || 'default-seed'; // Usa 'default-seed' si no se proporciona id
const rng = seedrandom(seed);
const width = 256;
const height = 256;
const canvas = createCanvas(width, height);
const ctx = canvas.getContext('2d');
for (let x = 0; x < width; x += 16) {
for (let y = 0; y < height; y += 16) {
ctx.fillStyle = getRandomColor(rng);
ctx.fillRect(x, y, 16, 16);
}
}
res.setHeader('Content-Type', 'image/png');
canvas.pngStream().pipe(res);
});
function getRandomColor(rng) {
const letters = '0123456789ABCDEF';
let color = '#';
for (let i = 0; i < 6; i++) {
color += letters[Math.floor(rng() * 16)];
}
return color;
}
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});