Layer compositing

This commit is contained in:
zenoamaro committed 2019-06-26 03:05:28 +02:00
1 parent 54ff6b6472
commit 3a31c8ed63
2 files changed
+16 -17

No files matched your search

+3 -3
View File
@@ -7,9 +7,9 @@
<style> <style>
* {box-sizing: border-box} * {box-sizing: border-box}
html, body {padding: 0; margin: 0} html, body {padding: 0; margin: 0}
body {background: black; height:100vh; overflow: hidden; body {background: black; height:100vh; overflow: hidden}
display:flex; align-items:center; justify-content:center;} canvas {image-rendering: pixelated; margin: auto;
canvas {image-rendering: pixelated} position:absolute; left:0; right:0; top:0; bottom:0}
@font-face { font-family: 'text'; src:url(font.ttf) } @font-face { font-family: 'text'; src:url(font.ttf) }
</style> </style>
</head> </head>
+13 -14
View File
@@ -16,16 +16,17 @@ let prevProjectile = null;
let fadeCount = 0; let fadeCount = 0;
let wind = 0; let wind = 0;
// Init canvas // Init layers
const fb = createCanvas(W, H);
fb.canvas.style.width = `${W * Z}px`;
fb.canvas.style.height = `${H * Z}px`;
document.body.appendChild(fb.canvas);
// Layers
const sky = createSky(W, H); const sky = createSky(W, H);
const terrain = createTerrain(W, H); const terrain = createTerrain(W, H);
const projectiles = createCanvas(W, H); const projectiles = createCanvas(W, H);
const foreground = createCanvas(W, H);
for (let c of [sky, terrain, projectiles, foreground]) {
c.canvas.style.width = `${W * Z}px`;
c.canvas.style.height = `${H * Z}px`;
document.body.appendChild(c.canvas);
}
// Init players // Init players
let i=0; let i=0;
@@ -117,10 +118,8 @@ function update() {
} }
function draw() { function draw() {
fb.drawImage(sky.canvas, 0, 0); foreground.clearRect(0, 0, W, H);
fb.drawImage(terrain.canvas, 0, 0);
if (projectile) drawProjectile(); if (projectile) drawProjectile();
fb.drawImage(projectiles.canvas, 0, 0);
for (let tank of players) drawPlayer(tank); for (let tank of players) drawPlayer(tank);
drawStatus(); drawStatus();
} }
@@ -128,8 +127,8 @@ function draw() {
function drawPlayer(player) { function drawPlayer(player) {
const {x, y, a, c} = player; const {x, y, a, c} = player;
const [px, py] = vec(x, y-3, a+180, 3); const [px, py] = vec(x, y-3, a+180, 3);
drawLine(fb, x, y-3, Math.round(px), Math.round(py), c); drawLine(foreground, x, y-3, Math.round(px), Math.round(py), c);
drawRect(fb, x-3, y-2, 6, 3, c); drawRect(foreground, x-3, y-2, 6, 3, c);
} }
function drawProjectile() { function drawProjectile() {
@@ -147,8 +146,8 @@ function fadeProjectiles(amount) {
function drawStatus() { function drawStatus() {
const player = players[currentPlayer]; const player = players[currentPlayer];
const {weapon} = player; const {weapon} = player;
drawText(fb, `AIM:${player.a} PWR:${player.p} ${weapon.name}`, 8, 8, player.c, 'left'); drawText(foreground, `AIM:${player.a} PWR:${player.p} ${weapon.name}`, 8, 8, player.c, 'left');
drawText(fb, `WIND: ${wind<=0?'<':''}${Math.abs(wind)}${wind>=0?'>':''}`, W-8, 8, 'white', 'right'); drawText(foreground, `WIND: ${wind<=0?'<':''}${Math.abs(wind)}${wind>=0?'>':''}`, W-8, 8, 'white', 'right');
} }
loop(() => { loop(() => {