You are reading the article How To Disable The Selectability Of Itext Using Fabricjs? updated in December 2023 on the website Achiashop.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested January 2024 How To Disable The Selectability Of Itext Using Fabricjs?
In this tutorial, we are going to learn how to disable the selectability of IText using FabricJS. The IText class was introduced in FabricJS version 1.4, extends chúng tôi and is used to create IText instances. An IText instance gives us the freedom to select, cut, paste or add new text without additional configurations. There are also various supported key combinations and mouse/touch combinations which make text interactive which are not provided in Text.
Textbox, however, which is based on IText allows us to resize the text rectangle and wraps lines automatically. This is not true for IText as height is not adjusted based on the wrapping of lines. We can manipulate our IText object by using various properties. In order to modify an object, we have to select it in FabricJS. However, we can change this behaviour by using the selectable property.
Syntax new fabric.IText(text: String, { selectable: Boolean }: Object) Parameters
text − This parameter accepts a String which is the text string that we want to display inside our IText object.
options (optional) − This parameter is an Object which provides additional customizations to our object. Using this parameter colour, cursor, stroke width and a lot of other properties can be changed related to the object of which selectable is a property.
Options Keys
selectable − This property accepts a Boolean value. When a ‘false’ value is assigned to it, the object cannot be selected for modifications. Its default value is true.
Example 1Default behaviour or when selectable property is set to ‘true’
Let’s see a code example to understand how the object behaves when by default the selectable property is set to true. We are allowed to select an object, move it around the canvas and make modifications to it when the selectable property is set to true.
var
canvas
=
new
fabric
.
Canvas
(
“canvas”
)
;
canvas
.
setWidth
(
document
.
body
.
scrollWidth
)
;
canvas
.
setHeight
(
250
)
;
var
itext
=
new
fabric
.
IText
(
width
:
300
,
left
:
50
,
top
:
70
,
fill
:
“#6ae18b”
,
}
)
;
canvas
.
add
(
itext
)
;
Example 2Passing selectable property as key with “false” value
In this example, we are assigning a false value to the selectable property. This means that we can no longer select the IText object for modifications.
var
canvas
=
new
fabric
.
Canvas
(
“canvas”
)
;
canvas
.
setWidth
(
document
.
body
.
scrollWidth
)
;
canvas
.
setHeight
(
250
)
;
var
itext
=
new
fabric
.
IText
(
width
:
300
,
left
:
50
,
top
:
70
,
fill
:
“#6ae18b”
,
selectable
:
false
,
}
)
;
canvas
.
add
(
itext
)
;
You're reading How To Disable The Selectability Of Itext Using Fabricjs?
How To Set The Horizontal Origin Of Transformation Of Text Using Fabricjs?
In this tutorial, we are going to learn how to set the horizontal origin of transformation of Text using FabricJS. We can display text on canvas by adding an instance of fabric.Text. Not only does it allow us to move, scale and change the dimensions of the text but it also provides additional functionality like text alignment, text decoration, line height which can be obtained by the properties textAlign, underline and lineHeight respectively. Similarly we can also set the horizontal origin of transformation by using the originX property.
Syntax new fabric.Text(text: String , { originX : String }: Object) Parameters
text − This parameter accepts a String which is the text string that we want to display.
options (optional) − This parameter is an Object which provides additional customizations to our text. Using this parameter colour, cursor, border width and a lot of other properties can be changed related to the object of which originX is a property.
Options Keys
originX − This property accepts a String value which allows us to set the horizontal origin of transformation. The possible values are “left”, “right”, and “center”. Its default value is “left”.
Example 1Default appearance of the Text object
Let’s see a code example to see how our text object looks when the originX property is not used. In this case, the horizontal origin of transformation will be left which is also the default.
var
canvas
=
new
fabric
.
Canvas
(
“canvas”
)
;
canvas
.
setWidth
(
document
.
body
.
scrollWidth
)
;
canvas
.
setHeight
(
250
)
;
width
:
300
,
left
:
60
,
top
:
70
,
fill
:
“green”
,
}
)
;
canvas
.
add
(
text
)
;
Example 2Passing the originX property as key with a value
In this example, we will see how assigning a value to the originX property changes the horizontal origin of transformation. We have used two text objects in this example to show the difference. In our first text object, since we have passed the value as “right”, the horizontal origin of transformation now lies towards the right. Same left property of 200 is applied to both text but still they are in different horizontal position because of change in the horizontal origin of transformation.
var
canvas
=
new
fabric
.
Canvas
(
“canvas”
)
;
canvas
.
setWidth
(
document
.
body
.
scrollWidth
)
;
canvas
.
setHeight
(
250
)
;
var
text1
=
new
fabric
.
Text
(
“Text 1”
,
{
width
:
300
,
left
:
200
,
top
:
70
,
fill
:
“green”
,
originX
:
“right”
,
}
)
;
var
text2
=
new
fabric
.
Text
(
“Text 2”
,
{
width
:
300
,
left
:
60
,
top
:
70
,
fill
:
“red”
,
}
)
;
canvas
.
add
(
text1
)
;
canvas
.
add
(
text2
)
;
How To Set The Size Of Superscript With Text Using Fabricjs?
In this tutorial, we are going to learn how to set the size of superscript with Text using FabricJS. We can display text on canvas by adding an instance of fabric.Text. Not only does it allow us to move, scale and change the dimensions of the text but it also provides additional functionality like text alignment, text decoration, line height which can be obtained by the properties textAlign, underline and lineHeight respectively. We can also use the superscript property where we can specify its size.
Syntax new fabric.Text(text: String , { superscript : {“size”: Number, "baseline": Number}: Object }: Object) Parameters
text − This parameter accepts a String which is the text string that we want to display.
options (optional) − This parameter is an Object which provides additional customizations to our text. Using this parameter colour, cursor, border width and a lot of other properties can be changed related to the object of which superscript is a property.
Options Keys
superscript − This property accepts an Object as value. Inside this object we can specify the size and baseline of our superscript text. The size determines how small our superscript will be whereas the baseline value determines how further below our superscript will be placed. The default value for size is 0.6.
Example 1Appearance of the Text object when default value of size is used
Let’s see a code example to see how our text object looks when the default value of size is used. In this case, our superscript text will have its default size value.
var
canvas
=
new
fabric
.
Canvas
(
“canvas”
)
;
canvas
.
setWidth
(
document
.
body
.
scrollWidth
)
;
canvas
.
setHeight
(
250
)
;
width
:
300
,
left
:
50
,
top
:
70
,
fill
:
“green”
,
superscript
:
{
“size”
:
0.6
}
}
)
;
text
.
setSuperscript
(
0
,
4
)
canvas
.
add
(
text
)
;
Example 2Using the setSuperscript method with superscript property
In this example, we will see how by using the setSuperscript method in conjunction with the superscript property we can manipulate the size of our superscript. Here we have specified the size as 0.3, which is our fontSize factor, which essentially makes the size of our superscript text as 12px (0.3*40=12).
var
canvas
=
new
fabric
.
Canvas
(
“canvas”
)
;
canvas
.
setWidth
(
document
.
body
.
scrollWidth
)
;
canvas
.
setHeight
(
250
)
;
width
:
300
,
left
:
50
,
top
:
70
,
fill
:
“green”
,
fontSize
:
40
,
superscript
:
{
“size”
:
0.3
}
}
)
;
text
.
setSuperscript
(
0
,
4
)
canvas
.
add
(
text
)
;
How To Set The Duration Of Animation On A Line Using Fabricjs?
In this tutorial, we are going to learn how to set the duration of animation on a Line using FabricJS. A Line element is one of the basic elements provided in FabricJS. It is used for creating straight lines. Because line elements are geometrically one-dimensional and do not contain an interior, they are never filled. We can create a line object by creating an instance of fabric.Line, specifying the x and y coordinates of the line and adding it to the canvas. In order to change the duration of animated text we use the duration property.
Syntax Parameters
property − This property accepts a String or Object value which determines which properties we want to animate.
value − This property accepts a Number or Object value which determines the values to animate properties.
Options Keys
duration − This property accepts a Number. It can be used to change the duration of an animation. The default value is 500ms.
Using default value of duration property ExampleLet’s see a code example to see how our line object looks when the animate method is used in conjunction with the default value of duration property. In this case, the left animation will have a duration of 500ms
var
canvas
=
new
fabric
.
Canvas
(
“canvas”
)
;
canvas
.
setWidth
(
document
.
body
.
scrollWidth
)
;
canvas
.
setHeight
(
250
)
;
var
line
=
new
fabric
.
Line
(
[
200
,
100
,
100
,
40
]
,
{
stroke
:
“blue”
,
strokeWidth
:
20
,
}
)
;
line
.
animate
(
“left”
,
“+=100”
,
{
onChange
:
canvas
.
renderAll
.
bind
(
canvas
)
,
duration
:
500
,
}
)
;
line
.
animate
(
“angle”
,
“90”
,
{
onChange
:
canvas
.
renderAll
.
bind
(
canvas
)
,
}
)
;
canvas
.
add
(
line
)
;
Using a custom value for duration property ExampleIn this example, we will see how by using the animate method along with the duration property, we can control the time for which an animation occurs. Here, we have specified the duration as 1000ms which means, our left animation will occur for 1 second.
var
canvas
=
new
fabric
.
Canvas
(
“canvas”
)
;
canvas
.
setWidth
(
document
.
body
.
scrollWidth
)
;
canvas
.
setHeight
(
250
)
;
var
line
=
new
fabric
.
Line
(
[
200
,
100
,
100
,
40
]
,
{
stroke
:
“blue”
,
strokeWidth
:
20
,
}
)
;
line
.
animate
(
“left”
,
“+=100”
,
{
onChange
:
canvas
.
renderAll
.
bind
(
canvas
)
,
duration
:
1000
,
}
)
;
line
.
animate
(
“angle”
,
“90”
,
{
onChange
:
canvas
.
renderAll
.
bind
(
canvas
)
,
}
)
;
canvas
.
add
(
line
)
;
How To Add The Coordinates In A Polygon Using Fabricjs?
We can create a Polygon object by creating an instance of fabric.Polygon. A polygon object can be characterized by any closed shape consisting of a set of connected straight line segments. Since it is one of the basic elements of FabricJS, we can also easily customize it by applying properties like angle, opacity etc. We can add the coordinates in a polygon by using points property.
Syntax new fabric.Polygon( points: Array, { points: Array }: Object ) Parameters
points − This parameter accepts an Array which denotes the array of points that make up the polygon object.
options (optional) − This parameter is an Object which provides additional customizations to our object. Using this parameter origin, stroke width and a lot of other properties can be changed related to the Polygon object of which points is a property.
Options Keys
points − This property accepts an Array which allows us to set the points array.
Example 1: Default Appearance of Polygon ObjectLet’s see a code example of how we can add a polygon object to our canvas. In this case we have not used the points property. We have specified the points array as the first argument itself which denotes the coordinate values to be used.
var canvas = new fabric.Canvas(“canvas”); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250);
var polygon = new fabric.Polygon( [ { x: -20, y: -35 }, { x: 20, y: -35 }, { x: 40, y: 0 }, { x: 20, y: 35 }, { x: -20, y: 35 }, { x: -40, y: 0 }, ], { top: 50, left: 50, } );
canvas.add(polygon);
Example 2: Using the Points PropertyIn this example, we have used the points property and assigned it an Array that consists of the coordinate values of the polygon where each point is an object with “x” and “y” values. As it can be seen, although we have already specified the points of the Polygon object, as soon as we use the points property, it overrides those values and the new coordinates are used.
var canvas = new fabric.Canvas(“canvas”); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250);
var points = [ { x: 0, y: 30 }, { x: 30, y: 30 }, { x: 30, y: 0 }, { x: 0, y: 0 }, ];
var polygon = new fabric.Polygon(points, { top: 50, left: 50, points: [ { x: 0, y: 70 }, { x: 70, y: 70 }, { x: 70, y: 0 }, { x: 0, y: 0 }, ], });
canvas.add(polygon);
ConclusionIn this tutorial, we used two simple examples to demonstrate how you can add the coordinates in a Polygon using FabricJS.
How To Create The Instance Of Fabric Image From A Url String Using Fabricjs
In this tutorial, we are going to learn how to create the instance of fabric.Image from a URL string using FabricJS. We can create an Image object by creating an instance of fabric.Image. Since it is one of the basic elements of FabricJS, we can also easily customize it by applying properties like angle, opacity etc. In order to create the instance of fabric.Image from a URL string, we use the fromURL method.
Syntax fromURL(url: String, callback: function, imgOptions: Object) Parameters
url − This parameter accepts a String which denotes the URL to create an image from.
callback (optional) − This parameter is a function which is called as soon as the image is created. To this function, the newly created image is passed as the first argument. The second argument is a boolean value that indicates whether an error has occurred or not. This parameter is optional.
imgOptions (optional) − This parameter is an optional Object which provides additional customizations to our image. Using this parameter origin, stroke width and a lot of other properties can be changed related to the image object.
Without using the fromURL method ExampleLet’s see a code example of how the Image object appears when the fromURL method is not used. In this case, we only need to create an instance of fabric.Image and add it to our canvas.
var
canvas
=
new
fabric
.
Canvas
(
“canvas”
)
;
canvas
.
setWidth
(
document
.
body
.
scrollWidth
)
;
canvas
.
setHeight
(
250
)
;
var
imageElement
=
document
.
getElementById
(
“img1”
)
;
var
image
=
new
fabric
.
Image
(
imageElement
,
{
top
:
50
,
left
:
110
,
}
)
;
canvas
.
add
(
image
)
;
Using the fromURL method ExampleIn this example, we have used the fromURL method to demonstrate that we can create an image object even when we don’t have the image element. In this case, we only need the URL of the image and pass the callback function the already created fabric.Image object as the first argument and then add it to the canvas.
You can see that the image object can be created from the image
URL
itself
var
canvas
=
new
fabric
.
Canvas
(
“canvas”
)
;
canvas
.
setWidth
(
document
.
body
.
scrollWidth
)
;
canvas
.
setHeight
(
250
)
;
fabric
.
Image
.
fromURL
(
function
(
Img
)
{
canvas
.
add
(
Img
)
;
}
)
;
ConclusionIn this tutorial, we used two examples to demonstrate how you can create the instance of fabric.Image from a URL string using FabricJS.
Update the detailed information about How To Disable The Selectability Of Itext Using Fabricjs? on the Achiashop.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!