jasondavies / d3-cloud

Create word clouds in JavaScript.
https://www.jasondavies.com/wordcloud/
Other
3.82k stars 1.07k forks source link

Cannot get SVG when running from nodejs #138

Closed schollii closed 6 years ago

schollii commented 6 years ago

I am trying to generate a word cloud using d3-cloud from nodejs. Starting from the example/node.js, I believe I have to get the SVG from the canvas.toBuffer(), which also requires creating the canvas with third argument "svg". But I get a segmentation fault.

I'm able to run the stock example/node.js. I can also add a line in the end() function to save cv.toBuffer() to a file, and it is indeed a PNG with what looks like words.

So I modified example/node.js further to initialize for SVG output:

 14 cloud().size([960, 500])
 15     .canvas(function() { cv = new Canvas(1, 1, "svg"); return cv; })
 16     .words(words)

Now if I run it, there is a segfault. If I run it in debug mode, the segfault occurs when calling start(). If I remove the "svg", there is no segfault (but the toBuffer() then returns PNG instead of SVG).

I verified that the problem is not cairo/canvas support for SVG:

var fs = require("fs")
var Canvas = require("canvas")
var canvas = new Canvas(200,200,"svg")
var ctx = canvas.getContext("2d")
ctx.fillRect(0,0,100,100) //Black square
ctx.save()
ctx.fillStyle = "darkblue"
ctx.fillRect(0,0,100,100) //Blue square
fs.writeFileSync("square1.svg", canvas.toBuffer()) //Saves blue square. Ok

The square1.svg contains xml in SVG format.

Any ideas?

schollii commented 6 years ago

Just want to check that you meant to close this since you did not include a justification why.