未捕获的TypeError:无法在字符串'wtd_background_image'

时间:2017-03-29 15:07:49

标签: javascript jquery html5 canvas fabricjs

我的wordpress插件适用于fabric.js 1.2.0,当我将其更改为使用fabric.js 1.7.9时,它会在插件页面以外的所有页面上抛出错误:

  

无法在字符串'wtd_background_image'

上创建属性'style'

在以下一行:

canvasObj= new fabric.Canvas('wtd_background_image');

enter image description here

1 个答案:

答案 0 :(得分:5)

您需要在所有网页上创建ID为wtd_background_image的HTML Canvas元素。

let canvas = new Canvas('wtd_background_image');
let canvasObj = new fabric.Canvas('wtd_background_image');

function Canvas(id) {
    this.canvas = document.createElement('canvas');
    this.canvas.id = id;
    document.body.appendChild(this.canvas);
    return this.canvas;
}

// for demonstration
let circle = new fabric.Circle({radius: 20, fill: '#07C', left: 30, top: 30});
canvasObj.add(circle);
<script src="http://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.7.9/fabric.min.js"></script>