& Lt; canvas & gt; The drawImage () method cannot display images,

Source: Internet
Author: User

<Canvas> the drawImage () method cannot display images,

As shown in the book, use <canvas> to draw an image and try it. At the beginning, my code is as follows:

<! DOCTYPE html> Unfortunately, there is only one black box in the browser with no images. I checked whether the image path is correct, the browser version is supported, and the Console does not report an error. It is not scientific. Then I went to w3school to check the drawImage () method and the sample code. After reading the sample code, I copied it and changed the image to my local image, however, it is useless and still not displayed. So I went to the Internet to check the cause.
<script>window.onload=function(){var drawimg = document.getElementById("drawimg");var image=document.getElementById("image");var context = drawimg.getContext("2d");context.drawImage(image,10,10); }</script>
This is the modified js code. I put the original code in onload, and the picture is displayed in the browser. Because the image is asynchronously loaded, the onload event occurs immediately after the page or image is loaded. In the previous code, the drawImage () method is called before the image is loaded successfully when the js code is executed, so the image cannot be displayed.

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.