简体   繁体   English

javascript在画布上上传图像

[英]javascript upload an image in canvas

I try to upload and draw an image into a canvas. 我尝试上传图像并将其绘制到画布中。 I have the html code for button and canvas and js code for onchange event. 我有用于按钮和画布的html代码以及用于onchange事件的js代码。 My code does not work and I do not have any errors to figure out what is going on. 我的代码不起作用,并且我没有任何错误可以弄清楚发生了什么。

The fix is by simply including your scripts at the end of the body before the closing tag </body> . 解决方法是简单地在结束标记</body>之前的正文末尾包含脚本

Your html code will appear like this: 您的html代码将如下所示:

<html>
<body>
    <div id="buttonsDiv" >
        <input type="file" accept="image/*" title="Upload Image" class="normal-button" id="uploadBtn"/>            
    </div>
    <canvas id= "myImgCanvas" title="Drop an image here to upload" ></canvas>

    <script src="myFileName.js"></script>
</body>
</html>

Note that you must pay attention when you should/ should not include your scripts in the <head> tag vs including it at the end of the </body> tag. 请注意,在应该/不应该将脚本包括在<head>标记中而不是在</body>标记的末尾包含脚本时,您必须注意。 You can check this post for the best practice. 您可以查看此帖子以获得最佳实践。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM