Giter Club home page Giter Club logo

canvas-mask's Introduction

Canvas Mask Utility

Use HTML5 Canvas to apply an alpha mask to an image element. Compatible with any browser supporting Canvas. This version is a fork of its original version from benbarnett. It can be found here : https://github.com/benbarnett/Canvas-Mask

This extended version adds positioning of the mask on the image.

Tested with Chrome 11 and Firefox 3.6+

Demo

Simple animation demo can be found here: http://playground.benbarnett.net/canvas-mask/

Note that this demo is based on the original version and not this one.

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:

JPEG Image: http://playground.benbarnett.net/canvas-mask/img/octagon.jpg

PNG-24 Mask: http://playground.benbarnett.net/canvas-mask/img/alpha.png

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(BASE IMAGE, MASK IMAGE, x,y, width, height), 0, 0, 0, 0, width, height );

The x and y arguments are the translation of the mask on the image.

Alternatively, you can specify a 7th 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(BASE IMAGE, MASK IMAGE, x,y, width, height, true) + ')' });

Changelog

0.1 (02/06/2011):

  • Initial release.

0.2 (03/05/2012):

  • Added mask translation. The mask doesn't need to have the same size as the image anymore. [xurei]

Credits

canvas-mask's People

Contributors

benbarnett avatar xurei avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.