Views
Instead of directly dealing with the DOM, Veloxi simplifies the handling of its elements by providing an easy, yet effective API for manipulation and animation.
Creating Views
There isn’t a dedicated API for manually creating views in Veloxi. Instead, Veloxi automatically generates views when you specify the element for which you want to create a view.
To achieve this, you use the data-vel-view="yourViewName"
attribute. However, it’s essential to remember that a view remains inactive until it’s associated with a specific plugin. To establish this association, you need to use the data-vel-plugin="yourPluginName"
attribute on the same element.
Here’s an example illustrating the creation of a view named circle
, which is used within a plugin called FollowMousePlugin
:”
Then you can access it in your plugin like this (for more details, check out our plugin guide):
View Properties
Every view object comes with a set of properties that you can both retrieve and modify. Currently, the supported properties include:
- Position: Specifies the x and y coordinates of the view on the page.
- Scale: Determines the scaleX and scaleY properties of the view, corresponding to transform’s scale properties.
- Size: Represents the actual width and height of the view.
- Rotation: Denotes the degree of rotation for the view, along the z-axis.
Each view property is an object that contains a list of methods and fields that enable you to read and update its values.
In the upcoming sections, we will examine each property in detail and explore how to retrieve and modify their values.
View Position
To access the position property, you can use view.position
. Each view begins with an initial position on the page, which you can retrieve using view.position.initialX
and view.position.initialY
.
To change the position of a view, you can use the set({ x, y }, runAnimation?)
method for that.
Here’s how you can reset the view’s position to its initial position:
You can find out how close the view is to a particular point as a percentage. This percentage ranges from 0 to 1. It starts at 0 and goes up as the view gets nearer to the target point. This calculation is based on the view’s starting position.
View Scale
Scaling in Veloxi works in a way similar to adjusting the transform’s scale property in CSS—essentially, it applies these values to the CSS scale property
So, the scale values (scaleX and scaleY) start at 1 for the initial size. If you set them to 0, the view disappears, and if you set them to 2, the view doubles in size. You can use view.scale.set({ x, y }, runAnimation?)
to change these values.
Sometimes, we don’t want to think about relative values like the ones mentioned above. Instead, we just want to resize using specific width and height values. That’s exactly what view.scale.setWithSize({ width, height }, runAnimation?)
is for.
Let’s look at some usage examples:“
View Size
The size properties allow you to both retrieve and modify the width and height of the view.
Here are some code examples:
View Rotation
In the current version, Veloxi only supports rotation along the z-axis. You can set and get values in either radians or degrees.
Here are some code examples:
View Animators
Animation plays an essential role in UI interaction, and Veloxi simplifies its use by encapsulating all animation configurations within an object called animator
.
You can directly access the animator object for any view proprety you wish to animate, such as position, scale, size, and rotation.
For instance, if you want to apply animation to the position property, you can invoke the set(animationName)
method on its animator, like this:
From that point onward, whenever the position of that view changes, it will use the spring animator for animation.
By default, animation is disabled for all properties. This is because the default animator type used is instant
, which effectively means ‘no animation.’
Currently, Veloxi supports the following animation types:
- instant: No animation.
- tween: Animation with a defined start and end, with control over its duration and easing function.
- dynamic: Tailored for real-time responsive interactions, such as following the mouse or dragging. You can control its speed.
- spring: Applies spring animation, with adjustable parameters for stiffness, damping, and speed.
Let’s take a look at some code examples:
View Data
If emitting events is how plugins communicate with the outside world, View Data is the method for communication in the opposite direction.
When building real-world applications, there’s typically some app or component state that manages the UI. Regardless of whether you’re working with a framework or vanilla JavaScript, it’s recommended to handle updates to that state outside of Veloxi. Instead, use Veloxi exclusively for creating interactions related to those state changes.
For instance, imagine you have a dropdown component that users can open and close with some cool animation. Clicking the top-level item in the dropdown should expand all nested items. At this point, you have two options: you can either respond directly to the click event and update the UI using CSS or JavaScript, or you can toggle a flag (or set some data) to indicate the change and let Veloxi handle the UI update and animation.
For the latter, you can use View Data to communicate the change to Veloxi.
In Veloxi, you can transmit data to views using a data attribute on the element, like this: data-vel-data-prop-name="propValue"
. In this notation, prop-name
can be any name you choose, and propValue
can be any value you want.
Within your plugin, you can access this data using myView.data.propName
. Notice how we use camel case for the data name.
In the context of the dropdown example, you can pass the current state of the dropdown to this data attribute, like so:”
And then, you can access that isOpen data in the DropdownPlugin.
Other View Fields
element
Returns the DOM element for the view.
hasElement(element: HTMLElement): boolean
Returns a boolean indicating whether a view contains a specific DOM element.
getScroll(): { x: number, y: number }
Returns the current horizontal and vertical scroll positions for the view on the page. It’s similar to getting the page scroll using window.scrollY
and window.scrollX
, but it also takes into account any scrolling happening inside a parent container, if there is one.
See how it’s used in Follow-The-Mouse example.
intersects(x: number, y: number): boolean
Returns whether a point (x and y) falls inside the view.
overlapsWith(view: View): boolean
Returns whether the view overlaps with another view.
distanceTo(view: View): number
Returns the distance between two views in pixels.