I'm using the Sharp NPM library with Node.JS and I'm trying to add text to my canvas. I found out here that I need to use .overlayWith()
, along with another library that can convert text to an SVG. A comment there suggested to use text-to-svg or vectorize-text, but both of those, along with text2svg, return an SVG. Sharp's .overlayWith()
function requires an image Buffer. How can I convert the SVG that these libraries return, into an image Buffer for Sharp?
Asked
Active
Viewed 7,514 times
2

APixel Visuals
- 1,508
- 4
- 20
- 38
-
Please not if you're on Windows the libraries sharp uses aren't thread safe, so sometimes you get little squares instead of text. Still trying to figure out a workaround. – Simon_Weaver Jun 27 '18 at 00:55
2 Answers
0
Found out the solution: The NPM module SVG2IMG takes an SVG string and converts it into an image Buffer.

APixel Visuals
- 1,508
- 4
- 20
- 38
-
1Please provide sample code! I've spent two days on this and all I get it little squares right now :-( – Simon_Weaver Jun 26 '18 at 23:42
0
Maybe you can have a try to build your all svg context via svg.js,
var draw = SVG(document.documentElement).size(width, height);
draw.text(text).font({
family: 'tAsset.fontFamily',
size: 'fontHeight',
leading: '1.2em',
anchor: "middle"
});
return draw.svg();
then use returned value in the sharp with creating new Buffer(svg).
sharp(buffer).overlayWith(new Buffer(svg), {
top: model.top,
left: model.left
}).toBuffer(function (error, data, info) {});

ns16
- 1,322
- 2
- 17
- 26

erhanasikoglu
- 1,685
- 1
- 21
- 33