Skip to content

Instantly share code, notes, and snippets.

View McFunkypants's full-sized avatar
💭
Making games

Christer Kaitila McFunkypants

💭
Making games
View GitHub Profile
// 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;
// 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;
// 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;
function redraw()
{
if (!spritesheetLoaded) return;
console.log('redrawing...');
var spriteNum = 0;
// clear the screen
ctx.fillStyle = '#000000';
// the spritesheet is ready
function loaded()
{
console.log('Spritesheet loaded.');
spritesheetLoaded = true;
createWorld();
}
// fill the world with walls
function createWorld()
// 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();
// 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
<!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>
// 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;