Skip to content

Commit d2dfee9

Browse files
committed
putImageData is a bit faster.
1 parent 5cee45b commit d2dfee9

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

tzmap.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -343,13 +343,21 @@
343343
var public_tileFor = function(lats, lons, polygonsArray) {
344344
var height = lats.length;
345345
var width = lons.length;
346+
var colorCanvas = document.createElement("canvas");
347+
colorCanvas.height = 1;
348+
colorCanvas.width = 1;
349+
var colorcx = colorCanvas.getContext("2d");
346350
var canvas = document.createElement("canvas");
347351
canvas.height = height;
348352
canvas.width = width;
349353
var cx = canvas.getContext("2d");
354+
var id = cx.createImageData(width, height);
350355
for (var polygonsIdx in polygonsArray) {
351356
var polygonsObj = polygonsArray[polygonsIdx];
352-
cx.fillStyle = polygonsObj.color;
357+
colorcx.fillStyle = polygonsObj.color;
358+
colorcx.clearRect(0, 0, 1, 1);
359+
colorcx.fillRect(0, 0, 1, 1);
360+
var colorArray = colorcx.getImageData(0, 0, 1, 1).data;
353361
for (var column = 0, column_end = width;
354362
column < column_end; ++column) {
355363
var lon = lons[column];
@@ -441,22 +449,17 @@
441449

442450
// Now actually fill in the pixels.
443451
var drawing = false;
444-
var rectTop = 0;
445452
for (var y = 0; y < height; ++y) {
446453
if (pixels[y]) {
447-
if (drawing) {
448-
cx.fillRect(column, rectTop, 1, y - rectTop);
449-
} else {
450-
rectTop = y;
451-
}
452454
drawing = !drawing;
453455
}
454-
}
455-
if (drawing) {
456-
cx.fillRect(column, rectTop, 1, height - rectTop);
456+
if (drawing) {
457+
id.data.set(colorArray, (y * width + column) * 4);
458+
}
457459
}
458460
}
459461
}
462+
cx.putImageData(id, 0, 0);
460463
return canvas;
461464
}
462465

0 commit comments

Comments
 (0)