Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting rid of rotation control in Fabric.js

Tags:

fabricjs

In this jsfiddle I have a Fabric.js text created with lockRotation: true to avoid rotation. The line disappeared, but the box control to rotate is still there, how to get rid of it?

HTML

<canvas id="c" width="300" height="300"></canvas>

Javascript

var canvas = new fabric.Canvas('c');

var text = new fabric.IText('This is the text',{
    left: 50,
    top: 50,
    fontFamily: 'Arial',
    fontWeight: 'normal',
    fontSize: 18,
    lockRotation: true,
    stroke: 'black',
    fill: 'black'
});
like image 402
ps0604 Avatar asked Jul 06 '18 15:07

ps0604


2 Answers

The accepted answer is no longer valid with Fabricjs versions from 4.0 (see changelog). hasRotatingPoint has been removed in favor of a more comprehensive controls api, although the documentation for it is still very slim.

To hide a certain control from a specific object, you have to redefine its controls property. Here is how you would hide the mtr control (Rotate):

text.controls = {
    ...fabric.Text.prototype.controls,
    mtr: new fabric.Control({ visible: false })
}

var canvas = new fabric.Canvas('c');
var text = new fabric.IText('This is the text',{
    left: 50,
    top: 50,
    fontFamily: 'Arial',
    fontWeight: 'normal',
    fontSize: 18,
    stroke: 'black',
    fill: 'black'
});
text.controls = {
    ...fabric.Text.prototype.controls,
    mtr: new fabric.Control({ visible: false })
}

canvas.add(text);
<script src="https://rawgit.com/kangax/fabric.js/master/dist/fabric.js"></script>
<canvas id="c" width="300" height="300"></canvas>

Here we create a new controls object using the default ones for Text but redefining the mtr set.

like image 110
Ben Avatar answered Sep 27 '22 00:09

Ben


Use object.hasRotatingPoint = false; to hide rotating point.

var canvas = new fabric.Canvas('c');
var text = new fabric.IText('This is the text',{
    left: 50,
    top: 50,
    fontFamily: 'Arial',
    fontWeight: 'normal',
    fontSize: 18,
    hasRotatingPoint: false,
    stroke: 'black',
    fill: 'black'
});

canvas.add(text);
<script src="https://rawgit.com/kangax/fabric.js/master/dist/fabric.js"></script>
<canvas id="c" width="300" height="300"></canvas>
like image 40
Durga Avatar answered Sep 23 '22 00:09

Durga