Skip to content

Instantly share code, notes, and snippets.

@mariotacke
Last active May 20, 2019 02:01
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 mariotacke/e652c124902edd41250085e6f96e8cf2 to your computer and use it in GitHub Desktop.
Save mariotacke/e652c124902edd41250085e6f96e8cf2 to your computer and use it in GitHub Desktop.
blog-real-time-word-cloud
async function init () {
const cloud = await getCloud();
const highestScore = Math.max(...cloud.map((x) => x.size));
var layout = d3.layout.cloud()
.size([500, 500])
.words(cloud)
.rotate(function () { return ~~(Math.random() * 2) * 90; })
.font("Impact")
.fontSize(function (d) { return d.size / highestScore * 90 })
.on("end", draw);
layout.start();
function draw (words) {
d3.select("body")
.append("svg")
.attr("width", layout.size()[0])
.attr("height", layout.size()[1])
.append("g")
.attr("transform", "translate(" + layout.size()[0] / 2 + "," + layout.size()[1] / 2 + ")")
.selectAll("text")
.data(words)
.enter().append("text")
.style("font-size", function (d) { return d.size + "px"; })
.style("font-family", "Impact")
.attr("text-anchor", "middle")
.attr("transform", function (d) {
return "translate(" + [d.x, d.y] + ")rotate(" + d.rotate + ")";
})
.text(function (d) { return d.text; });
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment