Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const readline = require('readline');
- const fs = require('fs');
- const express = require('express');
- const router = express();
- const { createCanvas } = require('canvas');
- const pixelSize = 20;
- var canvas;
- var context;
- async function processLines() {
- const fileStream = fs.createReadStream('input.txt');
- const rl = readline.createInterface({
- input: fileStream,
- crlfDelay: Infinity
- });
- var lines = [];
- var lineIndex = 0;
- for await (const line of rl) {
- var colors = {};
- colors.line = lineIndex;
- colors.pixels = [];
- pixels = line.split("][");
- for (i = 0; i < pixels.length; i++){
- colors.pixels[i] = pixels[i].replace(/\[|\]/g, "").split(",");
- }
- lines.push(colors);
- lineIndex++;
- }
- var longestWidth = 0;
- for (y = 0; y < lines.length; y++){
- var pixels = lines[y].pixels;
- if (pixels.length > longestWidth) longestWidth = pixels.length;
- }
- canvas = createCanvas(longestWidth*pixelSize, lines.length*pixelSize);
- context = canvas.getContext('2d');
- context.fillStyle = '#000';
- context.fillRect(0, 0, longestWidth*pixelSize, lines.length*pixelSize);
- for (y = 0; y < lines.length; y++){
- var line = lines[y];
- for (x = 0; x < line.pixels.length; x++){
- var pixel = line.pixels[x];
- context.clearRect(x*pixelSize,y*pixelSize,pixelSize,pixelSize);
- context.beginPath();
- context.rect(x*pixelSize,y*pixelSize,pixelSize,pixelSize);
- context.fillStyle = 'rgb(' + pixel[0] + ',' + pixel[1] + ',' + pixel[2] + ')';
- context.fill();
- }
- }
- }
- processLines();
- //The image will be available in the browser
- router.get('/', function (req, res, next) {
- res.setHeader('Content-Type', 'image/png');
- canvas.pngStream().pipe(res);
- });
- router.listen(80);
Advertisement
Add Comment
Please, Sign In to add comment