gpt4 book ai didi

JavaScript 运行时错误 : Unspecified error

转载 作者:行者123 更新时间:2023-12-02 16:12:06 24 4
gpt4 key购买 nike

我的要求是返回数据URL。但是,当我运行该应用程序时,出现运行时错误:

JavaScript runtime error: Unspecified error.

这是我使用过的代码。临时路径是图像所在的路径。

var canvas = document.createElement("canvas");   
var context = canvas.getContext("2d");
var img = new Image();
img.src = "@tempPath";
context.drawImage(img, 40, 40);
var dataURL = canvas.toDataURL("image/jpeg");
alert(dataURL);'

最佳答案

尝试以下代码,它对我有用:

<body>
<canvas id="myCanvas"></canvas>
<img id="profileImg" alt=""/>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
drawImg();
});
function drawImg() {
try {
var canvas = document.getElementById("myCanvas");
var context = canvas.getContext("2d");
var img = new Image();
img.src = $('#profileImg');
context.drawImage(img, 40, 40);
var dataURL = canvas.toDataURL("image/jpeg");
alert(dataURL);
} catch (e) {
if (e.name == "NS_ERROR_NOT_AVAILABLE") {
// This is a bug in Firefox. The easiest fix is to simply keep trying until the error goes away,
//since no event fires at the correct time.
// Wait before trying again; you can change the length of this delay.
setTimeout(drawImg, 100);
} else {
throw e;
}
}
}
</script>
</body>

与 IE 也能很好地配合。希望这会有所帮助。

关于JavaScript 运行时错误 : Unspecified error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30068074/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com