<canvas> setting wide-height problems

Source: Internet
Author: User

You must set the width and height when using the <canvas> element, specifying the size of the area that can be painted. But there is a small problem when setting the width and height here.

Sample code:

<!DOCTYPE HTML><HTML><Head><MetaCharSet= "Utf-8"/>    <title>Canvas Drawing</title>    </Head><Body><CanvasID= "Drawimg"width= "+"Height= "$">A drawimg of something.</Canvas></Body>    <Script>vardrawimg=document.getElementById ("drawimg");if(drawimg.getcontext) {varContext=Drawimg.getcontext ("2d"); Context.strokerect (0, 0,  -,  -); Context.fillrect (Wuyi, 0,  -,  -);}</Script></HTML>

Here I set the size of the painting area directly within the <canvas> tag. The effect shown in the browser is this

Now I'm not setting the width height inside the label.

<!DOCTYPE HTML><HTML><Head><MetaCharSet= "Utf-8"/>    <title>Canvas Drawing</title>
<style type= "Text/css" >#drawimg {width:300px; height:500px; }
</style></Head><Body><CanvasID= "Drawimg" >A drawimg of something.</Canvas></Body> <Script>vardrawimg=document.getElementById ("drawimg");if(drawimg.getcontext) {varContext=Drawimg.getcontext ("2d"); Context.strokerect (0, 0, -, -); Context.fillrect (Wuyi, 0, -, -);}</Script></HTML>

This is how the browser displays the effect.

As a result, the width is the same as the rectangle, the height is obviously elongated. What is this for?

In fact <canvas> has its own default width height 300px*150px, and defining width in <canvas>, height is different from defining width and height in style ,< The width and height of the canvas> tag are the actual widths and heights of the painting area, and the drawing is on top of it. The width and height of the style are the height and width that <canvas> is rendered in the browser. If the width and height of <canvas> are not specified or the value is incorrect, it is set to the default value {width:300px,height:150px}.

This explains why the second notation causes the drawing to be stretched, the size of the paint area is not defined in <canvas>, so the default {width:300px,height:150px} is set, and the <style> is set to {width : 300px;height:500px;} Stretches the height of the painting area.

<canvas> setting wide-height problems

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.