This file contains 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
// the game's canvas element | |
var canvas = null; | |
// the canvas 2d context | |
var ctx = null; | |
// an image containing all sprites | |
var spritesheet = null; | |
// true when the spritesheet has been downloaded | |
var spritesheetLoaded = false; |
This file contains 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> | |
<title>A-STAR Pathfinding for HTML5 Canvas by @McFunkypants</title> | |
<style> | |
body { padding:0; margin:0; height:100%; width:100%; } | |
div { font-family:arial; font-size:16px; font-weight:bold; text-align:center; } | |
</style> | |
<script type='text/javascript' src='astar-pathfinding-canvas.js'></script> | |
</head> |
This file contains 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
// the game's canvas element | |
var canvas = null; | |
// the canvas 2d context | |
var ctx = null; | |
// an image containing all sprites | |
var spritesheet = null; | |
// true when the spritesheet has been downloaded | |
var spritesheetLoaded = false; | |
// the world grid: a 2d array of tiles |
This file contains 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
// the html page is ready | |
function onload() | |
{ | |
console.log('Page loaded.'); | |
canvas = document.getElementById('gameCanvas'); | |
canvas.width = worldWidth * tileWidth; | |
canvas.height = worldHeight * tileHeight; | |
canvas.addEventListener("click", canvasClick, false); | |
ctx = canvas.getContext("2d"); | |
spritesheet = new Image(); |
This file contains 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
// the spritesheet is ready | |
function loaded() | |
{ | |
console.log('Spritesheet loaded.'); | |
spritesheetLoaded = true; | |
createWorld(); | |
} | |
// fill the world with walls | |
function createWorld() |
This file contains 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
function redraw() | |
{ | |
if (!spritesheetLoaded) return; | |
console.log('redrawing...'); | |
var spriteNum = 0; | |
// clear the screen | |
ctx.fillStyle = '#000000'; |
This file contains 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
// handle click events on the canvas | |
function canvasClick(e) | |
{ | |
var x; | |
var y; | |
// grab html page coords | |
if (e.pageX != undefined && e.pageY != undefined) | |
{ | |
x = e.pageX; |
This file contains 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
// handle click events on the canvas | |
function canvasClick(e) | |
{ | |
var x; | |
var y; | |
// grab html page coords | |
if (e.pageX != undefined && e.pageY != undefined) | |
{ | |
x = e.pageX; |
This file contains 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
// handle click events on the canvas | |
function canvasClick(e) | |
{ | |
var x; | |
var y; | |
// grab html page coords | |
if (e.pageX != undefined && e.pageY != undefined) | |
{ | |
x = e.pageX; |
This file contains 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
// world is a 2d array of integers (eg world[10][15] = 0) | |
// pathStart and pathEnd are arrays like [5,10] | |
function findPath(world, pathStart, pathEnd) | |
{ | |
// shortcuts for speed | |
var21abs = Math.abs; | |
var max = Math.max; | |
var pow = Math.pow; | |
var sqrt = Math.sqrt; | |
OlderNewer