benchmarks/assorted-dom/explanations/astar.html (64 lines of code) (raw):

<!DOCTYPE html> <html> <head> <meta charset=utf8> <!-- Copyright (C) 2007 Apple Inc. All rights reserved. Copyright (C) 2010 Mozilla Foundation Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. --> <title>Kraken JavaScript Benchmark: A* Search Algorithm</title> <link rel="stylesheet" href="../kraken.css"> <script src="astar.js"></script> <script> function getCanvasContext() { var canvas = document.getElementById('display'); if (canvas.getContext) return canvas.getContext('2d'); } function drawEndpoints(ctx, start, end) { drawCanvasCell(ctx, start.x, start.y, "rgb(256,0,0)"); drawCanvasCell(ctx, end.x, end.y, "rgb(256,256,0)"); } function drawSuccessfulPath(nodeList) { var ctx = getCanvasContext(); for (var i=0; i < nodeList.length; i++) { ctx.fillStyle = "rgb(256,0,0)"; ctx.fillRect((nodeList[i].x * 5) + 1, (nodeList[i].y * 5) + 1, 3, 3); } } function drawCanvas(graphSet) { var ctx = getCanvasContext(); for (var i = 0; i < graphSet.length; i++) { for (var j = 0; j < graphSet.length; j++) { var node = graphSet[i][j]; if (node.isWall()) { drawCanvasCell(ctx, node.x, node.y, "rgb(0,0,0)"); } else { drawCanvasCell(ctx, node.x, node.y, "rgb(50,50,50)"); } } } drawEndpoints(ctx, start, end); } function drawCanvasCell(ctx, x, y, style) { ctx.fillStyle = style; ctx.fillRect(x * 5, y * 5, 5, 5); } function doIt() { var div = document.getElementById("console"); div.innerHTML += go(); div.innerHTML += "<br>"; drawSuccessfulPath(path); } </script> <style> #display { border: 5px solid rgb(0,0,50);}</style> </head> <body onload="drawCanvas(g1);"> <div id="content"> <h2>Kraken JavaScript Benchmark: A* Search Algorithm</h2> <div id="results"> <p>This benchmark uses the <a href="http://en.wikipedia.org/wiki/A*_search_algorithm">A* search algorithm</a> is used to automatically plot an efficient path between two points,<br> in the presence of obstacles. Adapted from code by <a href="http://www.briangrinstead.com/blog/astar-search-algorithm-in-javascript">Brian Gringstead</a>.</p> <p>Below, you can watch the algorithm plot a path through the maze.</p> <canvas id="display" width=500 height=500></canvas> <p><input onclick="drawCanvas(g1); setTimeout(doIt, 10);" type="button" value="Try It!"></p> <div id="console"></div> </div> </div> </body> </html>