为了账号安全,请及时绑定邮箱和手机立即绑定

[Web浏览器] "Uncaught InvalidStateError: Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The HTMLImageElement provided is in the 'broken' state."

这是因为啥啊???求教"Uncaught InvalidStateError: Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The HTMLImageElement provided is in the 'broken' state."


var can1;

var can2;


var ctx1;

var ctx2;


var canWidth;

var canHeight;


var lastTime;

var deltaTime;


var bgPic = new Image();


document.body.onload = game;

function game()

{

init();

lastTime = Date.now();

deltaTime = 0;

gameloop();

}

function init()

{

//获得canvas can text

can1 = document.getElementById("canvas1");

ctx1 = can1.getContext('2d');

can2 = document.getElementById("canvas2");

ctx2 = can2.getContext('2d');

bgPic.src = "./src/background.jpg";

canWidth = can1.width;

canHeight = can1.height;

}

function gameloop()

{

window.requestAnimationFrame(gameloop);

var now = Date.now();

deltaTime=now - lastTime;

lastTime = now;

drawBackground();

}



正在回答

1 回答

ctx1.drawImage(img,100,100)调用该方法时img元素未获取

解决方法:

  1. html页面添加<img id='imgS' src="">标签

  2. 添加img= document.getElementById("imgS")获取图片语句;  


0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

[Web浏览器] "Uncaught InvalidStateError: Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The HTMLImageElement provided is in the 'broken' state."

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信