我有一个像这样呈现的自定义图像控件
<img id="2344324_Img" />
我在HTML 5画布对象中访问此图像,如下所示:
var imgOne = new Image();
imgOne = document.getElementById('2344324_Img');
imgOne.onload = function() {
// ..some code
canvas.drawImage(this, 20, 20);
};
这是正确的方法吗?
答案 0 :(得分:3)
如果您的标记中已有<img>
标记并打算使用getElementById()
访问该标记,则无需致电new Image()
:
<img id="2344324_Img"/>
<script type='text/javascript'>
// Don't need this...
//var imgOne = new Image();
// Instead just do:
var imgOne = document.getElementById('2344324_Img');
imgOne.onload = function() {
..some code
canvas.drawImage(this, 20, 20);
// Should be accessible either via `this` or `imgOne`
//canvas.drawImage(imgOne, 20, 20);
}
</script>