Canvas loses style when I create Fabric.js canvas object

自作多情 提交于 2020-01-07 02:53:08

问题


I have a Canvas Object with top and left attributes defined through with a previous JavaScript function but when I create a fabric Canvas object

  var fabricCanvas= new fabric.Canvas('mycanvas');

my HTML Canvas has top and left attributes set on 0.

I have tried setting the top and left attributes after the creation of fabric Canvas object through a script but when I do this the canvas changes position but the fabric function (selection and moving functions) remain where the canvas was located previously (where fabric Canvas has positioned it)!

What can I do to resolve this conflict? Is there a way to keep equal the canvas?


回答1:


When you create a canvas with Fabric.js it wraps it in a div and adds a second <canvas> (used to select the objects rendered in the first canvas). This is explained in the wiki page of Fabric.js. If you want to position your canvas apply CSS rules to the parent div element which is of the class "canvas-container".




回答2:


answer from my identical thread

apply

margin: 0 auto;

to class = canvas-container

canvas-container is automatically created by fabric.

see fiddle




回答3:


Please notice that if your using some kind of css preprocessor (for me it was scss stylesheet inside Angular) - targeting the "canvas-container" class inside the component scss file will not work. For me the workaround was the general project css file.



来源:https://stackoverflow.com/questions/9261918/canvas-loses-style-when-i-create-fabric-js-canvas-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!