Last active
December 10, 2015 10:58
-
-
Save rossta/4423991 to your computer and use it in GitHub Desktop.
Click to add balls
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]--> | |
| <!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]--> | |
| <!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]--> | |
| <!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]--> | |
| <head> | |
| <meta charset="utf-8"> | |
| <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> | |
| <title></title> | |
| <meta name="description" content=""> | |
| <meta name="viewport" content="width=device-width"> | |
| <style> | |
| body { | |
| background: #222; | |
| } | |
| circle { | |
| fill: none; | |
| stroke-width: 2px; | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <script src="http://d3js.org/d3.v3.min.js"></script> | |
| <script type="text/javascript"> | |
| var w = 960, | |
| h = 500, | |
| z = d3.scale.category20b(), | |
| i = 0; | |
| var balls = []; | |
| var svg = d3.select('body').append('svg:svg') | |
| .attr("width", w) | |
| .attr("height", h) | |
| .style("pointer-events", "all") | |
| .on("click", bounce) | |
| svg.selectAll("circle").data(balls); | |
| function Ball(attrs) { | |
| this.radius = 25; | |
| this.x = attrs.x || this.radius; | |
| this.y = attrs.y || this.radius; | |
| this.color = attrs.color || '#FFF'; | |
| } | |
| function bounce() { | |
| var mouse = d3.mouse(this), | |
| ball = new Ball({ | |
| x: mouse[0], | |
| y: mouse[1], | |
| color: z(++i) | |
| }); | |
| balls.push(ball); | |
| svg.selectAll("circle").data(balls) | |
| .enter() | |
| .append("svg:circle") | |
| .attr("cx", function(ball) { return ball.x }) | |
| .attr("cy", function(ball) { return ball.y }) | |
| .attr("r", function(ball) { return ball.radius }) | |
| .style("stroke", function(ball) { return ball.color }) | |
| .style("stroke-opacity", 1); | |
| } | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment