Compositing perf improvement

This commit is contained in:
zenoamaro committed 2019-06-30 18:43:57 +02:00
1 parent 584b00228e
commit f9f32e767f
1 file changed
+15 -9
+15 -9
View File
@@ -22,18 +22,20 @@ let trajectories = [];
let idle = false; let idle = false;
let winner; let winner;
// Music
// const music = createAudioLoop('assets/battle.mp3');
// Init layers // Init layers
const sky = createSky(W, H); const sky = createSky(W, H);
const traces = createCanvas(W, H); const traces = createCanvas(W, H);
const terrain = createTerrain(W, H); const terrain = createTerrain(W, H);
const foreground = createCanvas(W, H); const foreground = createCanvas(W, H);
// const music = createAudioLoop('assets/battle.mp3');
for (let c of [sky, traces, terrain, foreground]) { // Composited layer
c.canvas.style.width = `${W * Z}px`; const framebuffer = createCanvas(W, H);
c.canvas.style.height = `${H * Z}px`; framebuffer.canvas.style.width = `${W * Z}px`;
document.body.appendChild(c.canvas); framebuffer.canvas.style.height = `${H * Z}px`;
} document.body.appendChild(framebuffer.canvas);
// Init players // Init players
let i=0; let i=0;
@@ -358,6 +360,7 @@ function isTank(x, y) {
function draw() { function draw() {
if (idle && particles.length===0) return; if (idle && particles.length===0) return;
foreground.clearRect(0, 0, W, H); foreground.clearRect(0, 0, W, H);
drawTrajectories(); drawTrajectories();
drawPlayers(); drawPlayers();
@@ -365,6 +368,11 @@ function draw() {
drawExplosions(); drawExplosions();
drawParticles(); drawParticles();
drawStatus(); drawStatus();
for (let c of [sky, traces, terrain, foreground]) {
framebuffer.drawImage(c.canvas, 0, 0);
}
drawScreenShake(); drawScreenShake();
} }
@@ -439,9 +447,7 @@ function drawParticles() {
function drawScreenShake() { function drawScreenShake() {
const x = randomInt(-screenShake, screenShake); const x = randomInt(-screenShake, screenShake);
const y = randomInt(-screenShake, screenShake); const y = randomInt(-screenShake, screenShake);
for (let c of [sky, terrain, traces, foreground]) { framebuffer.canvas.style.transform = `translate(${x}px, ${y}px)`;
c.canvas.style.transform = `translate(${x}px, ${y}px)`;
}
} }
function drawStatus() { function drawStatus() {