html5 canvas 遮罩,GitHub - benbarnett/canvas-mask: Use HTML5 Canvas to apply an alpha mask to an image...

Canvas Mask Utility

Use HTML5 Canvas to apply an alpha mask to an image element.

Compatible with any browser supporting Canvas.

Tested with Chrome 11 and Firefox 3.6+

Demo

What it does

The function will use HTML5 Canvas to read alpha data from a PNG-24, and apply this to any image element. I have often used this technique to take advantage of the smaller file sizes with JPEG, and then a 2 colour PNG-24 to apply the transparency.

Example images:

Usage

Simply pass in two image elements (pre-loaded, as shown on demo), the width and the height and you're done.

By default the function will return Canvas pixel data which you can then use to draw on to a Canvas. For example:

output.putImageData(

applyCanvasMask(, , width, height), 0, 0, 0, 0, width, height

);

Alternatively, you can specify a 5th parameter to have the function return the masked image as a Base64 encoded data URL. This is useful for applying to background images. For example:

$('body').css({

'background-image': 'url(' + applyCanvasMask(, , width, height, true) + ')'

});

Changelog

0.2 (10/01/2013):

Merge pull request #3 - allow two transparent pngs to mask

0.1 (02/06/2011):

Initial release.

Credits

Author: Ben Barnett - http://www.benbarnett.net - @benpbarnett

Jake Archibald for originally demonstrating this type of pixel manipulation - http://jakearchibald.com/scratch/alphavid/ - @jaffathecake

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值