# Carousel with Perspective

> 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.

> Documentation index: https://coherent-labs.com/llms.txt lists every page of this documentation as Markdown; the full text is in https://coherent-labs.com/llms-full.txt.

Source: https://coherent-labs.com/blog/uitutorials/carousel-with-perspective/

## Carousel with Perspective

ui tutorials

3/28/2024

Tony Gruev

1. Layout of the Hero Item
   1. Carousel Item Layout and Styles
   2. Our Requirements
2. Carousel Component Integration
   1. Setup Walkthrough
   2. Usage
   3. Carousel Interaction
   4. Usage with Data Binding

The Carousel component is popular and used in many games on various platforms.

Creating a carousel menu in Gameface is straightforward using the [Carousel ](https://coherentlabs.github.io/GameUIComponents/en/components/carousel/)component, which can be customized to suit our requirements with minimal adjustments. Additionally, leveraging the [Data Binding ](https://docs.coherent-labs.com/cpp-gameface/integration/ui_scripting/htmldatabinding/)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:

```html
<div class="carousel-item">
    <div class="hero-class"></div>
    <div class="hero-name">Hero Name</div>
    <div class="hero-avatar"></div>
</div>
```

Since we are going to have this layout in the carousel item slot, we named it accordingly and styled it:

```css
.carousel-item {
    position: relative;
    width: 400px;
    height: 300px;
    background-image: url('../assets/item_background_bw.jpg');
    background-repeat: no-repeat;
    border-radius: 7%;
    perspective: 800px;
}
```

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.

```css
.hero-name,
.hero-avatar {
    transform: translateX(-50%) translateZ(0px);
    transition: transform 0.3s ease-in;
}
```

As for the background, we are going to use the `.carousel-item` element by adding a `linear-gradient` as a second background.

```css
.carousel-current-item .carousel-item {
    background-image: linear-gradient(rgba(0, 100, 170, 0.5), rgba(100, 100, 0, 0.5)),
    url('../assets/item_background_bw.jpg');
}
```

For the name and avatar there is going to be a simple transformation with `translateZ` to smoothly pop out those elements.

```css
.carousel-current-item .hero-name,
.carousel-current-item .hero-avatar {
    transform: translateX(-50%) translateZ(100px);
}
```

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 ](https://coherentlabs.github.io/GameUIComponents/en/components/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>`:

```html
<script src="../node_modules/coherent-gameface-carousel/dist/carousel.development.js"></script>
```

### Usage

Now it is time to not only place our HTML layout in the carousel but also explain how to utilize the [Data Binding ](https://docs.coherent-labs.com/cpp-gameface/integration/ui_scripting/htmldatabinding/)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.

```html
<gameface-carousel class="carousel-component">
    <div slot="carousel-content" class="carousel-item-wrapper">
        <!-- The layout for the carousel item. You can add the one from above.-->
    </div>
</gameface-carousel>
```

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.

```js
document.addEventListener('keydown', (event) => {
    const keyCode = event.keyCode;
    if (keyCode === 37) {
        changeCarouselCurrentItem(-1);
    } else if (keyCode === 39) {
        changeCarouselCurrentItem(1);
    }
});
```

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 ](https://docs.coherent-labs.com/cpp-gameface/integration/ui_scripting/htmldatabinding/).

```html
<gameface-carousel class="carousel-component">
    <div
        slot="carousel-content"
        class="carousel-item-wrapper"
        data-bind-for="hero:{{game.heroes}}">
        <!-- This is our layout with the additional data-bind-* properties-->
        <div class="carousel-item">
            <div class="hero-class" data-bind-style-background-image-url="{{hero.classImage}}"></div>
            <div class="hero-name" data-bind-value="{{hero.name}}"></div>
            <div class="hero-avatar" data-bind-style-background-image-url="{{hero.avatarImage}}"></div>
        </div>
    </div>
</gameface-carousel>
```

#### Data Binding (Mocked) Model

We need 2 more scripts to initialize and use the data binding provided with Gameface:

```html
<script src="cohtml.js"></script>
<script src="model.js"></script>
```

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 ](https://docs.coherent-labs.com/cpp-gameface/integration/ui_scripting/htmldatabinding/#creating-the-attribute-handlers).

Check the *src/index.js* file and also note the use of [engine.whenReady ](https://docs.coherent-labs.com/cpp-gameface/integration/ui_scripting/javascript_native/#enginewhenready).

So now when running the sample in the [Gameface Player ](https://docs.coherent-labs.com/cpp-gameface/quick_start/player/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.

## On this page
