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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:24:27+00:00 2026-05-29T11:24:27+00:00

I’m using an HTML5 canvas to create a series of trees and their corresponding

  • 0

I’m using an HTML5 canvas to create a series of trees and their corresponding shadows. I want each shadow to be based on the position of a light source. I can draw each tree and it’s shadow as well as skew all the shadows at once, but trying to skew each shadow individually eludes me. Here’s my code:

var ctx = cvs[0].getContext('2d');
ctx.save();
//skew the canvas
ctx.transform(1,0,0.3,-1,0,0);

ctx.drawImage(tree1,74,-117,20,40);
ctx.drawImage(tree1,126,-125,20,40);

var imgd = ctx.getImageData(0, 0, 500, 300);
var pix = imgd.data;
//convert the drawn trees to shadows
for (var i = 0, n = pix.length; i < n; i += 4) {
    pix[i  ] = 0;   // red
    pix[i+1] = 0;   // green
    pix[i+2] = 0;   // blue
    // alpha
}
ctx.putImageData(imgd, 0, 0);

//remove the skewing
ctx.restore();

//draw full color trees
ctx.drawImage(tree1,50,40,20,40);
ctx.drawImage(tree1,100,50,20,40);

Is there a way to skew the images as I draw them using drawImage? Thanks in advance!

  • 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-29T11:24:27+00:00Added an answer on May 29, 2026 at 11:24 am

    There are two things you have to understand here.

    When you apply a transformation to the context you do not apply it to anything already drawn. You only apply the transformation to things that are about to be drawn.

    In other words, it is always the case that you skew images as you draw them using drawImage! That’s the only way its done.

    There’s an exception to this rule that you should keep in mind since you’re using imageData: Putting imageData onto the canvas is pixel perfect and ignores any transformation matrix.

    So if you want a different skew for each shadow all you have to do is transform the context, draw thing A, then restore and transform the context in some other way and draw thing B.

    Here’s an example using your code:

    http://jsfiddle.net/QfrVB/


    On a side note, you really don’t have to use imageData here at all. Using it is a really slow operation (important if you’re making an animated app like a game) and you should avoid it if you can.

    Instead of doing this:

    var imgd = ctx.getImageData(0, 0, 500, 300);
    var pix = imgd.data;
    //convert the drawn trees to shadows
    for (var i = 0, n = pix.length; i < n; i += 4) {
        pix[i  ] = 0;   // red
        pix[i+1] = 0;   // green
        pix[i+2] = 0;   // blue
        // alpha
    }
    ctx.putImageData(imgd, 0, 0);
    

    You can just do this:

    ctx.globalCompositeOperation = 'source-atop';
    ctx.fillStyle = 'black';
    ctx.fillRect(0,0,500,300);
    ctx.globalCompositeOperation = 'source-over'; // back to normal
    

    What that code does is draw black over all pixels that currently exist on the canvas. Since you draw all shadows first you can black all of them out with one call to fillRect this way.

    See that live here:
    http://jsfiddle.net/QfrVB/2/

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

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
I want to count how many characters a certain string has in PHP, but
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
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

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.