2

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?

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 Answers2

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
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