Moving the start position of canvas pattern

后端 未结 3 833
误落风尘
误落风尘 2020-12-09 05:14

I have a code:

function draw(ctx) {
  // create new image object to use as pattern
  var img = new Image();
  img.onload = function(){
    // create pattern
         


        
3条回答
  •  眼角桃花
    2020-12-09 05:56

    More general, complex transforms of the pattern alone are easily done. The trick is to do them immediately before the call to fill() or stroke().

    function draw(ctx) {
        // create new image object to use as pattern
        var img = new Image();
        img.onload = function(){
            // create pattern
            var ptrn = ctx.createPattern(img,'repeat');
            ctx.fillStyle = ptrn;
            ctx.beginPath();
            ctx.rect(0, 0, 150, 150);
    
            ctx.translate(-33, -33);
            ctx.fill();
        }
    
        img.src = 'images/wallpaper.png?' + new Date().getTime();
    }
    

提交回复
热议问题