简体   繁体   English

未捕获的类型错误:无法读取 null 的属性“naturalWidth” - FabricJs - 通过 loadFromJSON 在 Fabric.js 中预览视频

[英]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.

相关问题 Fabricjs - 通过 loadFromJSON 在 FabricJs 中预览视频 - Fabricjs - Preview Video in FabricJs via loadFromJSON 用于canvas.loadFromJSON的Fabric.js和oCoords - Fabric.js and oCoords for canvas.loadFromJSON Fabric.js loadFromJSON无法与RGBBA填充一起使用 - Fabric.js loadFromJSON is not working with rgba fill typeahead.js:未被捕获的TypeError:无法读取null的属性“ length” - typeahead.js: Uncaught TypeError: Cannot read property 'length' of null leaflet.js:6未捕获的TypeError:无法读取null的属性“ lat” - leaflet.js:6 Uncaught TypeError: Cannot read property 'lat' of null 未被捕获的TypeError:无法读取空Knockout js的属性“ map” - Uncaught TypeError: Cannot read property 'map' of null Knockout js mapbox gl js-未捕获的TypeError:无法读取null的属性“ classList” - mapbox gl js - Uncaught TypeError: Cannot read property 'classList' of null 未捕获(承诺)类型错误:无法读取 null 的属性“追加” - JS - Uncaught (in promise) TypeError: Cannot read property 'append' of null - JS Ember JS:Uncaught TypeError:无法读取属性&#39;createRecord&#39;的null - Ember JS : Uncaught TypeError: Cannot read property 'createRecord' of null 未捕获的类型错误:无法在 rating.js:4 处读取 null 的属性“长度” - Uncaught TypeError: Cannot read property 'length' of null at rating.js:4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM