Carousel with Perspective
3/28/2024
Tony Gruev
The Carousel component is popular and used in many games on various platforms.
Creating a carousel menu in Gameface is straightforward using the Carousel component, which can be customized to suit our requirements with minimal adjustments. Additionally, leveraging the Data Binding feature in Gameface allows us to do dynamic initialization and visual modifications of the carousel menu.
This is not a full guide covering every line of code, as the complete sample demonstrating carousel usage is accessible within the Gameface package. You can find it in the ${Gameface package}/Samples/uiresources/UITutorials/carousel-perspective directory
.
Layout of the Hero Item
Initially, we will focus on the design of how the carousel item will look - the layout and the styling of it. Then we are going to proceed with the integration of the carousel and its usage.
Carousel Item Layout and Styles
This is going to be the layout:
Since we are going to have this layout in the carousel item slot, we named it accordingly and styled it:
We are going to use perspective
to achieve a nicer transition effect while navigating through the carousel menu.
For the full styling, check the CSS file in src/main.css.
Our Requirements
We want a background that is going to change when an item is selected, an animation for the name of the hero and its avatar and have a class image for the hero.
The inner elements of the .carousel-item
are sized and positioned accordingly.
Additionally, we are preparing for the animations to be added to the name and the avatar elements when changing the current carousel item.
As for the background, we are going to use the .carousel-item
element by adding a linear-gradient
as a second background.
For the name and avatar there is going to be a simple transformation with translateZ
to smoothly pop out those elements.
The idea here is to dynamically set a class on a (carousel) wrapper and style accordingly when transitioning from one item to another and we are going to use the .carousel-current-item
class for that.
Carousel Component Integration
Since we are ready with our design, it is time to integrate, style and use the Carousel component.
Setup Walkthrough
- Run
npm i
in the root folder of the sample which holds the package.json with the carousel component - In the html file src/index.html the component must be placed in the
<body>
:
Usage
Now it is time to not only place our HTML layout in the carousel but also explain how to utilize the Data Binding of Gameface.
Carousel Usage
You can paste the layout we already created and duplicate as many items as you like in the slot="carousel-slot"
element to try it out.
If you only need the carousel (without the data binding), add only the layout we created and the carousel can be used in its full potential.
Carousel Interaction
We have added an event listener and used a function to toggle the .carousel-current-item
class according to the current carousel item using the available Carousel currentItemIndex
setter.
For the full code - check the src/index.js file.
Usage with Data Binding
From now on we are proceeding with the additional properties we are going to need for the Data Binding .
Data Binding (Mocked) Model
We need 2 more scripts to initialize and use the data binding provided with Gameface:
In short, the model.js creates a JS (mocked) model with the heroes - their names and avatar and class image paths for the data binding to use. Make sure to check the use of engine.createJSModel .
Check the src/index.js file and also note the use of engine.whenReady .
So now when running the sample in the Gameface Player , the data-bind-for
property is populating elements according to the model and setting the name for the hero and the class and avatar images.