简体   繁体   中英

HTML5 ( canvas, javascript ) The image, makes traces

enter image description here firstly sorry for my english. Second, I have a problem with my javascript. I trying to make something like baner which changes images inside. But when i want to contiune my script, the last image out of canvas field makes me problem. Its makes leaving traces. Its my code.

<html>
<head>
    <link rel="stylesheet" href="style.css">
    <script>
       
       var cat= new Image();

       function init(){
           cat.src = 'cat.png';
           window.requestAnimationFrame(draw);
       }

       function draw() {

        var context = document.getElementById('spin').getContext('2d');
        
        context.clearRect(0, 0, 530, 110);
        context.save();
        context.translate(-1, 0);
        
     
        context.drawImage(cat, 5, 0);
        context.drawImage(cat, 110, 0);
        context.drawImage(cat, 215, 0);
        context.drawImage(cat, 320, 0);
        context.drawImage(cat, 425, 0);
        context.drawImage(cat, 530, 0); /// its an image with problem
        
        
        
        
        

        window.requestAnimationFrame(draw);

       }

       init();

     
    </script>
</head>
<body>

 <div class="roulette">
       <canvas id="spin" width="530" height="110"></canvas>
    </div>

</body>

When loading images in javascript, note that this process is asynchronous. When assigning a value to the src attribute of an Image element, you need to get the element in the onload callback function of this element. Otherwise, when drawing to the canvas, the element may not have fully loaded the corresponding image resource.

const img = new Image()

img.onload = function () {
  // img fully loaded here
}

img.src = 'path/to/image/sources'

You never call context.restore() , so you are stacking a lot of CanvasStates in memory (which will make your whole application slow down in few minutes), and more directly noticeable, your context transform is never reset, meaning that at the second call the coordinate 0, 0 will be the pixels at coordinates -1, 0 and at third call -2, 0 etc. which will make your call to clearRect() miss one more pixel on the right side of the canvas every time.

It's quite unclear what you were after, but to correctly clear your context, call context.setTransform(1, 0, 0, 1, 0, 0) before calling clearRect() , this will reset the transformation matrix to its identity matrix.
Then if you want to translate your context by an ever decreasing value, store that value in a variable and use it in your call to translate() .
Finally, remove that call to save() because it's not needed.

 let x = 0; // the variable where we store the current offset var cat = new Image(); function init() { cat.src = 'https://picsum.photos/110/110'; // always wait for your image has loaded before doing anything with it cat.onload = evt => window.requestAnimationFrame(draw); } function draw() { var context = document.getElementById('spin').getContext('2d'); // update the offset x -= 1; // last image is hidden, stop the loop? if (x < (530 + 110) * -1) { return; } // reset the context matrix context.setTransform(1, 0, 0, 1, 0, 0); // clear the full canvas context.clearRect(0, 0, 530, 110); // set the updated offset context.translate(x, 0); context.drawImage(cat, 5, 0); context.drawImage(cat, 110, 0); context.drawImage(cat, 215, 0); context.drawImage(cat, 320, 0); context.drawImage(cat, 425, 0); context.drawImage(cat, 530, 0); window.requestAnimationFrame(draw); } init();
 <canvas width="530" height="110" id="spin"></canvas>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM