I'm trying to display some shapes using the Fabric.js library. From the console, seems like the fabric.canvas is built correctly, but nothing is being rendered. My code:
HTML
<template>
<lightning-card title="testFabric" icon-name="custom:custom19">
<div class="slds-var-m-around_medium">
<canvas
width="500"
height="500"
id="c"
class="c"
lwc:dom="manual"
></canvas>
</div>
</lightning-card>
</template>
JS
import { LightningElement, api } from 'lwc';
import fabricLib from '@salesforce/resourceUrl/fabric';
import { loadScript } from 'lightning/platformResourceLoader';
export default class ShowShapes extends LightningElement {
@api recordId;
//retrieve canvase and context
renderedCallback() {
Promise.all([
loadScript(this, fabricLib + '/fabric.min.js')
])
.then(() => {
console.log(fabric?.Canvas, 'canvas');
let canvas = this.__canvas = new fabric.Canvas('c');
console.log(canvas, 'created canvas');
var rect = new fabric.Rect({left: 100,top: 50,fill: '#D81B60',width: 50,height: 50,strokeWidth: 2,stroke: "#880E4F",rx: 10,ry: 10,angle: 45,scaleX: 3,scaleY: 3,hasControls: true});
canvas.add(rect);
console.log(canvas);
canvas.requestRenderAll();
canvas.renderAll();
})
.catch((error) => {
console.log(error);
this.dispatchEvent(
new ShowToastEvent({
title: 'Error loading Fabric',
message: error.message,
variant: 'error'
})
);
});
}
}