我正在尝试使用Fabric JS创建一个效果,使字母看起来像是“绣”在毛衣上,效果如下:
我可以通过使用这个动作在Photoshop中实现此效果。
我打算通过从Photoshop渲染出每个绣花字母的png来将其放置在基于用户输入的画布上。
但是,这种方法将没有正确的字距。
为了解决这个问题,我试图使用相同字体在Fabric中写出文本,然后在要替换的字母上方覆盖每个绣花png(然后隐藏文本本身)。
以下是我如何呈现文本的方式:
window.chest_text = new fabric.IText("NYC", {
fill: '#000',
fontSize: 12,
left: 210,
top: 100,
fontFamily: 'Graphik',
fontWeight: 500,
lineHeight: 1,
originX: 'center',
});
然后这是我如何渲染刺绣字母的方式:
var n_url = 'https://res.cloudinary.com/tricot/image/upload/v1598820746/tmp/n-embroidery-test.png'
var y_url = 'https://res.cloudinary.com/tricot/image/upload/v1598820745/tmp/y-embroidery-test.png'
var c_url = 'https://res.cloudinary.com/tricot/image/upload/v1598820745/tmp/c-embroidery-test.png'
fabric.Image.fromURL(n_url, function(img) {
img.set({
left: Math.round(window.chest_text.aCoords.bl.x),
top: window.chest_text.top
})
img.scaleToHeight(Math.floor(window.chest_text.__charBounds[0][0].height / 1.13), true)
canvas.add(img);
})
fabric.Image.fromURL(y_url, function(img) {
img.set({
left: Math.round(window.chest_text.aCoords.bl.x + window.chest_text.__charBounds[0][1].left),
top: window.chest_text.top
})
img.scaleToHeight(Math.floor(window.chest_text.__charBounds[0][1].height / 1.13), true)
canvas.add(img);
})
fabric.Image.fromURL(c_url, function(img) {
img.set({
left: Math.round(window.chest_text.aCoords.bl.x + window.chest_text.__charBounds[0][2].left),
top: window.chest_text.top
})
img.scaleToHeight(Math.floor(window.chest_text.__charBounds[0][2].height / 1.13), true)
canvas.add(img);
})
window.chest_text.opacity = 0.5
window.canvas.renderAll()
然而,我无法使绣花字母与普通文本完全重叠(即使它们是相同的字体):
我该如何实现这一点?有没有更好的方法让字距正常工作?