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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:24:21+00:00 2026-06-12T09:24:21+00:00

Currently I have an HTML5 canvas that is a fixed size, something like this:

  • 0

Currently I have an HTML5 canvas that is a fixed size, something like this:

<div class="container">
<canvas id="example" width="350" height="250"></canvas>
</div>

However I want to be able to scale the canvas so that it is the same size as the container around it, obviously it still has to keep the same aspect ratio. I also want it to be fluid so that if a user resizes the browser then the canvas will scale with the fluid container.

So for example if my container was set to a width of 500px then the width of the canvas would scale to the same size. Eg:

<div class="container" style="width:500px;">
<canvas id="example"></canvas>
</div>

For example the canvas would get a width of 500px to match the container and the height would automatically be set to 357px which keeps the same aspect ratio.

I believe this could be achieved with some javascript, I just do not know how to do it …
I hope I have provided enough info for someone to help me. Would appreciate it if someone could whip up a jsfiddle and provide some example code of it working if possible.

Thanks.

  • 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-12T09:24:22+00:00Added an answer on June 12, 2026 at 9:24 am

    Here’s a basic implementation that maintains aspect ratio: http://jsfiddle.net/wtVX2/1/

    function resizeCanvas(){
        var con = document.getElementById("container"),
            canvas = document.getElementById("canvas"),
            aspect = canvas.height/canvas.width,    
            width = con.offsetWidth,
            height = con.offsetHeight;
    
        canvas.width = width;
        canvas.height = Math.round(width * aspect);
    }
    

    Note it is only fluid horizontally.

    The jQuery would be:

    function resizeCanvas(){
        var con = $("#container"),
            canvas = $("#canvas")[0],
            aspect = canvas.height/canvas.width,
            width = con.width(),
            height = con.height();
    
        canvas.width = width;
        canvas.height = Math.round(width * aspect);
    }
    

    Note that I’m still setting the width and height of canvas the same way. If we set the width/height with CSS, it will stretch/squash the drawing of the pixels, and you’ll get fuzzy drawings.

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

Sidebar

Related Questions

this seems like a simple problem. I have a canvas application that I am
I have currently two circles in a <canvas> tag with HTML5 & JavaScript. Now
Currently i have made a bar chart from a listbox from this example: http://weblogs.thinktecture.com/cnagel/2011/06/wpf-listbox-as-bar-chart.html
I have an html5 canvas element within a flextable, that flextable is added to
I currently have an HTML page running a full screen canvas element. In this
I have some html5 canvas code to make an image, the current fillstyle is
I currently have the following CSS (called splash.css): html { background:url(splash.jpg) center no-repeat; background-size:auto
I currently have a product view page that contains an MVCContrib HTML Grid with
i have this html menu: <ul id='main'> <li class='active current' id='lighty'> <a href='/'>Lighty</a> <ul>
I have this script I am working on that utilizes the oCanvas JS Library

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.