html画布绘制图像不起作用
html canvas draw image not working
我学习了W3学校的教程,最终编写了以下代码,这些代码不需要在画布元素中复制图像。画布为空。。。
<img id="image" src="assets/images/bw.jpg" width="200" height="276" alt="" />
<canvas id="canvas" width="220" height="286"></canvas>
Javascript如下:
var c=document.getElementById("canvas");
var ctx=c.getContext("2d");
var img = document.getElementById("image");
ctx.drawImage(img,0,0);
为什么这个简单的例子不起作用???
尝试在.drawImage()
调用中包含width
、height
;即
正如@Kaiido耐心指出的那样,实际的解决方案是使用<img>
元素附带的onload
事件属性
<img id="image" onload="draw()" src="http://lorempixel.com/50/50" width="200" height="276" alt="" />
<canvas id="canvas" width="220" height="286"></canvas>
<script>
function draw() {
var c=document.getElementById("canvas");
var ctx=c.getContext("2d");
var img = document.getElementById("image");
ctx.drawImage(img,0,0);
}
</script>
或HTMLImageElement.addEventListener("load", handler)
相关文章:
- 本地存储功能中的图像不起作用
- 简单的 Java 脚本背景图像不起作用
- html画布绘制图像不起作用
- jquery选择图像不起作用
- 在html画布代码中插入图像不起作用
- 设置来自 javascript 的背景图像不起作用
- 在剑道 UI 树视图项旁边添加图像不起作用
- 尝试使用 jquery 加载随机图像 -- 不起作用
- ajax 响应包括 image,onClick 事件对图像不起作用 jQuery
- 在图像不起作用的 src 中添加 jQuery 变量
- 显示数组中的图像不起作用
- 使用 jQuery 替换背景图像不起作用
- 淡入淡出 JavaScript 中的图像不起作用
- GD库裁剪图像不起作用
- 画布绘制图像不起作用,根本没有错误
- Vue.js 数据绑定样式背景图像不起作用
- 为什么我的可点击图像不起作用
- Javascript set src 属性的图像不起作用
- 画布绘制图像不起作用
- SVG作为Div的内联背景图像不起作用