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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:01:07+00:00 2026-06-02T08:01:07+00:00

I’m using context-blender to apply a multiply effect on the first 192 pixels of

  • 0

I’m using context-blender to apply a multiply effect on the first 192 pixels of the html background-image with a fixed color to achieve a transparency effect on the header of the page.

On the html I have 2 canvas. One for the part of the image to apply the multiply effect and one for the color.

On the javascript, after setting the color of the color-canvas and the width of both canvas to the window.innerWidth I’m getting the background image with:

imageObj.src = $('html').css('background-image').replace(/^url|[\(\)]/g, '');

Now comes the problem. I want to draw a cropped image to the image to the image-canvas so I can apply the multiply effect. I’m trying to do the following:

imageObj.onload = function(){
    // getting the background-image height
    var imageHeight = window.innerWidth * imageObj.height / imageObj.width;

    // get the corresponding pixels of the source image that correspond to the first 192 pixels of the background-image
    var croppedHeight = 192 * imageObj.height / imageHeight;

    // draw the image to the canvas
    imageCanvas.drawImage(imageObj, 0, 0, imageObj.width, croppedHeight, 0, 0, window.innerWidth, 192);

    // apply the multiply effect
    colorCanvas.blendOnto( imageCanvas, 'multiply');
}

But I’m doing something wrong getting the cropped height.

Ex: For an 1536×1152 image and a 1293×679 browser container, the value I’m getting for the source cropped height is 230 but to get the correct crop I need to use something around 296.

Edit:

I’m using background-size: cover on the css to create the background-image

Edit2:

I created a fiddle to illustrate the problem. If you uncomment the line //cHeight *= magicConstant; the cropped image looks a lot better but things stop making sense. I removed the multiply effect on the fiddler but that’s not required to reproduce the problem. I also noticed that the behavior changed if I remove the second canvas from the URL.

Btw, this behavior happened with google chrome, but I think the same thing happens on safari and firefox.

  • 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-02T08:01:08+00:00Added an answer on June 2, 2026 at 8:01 am

    OK, I’ve fixed it. Man was that hard! Mainly because you forgot to set the imageCanvas’ canvas height. It also didn’t help that the image has a white border. I spent a hell of a lot of time trying to figure out where the padding was coming from.

    So to start, for the case of the fiddle, in function doBlending(), set imageCanvas.canvas.height = height;

    Then the calculations in crop() need to cover 2 possibilities. Is the image being scaled for height and truncated on the left or scaled for width and truncated on the bottom? I’m not going to write both for you, but here’s the one for the case where it is scaled for height:

    function crop(imageObj, imageCanvas, colorCanvas) {
        // Assumes bg image is scaled for hight
        var scale = imageObj.height / window.innerHeight; 
        var targetHeight = imageCanvas.canvas.height;
        var targetWidth = window.innerWidth;
    
        imageCanvas.drawImage(imageObj, 
                              0, 0, targetWidth * scale, targetHeight * scale,
                              0, 0, targetWidth, targetHeight);
    }
    

    I really have no idea where you came up with the scaling factors in your example. The image is going to be scaled by multiplying both the x and y dimensions by some scale factor. That’s how you preserve the aspect ratio. The scale factor will be the larger of the one to make the height of the image match the height of the window and the one to make the width of the image match the width of the window.

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

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
We're building an app, our first using Rails 3, and we're having to build
I have thousands of HTML files to process using Groovy/Java and I need to
I'm making a simple page using Google Maps API 3. My first. One marker
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.