调用drawImage函数时未捕获TypeError

编程入门 行业动态 更新时间:2024-10-12 01:30:39
本文介绍了调用drawImage函数时未捕获TypeError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

Bonjour!

我正在尝试使用ReactJS下的canvas元素。当我调用drawImage()时,我收到一个错误。一切都工作,除了drawImage()..?

I'm trying to make use of canvas element under ReactJS. I'm getting an error when i call drawImage(). Everything work except the drawImage().. ?

未捕获TypeError:无法在'CanvasRenderingContext2D'上执行'drawImage':提供的值是不是类型'(HTMLImageElement或HTMLVideoElement或HTMLCanvasElement或ImageBitmap)'

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} />; } });

推荐答案

你确定image.jpg之前已加载你打电话给drawImage()?

Are you sure the image.jpg has been loaded before you call drawImage()?

来自 w3schools :

注意:在加载图像之前,不能调用drawImage()方法。要确保已加载图像,可以从window.onload()或document.getElementById(imageID)。onload调用drawImage()。

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.

更多推荐

调用drawImage函数时未捕获TypeError

本文发布于:2023-11-27 22:23:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639831.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   drawImage   TypeError

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!