简体   繁体   中英

canvas not rendering shape after adding new shape

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

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

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()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM