【问题标题】:drawImage() is not workingdrawImage() 不工作
【发布时间】:2018-10-17 19:45:28
【问题描述】:

当我在 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>

【问题讨论】:

  • 错误的选择器,对于初学者来说。

标签: javascript html5-canvas requestanimationframe


【解决方案1】:

这是你的问题:

ctx.drawImage(document.getElementById('Mario'), 50, 61, 0, 0);

您已将图像宽度和高度设置为0。改为:

ctx.drawImage(document.getElementById('Mario'), 0, 0, 50, 61);

【讨论】:

  • 对不起,我把错误和更正切换了。它应该工作。检查小提琴:jsfiddle.net/tceqmprx/1
  • 好吧,它成功了,但为什么图片质量看起来这么差?
  • 如果你的意思是在我的例子中,那是因为像素大小与传递给drawImage()的尺寸不匹配
  • 不,我的意思是在我的照片中,一切看起来都很糟糕
  • 也许它与 window.requestAnimationFrame 有关系,因为它一开始很好,然后变得更糟
猜你喜欢
  • 2013-02-09
  • 2012-03-22
  • 1970-01-01
  • 1970-01-01
  • 2012-10-18
  • 2014-12-20
  • 1970-01-01
  • 2022-09-30
相关资源
最近更新 更多