Skip to content

Instantly share code, notes, and snippets.

@mariotacke
Last active May 20, 2019 02:01
Show Gist options
  • Select an option

  • Save mariotacke/e652c124902edd41250085e6f96e8cf2 to your computer and use it in GitHub Desktop.

Select an option

Save mariotacke/e652c124902edd41250085e6f96e8cf2 to your computer and use it in GitHub Desktop.

Revisions

  1. mariotacke revised this gist May 20, 2019. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion index.js
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,8 @@ async function init () {
    layout.start();

    function draw (words) {
    d3.select("body").append("svg")
    d3.select("body")
    .append("svg")
    .attr("width", layout.size()[0])
    .attr("height", layout.size()[1])
    .append("g")
  2. mariotacke renamed this gist May 20, 2019. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. mariotacke created this gist May 20, 2019.
    33 changes: 33 additions & 0 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    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; });
    }
    }