Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Branch: master
Fetching contributors…

Cannot retrieve contributors at this time

87 lines (63 sloc) 2.199 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Example 12 - (Capturing click events and translating them to matrix coordinates)</title>
<script>
window.onload = function () {
var grid = {
width: 10,
height: 10
}
var canvas = document.getElementById('myCanvas');
var c = canvas.getContext('2d');
var tileMap = [];
var tile = new Image();
tile.src = "../img/tile.png";
var dirt = new Image();
dirt.src = "../img/dirt.png";
canvas.addEventListener('mousedown', handleMouseDown, false);
draw();
function handleMouseDown(e) {
var gridOffsetY = 0;
var gridOffsetX = 0;
// Take into account the offset on the X axis caused by centering the grid horizontally
gridOffsetX += (canvas.width / 2) - (tile.width / 2);
var col = (e.clientY - gridOffsetY) * 2;
col = ((gridOffsetX + col) - e.clientX) / 2;
var row = ((e.clientX + col) - tile.height) - gridOffsetX;
row = Math.round(row / tile.height);
col = Math.round(col / tile.height);
// Check the boundaries!
if (row >= 0 &&
col >= 0 &&
row <= grid.width &&
col <= grid.height) {
tileMap[row] = (tileMap[row] === undefined) ? [] : tileMap[row];
tileMap[row][col] = 1;
draw();
}
}
function draw() {
c.clearRect (0, 0, canvas.width, canvas.height);
for (var row = 0; row < grid.width; row++) {
for (var col = 0; col < grid.height; col++) {
var tilePositionX = (row - col) * tile.height;
// Center the grid horizontally
tilePositionX += (canvas.width / 2) - (tile.width / 2);
var tilePositionY = (row + col) * (tile.height / 2);
if (tileMap[row] != null && tileMap[row][col] != null) {
c.drawImage(dirt, Math.round(tilePositionX), Math.round(tilePositionY), dirt.width, dirt.height);
} else {
c.drawImage(tile, Math.round(tilePositionX), Math.round(tilePositionY), tile.width, tile.height);
}
}
}
}
}
</script>
</head>
<body>
<canvas id="myCanvas" width="600" height="300"></canvas>
</body>
</html>
Jump to Line
Something went wrong with that request. Please try again.