How to highlight an object when the mouse is over it using FabricJS?

王林
Release: 2023-08-29 11:21:16
forward
779 people have browsed it

如何使用 FabricJS 当鼠标悬停在某个对象上时突出显示该对象?

We can create a Polygon object by creating an instance offabric.Polygon. A polygon object can be characterized as 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 such as angle, opacity, etc. FabricJS provides us with an extensive set of events that we can use to create different effects.

Since we want the changes to occur on mouseover, we will use themouse:moveevent that fires when the mouse is moved. Our second requirement is to highlight an object, this can be achieved by using theopacityproperty, however, when there are many objects on the canvas and we want to highlight the object that is hovering over it, We need to use theforEachObjectmethod. This method runs a for-each loop for the given function, thereby executing it for each object.

grammar

forEachObject( callback: function, context: object ): Self
Copy after login

parameter

  • callback- This property accepts afunctionthat takes the current object as the first argument, the index as the second argument, and An array of all objects as the third one.

  • context- This property accepts anObjectthat represents the context in which the callback function was called.

Example 1: Display highlight effect with only one object

Let's look at a code example to see how to add a highlight effect when there is only one object on the canvas. We have attached themouseoverandmouseoutevents to the polygon object (in this case, the triangle).mouseoverExecuted when the mouse moves over the object,mouseoutExecuted when the mouse hovers outside the object. As soon as we move the cursor over the element, its opacity will change from 0.5 to 1 and vice versa.

     

Displaying highlight effect with only one object

You can see that the object is being highlighted when the cursor is moved onto the element

Copy after login

Example 2: Display the highlight effect of multiple objects

In this example, we will see how to highlight an object when the mouse is hovering over it. Every time the mouse moves, themouse:moveevent is triggered. Here we use the mathematical distance formula to calculate the distance between two points in the coordinate plane through the "x" and "y" positions of the mouse pointer. This distance is then divided by 50, which is an arbitrary number that makes (dist/50) the fraction smaller (we know that as the denominator gets larger, the fraction gets smaller), so that when it is divided by 1, we get a The value is larger and the opacity increases.

     

Displaying highlight effect with multiple objects

You can see that an object is being highlighted only when the cursor is moved onto the element and is depended on the distance

Copy after login

in conclusion

In this tutorial, we use two simple examples to demonstrate how to use FabricJS to highlight objects when the mouse is hovering over them.

The above is the detailed content of How to highlight an object when the mouse is over it using FabricJS?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!