gpt4 book ai didi

javascript - 调用 drawImage 函数时未捕获的 TypeError

转载 作者:数据小太阳 更新时间:2023-10-29 04:05:12 24 4
gpt4 key购买 nike

您好!

我正在尝试在 ReactJS 下使用 canvas 元素。当我调用 drawImage() 时出现错误。除了 drawImage().. 一切正常?

Uncaught TypeError: Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The provided value is not of type '(HTMLImageElement or HTMLVideoElement or HTMLCanvasElement or ImageBitmap)'

var Canvas = React.createClass({
componentDidUpdate: function() {
console.log("componentDidUpdate");
},

componentDidMount: function() {
var context = this.getDOMNode().getContext('2d');
this.paint(context);
},

paint: function(context) {
context.save();
context.fillStyle = '#F00';
context.fillRect(0, 0, 400, 400);
context.drawImage("image.jpg", 0, 0);
context.restore();
},

render: function(){
return <canvas width={400} height={400} />;
}
});

最佳答案

您确定在调用 drawImage() 之前已加载 image.jpg 吗?

来自 w3schools :

Note: You cannot call the drawImage() method before the image has loaded. To ensure that the image has been loaded, you can call drawImage() from window.onload() or from document.getElementById("imageID").onload.

关于javascript - 调用 drawImage 函数时未捕获的 TypeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31457605/

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