开发者

How to strip background color when using html5 canvas image?

If canvas can strip background color开发者_C百科 from a external image which is loaded(drawimage) into this canvas?

I think canvas can manipulate pixels, so it should strip the image background color. After some searching, I still can't find a answer or any idea for it.


in a demo I wrote (personal spam ;) there is something you could need:

the full source is here

  • first it reads an image

    brush.load({
    imageUrl: "brush-2.png",
    onload: function (brush) { 
        setBG(brush);
    }
    

    });

  • then it manipulates the pixels

    for(p = 0; p < l; p = p + 4){
      r = pixels.data[p + 0];
      g = pixels.data[p + 1];
      b = pixels.data[p + 2];
      a = pixels.data[p + 3];
    
      pixels.data[p + 0] = k;
      pixels.data[p + 1] = m;
      pixels.data[p + 2] = o;
      pixels.data[p + 3] = a;
    

    }

  • then it sets the image from the imageData

    document.body.style.backgroundImage= "url(" + ctx.canvas.toDataURL() + ")";
    

more here

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜