Éviter de redessiner le background à chaque fois

This commit is contained in:
François Pelletier 2024-09-06 18:02:34 -04:00
parent 13c233daab
commit bb95f8f8fd

View file

@ -1,6 +1,10 @@
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
// Load background image once, outside of any function
const backgroundImage = new Image();
backgroundImage.src = '/assets/images/background.png';
let bricks = [];
let paddle = { x: 175, y: 510, width: 75, height: 15 };
let ball = { x: 200, y: 370, dx: 4, dy: -4, radius: 8 };
@ -85,9 +89,7 @@ function update() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw background image
let background = new Image();
background.src = '/assets/images/background.png';
ctx.drawImage(background, 0, 0, canvas.width, canvas.height);
ctx.drawImage(backgroundImage, 0, 0, canvas.width, canvas.height);
// Draw floor line
ctx.beginPath();