-
Notifications
You must be signed in to change notification settings - Fork 8
/
infinite-canvas.js
235 lines (197 loc) · 9.27 KB
/
infinite-canvas.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
(function () {
// Chunksize might not be optimal. It has been found that really small chunk sizes
// have a (very) negative impact on performance, other than that, not much experimentation
// has been done with the chunksizes.
var configuration = {
chunkWidth: 500,
chunkHeight: 500
};
function initializeWorld(ctx) {
var offscreenRenderCanvas = document.createElement("canvas");
var offscreenRenderCtx = offscreenRenderCanvas.getContext("2d");
offscreenRenderCanvas.width = configuration.chunkWidth;
offscreenRenderCanvas.height = configuration.chunkHeight;
var canvas = ctx.canvas;
var infinity = {
position: {
x: 0,
y: 0
},
// stores ImageData per chunk, used to draw on
chunks: {},
canvas: canvas,
ctx: ctx,
configuration: configuration,
debugMode: false
};
function constructChunkKey(x, y) {
// chunk keys look like "0, 1"
return x.toString() + ", " + y.toString();
}
function parseChunkKey(key) {
var split = key.split(", ");
return {
x: parseInt(split[0]),
y: parseInt(split[1])
};
}
function worldCoordToChunkCoord(x, y) {
// Takes a position in the world
// returns the coordinate of the grid this position lies in
return {
x: Math.floor(x / configuration.chunkWidth),
y: Math.floor(y / configuration.chunkHeight)
};
}
function chunkCoordToWorldCoord(x, y) {
return {
x: x * configuration.chunkWidth,
y: y * configuration.chunkHeight
};
}
function chunkCoordToRenderCoord(x, y) {
// Takes a chunk position
// returns corresponding (x, y) coordinates in the viewport
return {
x: (x * configuration.chunkWidth) - infinity.position.x,
y: (y * configuration.chunkHeight) - infinity.position.y
};
}
function getChunk(chunkId) {
// we serialize the coordinate of a chunk with a key, computed from its x and y coordinates
// say we have a chunk at {x: 1, y: 3}, then our chunks dict looks like
// {
// ...
// "1, 3": ..chunkData
// ...
// }
// if the chunk doesn't exist, create it!
if (! infinity.chunks[chunkId]) {
infinity.chunks[chunkId] = new Image(configuration.chunkWidth, configuration.chunkHeight);
}
// now that we're sure that it exists, return the motherfucker <3
return infinity.chunks[chunkId];
}
function renderChunks(chunks) {
Object.keys(chunks).forEach(function (key) {
var coord = parseChunkKey(key);
var renderCoordinate = chunkCoordToRenderCoord(coord.x, coord.y);
try {
// drawImage can fail with <img> tags on firefox, this is a confirmed bug.
// see http://stackoverflow.com/a/18580878 for more details
ctx.drawImage(infinity.chunks[key], renderCoordinate.x, renderCoordinate.y);
} catch (error) {
if (error.name == "NS_ERROR_NOT_AVAILABLE") {
// failed! Doesn't matter much for rendering, it will be rerendered the next frame
} else {
throw error;
}
}
});
}
function getChunksInViewport() {
var chunksInViewport = {};
// we need to figure out what chunks are in the viewport
// what we need to do is find the coordinates of the chunks in the four corners
// and then retrieve those chunks, as well as all chunks inbetween those corners
var topLeft = worldCoordToChunkCoord(infinity.position.x, infinity.position.y);
var bottomRight = worldCoordToChunkCoord(infinity.position.x + canvas.width, infinity.position.y + canvas.height);
var chunksOnXAxis = Math.abs(topLeft.x - bottomRight.x);
var chunksOnYAxis = Math.abs(topLeft.y - bottomRight.y);
// <= instead of < because we definitely need to include the outer layer of chunks as well!
for (var x = 0; x <= chunksOnXAxis; x++) {
for (var y = 0; y <= chunksOnYAxis; y++) {
var chunkKey = constructChunkKey(topLeft.x + x, topLeft.y + y);
chunksInViewport[chunkKey] = getChunk(chunkKey);
}
}
return chunksInViewport;
}
// =====================================
// API Methods
// =====================================
infinity.updateChunks = function () {
// a way to speed up this method quite significantly is by preventing
// updating chunks that did not change.
// A really simple method would be to pass a bounding box to this function
// only the chunks inside the bounding box will get updated, this will
// remove a large part of the redundancy.
// the bounding box will be generated by the caller
// for example, draw a box around a path drawn with the mouse, and you have a
// bounding box around all edited boxes.
var chunks = getChunksInViewport();
Object.keys(chunks).forEach(function (key) {
var coord = parseChunkKey(key);
var renderCoord = chunkCoordToRenderCoord(coord.x, coord.y);
var chunkWorldCoord = chunkCoordToWorldCoord(coord.x, coord.y);
var chunkSourceCoord = {
x: Math.max(renderCoord.x, 0),
y: Math.max(renderCoord.y, 0)
};
var width = Math.min(renderCoord.x + configuration.chunkWidth , canvas.width) - chunkSourceCoord.x;
var height = Math.min(renderCoord.y + configuration.chunkHeight, canvas.height) - chunkSourceCoord.y;
// don't even bother to update chunks that are not even visible on the canvas! :o
if (width <= 0 || height <= 0) return;
var putLocation = {
x: configuration.chunkWidth - width,
y: configuration.chunkHeight - height
};
if (chunkWorldCoord.x >= infinity.position.x) {
putLocation.x = 0;
}
if (chunkWorldCoord.y >= infinity.position.y) {
putLocation.y = 0;
}
if (chunks[key].src) {
offscreenRenderCtx.drawImage(chunks[key], 0, 0);
}
// ..clear the visible part of the chunk
offscreenRenderCtx.clearRect(putLocation.x, putLocation.y, width, height);
// ..render the contents of the main canvas to the offscreen context
offscreenRenderCtx.drawImage(canvas, chunkSourceCoord.x, chunkSourceCoord.y, width, height, putLocation.x, putLocation.y, width, height);
if (configuration.debugMode) {
offscreenRenderCtx.strokeRect(0, 0, configuration.chunkWidth, configuration.chunkHeight);
offscreenRenderCtx.fillText(key, 15, 15);
}
// ..serialize the offscreen context
infinity.chunks[key].src = offscreenRenderCtx.canvas.toDataURL();
// ..finally, clear up the offscreen context so the contents won't be dupllicated to other chunks later in this loop
offscreenRenderCtx.clearRect(0, 0, configuration.chunkWidth, configuration.chunkHeight);
});
};
infinity.moveBy = function(dx, dy, render) {
// default `render` to true, only skip rendering when it's false
render = render === undefined? true : render;
infinity.position.x += dx;
infinity.position.y += dy;
if (render) {
renderChunks(getChunksInViewport());
}
};
infinity.moveTo = function(x, y, render) {
// default `render` to true, only skip rendering when it's false
render = render === undefined? true : render;
infinity.position.x = x;
infinity.position.y = y;
if (render) {
renderChunks(getChunksInViewport());
}
};
infinity.refresh = function () {
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
infinity.moveBy(0, 0);
};
infinity.getAllChunks = function () {
return infinity.chunks;
};
// expects a chunk-id, ex "2, 3" and an <img></img> with a src
infinity.loadChunk = function (chunkId, chunk) {
infinity.chunks[chunkId] = chunk;
infinity.refresh();
};
return infinity;
}
window.infiniteCanvas = {
initialize: initializeWorld
};
}());