66 lines
1.6 KiB
JavaScript
66 lines
1.6 KiB
JavaScript
const express = require("express");
|
|
const path = require("path");
|
|
const { port, width, height } = require("./config.json");
|
|
const app = express();
|
|
|
|
const indexResponse = `
|
|
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<title>draw</title>
|
|
</head>
|
|
<body>
|
|
<img src="/image" /><br />
|
|
<h3>send a get request to /set/[x]/[y]/[r]/[g]/[b] to change the image</h3>
|
|
</body>
|
|
</html>
|
|
`;
|
|
|
|
let image = Buffer.alloc(14 + 12 + width * height * 3);
|
|
|
|
app.get("/image", (req, res) => {
|
|
res.writeHead(200, { "Content-Type": "image/bmp", "Content-Length": image.length });
|
|
res.end(image);
|
|
});
|
|
|
|
app.get("/", (req, res) => {
|
|
res.send(indexResponse);
|
|
});
|
|
|
|
app.get("/set/:x/:y/:r/:g/:b", (req, res) => {
|
|
let { x, y, r, g, b } = req.params;
|
|
[ x, y, r, g, b ] = [ +x, +y, +r, +g, +b ];
|
|
|
|
if (x >= width || y >= height) {
|
|
res.status(400).send({ success: 0, error: "coordinates outside of bounds" });
|
|
} else if (r >= 256 || g >= 256 || b >= 256) {
|
|
res.status(400).send({ success: 0, error: "check your colour values....." });
|
|
} else {
|
|
const pos = (x + (height - y - 1) * width) * 3 + 26;
|
|
image[pos + 0] = r;
|
|
image[pos + 1] = g;
|
|
image[pos + 2] = b;
|
|
|
|
res.status(200).send({ success: 1 });
|
|
}
|
|
});
|
|
|
|
app.listen(port, () => {
|
|
// manually writing the header :michael:
|
|
const length = image.length;
|
|
image.set([
|
|
0x42, 0x4D,
|
|
(length >> 0) & 0xFF, (length >> 8) & 0xFF, (length >> 16) & 0xFF, (length >> 24) & 0xFF,
|
|
0x00, 0x00,
|
|
0x00, 0x00,
|
|
0x1A, 0x00, 0x00, 0x00,
|
|
0x0C, 0x00, 0x00, 0x00,
|
|
(width >> 0) & 0xFF, (width >> 8) & 0xFF,
|
|
(height >> 0) & 0xFF, (height >> 8) & 0xFF,
|
|
0x01, 0x00,
|
|
0x18, 0x00
|
|
], 0);
|
|
|
|
console.log(`deatj at http://0.0.0.0:${port}/`);
|
|
});
|