Canvas loses style when I create Fabric.js canvas object
Asked Answered
B

5

3

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?

Bop answered 13/2, 2012 at 14:8 Comment(0)
S
5

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".

Stalingrad answered 7/9, 2012 at 14:46 Comment(3)
I was unable to over-ride fabric's positioning of the canvas.Fayth
canvas-container class is not overriding, its not applying any css to this div.Incommensurable
@PiyushDholariya I think you are using the CSS in js or something that scoped CSS, actually, I was using fabric with vue.js and facing the same issue but when I moved my canvas-container CSS rule to global file it worksCarlsbad
F
0

answer from my identical thread

apply

margin: 0 auto;

to class = canvas-container

canvas-container is automatically created by fabric.

see fiddle

Fayth answered 26/2, 2013 at 14:21 Comment(1)
canvas-container class is not overriding, its not applying any css to this divIncommensurable
R
0

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.

Ringleader answered 17/2, 2019 at 7:48 Comment(0)
R
0

You may override the canvas-container class upon initialization like so:

new fabric.Canvas('canvasId', {containerClass: 'custom-container-class'});

http://fabricjs.com/docs/fabric.Canvas.html#containerClass

Rhapsody answered 18/2, 2022 at 16:37 Comment(0)
C
0

I was able to override the style of the canvas-container by setting the element style after DOM was loaded.

HTML

<canvas id="canvas"></canvas>

JAVASCPRIT

const fabricCanvas = new fabric.Canvas("canvas", { containerClass: "myFabricCanvas"})


document.addEventListener('DOMContentLoaded', async function () {
document.getElementsByClassName('myFabricCanvas')[0].style.position="absolute"
document.getElementsByClassName('myFabricCanvas')[0].style.top=0
document.getElementsByClassName('myFabricCanvas')[0].style.left=0
});
Catlee answered 17/4, 2024 at 14:11 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.