Skip to content

Instantly share code, notes, and snippets.

@kmels
Created October 15, 2013 05:11
Show Gist options
  • Select an option

  • Save kmels/6986793 to your computer and use it in GitHub Desktop.

Select an option

Save kmels/6986793 to your computer and use it in GitHub Desktop.
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