使用wpaint绘图软件时:Uncaught DOMException: Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The image argument is a canvas element with a width or height of 0.
一、总结
一句话总结:通过删除法和最简单模式找错,发现是style="display: none;"这个位置引发的错误
1 <div class="student_note_type_item" id="student_note_type_picture1" style="display: none;"> 2 {include file="common/wpaint" /} 3 </div>
1、Uncaught DOMException表示什么?
Uncaught DOMException表明未获取dom元素
2、排查错误的方法?
删除法:删除法删除代码以方便找到冲突代码
(因为有些情况下是好的,多了代码就出错,哪肯定是多的代码的问题)
最简单模式:查看最简单模式下是不是好的,如果是,逐步增加代码查找冲突源
3、使用wpaint绘图软件时:Uncaught DOMException出错的教训是什么?
给外部插件加的样式也很有可能是外部插件出错的原因
就是外部插件出错,不仅可能是js错误,css错误也非常常见
二、Uncaught DOMException: Failed to execute 'drawImage' on 'CanvasRenderingContext2D':
问题:
Uncaught DOMException: Failed to execute 'drawImage' on 'CanvasRenderingContext2D':The HTMLImageElement provided is in the 'broken' state.
分析:
Uncaught DOMException表明未获取dom元素
The HTMLImageElement provided is in the 'broken' state.表明元素在获取过程中被中断了,也即元素可能没有正确获取
图片路径不正确导致图片未正确获取也将导致该错误
代码:
HTML文件
<!DOCTYPE>
<html>
<head>
<mata name="viewport" content="width=device-width,initial-scale=1">
<script type="text/javascript" src="js/jquery-1.11.0.min.js"></script>
</head>
<body>
<div>
<canvas id="start" width="800" height="600">
</canvas>
</div>
<img src="./img/girl.jpg" id="img" style="display:none"/> <!--正确路径-->
<script type="text/javascript" src="js/commonFunctions.js"></script>
<script type="text/javascript" src="js/star.js"></script>
</body>
</html>
start.js:
var can;
var ctx;
var width;
var height;
var imgGirl=new Image();
// $(document).ready(function(){
// init();
// })
document.body.onload=init;
function init(){
can=document.getElementById("start");
ctx=can.getContext("2d");
width=can.width;
height=can.height;
imgGirl.src="../img/girl.jpg"//注意文件路径 正确路径为:./img/girl.jpg
gameLoop();
}
function drawBg(){
ctx.fillStyle="#393550";
ctx.fillRect(0,0,width,height);
}
/** [gameLoop 刷新画布] */
function gameLoop(){
window.requestAnimFrame(gameLoop);
drawBg();
drawImg();
}
//根据设备性能进行调用
function drawImg(){
// star.js:39 Uncaught DOMException: Failed to execute 'drawImage' on 'CanvasRenderingContext2D':
// The HTMLImageElement provided is in the 'broken' state.
// 分析:Uncaught DOMException表明未获取dom元素
// The HTMLImageElement provided is in the 'broken' state.表明元素在获取过程中被中断了,也即元素可能还在加载中
ctx.drawImage(imgGirl,100,100)
}
解决办法:
先判断图片路径在js中是否正确:
在html中添加img标签,通过js赋值看是否可以正常显示,若可以则路径正确
参考:Uncaught DOMException: Failed to execute 'drawImage' on 'CanvasRenderingContext2D': - CSDN博客
https://blog.csdn.net/tjj3027/article/details/78914071
https://blog.csdn.net/tjj3027/article/details/78914071
三、使用wpaint绘图软件时:Uncaught DOMException: Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The image argument is a canvas element with a width or height of 0.
通过删除法和最简单模式逐步缩小错误代码范围找错,发现是style="display: none;"这个位置引发的错误
1 <div class="student_note_type_item" id="student_note_type_picture1" style="display: none;">
2 {include file="common/wpaint" /}
3 </div>
{include file="common/wpaint" /}里面是wpaint