Created
January 1, 2013 18:24
-
-
Save rossta/4429143 to your computer and use it in GitHub Desktop.
Balls with wall collision; 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 screenW = 960, | |
| screenH = 500, | |
| z = d3.scale.category20b(), | |
| i = 0; | |
| var balls = []; | |
| var svg = d3.select('body').append('svg:svg') | |
| .attr("width", screenW) | |
| .attr("height", screenH) | |
| .style("pointer-events", "all") | |
| .on("click", addBall) | |
| svg.selectAll("circle").data(balls); | |
| d3.timer(rollBalls); | |
| function Ball(attrs) { | |
| this.radius = 25; | |
| this.position = attrs.position; | |
| this.velocity = attrs.velocity; | |
| this.color = attrs.color || '#FFF'; | |
| } | |
| Ball.prototype.x = function() { return this.position.x; }; | |
| Ball.prototype.y = function() { return this.position.y; }; | |
| Ball.prototype.moveOneStep = function() { | |
| var boundary = this.boundary(), | |
| x = this.position.x + this.velocity.x, | |
| y = this.position.y + this.velocity.y; | |
| console.log("x", x, "y", y, "boundary", boundary); | |
| if (x < boundary.min.x) { | |
| this.reflect("x"); | |
| x = boundary.min.x; | |
| } else if (x > boundary.max.x) { | |
| this.reflect("x"); | |
| x = boundary.max.x; | |
| } | |
| if (y < boundary.min.y) { | |
| this.reflect("y"); | |
| y = boundary.min.y; | |
| } else if (y > boundary.max.y) { | |
| this.reflect("y"); | |
| y = boundary.max.y | |
| } | |
| this.position.x = x; | |
| this.position.y = y; | |
| }; | |
| Ball.prototype.reflect = function(xOrY) { | |
| this.velocity[xOrY] = -this.velocity[xOrY]; | |
| }; | |
| Ball.prototype.boundary = function() { | |
| this._boundary || (this._boundary = { | |
| min: { | |
| x: 0 + this.radius, | |
| y: 0 + this.radius | |
| }, | |
| max: { | |
| x: screenW - this.radius, | |
| y: screenH - this.radius | |
| } | |
| }); | |
| return this._boundary; | |
| }; | |
| function addBall() { | |
| var mouse = d3.mouse(this), | |
| ball = new Ball({ | |
| position: { | |
| x: mouse[0], | |
| y: mouse[1] | |
| }, | |
| velocity: { | |
| x: 10, | |
| y: -10 | |
| }, | |
| 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); | |
| } | |
| function rollBalls() { | |
| balls.forEach(function(ball) { | |
| ball.moveOneStep(); | |
| }); | |
| svg.selectAll("circle") | |
| .attr("cx", function(ball) { return ball.x() }) | |
| .attr("cy", function(ball) { return ball.y() }); | |
| } | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment