Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7171693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:27:55+00:00 2026-05-28T15:27:55+00:00

Here’s what I’m trying to do: Get image A, and image B. Image B

  • 0

Here’s what I’m trying to do:

  1. Get image A, and image B. Image B is a black and white mask image.
  2. Replace image A’s alpha channel with image B’s red channel.
  3. Draw image C on the canvas.
  4. Draw image A on top of image C.

Everything seems ok until step 4. Image C isn’t visible at all and where image A should be transparent there’s white color.

cx.putImageData(imageA, 0, 0);
var resultData = cx.getImageData(0, 0, view.width, view.height);

for (var h=0; h<resultData.data.length; h+=4) {
    resultData.data[h+3] = imageB.data[h];
}

cx.putImageData(imageC, 0, 0);
cx.putImageData(resultData, 0, 0);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T15:27:56+00:00Added an answer on May 28, 2026 at 3:27 pm

    Simon is right: the putImageData method does not pay any attention to compositing; it merely copies pixel values. In order to get compositing, we need to use drawing operations.

    We need to mess with the channels (turn red into alpha) with the pixel data, put that changed pixel data into an image, and then use a composite operation to get the desired masking.

    Kinda lame

    //copy from one channel to another
    var assignChannel = function(imageData, channelTo, channelFrom) {
      if(channelTo < 0 || channelTo > 3 || channelFrom < 0 || channelFrom > 3) {
        throw new Error("bad channel number");
      }
      if(channelTo == channelFrom)
        return;
      var px = imageData.data;
      for(var i = 0; i < px.length; i += 4) {
        px[i + channelTo] = px[i + channelFrom];
      }
    };
    /**============================================================================ 
      * this function uses 3 or 4 canvases for clarity / pedagogical reasons:
      * redCanvas has our mask image;
      * maskCanvas will be used to store the alpha channel conversion of redCanvas' image;
      * imageCanvas contains the image to be masked;
      * ctx is the context of the canvas to which the masked image will be drawn.
    ============================================================================**/
    var drawOnTopOfRed = function(redCanvas, maskCanvas, imageCanvas, ctx) {
      var redImageData = redCanvas.getContext("2d").getImageData(0, 0, w, h);
    
      //assign the alpha channel
      assignChannel(redImageData, 3, 0);
    
      //write the mask image
      maskCanvas.getContext("2d").putImageData(redImageData, 0, 0);
    
      ctx.save();
    
      //draw the mask
      ctx.globalCompositeOperation = "copy";
      ctx.drawImage(maskCanvas, 0, 0);
    
      //draw the image to be masked, but only where both it
      //and the mask are opaque; see http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#compositing for details.
      ctx.globalCompositeOperation = "source-in";
      ctx.drawImage(imageCanvas, 0, 0);
      ctx.restore();
    };
    

    jsfiddle example

    A doodle with the example:

    Kinda lame
    about the same

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the code: http://jsfiddle.net/GKBfL/ I am trying to get collection.prototype.add to return a
Here's the basic setup: I have a thin bar at the top of a
Here's what I'm trying to accomplish with this program: a recursive method that checks
Here is the css: #content ul { font-size: 12px; } I am trying this:
Here's the flow that I am trying to achieve: 1) User uploads an audio
Here is what I am trying to achieve in PHP: I have this string:
Here's what I'm trying to do... It's quite simple and obviously there must be
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Here is binding: <Image Width=16 Height=16 Source={Binding SwitchForImage, Converter={StaticResource stringToImage}} HorizontalAlignment=Left> </Image> and here
Here is my code. When I run this I get an error saying :

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.