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

  • Home
  • SEARCH
  • 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 4121996
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:25:28+00:00 2026-05-20T23:25:28+00:00

Consider the following code (includes jQuery): <!DOCTYPE html PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN http://www.w3.org/TR/html4/loose.dtd>

  • 0

Consider the following code (includes jQuery):

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src = "js/jquery-1.4.2.js"></script>
<script type="text/javascript">

jQuery(window).bind("load", function() {
    var elem = $("<canvas>", {width:300, height:300});

    var canvele = elem[0];


    var imm = new Image();

    imm.src = "card1/mask-x-6.png";
    imm.onload = function(){
        var ctx = canvele.getContext('2d');
        ctx.drawImage(imm, 0,0);
        $('body').append(elem);
    }
});


</script>
</head>
<body>

</body>
</html>

This will result in the Image STRETCHED, not in it original aspect ratio. Both on firefox and Chrome. Why this happens? Workarounds?


Update

My apologies this thing doesn’t happen if I don’t use jQuery to generate the canvas Element. I’ll post this to jQuery forums too.

  • 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-20T23:25:29+00:00Added an answer on May 20, 2026 at 11:25 pm

    An HTML5 Canvas, like an HTML image, has both an intrinsic size—the number of pixels wide and tall in the source image—and a display size—how large it is drawn on the HTML page. If these two sizes are set differently, the image will be stretched.

    The jQuery code you have used to create your canvas element sets the display size to 300x300px by setting the CSS width and height attributes, but does not set the intrinsic size differently than the default of 300×150. As such, anything you draw will be stretched twice as tall.

    To fix it, set the actual width and height attributes of the canvas element directly. In the following code I’ve made this fix. I’ve also fixed it to use an HTML5 DOCTYPE, and I set the img.onload before the img.src:

    <!DOCTYPE html>
    <html><head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Setting Canvas Dimensions</title>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js">
    </script>
    <script type="text/javascript">
    $(function(){
      var elem = $("<canvas>", {width:300, height:300});
      var canvele = elem[0];
      canvele.width = canvele.height = 300;
    
      var imm = new Image;
      imm.onload = function(){
        var ctx = canvele.getContext('2d');
        ctx.drawImage(imm, 0,0);
        $('body').append(elem);
      };
      imm.src = "http://phrogz.net/tmp/gkhead.jpg";
    });
    </script>
    </head><body></body></html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following html code: <div id='x'><div id='y'>Y content</div>X content</div> I'd like to extract
Consider the following code : #include <vector> #include <iostream> class a { public: int
Consider the following code: #include <stdio.h> namespace Foo { template <typename T> void foo(T
Consider the following code: $(a).attr(disabled, disabled); In IE and FF, this will make anchors
Consider the following code: void Handler(object o, EventArgs e) { // I swear o
Consider the following code: abstract class SomeClassX<T> { // blah } class SomeClassY: SomeClassX<int>
Consider the following code: client.Send(data, data.Length, endpoint); byte[] response = client.Receive(ref endpoint); While, according
Consider the following code in VB9: Dim text = Line1<br/>Line2 Dim markup = <span><%=
Consider the following code snippet private void ProcessFile(string fullPath) { XmlTextReader rdr = new
Consider the following code: template <int dim> struct vec { vec normalize(); }; template

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.