Skip to content

Instantly share code, notes, and snippets.

@dmadan86
Created October 21, 2019 07:00
Show Gist options
  • Save dmadan86/e2918153ec85a63563a0dc5f9ae3af9f to your computer and use it in GitHub Desktop.
Save dmadan86/e2918153ec85a63563a0dc5f9ae3af9f to your computer and use it in GitHub Desktop.
JS to Image
var _canvas = require('canvas'),
fs = require('fs');
fs.readFile('jquery.js', 'utf8', function (err,data) {
if (err) {
return console.log(err);
}
var stringData = data,
dataLen = stringData.length,
width = Math.ceil(Math.sqrt(dataLen)),
height = width,
canvas = new _canvas.Canvas(width, height),
ctx = canvas.getContext('2d')
asciiValue = 32,
imgData = ctx.getImageData(0,0,width,height),
data = imgData.data,
len = data.length;
for(var i=0, index=0, asciiValue;i<len;i=i+4){
asciiValue = 32
if(index<dataLen){
asciiValue = stringData[index].charCodeAt(0);
console.log(asciiValue)
if(asciiValue <0 && asciiValue > 255){
asciiValue=32;
}
index++;
}
data[i] = asciiValue;
data[i+1] = asciiValue;
data[i+2] = asciiValue;
data[i+3] = 255;
}
ctx.putImageData(imgData, 0, 0);
var out = fs.createWriteStream(__dirname + '/text.png'),
stream = canvas.pngStream();
stream.on('data', function(chunk){
out.write(chunk);
});
stream.on('end', function(){
console.log('saved png');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment