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 8361593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:44:00+00:00 2026-06-09T11:44:00+00:00

I am designing a Photoshop-style web application running on the HTML5 Canvas element. The

  • 0

I am designing a Photoshop-style web application running on the HTML5 Canvas element. The program runs well and is very speedy until I add blend modes into the equation. I achieve blend modes by merging each canvas element into one and combining each pixel from each canvas using the right blend modes starting from the bottom canvas.

for (int i=0; i<width*height*4; i+=4) {
    var base = [layer[0][i],layer[0][i+1],layer[0][i+2],layer[0][i+3]];
    var nextLayerPixel = [layer[1][i],layer[1][i+1],layer[1][i+2],layer[1][i+3]];
    //Apply first blend between first and second layer
    basePixel = blend(base,nextLayerPixel);
    for(int j=0;j+1 != layer.length;j++){
        //Apply subsequent blends here to basePixel
        nextLayerPixel = [layer[j+1][i],layer[j+1][i+1],layer[j+1][i+2],layer[j+1][i+3]];
        basePixel = blend(basePixel,nextLayerPixel);
   }
   pixels[i] = base[0];
   pixels[i+1] = base[1];
   pixels[i+2] = base[2];
   pixels[i+3] = base[3];
}
canvas.getContext('2d').putImageData(imgData,x,y);

With it calling blend for different blend modes. My ‘normal’ blend mode is as follows:

var blend = function(base,blend) {
    var fgAlpha = blend[3]/255;
    var bgAlpha = (1-blend[3]/255)*base[3]/255;
    blend[0] = (blend[0]*fgAlpha+base[0]*bgAlpha);
    blend[1] = (blend[1]*fgAlpha+base[1]*bgAlpha);
    blend[2] = (blend[2]*fgAlpha+base[2]*bgAlpha);
    blend[3] = ((blend[3]/255+base[3])-(blend[3]/255*base[3]))*255;
    return blend;
}

My test results in Chrome (yielding some of the best out of the tested browsers) was around 400ms blending three layers together on a canvas 620×385 (238,700 pixels).

This is a very small implementation as most projects will be larger in size and include more layers which will make the execution time skyrocket under this method.

I’m wondering if there is any faster way to combine two canvas contexts with a blend mode without having to go through every pixel.

  • 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-06-09T11:44:01+00:00Added an answer on June 9, 2026 at 11:44 am

    Don’t create so many 4-value-arrays, it should go much faster when using the existent memory. Also, you might want to use the reduce function on your layer array, this seems exactly what you need. However, using no functions at all might be another touch faster – no creation of execution contexts needed. The following code will invoke the blend function only for each layer, not each pixel * layers.

    var layer = [...]; // an array of CanvasPixelArrays
    var base = imgData.data; // the base CanvasPixelArray whose values will be changed
                             // if you don't have one, copy layer[0]
    layer.reduce(blend, base); // returns the base, on which all layers are blended
    canvas.getContext('2d').putImageData(imgData, x, y);
    
    function blend(base, pixel) {
    // blends the pixel array into the base array and returns base
        for (int i=0; i<width*height*4; i+=4) {
            var fgAlpha = pixel[i+3]/255,
                bgAlpha = (1-pixel[i+3]/255)*fgAlpha;
            base[i  ] = (pixel[i  ]*fgAlpha+base[i  ]*bgAlpha);
            base[i+1] = (pixel[i+1]*fgAlpha+base[i+1]*bgAlpha);
            base[i+2] = (pixel[i+2]*fgAlpha+base[i+2]*bgAlpha);
            base[i+3] = ((fgAlpha+base[i+3])-(fgAlpha*base[i+3]))*255;
    //                           ^ this seems wrong, but I don't know how to fix it
        }
        return base;
    }
    

    Alternative solution: Don’t blend the layers together in javascript at all. Just absolutely position your canvases over each other and give them a CSS opacity. This should speed up the displaying a lot. Only I’m not sure whether this will work together with your other effects, should they need to be applied on multiple layers.

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

Sidebar

Related Questions

Designing a web application with ASP.NET MVC I asked myself how can I also
At work I see one colleague designing a site in Photoshop/Fireworks, I see another
While designing a new WPF application I noticed exceptions not being thrown during data
When designing a web service that will allow the consumer of the service to
While designing applications it is a very good practice to have all the business
All the (web)designers I've known or worked with, used photoshop almost exclusively, and yet,
When designing an application with a large number of remote services, is it better
When designing a ASP.net WebForm application what are some important steps to take (or
I'm new to web designing. I started to designing a sample page but I
Possible Duplicate: Asp.net how to correct the error I'm designing my web page using

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.