June
June

Reputation: 39

Set background position Javascript

So Im building a game and have a js script which loads a background image for each level only problem is I want the background to be in a certain position. How would I achieve this within the script?

My code is as follows:

var levels = {
    level1: {
                level: [[["0","1","0","1"],["0","0","0","0"],["0","0","0","0"],["0","0","0","0"],["0","0","0","0"],["0","0","0","0"]],[["0","0","1","1"],["0","1","1","0"],["0","0","0","0"],["0","0","0","0"],["0","0","0","0"],["0","0","0","0"]],[["0","0","0","0"],["0","0","1","1"],["1","1","0","0"],["0","0","0","0"],["0","0","0","0"],["0","0","0","0"]],[["0","0","0","0"],["0","0","0","0"],["0","0","1","1"],["1","0","1","0"],["1","0","1","0"],["1","1","0","0"]]],
                backgroundURL : "http://google.co.uk/level1.jpg" 
            },

Upvotes: 0

Views: 65

Answers (1)

Wolfgang Noichl
Wolfgang Noichl

Reputation: 183

Just use the css attibute background-position, as described in another stackoverflow post.

Provided you use jQuery, you could do

$('#canvas').css('background-position', '10px 10px');

where

<div id="canvas">

is the relevant HTML element.

Upvotes: 1

Related Questions