Skip to content

Instantly share code, notes, and snippets.

@taesiri
Created November 8, 2018 22:40
Show Gist options
  • Save taesiri/d46e1a9f68c14ef026c95c020ae59d0b to your computer and use it in GitHub Desktop.
Save taesiri/d46e1a9f68c14ef026c95c020ae59d0b to your computer and use it in GitHub Desktop.
Canvas Drawing
var fs = require('fs');
var es = require('event-stream');
const { createCanvas } = require('canvas');
const canvas = createCanvas(1000, 1000);
const ctx = canvas.getContext('2d');
Number.prototype.clamp = function(min, max) {
return Math.min(Math.max(this, min), max);
};
ctx.fillStyle = "white";
ctx.fillRect(0, 0, 1000, 1000);
pts = [];
var s = fs.createReadStream('/mnt/d/E.txt')
.pipe(es.split('\n'))
.pipe(es.mapSync(function(line){
// pause the readstream
s.pause();
// process line here and call s.resume() when rdy
// function below was for logging memory usage
//logMemoryUsage(lineNr);
var splited = line.split(', ');
var x = 250+(10*parseFloat(splited[0].replace("(", ""))).clamp(-250, 750);
var y = 900-(10*parseFloat(splited[1])).clamp(-100, 900);
pts.push({X: x, Y:y});
// resume the readstream, possibly from a callback
s.resume();
})
.on('error', function(err){
console.log('Error while reading file.', err);
})
.on('end', function(){
console.log('Read entire file.');
console.log("drawing");
pts.forEach(element => {
ctx.beginPath();
ctx.fillStyle = "rgba(0, 0, 0, 0.01)";
ctx.arc(element.X, element.Y, 0.75, 0, Math.PI * 2, true);
ctx.fill();
ctx.closePath();
// console.log("draw", element);
});
var buf = canvas.toBuffer();
fs.writeFileSync("D.png", buf);
console.log("Done");
})
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment