Skip to content

Commit

Permalink
Cancel animation when reset is pressed.
Browse files Browse the repository at this point in the history
  • Loading branch information
EkkiD committed Apr 7, 2012
1 parent 7c0dd66 commit ccb973e
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 11 deletions.
2 changes: 1 addition & 1 deletion DFS.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Maze.prototype.DFSGenerate = function(){
maze.DrawScreen();
}
if(maze.cellStack.length !== 0){
window.requestAnimFrame(maze.DFSGenerate);
anim_request = window.requestAnimFrame(maze.DFSGenerate);
}
try{
var stacktop = maze.cellStack.pop();
Expand Down
2 changes: 1 addition & 1 deletion Prim.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ Maze.prototype.PrimGenerate = function(){
maze.AddIn(maze.fringes, row, col);
}
if(maze.fringes.length !== 0){
window.requestAnimFrame(maze.PrimGenerate);
anim_request = window.requestAnimFrame(maze.PrimGenerate);
}
else{ if(render_steps){ maze.DrawScreen(); } return; }

Expand Down
8 changes: 0 additions & 8 deletions maze.htm
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,8 @@
</head>
<body>
<div id="header">
<h1>Erick Dransch</h1>
<p>3B Computer Science at the University of Waterloo</p>
<p><a href="https://www.github.com/EkkiD">www.github.com/EkkiD</a></p>
</div>

<div id="menu">
<ul>
<li><a id="current" href="">Main</a></li>
</ul>
</div>
<div id="content">
<canvas id="myCanvas" width="700" height="600"> </canvas>
<br />
Expand Down
11 changes: 11 additions & 0 deletions maze.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ var current = 3;

var render_steps = true;

var anim_request;

window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
Expand All @@ -33,6 +35,9 @@ window.requestAnimFrame = (function(){
};
}());


window.cancelAnimationFrame = window.cancelAnimationFrame || window.mozCancelAnimationFrame;

function Node(row, col) {
this.stat = untouched;
this.walls = north | south | east | west;
Expand Down Expand Up @@ -167,6 +172,12 @@ Maze.prototype.runAlgorithm = function(algorithm){
};

Maze.prototype.clearMaze = function(){
// Cancel any animations that are in process.
if( anim_request ) {
window.cancelAnimationFrame(anim_request);
anim_request = null;
}

this.cellStack = [];
this.solve_start_x = 0;
this.solve_start_y = 0;
Expand Down
2 changes: 1 addition & 1 deletion solve.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Maze.prototype.DFSSolve = function(){
return;
}
else{
window.requestAnimFrame(maze.DFSSolve);
anim_request = window.requestAnimFrame(maze.DFSSolve);
}
var row = stacktop[0];
var col = stacktop[1];
Expand Down

0 comments on commit ccb973e

Please sign in to comment.