& Lt; canvas & gt; problems with width and height, and problems with canvas settings

Source: Internet
Author: User

<Canvas> canvas settings encounter problems when setting width and height.

When using the <canvas> element, you must set the width and height to specify the size of the area that can be painted. However, there is a small problem when setting the width and height.

Sample Code:

<! DOCTYPE html> 

Here, I directly go to <canvas>The size of the painting area is set in the label.. The effect displayed in the browser is as follows:

Now I am not setting the width and height in the label.

<! DOCTYPE html> <Style type = "text/css"> # drawimg {width: 300px; height: 500px ;}
</Style>

The effect displayed in the browser is as follows:

As a result, it was originally a rectangle with the same width and height, and the height was obviously lengthened. Why?

In fact, <canvas> has its own default width and height: 300px * 150px, andDefining width and height in <canvas> differs from defining width and height in style.<Canvas> the width and height of the label are the actual width and height of the painting area. The width and height of the style are the rendered height and width of <canvas> in the browser. If the width and height of <canvas> are not specified or the value is incorrect, it is setThe default value is {width: 300px, and height: 150px }.

This explains why the second method causes the image to be stretched and the size of the painting area is not defined in <canvas>. Therefore, the default {width: 300px, height: 150px} is set }, in <style>, {width: 300px; height: 500px;} is used to stretch the height of the painting area.

Related Article

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.