block by vlandham c553c451c49f59261b68

radial

Full Screen

Built with blockbuilder.org

index.html

<!DOCTYPE html>
<head>
  <meta charset="utf-8">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
  <script src="./d3-radial.js"></script>
  <style>
    body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
    svg { width:100%; height: 100% }
  </style>
</head>

<body>
  <script>
    var pos = radial();
    // Feel free to change or delete any of the code you see!
    var svg = d3.select("body").append("svg")
    svg.append("rect")
      .attr({x: 100, y: 10, width: 700, height: 480})
      .style({ fill: "#a72d1a"})
      .transition().duration(3000).ease("bounce")
      .style({ fill: "#5db9e3"})

    console.log("you are now rocking with d3", d3);
  </script>
</body>

d3-radial.js

(function (global, factory) {
  typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  typeof define === 'function' && define.amd ? define('d3-radial', ['exports'], factory) :
  factory((global.d3_radial = {}));
}(this, function (exports) { 'use strict';

  function radial() {
    var increment = 0;
    var width = 500;
    var height = 500;
    var tapper = 0;
    var center = [0,0];
    var start = -90;

    var current = start;

    var radialLocation = function(center, angle, width, height, tapper) {
      return {"x":(center[0] + (width * Math.cos(angle * Math.PI / 180) - tapper)),
              "y": (center[1] + (height * Math.sin(angle * Math.PI / 180) + tapper))};
    };

    var place = function(obj) {
      var value = radialLocation(center, current, width, height, tapper);

      // now it just adds attributes to the object.
      obj.x = value.x;
      obj.y = value.y;
      obj.angle = current;

      current += increment;
      tapper += increment;
      tapper = Math.min(tapper, 0);
      return value;
    };

    var placement = function(objs) {
      increment = 360 / objs.length;

      objs.forEach(function(obj) {
        place(obj);
      });
    };

    placement.center = function(_) {
      if (!arguments.length) {
        return center;
      }
      center = _;
      return placement;
    };

    placement.width = function(_) {
      if (!arguments.length) {
        return width;
      }

      width = _;
      return placement;
    };

    placement.height = function(_) {
      if (!arguments.length) {
        return height;
      }

      height = _;
      return placement;
    };

    placement.start = function(_) {
      if (!arguments.length) {
        return start;
      }
      start = _;
      return placement;
    };

    return placement;
  }

  var version = "0.0.1";

  exports.version = version;
  exports.radial = radial;

}));