nizzle
nizzle

Reputation: 1046

HTML canvas, use greyscale image as mask

I'm trying to apply a mask as it works in Photoshop. Black is transparent, white is visible. However, the composite modes don't allow for this, I think.

I can only find modes where it uses the alpha channel, but I'd like to use a jpg for smaller filesize...

Upvotes: 3

Views: 2552

Answers (1)

Blindman67
Blindman67

Reputation: 54026

Use getImageData and putImageData to access raw pixel data

You need to manually move the pixels.

To do this load the image, put it onto a canvas, get the canvas pixels. Convert to greyscale and move to alpha. THen put the pixels back onto the canvas.

var mask;
var image = new Image;
image.src = "image name.jpg";
image.onload = function(){
    var canvas = document.createElement("canvas");
    canvas.width = this.width;
    canvas.height = this.height;
    var ctx = canvas.getContext("2d");
    ctx.drawImage(this,0,0);
    var data = ctx.getImageData(0,0,this.width,this.height);
    var i = 0;
    while(i < data.data.length){
        var rgb = data.data[i++] + data.data[i++] + data.data[i++];
        data.data[i ++]  = rgb / 3;
    }
    ctx.putImageData(data,0,0);
    mask = canvas;
}

Once loaded mask is a copy of the image with the alpha channel contains the mean of the RGB channels.

To use it

// ctx is the canvas context you want to draw to with the mask

if(mask){
     ctx.globalCompositeOperation = "destination-out";
     ctx.drawImage(mask,0,0);
     ctx.globalCompositeOperation = "source-over";
}

Upvotes: 5

Related Questions