instant-meme
Advanced tools
Comparing version 2.1.0 to 2.2.0
const { createCanvas, loadImage, registerFont } = require('canvas') | ||
/** writes tet to a context, wrapping at a maxwidth. | ||
* | ||
* @param {CanvasRenderingContext2D} context Canvase contex to be written to. | ||
* @param {String} text The text to be written | ||
* @param {Number} x starting position of the text | ||
* @param {Number} y starting position of the text | ||
* @param {Number} maxwidth | ||
* @param {Number} fontHeight | ||
*/ | ||
function wraptext(context, text, x, y, maxwidth,fontHeight) { | ||
fontHeight = Number(fontHeight.replace('px','')); | ||
const lines = ['']; | ||
const words = text.split(' '); | ||
//create the lines | ||
for(let word of words) { | ||
const lastLine = lines[lines.length-1] || ''; | ||
const length_lastline = context.measureText(lastLine.trim()).width; | ||
const length_word = context.measureText(word.trim()).width; | ||
if(length_lastline + length_word <= maxwidth) lines[lines.length-1] = lastLine +' '+ word; | ||
else lines[lines.length] = word; | ||
} | ||
//write the lines | ||
for(let index in lines){ | ||
let line = lines[index].trim(); | ||
[x,y,fontHeight, index] = [x,y,fontHeight, index].map(n => Number(n)); | ||
context.fillText(line,x,(y+(fontHeight*index)),maxwidth); | ||
} | ||
} | ||
/** | ||
@@ -17,4 +46,6 @@ * | ||
for(textData of Object.values(templateData.texts)) { | ||
const {text='',x=100,y=100,h=null,w=null} = textData; | ||
ctx.fillText(text,x,y); | ||
maxwidth_default = textData.x ? image.naturalWidth - textData.x : image.naturalWidth - 100; | ||
const {text='',x=100,y=100,h=null,w=null,maxwidth=maxwidth_default} = textData; | ||
wraptext(ctx,text,x,y,maxwidth,templateData.font_size); | ||
// ctx.fillText(text,x,y,maxwidth); | ||
} | ||
@@ -21,0 +52,0 @@ return canvas.toBuffer(); |
{ | ||
"name": "instant-meme", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"description": "A express middleware to build 'memes' on the fly by specifying the image and text content in the url. Because photoshop is to slow for conversation.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
8829
115