fabric.js loadSVGFromString Uncaught TypeError: Cannot set property 'crossOrigin

410 阅读1分钟

fabric.js loadSVGFromString Uncaught TypeError: Cannot set property 'crossOrigin' of undefined

crossOriginfabric.js 的项目中,使用 canvas.toSVG() 生成的 SVG 代码,在通过 loadSVGFromString 来进行渲染,报错

错误内容

fabric.js:4477 Uncaught TypeError: Cannot set property 'crossOrigin' of undefined
    at Object.fabric.parseSVGDocument (fabric.js:4477)
    at Object.loadSVGFromString (fabric.js:4856)
    at testFabric4.html:225

原因:生成的 SVG 内容头部引入的 http: 链接,改成 https: 即可

svgContext = svgContext.replace(/http:\/\//g, 'https://')
fabric.loadSVGFromString(svgContext, () => {
    // ...  
})