HTML5/JS不在画布中渲染图像
HTML5/JS Not Rendering Image in Canvas.
我正在一个web开发实验室工作,但我的图像没有显示出来。我参考图片时有没有做错什么?以下是图像本身的链接:http://tuftsdev.github.com/WebProgramming/assignments/pacman10-hp-sprite.png
注意:我将图像复制到了本地目录中,所以我知道引用是正确的。
<!DOCTYPE html>
<html>
<head>
<title>Ms. Pacman</title>
<script>
function draw() {
canvas = document.getElementById('simple');
// Check if canvas is supported on browser
if (canvas.getContext) {
ctx = canvas.getContext('2d');
var img = new Image();
img.src = '"pacman10-hp-sprite.png';
ctx.drawImage(img, 10, 10);
}
else {
alert('Sorry, canvas is not supported on your browser!');
}
}
</script>
</head>
<body onload="draw();">
<canvas id="simple" width="800" height="800"></canvas>
</body>
</html>
您需要设置一个回调,并在图像实际加载后将图像绘制到画布上:
function draw() {
canvas = document.getElementById('simple');
// Check if canvas is supported on browser
if (canvas.getContext) {
ctx = canvas.getContext('2d');
var img = new Image();
// If you don't set this callback before you assign
// the img.src, the call ctx.drawImage will have
// a null img element. That's why it was failing before
img.onload = function(){
ctx.drawImage(this, 10, 10);
};
img.src = "pacman10-hp-sprite.png";
} else {
alert('Sorry, canvas is not supported on your browser!');
}
}
相关文章:
- 如何在Edge中下载图像/png数据URI
- 阵列中随机图像的问题
- 如何在另一个html文件的框架中包含图像
- 在javascript中调整图像大小
- 防止在AngularJs中渲染图像
- 无法在java脚本中调用图像的点击事件函数
- 以Jquery为中心的Div中的图像对齐
- 如何通过资产管道提供Javascript中子文件夹中的图像
- 使用AssetPipeline如何在Javascript中提供图像
- 如何使用javascript从文件夹中获取图像并发布直接链接
- 通过Webpack导入模块中的图像
- 显示JSON数据中的图像
- 使用javascript从HTML网页中提取图像url
- 如何将JSON结果设置为'src'共'img'以在ASP.NET MVC4中显示图像
- 调整节点js中的图像大小
- 使用jquery Find()在复选框中查找图像
- 如何在fabricjs中嵌入图像base64将画布转换为SVG
- 如何在Node.js中调整图像大小
- 无法在cakepp布局中显示图像
- 当我点击MVC视图中的图像时,如何打开另一个页面