javascript - drawImage() 不工作
问题描述
当我在 chrome 中打开 html 文件时,我只看到背景,我打开控制台,我看到“hi”曾经框架但我看不到我使用 drawImage() “绘制”的图像`
var ctx = document.querySelector('Canvas').getContext('2d')
function draw() {
ctx.drawImage(document.getElementById('Mario'), 50, 61, 0, 0)
window.requestAnimationFrame(draw)
console.log('hi')
}
draw()
#Canvas {
width: 100%;
height: 100%;
background: url(Pictures/Background.jpg);
margin: -8px;
}
<canvas id='Canvas'></canvas>
<div style='display:none;'>
<img id='Mario' src='Pictures/Mario.png'>
</div>
解决方案
这是你的问题:
ctx.drawImage(document.getElementById('Mario'), 50, 61, 0, 0);
您已将图像宽度和高度设置为0
. 改为:
ctx.drawImage(document.getElementById('Mario'), 0, 0, 50, 61);
推荐阅读
- docker - 无法在 Raspberry Pi 4 上安装 apt-transport-https
- python - python - 如何在python中将字符串连接到每个JSON节点结果?
- python - Python if 语句不调用函数?
- haskell - 如何返回元组列表的最小值元组?
- javascript - Javascript 从浏览器检查应用程序是否已安装或打开应用程序 (ios)
- python - 结合Splitline()、Enumerate和RegEx拉取数据
- html - CSS选择器以不在另一个元素中的img为目标
- javascript - 提交按钮不验证表单 - Parsley js
- powershell - Powershell if 语句
- r - fread 无法读取 csv 单元格中带有双引号的文件