-
Notifications
You must be signed in to change notification settings - Fork 0
/
tiledCanvas.js
127 lines (117 loc) · 5.38 KB
/
tiledCanvas.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/** Creates a tile-based grid and extends a canvas element with the related methods
* @param {object} canvas - The canvas element object obtained from document.getElementById
* @param {number} tileSize - The size of the tiles of the grid
* @returns {object} - An object containing the methods used to extend the initial canvas object
*/
var TiledCanvas = Object.create({}, {
'extend': {
value: tiledCanvas
}
});
function tiledCanvas(canvas, tileSize) {
// Validate that the canvas parameter is indeed an existing canvas element
if (canvas.nodeName !== 'CANVAS') {
console.log('ERROR: The element provided is not a canvas element.');
return;
}
// Check what parameters were specified, or used the default values
tileSize = tileSize || canvas.tileSize;
var tileSize = (typeof tileSize === 'number') ? tileSize : 1,
context = canvas.getContext('2d');
// Define the canvas object interface
var properties = {
grid: {
value: {
tileSize: tileSize,
rows: Math.floor(canvas.height/tileSize),
columns: Math.floor(canvas.width/tileSize),
top: 0,
left: 0,
right: canvas.width,
bottom: canvas.height,
offset: {
top: 0,
left: 0,
},
draw: function () {
var zoom = 1;
if (canvas.zoom) {
zoom = canvas.zoom.scale;
if (typeof zoom !== 'number' || isNaN(zoom)) {
zoom = 1;
}
};
var tileSize = canvas.grid.tileSize,
offsetTop = canvas.grid.offset.top,
offsetLeft = canvas.grid.offset.left,
top = Math.floor(offsetTop/tileSize),
left = Math.floor(offsetLeft/tileSize),
bottom = Math.floor((offsetTop + canvas.height)/tileSize),
right = Math.floor((offsetLeft + canvas.width)/tileSize);
context.lineWidth = 1;
context.strokeStyle = '#ccc';
for (var i = top, y = Math.ceil((i * tileSize - offsetTop) * zoom) + 0.5;
i <= bottom; i++, y = Math.ceil((i * tileSize - offsetTop) * zoom) + 0.5) {
// Draw horizontal lines
context.beginPath();
context.moveTo(0, y);
context.lineTo(canvas.width, y);
context.stroke();
for (var j = left, x = Math.ceil((j * tileSize - offsetLeft) * zoom) + 0.5;
j <= right; j++, x = Math.ceil((j * tileSize - offsetLeft) * zoom) + 0.5) {
// Draw vertical lines
context.beginPath();
context.moveTo(x, 0);
context.lineTo(x, canvas.height);
context.stroke();
}
}
},
drawTile: function (row, column, color) {
var zoom = 1;
if (canvas.zoom) {
zoom = canvas.zoom.scale;
if (typeof zoom !== 'number' || isNaN(zoom)) {
zoom = 1;
}
};
var tileSize = canvas.grid.tileSize,
offsetTop = canvas.grid.offset.top,
offsetLeft = canvas.grid.offset.left,
x = Math.ceil((column * tileSize - offsetLeft) * zoom) + 1,
y = Math.ceil((row * tileSize - offsetTop) * zoom) + 1;
if (x + tileSize >= 0 && x <= canvas.width &&
y + tileSize >= 0 && y <= canvas.height) {
context.fillStyle = color || '#ccc';
context.beginPath();
context.fillRect(x, y, Math.ceil(tileSize * zoom) - 1, Math.ceil(tileSize * zoom) - 1);
context.stroke();
}
},
getTile: function (x, y, offset) {
return {
row: Math.floor(y/canvas.grid.tileSize),
column: Math.floor(x/canvas.grid.tileSize)
};
},
setSize: function (newWidth, newHeight) {
var width = newWidth || window.innerWidth,
height = newHeight || window.innerHeight;
canvas.grid.right = canvas.grid.left + width;
canvas.grid.bottom = canvas.grid.top + height;
canvas.grid.rows = Math.floor(height/canvas.grid.tileSize);
canvas.grid.columns = Math.floor(width/canvas.grid.tileSize);
}
}
},
setSize: {
writable: true,
value: function (newWidth, newHeight) {
canvas.width = newWidth || window.innerWidth;
canvas.height = newHeight || window.innerHeight;
}
}
}
Object.defineProperties(canvas, properties);
return Object.create({}, properties);
}