Created
November 3, 2011 14:41
-
-
Save scan/1336658 to your computer and use it in GitHub Desktop.
Bouncing 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
$ = jQuery | |
requestAnimFrame = window.requestAnimationFrame or window.mozRequestAnimationFrame or window.webkitRequestAnimationFrame or window.msRequestAnimationFrame or window.oRequestAnimationFrame or (callback) -> window.setTimeout(callback, 1000 / 60) | |
$.fn.axiom = (render) -> | |
$element = $ this | |
$canvas = $("<canvas width=\"#{$element.width()}\" height=\"#{$element.height()}\" />").appendTo(this) | |
screen = $canvas[0].getContext '2d' | |
$doublebuffer = $("<canvas width=\"#{$element.width()}\" height=\"#{$element.height()}\" />")[0] | |
ctx = $doublebuffer.getContext '2d' | |
update = -> | |
requestAnimFrame update | |
render ctx | |
screen.clearRect 0, 0, $canvas.width(), $canvas.height() | |
screen.drawImage $doublebuffer, 0, 0 | |
requestAnimFrame update |
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> | |
<html lang="en"> | |
<head> | |
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script> | |
<script type="text/javascript" src="axiom.js"></script> | |
<script type="text/javascript" src="test.js"></script> | |
<style type="text/css"> | |
#game{width:800px;height:600px;margin:auto;} | |
</style> | |
</head> | |
<body> | |
<div id="game"> | |
</div> | |
</body> | |
</html> |
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
WIDTH = 800 | |
HEIGHT = 600 | |
rand = (m) -> (Math.random() * m) | |
requestAnimFrame = window.requestAnimationFrame or window.mozRequestAnimationFrame or window.webkitRequestAnimationFrame or window.msRequestAnimationFrame or window.oRequestAnimationFrame or (callback) -> window.setTimeout(callback, 1000 / 60) | |
$ -> | |
balls = for i in [0..1000] | |
ball = | |
x: rand WIDTH | |
y: rand HEIGHT | |
vx: rand(3) - 1 | |
vy: rand(3) - 1 | |
$('#game').axiom (ctx) -> | |
ctx.clearRect 0,0,WIDTH,HEIGHT | |
ctx.fillStyle = '#f00' | |
for ball in balls | |
ball.x += ball.vx | |
ball.y += ball.vy | |
ball.vx = -ball.vx if ball.x > WIDTH or ball.x < 0 | |
ball.vy = -ball.vy if ball.y > HEIGHT or ball.y < 0 | |
ctx.beginPath() | |
ctx.arc Math.round(ball.x), Math.round(ball.y), 5, 0, Math.PI*2, true | |
ctx.closePath() | |
ctx.fill() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment