Вот как я бы изменил ваш код.Основные изменения:
- Не вызывайте
drawMap
настолько быстро, насколько это возможно, вместо этого увеличивайте скорость до 30 кадров в секунду (и на практике немного меньше). - Пересчитайте смещения xy, пропуская нулитолько однажды.Это значительно сокращает количество итераций, которые вы проходите в
drawMap
.(~ 970 против ~ 2700) - Используйте тот факт, что
drawMap
является закрытием для непосредственного изменения red
без необходимости возвращать / назначать его. - Удалите жесткое кодирование
12
, вместо того, чтобы позволить вам установить squareSize
в одном месте и соответственно обновить код и холст. - Измените смещение для каждого кадра на
5
вместо 1
, чтобы учесть более низкое значениекадров в секунду.В действительности, лучшим выбором будет указать переменную redPerSecond и вычислить межкадровую задержку между вызовами на drawMap
, чтобы цветовая пульсация происходила с одинаковой мировой скоростью независимо от частоты кадров. - Удалить все десятичные дробииз вашего массива карты для более короткого кода и внесите разумные разрывы строк, чтобы мой текстовый редактор не плакал.
- Нет необходимости устанавливать
strokeStyle
на одно и то же значение при каждом вызове drawMap()
.
var map = [
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,21,22,0,0,25,26,27,28,29,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,18,19,20,21,22,23,24,25,26,27,28,29,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,12,13,14,15,16,17,18,19,0,0,22,23,24,25,26,27,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,44,45,46,47,48,49,50,0,0,53,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,0,22,23,24,25,26,0,0,0,0,0,0,33,34,35,36,37,0,0,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,0,0,0,0,0,0,0,0,0,0,0],
[0,0,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,0,0,22,23,0,0,0,27,28,0,0,0,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,0,0,0,0,0,0,0,0,0],
[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,0,0,0,0,0,0,0,0,0,0,0,0,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,0,0,0,0,0,0,0,0,0,0,0],
[0,1,2,0,0,0,6,7,8,9,10,11,12,13,14,15,16,17,18,19,0,0,0,0,0,0,0,0,0,0,30,0,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,0,0,0,60,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,6,7,8,9,10,11,12,13,14,15,16,17,18,19,0,0,0,0,0,0,0,0,0,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,0,0,60,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,6,7,8,9,10,11,12,13,14,15,16,17,18,19,0,0,0,0,0,0,0,0,0,0,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,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,5,6,7,8,9,10,11,12,13,14,15,16,17,18,0,0,0,0,0,0,0,0,0,0,0,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,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,5,6,7,8,9,10,11,12,13,14,15,16,0,0,0,0,0,0,0,0,0,0,0,0,29,30,0,0,0,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,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,5,6,7,8,9,10,11,12,13,14,0,0,0,0,0,0,0,0,0,0,0,0,0,0,29,0,0,0,33,34,35,0,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,5,6,7,8,9,10,11,12,13,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,29,30,31,32,0,0,0,0,0,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,5,6,7,8,9,10,11,12,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,6,7,8,9,0,0,0,13,0,0,0,0,0,0,0,0,0,0,0,0,0,27,28,29,30,31,32,33,34,35,36,37,0,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,6,7,8,0,0,0,12,13,14,0,0,0,0,0,0,0,0,0,0,0,0,27,28,29,30,31,32,33,34,35,36,37,0,39,40,41,42,0,0,0,46,47,48,49,50,51,52,53,54,55,56,57,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,7,8,0,10,11,0,13,14,15,16,0,0,0,0,0,0,0,0,0,0,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,0,0,46,47,48,49,0,51,52,53,54,0,0,57,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,8,9,10,11,0,0,0,0,0,17,0,0,0,0,0,0,0,25,0,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,0,0,0,0,0,47,48,0,0,51,52,53,54,0,0,57,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,11,0,0,14,0,0,17,0,0,0,0,0,0,0,0,0,27,28,29,30,31,32,33,34,35,36,37,38,39,40,0,0,0,0,0,0,47,0,0,0,0,52,53,54,0,0,57,58,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,12,13,14,15,16,17,0,0,0,0,0,0,0,0,0,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,0,0,0,0,0,0,48,0,0,0,52,0,0,0,0,57,58,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,13,14,15,16,17,18,0,0,0,0,0,0,0,0,0,28,29,30,31,32,33,34,35,36,37,38,39,40,41,0,0,0,0,46,0,0,0,0,0,52,53,0,0,56,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,12,13,14,15,16,17,18,0,0,0,0,0,0,0,0,0,0,0,0,0,32,33,34,35,36,37,38,39,40,0,0,0,0,0,0,0,0,0,0,0,0,53,0,55,56,57,58,59,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,12,13,14,15,16,17,18,19,20,0,0,0,0,0,0,0,0,0,0,0,0,33,34,35,36,37,38,39,0,0,0,0,0,0,0,0,0,0,0,0,0,53,54,55,56,57,0,59,60,61,62,0,64,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,12,13,14,15,16,17,18,19,20,21,22,0,0,0,0,0,0,0,0,0,0,33,34,35,36,37,38,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,54,55,0,0,0,0,0,61,62,63,64,65,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,13,14,15,16,17,18,19,20,21,22,0,0,0,0,0,0,0,0,0,0,33,34,35,36,37,38,39,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,57,58,0,0,0,0,63,64,0,66,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,13,14,15,16,17,18,19,20,21,0,0,0,0,0,0,0,0,0,0,0,33,34,35,36,37,38,39,0,41,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,59,60,0,62,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,14,15,16,17,18,19,20,21,0,0,0,0,0,0,0,0,0,0,0,33,34,35,36,37,38,0,40,41,0,0,0,0,0,0,0,0,0,0,0,0,0,55,56,57,58,59,60,61,62,0,0,0,0,67,0,69,0,71],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,16,17,18,19,20,21,0,0,0,0,0,0,0,0,0,0,0,33,34,35,36,37,38,0,40,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,56,57,58,59,60,61,62,63,0,0,66,0,0,0,0,71],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,16,17,18,19,20,0,0,0,0,0,0,0,0,0,0,0,0,33,34,35,36,37,0,0,40,0,0,0,0,0,0,0,0,0,0,0,0,0,0,55,56,57,58,59,60,61,62,63,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,16,17,18,19,0,0,0,0,0,0,0,0,0,0,0,0,0,0,34,35,36,37,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,55,56,57,58,59,60,61,62,63,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,16,17,18,19,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,35,36,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,55,56,57,58,59,60,61,62,63,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,16,17,18,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,60,61,62,0,0,0,0,67,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,16,17,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,60,61,0,0,0,0,66,67,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,16,17,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,60,0,0,0,0,65,66,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,16,17,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,64,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,16,17,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,17,18,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
];
var squareSize = 12;
var nonZeroIndices = [];
var rows = map.length, cols=map[0].length;
for (var row=0; row<rows; ++row){
for (var col=0; col<cols; ++col){
if (map[row][col]) nonZeroIndices.push( [col*squareSize,row*squareSize] );
}
}
var nonZeroes = nonZeroIndices.length;
window.onload = function(){
var layer1 = document.getElementById("backdrop");
var ctx1 = layer1.getContext('2d');
var red=255, offset=5;
var w = layer1.width = map[0].length * squareSize;
var h = layer1.height = map.length * squareSize;
ctx1.strokeStyle = '#000';
var drawMap = function(){
ctx1.clearRect(0,0,w,h);
red -= offset;
ctx1.fillStyle = 'rgb('+red+',0,0)';
for (var i=0; i<nonZeroes; ++i){
var xy = nonZeroIndices[i];
ctx1.fillRect(xy[0],xy[1],squareSize,squareSize);
ctx1.strokeRect(xy[0],xy[1],squareSize,squareSize);
}
if (red<0 || red>255) offset *= -1;
};
setInterval( drawMap, 1000/30 );
};
<div id="canvasesdiv" style="position:relative; width:864px; height:456px">
<canvas id="world-map" width="864" height="456" style="position:absolute; left:0px; top:0px; z-index:1;"></canvas>
<canvas id="backdrop" width="864" height="456" style="position:absolute; left:0px; top:0px; z-index:0;"></canvas>
</div>