Skip to content

Instantly share code, notes, and snippets.

@mooyoul
Created August 29, 2017 16:48
Show Gist options
  • Save mooyoul/4817be417dd7c0d8cfec6ec88e3088c3 to your computer and use it in GitHub Desktop.
Save mooyoul/4817be417dd7c0d8cfec6ec88e3088c3 to your computer and use it in GitHub Desktop.
Draw text in Node.js
'use strict';
const fs = require('fs');
const Canvas = require('canvas');
const width = 600;
const height = 300;
const canvas = new Canvas(width, height); // creates 400px * 200px size canvas
// draw
const ctx = canvas.getContext('2d'); // get context to draw
// fill whole background
ctx.fillStyle = "#fff";
ctx.fillRect(0, 0, width, height);
// draw text
ctx.font = '48px italic "Apple SD Gothic Neo"'; // sets font size/style
ctx.fillStyle = '#333'; // sets font color
ctx.rotate(0.3); // rotates text, please note that rotate is not required for drawing text. this is just for demo purpose :)
ctx.fillText('태사기님 안녕하세요!', 40, 60); // draws text at point (40px - X axis, 60px - Y axis)
// save image
fs.writeFile('text.png', canvas.toBuffer(), (e) => {
if (e) {
// handle error at here
console.error('oops, something went wrong!', e.stack);
return;
}
console.log('done!');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment