Created
October 15, 2013 05:11
-
-
Save kmels/6986793 to your computer and use it in GitHub Desktop.
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
| canvas = document.getElementById("canvas"); | |
| field = document.getElementById("field"); | |
| robot = document.getElementById("robot"); | |
| goal = document.getElementById("goal"); | |
| ctx = canvas.getContext("2d"); | |
| mkImagePattern = (img) -> ctx.createPattern(img, 'repeat'); | |
| # fills canvas with a field pattern | |
| paintField = () -> | |
| imagePattern = mkImagePattern(field) | |
| ctx.rect(0, 0, canvas.width, canvas.height) | |
| ctx.fillStyle = imagePattern | |
| ctx.fill(); | |
| # draws the robot robot with a certain degree of rotation | |
| drawRobot = (degrees) -> | |
| image = robot | |
| ctx.save(); | |
| ctx.translate(canvas.width/2,canvas.height/2); | |
| ctx.rotate(degrees*Math.PI/180); | |
| #ctx.drawImage(robot,-robot.width/2,-robot.width/2); | |
| ctx.drawImage(robot,0,0); | |
| ctx.restore(); | |
| robot.angles = 0 | |
| paint = () -> | |
| paintField() | |
| ctx.drawImage(goal,250,250) | |
| robot.position = {x: canvas.width/2, y: canvas.height/2} | |
| drawRobot(robot.angles); | |
| robot.angles += 1 | |
| if (robot.angles >= 360) | |
| robot.angles = 0 | |
| moveRobotTo = (pos) -> | |
| robot.target = pos | |
| callbackHandler = undefined | |
| ### | |
| # Optimize the repaint frequency | |
| # See http://www.w3.org/TR/animation-timing/ | |
| ### | |
| requestID = 0 | |
| # for compatibility | |
| requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || | |
| window.webkitRequestAnimationFrame || window.msRequestAnimationFrame; | |
| cancelAnimationFrame = window.cancelAnimationFrame || window.mozCancelAnimationFrame; | |
| start = () -> | |
| repaint = (timestamp) -> | |
| paint() | |
| requestID = requestAnimationFrame(repaint); | |
| requestID = requestAnimationFrame(repaint); | |
| stop = () -> | |
| if (requestID) | |
| cancelAnimationFrame(requestID); | |
| requestId = 0; | |
| start() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment