Skip to content

Instantly share code, notes, and snippets.

@hisasann
Created October 24, 2018 07:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hisasann/a36714168e60a6528d3bbf2133d19ad9 to your computer and use it in GitHub Desktop.
Save hisasann/a36714168e60a6528d3bbf2133d19ad9 to your computer and use it in GitHub Desktop.
const { registerFont, createCanvas, loadImage } = require('canvas');
const fs = require('fs')
registerFont('./fonts/sazanami/sazanami-gothic.ttf', {family: 'Sazanami Gothic'});
const canvas = createCanvas(500, 500);
const ctx = canvas.getContext('2d');
// Draw cat with lime helmet
loadImage('images/lemon-sour.jpg').then((image) => {
ctx.drawImage(image, 0, 0, 500, 500);
// Write "Awesome!"
ctx.font = '50px "Sazanami Gothic"';
// ctx.rotate(0.1);
ctx.fillText('レモンサワー!', 100, 100);
// Draw line under text
var text = ctx.measureText('Awesome!');
ctx.strokeStyle = 'rgba(0,0,0,0.9)';
ctx.beginPath();
ctx.lineTo(100, 102);
ctx.lineTo(100 + text.width + 110, 102);
ctx.stroke();
// console.log('<img src="' + canvas.toDataURL() + '" />');
const out = fs.createWriteStream(__dirname + '/dist/generate-image.jpeg');
const stream = canvas.createJPEGStream();
stream.pipe(out);
out.on('finish', () => console.log('The JPEG file was created.'));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment