Html5 Canvas DrawImage Stretched - Javascript - Stack Overflow

    1. Home
    2. Questions
    3. Tags
    4. Users
    5. Companies
    6. Labs
    7. Jobs
    8. Discussions
    9. Collectives
    10. Communities for your favorite technologies. Explore all Collectives

  1. Teams

    Ask questions, find answers and collaborate at work with Stack Overflow for Teams.

    Try Teams for free Explore Teams
  2. Teams
  3. Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams

Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Get early access and see previews of new features.

Learn more about Labs Html5 canvas drawImage stretched Ask Question Asked 11 years, 6 months ago Modified 11 years, 6 months ago Viewed 18k times 20

I tried to draw image on html5 canvas . The issue is image stretched in canvas

load('img_the_scream', 'http://www.w3schools.com/tags/img_the_scream.jpg', function( img ){ console.log( img.width , img.height ); ctx.drawImage( img, 10, 10 , img.width , img.height ); } );

I added this issue in http://jsfiddle.net/75rAU/

Share Improve this question Follow asked May 11, 2013 at 17:10 rab's user avatar rabrab 4,1441 gold badge31 silver badges42 bronze badges Add a comment |

2 Answers 2

Sorted by: Reset to default Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first) 44

That is because you modified the canvas size in CSS. The canvas has two separate size : The HTML size (the one you put inside the canvas tag) and the css size, which is actually a rescaling of the canvas. The HTML size is the size you control (when you draw, it uses this size) and the CSS size is the displayed size which is a rescaling of the HTML size.

So here, your canvas has the default size (which I don't remember) but is resized (and stretched) by the css

HTML :

<canvas id="cv" width="350" height="350"></canvas>

CSS :

#cv { background:#ff0; }

Here I updated your fiddle with the correct size assignation

Share Improve this answer Follow answered May 11, 2013 at 17:32 nialna2's user avatar nialna2nialna2 2,1843 gold badges25 silver badges33 bronze badges Add a comment | 7

Canvas has it's own sizes. 300x150 at default. Styles (width/height) just stretch canvas like any image. So, you should strongly set sizes to what you want. You may do it through html <canvas width="123" height="123"></canvas> or from JS code canvas.width = canvas.height = 123. Also, here you may set sizes by image properties canvas.width = img.width etc.

So, look at jsfiddle: http://jsfiddle.net/75rAU/3/ It works well

Little upd: http://jsfiddle.net/75rAU/4/ — this may help too

Share Improve this answer Follow answered May 11, 2013 at 17:33 Novelist's user avatar NovelistNovelist 4794 silver badges15 bronze badges Add a comment |

Your Answer

Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid …

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

Draft saved Draft discarded

Sign up or log in

Sign up using Google Sign up using Email and Password Submit

Post as a guest

Name Email

Required, but never shown

Post Your Answer Discard

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.

  • The Overflow Blog
  • We'll Be In Touch - A New Podcast From Stack Overflow!
  • The app that fights for your data privacy rights
  • Featured on Meta
  • More network sites to see advertising test
  • We’re (finally!) going to the cloud!
  • Call for testers for an early access release of a Stack Overflow extension...
17 HTML5 canvas: image resizing 2 Html5 Canvas resize 2 html5 canvas image resize 71 Canvas drawImage scaling 2 javascript canvas stretches my Image 35 drawImage and resize to Canvas 0 Drawing Image onto Canvas Stretches Image? 3 JavaScript Canvas drawImage deforms my image 0 HTML5 Canvas drawImage resizing issues 0 Canvas drawImage drawing unwanted pixels when stretching

Hot Network Questions

  • Why do some people write text all in lower case?
  • How far above a forest fire or conflagration would you need to be, to not burn alive?
  • How do I publish my book free as a free to read website with webpages?
  • In Catholic atonement theology, if God can save Mary from all sin without Christ, what was the point of Christ's death?
  • How would Merfolk make a solar oven
  • Can a toilet paper holder be mounted to the side of a fiberglass tub?
  • Dominant chord -- is its definiton super flexible in blues or I spotted a mistake?
  • Could you make a quadcopter whose propellers can also work as wheels?
  • How quickly could a probe reach the heliopause?
  • Power series of the reciprocal of f defined as a power series
  • Boy who can see EM waves but loses the ability because of a thunderstorm
  • What is the correct article before a letter sound?
  • "Your move, bud."
  • eLife-like publications and Tenure Decisions
  • What is the probability that there will be at least one empty box?
  • How is the satyrical phrase "Vae me, puto concacavi me!" by Seneca the Younger grammatical? Shouldn't it be "Vae mihi, puto me concacavisse!"?
  • What does "the next" refer to?
  • How to write a cooking scene without it sounding like a recipe?
  • how to stop using a command directly
  • Naive proof that subgroup of free group is free, what's wrong?
  • Is is plausible that we could have neuronal maps of human brains without mind uploading being possible?
  • How do you build Mizar locally?
  • Is partial correctness decidable?
  • Understanding Linux 'top' command: Memory vs Swap display format confusion
more hot questions Question feed Subscribe to RSS Question feed

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

default

Từ khóa » Html Canvas Stretch Image