From bb95f8f8fd879b9d64b9c4dfe7715046ba813b6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Pelletier?= Date: Fri, 6 Sep 2024 18:02:34 -0400 Subject: [PATCH] =?UTF-8?q?=C3=89viter=20de=20redessiner=20le=20background?= =?UTF-8?q?=20=C3=A0=20chaque=20fois?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- public/javascripts/game.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/public/javascripts/game.js b/public/javascripts/game.js index 3f4145b..ce6cbb2 100644 --- a/public/javascripts/game.js +++ b/public/javascripts/game.js @@ -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();