M
M
melior332021-05-10 06:41:39
Canvas
melior33, 2021-05-10 06:41:39

What is the correct way to output an array of canvas images?

Hello everyone, is there anyone who understands canvas?
I'm trying to make a game (I'm still learning), and there was such a problem that when using rect, everything is fine ( https://codepen.io/dvill/pen/WNpvMba ), but not so much when using drawimage ( https://codepen.io/dvill /pen/rNyVJxN )
I will be grateful if you can tell me what I can be wrong

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
twobomb, 2021-05-10
@melior33

Instead of repaintPanel add

colors.forEach(e=>{
  let img = new Image();
  img.src= e.img;
  img.onload = function(){
      e.isLoaded = true;
      if(colors.every(e1=>e1.isLoaded))        
        repaintPanel();
  }
});

PS In general, the implementation through setInterval is just awful, make one game loop and use requestAnimationFrame or setTimeout at worst .
The first thing that came across , read it if you have no idea what it is

D
Dmitry Belyaev, 2021-05-10
@bingo347

The problem is that if you assign a new url to the src property of the Image object, the image will not appear there instantly, it will be loaded in the background. Accordingly, you need to wait for loading through the event before drawing.
Well, and another tip, if you load pictures in parallel, each in its own Image, it will be faster.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question