0
votes

I have a problem with fabric.js. I can use fabric.version, but after new fabric.Rect{top : 100,left : 100,width : 120,height : 30, fill : 'red'} and use canvas .add it, the rect not show but mouseEvent is ok. my fabric.js version is 3.4.0

@angular/core": "~7.2.0","fabric": "^3.4.0"

  ngOnInit() {
    let canva1 = new fabric.Canvas('myCanvas');
    console.log(fabric.version);
    let rect = new fabric.Rect({
      top : 100,
      left : 100,
      width : 120,
      height : 30,
      fill : 'red'
  });
  canva1.add(rect);
  }

I expect a rect show on the canvas but no rect show

2
you can also vist my gitHub github.com/zhangManGod/canvas.git to show this question - 张艺炜

2 Answers

0
votes

You have style="background: white" on your canvas element and it just blends with the background.

https://github.com/zhangManGod/canvas/blob/master/src/app/right/right.component.html#L3

0
votes

Unless you have created your canvas with renderOnAddRemove = true (http://fabricjs.com/docs/fabric.Canvas.html#renderOnAddRemove) then you will need to call requestRenderAll on your canvas after you have added or removed items(s).

(http://fabricjs.com/docs/fabric.Canvas.html#requestRenderAll)

ngOnInit() 
{
    let canva1 = new fabric.Canvas('myCanvas');
    console.log(fabric.version);
    let rect = new fabric.Rect(
    {
        top : 100,
        left : 100,
        width : 120,
        height : 30,
        fill : 'red'
    });
    canva1.add(rect);
    canva1.requestRenderAll()
}