2D side scrolling camera view in html5

后端 未结 2 454
南笙
南笙 2020-12-24 15:40

I was wondering how I could go about making a camera-like view where I could scroll a level within a canvas element just like this:

http://playbiolab.com/

2条回答
  •  醉话见心
    2020-12-24 15:53

    I've always found it's more efficient to wrap your canvas in a div with the width and height of your viewport and the overflow set to hidden then just draw your whole canvas and scroll the div to where you where you want to view.

    So the html would be:

    and the javascript would be something like

    function scrollWrapper(x, y){
        var wrapper = document.getElementById('wrapper');  
        wrapper.scrollTop = x;
        wrapper.scrollLeft = y;
    }
    

    Then just call the function with the x and y you want to view. you could wrap it in a setTimeout or something if you wanted to move there instead of just jumping there.

提交回复
热议问题