[英]Uncaught TypeError: Cannot read property 'naturalWidth' of null - FabricJs - preview a video in Fabric.js via loadFromJSON
There is one issue I am facing while trying to preview video on a fabric.js canvas.尝试在 fabric.js canvas 上预览视频时,我遇到了一个问题。
FabricJS Version Tried: 4.0.0-beta.8 And 3.6.3 same error in both FabricJS 版本已尝试:4.0.0-beta.8 和 3.6.3 两者都有相同的错误
It gives error saying它给出了错误的说法
Uncaught TypeError: Cannot read property 'naturalWidth' of null :fabric.js:20490
Code for how I am adding video to canvas:我如何将视频添加到 canvas 的代码:
let objects = slideData.getObjects();
for (var i = 0; i < objects.length; i++) {
if (objects[i].hasOwnProperty('videoUrl')) {
var videoElement = createVideoElement(objects[i]['videoUrl']); //this function returns a video element
var video = new fabric.Image(videoElement, {left: objects[i]['left'], top: objects[i]['top'], isBackgroundVideo: true});
slideData.add(video);//adding video to canvas.
video.sendBackwards();//setting video to be on the back so I can show text in front of it
console.log(slideData);
}
}
Code for how I am trying to preview video on canvas:我如何尝试在 canvas 上预览视频的代码:
let jsonData = JSON.stringify(slideData.toObject(propertiesToSave)); //this is an array of properties which we want to save which includes isBackgroundVideo: true
slideData.loadFromJSON(jsonData, slideData.renderAll.bind(slideData), function(o, object) { //loading saved canvas data from JSON
object.set('selectable', false);
object.set('cursorHover', 'default');
let objects = slideData.getObjects();
for (var i = 0; i < objects.length; i++) {
if (objects[i].hasOwnProperty('isBackgroundVideo')) {
objects[i].getElement().play();
fabric.util.requestAnimFrame(function render() {
slideData.renderAll();
fabric.util.requestAnimFrame(render);
});
}
}
});
But when i execute this code then fabric.js gives error.但是当我执行这段代码时,fabric.js 会出错。
Uncaught TypeError: Cannot read property 'naturalWidth' of null :fabric.js:20490
I tried most solutions but it just does not work.我尝试了大多数解决方案,但它不起作用。 I am not sure what is wrong here.
我不确定这里有什么问题。 If I am doing something wrong or this is just a bug in fabric.js
如果我做错了什么或者这只是 fabric.js 中的一个错误
If you guys need more code then I can provide just comment what you need.如果你们需要更多代码,那么我可以提供您需要的评论。 Thanks
谢谢
Pay atention to the statement slideData.renderAll.bind(slideData)
.注意语句
slideData.renderAll.bind(slideData)
。
Is it possible that you dispose slideData
canvas somewhere else in your code?您是否可以在代码中的其他位置处理
slideData
canvas ?
After the disposal slideData.contextContainer
becomes null
.处置后
slideData.contextContainer
变为null
。
And there's a reference to slideData.contextContainer.naturalWidth
inside some internal fabric.js function (I don't know exactly, you can find function name in the error stack trace in web console. let's assume slideData.renderAll
). And there's a reference to
slideData.contextContainer.naturalWidth
inside some internal fabric.js function (I don't know exactly, you can find function name in the error stack trace in web console. let's assume slideData.renderAll
). It explains why do you get Cannot read property 'naturalWidth' of null
error.它解释了为什么您会收到
Cannot read property 'naturalWidth' of null
。
The fix depends on your situation.修复取决于您的情况。 For example, add a check
slideData.contextContainer !== null
or change the execution flow to call slideData.dispose()
in a different place.例如,添加检查
slideData.contextContainer !== null
或更改执行流程以在不同的位置调用slideData.dispose()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.