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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:32:49+00:00 2026-06-12T05:32:49+00:00

I am working with the HTML5 canvas element. I am using the following javascript

  • 0

I am working with the HTML5 canvas element. I am using the following javascript to draw an image into the canvas:

var img = new Image();
var canvas = this.e;
var ctx = canvas.getContext('2d');
img.src = options.imageSrc;
img.onload = function() {
    ctx.drawImage(img,0,0);
}

This works fine. However I am experiencing some weird css issues. When I use inline css like the following, the image scales perfectly in the canvas:

<canvas id="canvas" width="800" height="448"></canvas>

However, when I remove the inline css and use an external css file the image is drawn too big for the canvas and I only see the top left corner of the image. Here is the css I am using:

#canvas {
    height:448px;
    width:800px;
    border:none;
}

When I use the inline css is the drawn image somehow inheriting the css from the canvas? Not sure what is going on here but I would like to use the external css file.

EDIT
If I provide no styling for the canvas element, it is smaller but still shows just the top corner of the image. When I do style it with a css file, the canvas is bigger but it is as if it just zoomed in on the image. The same amount of the image is shown, it is now just bigger.

EDIT 2
Based on the answer received I am now sizing the dom size of the canvas with javascript. I changed my img.onload function to the following:

img.onload = function() {
    if (options.imageWidth == 0 && options.imageHeight == 0) {
        canvas.height = this.height;
        canvas.width = this.width;
    } else {
        canvas.height = options.imageHeight;
        canvas.width = options.imageWidth;
    }
    ctx.drawImage(img,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-06-12T05:32:50+00:00Added an answer on June 12, 2026 at 5:32 am

    Canvases have two sizes – the actual pixel grid size (in the width and height attributes) and the CSS size.

    If the pixel size is not specified it typically defaults to something like 300×300.

    The CSS size only specifies the amount of space taken in the page, and defaults to the pixel size. If they are not equal the image will be scaled.

    You MUST specify the pixel size in the <canvas> element, or set it using Javascript:

    var img = new Image();
    var canvas = this.e;
    var ctx = canvas.getContext('2d');
    img.src = options.imageSrc;
    img.onload = function() {
        canvas.width = this.width;    // new code
        canvas.height = this.height;  //    "
        ctx.drawImage(img, 0, 0);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Working on a small game using an HTML5 canvas, and javascript. And it's working
I am working on a new graph for my site, using CSS3/HTML5 (canvas tag).
I'm working on a HTML5 canvas game using a JavaScript library called Propulsionjs.com. The
I am playing with the HTML5 canvas element, using JS to draw some rectangles
I'm working on an online javascript canvas (not the HTML5 element, but a painting
I am working on an animation using Javascript and HTML5 CAnvas. How do achieve
I'm working on a simple JavaScript game using HTML5 canvas. It's a very typical
I'm currently working on a JavaScript project that uses the HTML5 canvas as a
I'm trying to resize a HTML5 canvas element using jQuery. (NOTICE: Not the objects
I'm working with the HTML5 canvas element. Let's say I have 2 ImageData objects

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.