Question

Here i am trying to set object on center of the canvas.Fit center command is working good.Having issue with fit to left center command. see the bellow snapshot for fit center command.when i scale the text in larger size and set it center its working good.

Demo Link:- Click Here

Fit to center

But when i scale same object and run the command Fit To left center you can see here object is going outside of the center origon y of the grid.

Fit to left center

And when i set default text (without scale) and run the command Fit To left center its working good like bellow snap shot.

Fit to left center without scale

Bellow is my sample code to fit object in center left of canvas

$("#fitleft").click(function() {
            $("#fitleft").css('display','none');
   $("#fitright").css('display','block');   

   var obj = canvas.getActiveObject();



    obj.set( {
              left: 315.4000015258789,
             // top: Top
             top: obj.get('top')  
               });        

            obj.setCoords();
                      canvas.renderAll();



   });
Was it helpful?

Solution

I have made the answer for this

Demo Link:- Click here

document.getElementById('fticenter').addEventListener('click', function (e) {
   var obj = canvas.getActiveObject();
     obj.center();

     obj.setCoords();


     canvas.renderAll();


});

document.getElementById('fittoleftcenter').addEventListener('click', function (e) {




    var obj = canvas.getActiveObject();

           var leftcenter=    canvas.width/2
        var halfleft = obj.currentWidth /2; 


       obj.set("left", leftcenter - halfleft);
       obj.set("top", obj.get('top'));   

            obj.setCoords();
                      canvas.renderAll();



   });


document.getElementById('fittorightcenter').addEventListener('click', function (e) {




    var obj = canvas.getActiveObject();
    var rightcenter=    canvas.width/2
        var halfright = obj.currentWidth /2; 
      obj.set("left", rightcenter - halfright);
       obj.set("top", obj.get('top'));    

            obj.setCoords();
                      canvas.renderAll();



   });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top