Tell me more ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

So I'm trying to repeat a part of my spritesheet as a background (js, canvas).

My code so far:

var canvas = $("#board")[0],
    ctx = canvas.getContext("2d"),
    sprite = new Image();
    sprite.src = "spritesheet.png";
sprite.onload = function(){
    ctx.fillStyle = ctx.createPattern(spriteBg, "repeat");
    ctx.fillRect(0, 25, 500, 500);
}

This is fine, but as you can see, it repeat the whole sprite, not just a part of it, and I just can't figure out how to do it D:

share|improve this question
1  
@KeithThomas +1, thanks! – Mobilpadde Oct 7 '12 at 15:32
Mobil: If that solved your problem, you should post your conclusions as an answer. Then others with the same problem can find it too. – Anko Dec 8 '12 at 11:48
@KeithThomas Yea sorry. Posted my answer now :D – Mobilpadde Dec 8 '12 at 16:05

1 Answer

up vote 0 down vote accepted

This'll do! :D

var sprite     = new Image(),
    sprite.src = "spritesheet.png",
    texture    = {y: 42, x: 2*14},
    ptrn       = generatePattern(sprite, texture.x, texture.y);
ctx.fillStyle  = ctx.createPattern(ptrn, "repeat");
ctx.fillRect(0, 0, 500, 500);

Thanks to @KeithThomas for linking to http://stackoverflow.com/questions/8761670/multiple-image-patterns-in-2d-context-of-html5-canvas

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.