2017-08-20 45 views
0

有一些阵这样的数组:乘的drawText从节点-GM

var myArray=[ 
    { title: 'Some title', time: 'Some time'}, 
    ... 
    ]; 

和Node-GM代码:

gm('bg.png') 
    .fill("#ffffff") 
    .font("Phenomena-Bold.otf", 27) 
    .drawText(225, 75, "Some text") 
    .write("result.png", function (err) { 
    if (!err) console.log('done'); 
    }); 

需要的每个项目myArray的增加.drawText()与将y轴移动到45px。 任何想法?

回答

0

你问过了,我写过并测试过它。

取(:!

const 
    gm = require('gm'); 

const myArray = [ 
    { title: 'Some title', time: 'Some time'}, 
    { title: 'Second title', time: 'second time'} 
]; 

const 
    image = gm('bg.jpg') 
      .fill('#ffffff') 
      .font('Arial', 27) // I didn't wanted to play with fonts, so used normal default thing (: 
      .drawText(225, 75, "Some text"); 

let x = 225; 
let y = 75; 
for(const text of myArray) { 
    y += 45; 
    image.drawText(x, y, text.title); 
} 
image.write('result.png', err => { 
    if(err) return console.error(err); 
    console.log('done'); 
}); 

prove of work

+1

感谢很多真正有用的解决方案 –

+0

也许你知道如何改变字体颜色在一个DrawText函数 例如有白色,字前第一个字?与红色: .drawText(10,10,'白色文字,然后红色文字') –

+0

@DanMishin不要懒惰,阅读手册:http://aheckmann.github.io/gm/docs.html它的名称填充,你的文字也是形状,并在绘制东西之前你可以调用'填充' – num8er