8

I am building a canvas game. In this i want to slide the background image in a loop. I don't know how to do this using javascript. I will be using single image which will slide continuously into the background. Thanks in advance.

rgolekar
  • 105
  • 1
  • 1
  • 6
  • I'm not sure you could continuously slide one image - you could slide two halves which join seamlessly at the edges. – Widor Jun 12 '12 at 11:08
  • @Coulton theres no reason to use jQuery it does nothing with canvas. – Loktar Jun 12 '12 at 13:27
  • @Widor You can most definitely use one image, you just copy portions of it to the canvas creating the seamless effect. – Loktar Jun 12 '12 at 13:27

1 Answers1

14

Theres a few ways to achieve this the first one will take a performance hit using putImageData, the second method just uses drawImage. Also note the 2nd method has the code to make it go either from left to right, or right to left.

http://www.somethinghitme.com/projects/bgscroll/

var ctx = document.getElementById("canvas").getContext("2d"),
    canvasTemp = document.createElement("canvas"),
    scrollImg = new Image(),
    tempContext = canvasTemp.getContext("2d"),
    imgWidth = 0,
    imgHeight =0,
    imageData = {},
    canvasWidth = 600,
    canvasHeight = 240,
    scrollVal = 0,
    speed =2;

    scrollImg.src = "citybg.png";
    scrollImg.onload = loadImage;

    function loadImage(){
        imgWidth = scrollImg.width,
        imgHeight = scrollImg.height;
        canvasTemp.width = imgWidth;
        canvasTemp.height =  imgHeight;    
        tempContext.drawImage(scrollImg, 0,0, imgWidth, imgHeight); 
        imageData = tempContext.getImageData(0,0,imgWidth,imgHeight);
        render();                
    }

    function render(){
        ctx.clearRect(0,0,canvasWidth,canvasHeight);

        if(scrollVal >= canvasWidth-speed){
            scrollVal = 0;
        }

        scrollVal+=speed;

        // This is the bread and butter, you have to make sure the imagedata isnt larger than the canvas your putting image data to.
        imageData = tempContext.getImageData(canvasWidth-scrollVal,0,scrollVal,canvasHeight);
        ctx.putImageData(imageData, 0,0,0,0,scrollVal, imgHeight);
        imageData = tempContext.getImageData(0,0,canvasWidth-scrollVal,canvasHeight);
        ctx.putImageData(imageData, scrollVal,0,0,0,canvasWidth-scrollVal, imgHeight);

        setTimeout(function(){render();},10);
    }

2nd Method uses the same code as above, just change these two functions to the following.

http://www.somethinghitme.com/projects/bgscroll/scrolldrawimage.html

function loadImage(){
    imgWidth = scrollImg.width,
    imgHeight = scrollImg.height;
    canvasTemp.width = imgWidth;
    canvasTemp.height =  imgHeight;    
    render();                
}

function render(){
    ctx.clearRect(0,0,canvasWidth,canvasHeight);

    if(scrollVal >= canvasWidth){
        scrollVal = 0;
    }

    scrollVal+=speed;                   
    ctx.drawImage(scrollImg,canvasWidth-scrollVal,0,scrollVal,imgHeight, 0, 0, scrollVal,imgHeight);
    ctx.drawImage(scrollImg,scrollVal,0,imgWidth, imgHeight);

     // To go the other way instead
     ctx.drawImage(scrollImg,-scrollVal,0,imgWidth, imgHeight);
     ctx.drawImage(scrollImg,canvasWidth-scrollVal,0,imgWidth, imgHeight);

    setTimeout(function(){render();},10);
}
Loktar
  • 34,764
  • 7
  • 90
  • 104
  • Hi Loktar, thanks for the answer. Could you please tell me how to reverse the direction (from right to left). I tried it but no success. – rgolekar Jun 13 '12 at 04:54
  • 1
    @user1451031 I edited my answer and added it to the second method under the comment `//To go the other way` since the 2nd method is the one that performs better. – Loktar Jun 13 '12 at 12:39
  • 1
    Thanks a lot ! I try to use the 2nd method and the reverse block ("right to left"). The problem I have is that I need to position my images but if I add the 6th,7th,8th and 9th parameters to [drawImage](http://www.w3schools.com/tags/canvas_drawimage.asp), it reverses and goes left to right again. Very,very weird. An idea about it ? – gordie Apr 18 '15 at 09:41
  • 1
    About my previous comment, I finally managed to do it : http://stackoverflow.com/a/29731069/782013 – gordie Apr 19 '15 at 14:19