Skip to content

Instantly share code, notes, and snippets.

@linkgod
Created September 11, 2013 03:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save linkgod/6519156 to your computer and use it in GitHub Desktop.
Save linkgod/6519156 to your computer and use it in GitHub Desktop.
// Converts a given word cloud to image/png.
function downloadPNG() {
var canvas = document.createElement("canvas"),
c = canvas.getContext("2d");
canvas.width = w;
canvas.height = h;
c.translate(w >> 1, h >> 1);
c.scale(scale, scale);
words.forEach(function(word, i) {
c.save();
c.translate(word.x, word.y);
c.rotate(word.rotate * Math.PI / 180);
c.textAlign = "center";
c.fillStyle = fill(word.text.toLowerCase());
c.font = word.size + "px " + word.font;
c.fillText(word.text, 0, 0);
c.restore();
});
d3.select(this).attr("href", canvas.toDataURL("image/png"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment