# Creating hexagonal skill tree

> Learn how to use the power of SVGs to create a visually appealing hexagonal skill tree inspired by the Batman: Arkham Knight game.

> 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/hexagon-skill-tree/

## Creating hexagonal skill tree

ui tutorials

1/7/2025

Martin Bozhilov

1. Source location
2. Preparing the assets
   1. Hexagon SVGs
   2. SkillTree background SVG
3. Setting up the project
   1. Skill Tree movement
   2. Creating the hexagons
4. Categorizing the skills
5. Arrange the hexagons on the skill tree
   1. Mocking game data with a model object
   2. Connecting model data to the html with HTML data-binding
   3. Replacing the skill tree svg
6. Adding effects and animations
   1. Glow effect
   2. Active skill effect
   3. Adding keyboard navigation
7. Making it all interactive
   1. Adding tooltip with skill information
   2. Adding skill unlock functionality
   3. Adding error Handling
   4. Quality of life improvements

In this tutorial, we will explore how to create a visually appealing hexagonal skill tree inspired by the Batman: Arkham Knight game.

This guide will walk you through the process of designing and implementing a skill tree using SVGs, CSS, and JavaScript. By the end of this tutorial, you’ll have a dynamic and interactive skill tree for your game.

## Source location

You can find the complete sample source within the `${Gameface package}/Samples/uiresources/UITutorials/HexagonalSkillTree` directory.

## Preparing the assets

Before we start coding, we need to prepare the assets for our skill tree. The easiest and most performant way to create the hexagons is by using SVGs.

### Hexagon SVGs

To create the hexagons, we will head to Figma and design a hexagon shape. We will then export the hexagon as an SVG file.

The simplest way to create a hexagon in Figma is by selecting the `polygon` option in the creation tool and setting the edges count to 6:

And then exporting it as an SVG file from the bottom right section.

### SkillTree background SVG

We will also need a background SVG for the skill tree. This SVG will serve as the base for our skill tree on which we will place the elements.

We will use the hexagon we just created in Figma and duplicate it as many times as we want and after that connecting them with the pen tool.

After you are done with the design, export the background SVG file. Feel free to experiment with the design and create a unique background for your skill tree.

## Setting up the project

Let’s begin by setting up the project structure, which includes adding the SVG skill tree we just created and some basic styles.

```html
<div class="container">
    <div class="skill-container">
    </div>
    <svg class="svg-paths" width="100%" height="100%" viewBox="0 0 1466 1380" xmlns="http://www.w3.org/2000/svg">
        <path d="M820.385 630.74L733.448 582.566L646.063 630.74V729.104L733.448 777.053L820.385 729.104V630.74Z" stroke="white" stroke-width="5"/>
        <path d="M820.385 1186.42L733.448 1138.24L646.063 1186.42V1284.78L733.448 1332.73L820.385 1284.78V1186.42Z" stroke="white" stroke-width="5"/>
        <path d="M819.04 70.5802L732.104 22.4065L644.719 70.5802V168.944L732.104 216.894L819.04 168.944V70.5802Z" stroke="white" stroke-width="5"/>
        <path d="M1033.69 631.188L946.757 583.014L859.372 631.188V729.552L946.757 777.502L1033.69 729.552V631.188Z" stroke="white" stroke-width="5"/>
        <path d="M1248.35 630.291L1161.41 582.118L1074.03 630.291V728.655L1161.41 776.605L1248.35 728.655V630.291Z" stroke="white" stroke-width="5"/>
        <path d="M1463 630.291L1376.06 582.118L1288.68 630.291V728.655L1376.06 776.605L1463 728.655V630.291Z" stroke="white" stroke-width="5"/>
        <path d="M819.041 681.154H858.924" stroke="white" stroke-width="5"/>
        <path d="M1033.69 680.257H1073.58" stroke="white" stroke-width="5"/>
        <path d="M1248.35 680.257H1288.23" stroke="white" stroke-width="5"/>
        <path d="M542.142 445.103L543.891 544.479L629.303 596.07L714.489 546.888L712.322 447.235L627.328 395.921L542.142 445.103Z" stroke="white" stroke-width="5"/>
        <path d="M431.902 259.13L433.651 358.506L519.063 410.097L604.248 360.915L602.082 261.263L517.088 209.948L431.902 259.13Z" stroke="white" stroke-width="5"/>
        <path d="M324.575 73.1568L326.324 172.533L411.736 224.124L496.922 174.942L494.755 75.2894L409.761 23.9748L324.575 73.1568Z" stroke="white" stroke-width="5"/>
        <path d="M692.74 606.015L672.799 571.475" stroke="white" stroke-width="5"/>
        <path d="M582.5 420.042L562.559 385.502" stroke="white" stroke-width="5"/>
        <path d="M475.174 234.069L455.232 199.529" stroke="white" stroke-width="5"/>
        <path d="M843.059 398.273L757.871 449.476L755.898 549.24L841.084 598.422L926.302 546.72L928.245 447.455L843.059 398.273Z" stroke="white" stroke-width="5"/>
        <path d="M950.162 212.301L864.974 263.503L863.001 363.268L948.187 412.45L1033.4 360.747L1035.35 261.483L950.162 212.301Z" stroke="white" stroke-width="5"/>
        <path d="M1064.88 26.3274L979.694 77.53L977.721 177.294L1062.91 226.476L1148.13 174.774L1150.07 75.5094L1064.88 26.3274Z" stroke="white" stroke-width="5"/>
        <path d="M779.004 609.151L798.946 574.611" stroke="white" stroke-width="5"/>
        <path d="M886.107 423.179L906.049 388.639" stroke="white" stroke-width="5"/>
        <path d="M1000.83 237.206L1020.77 202.666" stroke="white" stroke-width="5"/>
        <path d="M432.397 729.328L519.334 777.502L606.719 729.328L606.719 630.964L519.334 583.014L432.397 630.964L432.397 729.328Z" stroke="white" stroke-width="5"/>
        <path d="M217.653 730.224L304.59 778.398L391.975 730.224L391.975 631.86L304.59 583.91L217.653 631.86L217.653 730.224Z" stroke="white" stroke-width="5"/>
        <path d="M2.99986 730.224L89.9366 778.398L177.322 730.224L177.322 631.86L89.9366 583.91L2.99986 631.86L2.99986 730.224Z" stroke="white" stroke-width="5"/>
        <path d="M647.05 679.362H607.167" stroke="white" stroke-width="5"/>
        <path d="M432.306 680.257H392.423" stroke="white" stroke-width="5"/>
        <path d="M217.653 680.257H177.769" stroke="white" stroke-width="5"/>
        <path d="M623.917 963.548L709.105 912.345L711.077 812.581L625.892 763.399L540.674 815.102L538.731 914.366L623.917 963.548Z" stroke="white" stroke-width="5"/>
        <path d="M516.894 1149.48L602.082 1098.28L604.055 998.515L518.869 949.333L433.651 1001.04L431.708 1100.3L516.894 1149.48Z" stroke="white" stroke-width="5"/>
        <path d="M409.632 1335.46L494.82 1284.25L496.793 1184.49L411.607 1135.31L326.389 1187.01L324.446 1286.27L409.632 1335.46Z" stroke="white" stroke-width="5"/>
        <path d="M687.971 752.67L668.03 787.21" stroke="white" stroke-width="5"/>
        <path d="M580.948 938.603L561.007 973.143" stroke="white" stroke-width="5"/>
        <path d="M473.687 1124.58L453.745 1159.12" stroke="white" stroke-width="5"/>
        <path d="M928.116 914.439L926.367 815.062L840.955 763.472L755.769 812.654L757.936 912.306L842.93 963.621L928.116 914.439Z" stroke="white" stroke-width="5"/>
        <path d="M1038.74 1100.41L1036.99 1001.04L951.578 949.445L866.392 998.627L868.559 1098.28L953.553 1149.59L1038.74 1100.41Z" stroke="white" stroke-width="5"/>
        <path d="M1149.94 1286.38L1148.19 1187.01L1062.78 1135.42L977.593 1184.6L979.759 1284.25L1064.75 1335.57L1149.94 1286.38Z" stroke="white" stroke-width="5"/>
        <path d="M777.518 753.526L797.459 788.066" stroke="white" stroke-width="5"/>
        <path d="M888.14 939.5L908.081 974.04" stroke="white" stroke-width="5"/>
        <path d="M999.34 1125.47L1019.28 1160.01" stroke="white" stroke-width="5"/>
        <path d="M499.525 1244.9H643.15M822.85 1244.9H978.798" stroke="white" stroke-width="5"/>
        <path d="M498.181 129.061H641.806M821.505 129.061H977.454" stroke="white" stroke-width="5"/>
    </svg>
</div>
```

```css
@font-face {
    font-family: 'Nunito';
    src: url(./assets/Nunito-VariableFont_wght.ttf);
}

body{
    margin: 0;
    width: 100vw;
    height: 100vh;
    background: linear-gradient(to bottom, #141e30, #243b55);
    overflow: hidden;
    font-family: 'Nunito';
    color: white;
}

.container {
    position: absolute;
    right: 0;
    width: 75%;
    height: 140%;
    /* This transition will control the camera movement */
    transition: transform 0.2s linear;
    z-index: -1;
}
```

### Skill Tree movement

Since skill trees can be quite big we want to have the skill tree follow the mouse movement.

To achieve this we will add an event listener for the `mousemove` event and update the `transform` property of the container element. We will calculate the percentage of the mouse position relative to the window size and then map it to the desired movement range.

In our case we want the maxium movement to be 10% of the window width and 30% of the window height. So the maximum `translateX` value will be -10% and the maximum `translateY` value will be -30%.

```javascript
const skillTree = document.querySelector('.container');
const treePaths = document.querySelector('.svg-paths');

function translateSkillTreeIntoView(x, y) {
    const percentageX = (x / window.innerWidth) * 100;
    const percentageY = (y / window.innerHeight) * 100;

    const mappedX = percentageX * -0.10;
    const mappedY = percentageY * -0.30;

    skillTree.style.transform = `translateX(${mappedX.toFixed(2)}%) translateY(${mappedY.toFixed(2)}%)`;
}

document.addEventListener('mousemove', (event) => {
    const { clientX, clientY } = event;

    translateSkillTreeIntoView(clientX, clientY);
})
```

You SVG should now follow the mouse movement.

### Creating the hexagons

Now that we have the basic structure set up, we can start creating the hexagons that will represent the skills in our skill tree.

```html
<div class="skill-container">
   <div class="hex">
       <div class="hex-content"></div>
       <svg class="svg svg-main" viewBox="0 0 395 441" fill="none" xmlns="http://www.w3.org/2000/svg">
           <!-- Side 1 -->
           <path d="M198 3.5L392 111" />
           <!-- Side 2 -->
           <path d="M392 111L392 330.5" />
           <!-- Side 3 -->
           <path class="strike-dash" d="M392 330.5L198 437.5" />
           <!-- Side 4 (with stroke-dasharray) -->
           <path d="M198 437.5L3 330.5" />
           <!-- Side 5 -->
           <path d="M3 330.5L3 111" />
           <!-- Side 6 -->
           <path class="strike-dash" d="M3 111L198 3.5" />
       </svg>
   </div>
</div>
```

One thing we changed is the separation of the svg from one `path` to six `path` elements for each side and the addition of the `strike-dash` class to the paths that we want to have a dashed stroke. We will use this class to apply a dashed stroke to the hexagon sides, creating a unique hexagon shape.

**Note:** You can easily separate your hexagon svg into six different path elements for each side by provding it to an AI language model and asking it to do so.

```css
.hex {
    width: 9vw;
    height: 19vh;
    position: absolute;
    transition: all 0.3s ease-in-out;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
}

.hex-content {
    width: 70%;
    height: 70%;
    mask-size: 100%;
    mask-repeat: no-repeat;
    mask-position: center 60%;
    background-color: crimson;
    mask-image: url(./assets/strength/strength-plus.png);
}

.hex path {
    stroke: crimson;
}

.svg {
    width: 9vw;
    height: 19vh;
    position: absolute;
}

.svg-main path {
    stroke-width: 10;
}

.strike-dash {
    stroke-dasharray: 6;
}
```

Our hexagon elements will consist of a hexagon SVG as the outline shape of the element and a content div that will hold the skill icon, which will be placed as a mask image. We will leverage the power of CSS masks to create different color icons for the skills while keeping the background transparent.

## Categorizing the skills

We are going to categorize the skills by their type (strength, durability, health, etc) and apply different colors to the hexagons. We can easily do that by applying different classes to the hexagon elements. And use the classes with the help of CSS variables to apply different colors to the hexagons.

```css
/* Category colors */
.hex-strength {
    --shadow-color: crimson;
}

.hex-durability {
    --shadow-color: #FF9F43;
}

.hex-health {
    --shadow-color: #4CAF50;
}

.hex-mana {
    --shadow-color: #42A5F5;
}

.hex-stamina {
    --shadow-color: #FFEB3B;
}

.hex-misc {
    --shadow-color: #AB47BC;
}

/* Combined skills */
.hex-strength-durability .path-left-side {
    --shadow-color: crimson;
}

.hex-strength-durability .path-right-side {
    --shadow-color: #FF9F43;
}

.hex-health-mana .path-left-side {
    --shadow-color: #42A5F5;
}

.hex-health-mana .path-right-side {
    --shadow-color: #4CAF50;
}

/* LOCKED STATE */
.hex-locked,
.hex-locked .path-left-side,
.hex-locked .path-right-side {
    --shadow-color: #ffffff;
}

.hex-locked {
    opacity: 0.5;
}
```

And now simply apply the variable to the svg `path` element.

```css
.hex-skill path {
    stroke: var(--shadow-color);
}
```

With this setup we can easily change the color of the hexagons based on the skill type as the `--shadow-color` variable will take the color from the category class placed on the parent.

## Arrange the hexagons on the skill tree

Now that we have the hexagons created, we can start placing them on the skill tree. One way to do it is to manually position each hexagon on the skill tree background SVG by setting the `left` and `top` properties of the hexagon elements untill they fit perfectly.

### Mocking game data with a model object

Since we don’t have a game to provide us with the back-end data, we are going to mock some game data that will represent the skills and their relationships. We will create a mock model object that will hold all the information about the skills we need.

```js
const SkillsModel = {
    points: 24,
    skills: [
        {
            name: 'Health +',
            id: 1,
            description: 'Increase your max health',
            unlocked: false,
            skillPoints: 1,
            image: 'url(./assets/health/health-plus.png)',
            x: '51.5%',
            y: '77vh',
            parents: null,
            type: 'health',
        },
        {
            name: 'Health ++',
            id: 2,
            description: 'Increase your max health',
            unlocked: false,
            skillPoints: 2,
            image: 'url(./assets/health/health-plus-plus.png)',
            x: '59%',
            y: '95.25vh',
            parents: 1,
            type: 'health',
        },
        {
            name: 'Health +++',
            id: 3,
            description: 'Increase your max health',
            unlocked: false,
            skillPoints: 3,
            image: 'url(./assets/health/health-plus-plus-plus.png)',
            x: '66.5%',
            y: '113vh',
            parents: 2,
            type: 'health',
        },
        {
            name: 'Mana +',
            id: 4,
            description: 'Increase your max mana',
            unlocked: false,
            skillPoints: 1,
            image: 'url(./assets/mana/mana-plus.png)',
            x: '36.5%',
            y: '77vh',
            parents: null,
            type: 'mana',
        },
        {
            name: 'Mana ++',
            id: 5,
            description: 'Increase your max mana',
            unlocked: false,
            skillPoints: 2,
            image: 'url(./assets/mana/mana-plus-plus.png)',
            x: '29.5%',
            y: '95.25vh',
            parents: 4,
            type: 'mana',
        },
        {
            name: 'Mana +++',
            id: 6,
            description: 'Increase your max mana',
            unlocked: false,
            skillPoints: 3,
            image: 'url(./assets/mana/mana-plus-plus-plus.png)',
            x: '22%',
            y: '113vh',
            parents: 5,
            type: 'mana',
        },
        {
            name: 'Durability +',
            id: 7,
            description: 'Increase your max durability',
            unlocked: false,
            skillPoints: 1,
            image: 'url(./assets/durability/durability-plus.png)',
            x: '51.5%',
            y: '41.75vh',
            parents: null,
            type: 'durability',
        },
        {
            name: 'Durability ++',
            id: 8,
            description: 'Increase your max durability',
            unlocked: false,
            skillPoints: 2,
            image: 'url(./assets/durability/durability-plus-plus.png)',
            x: '59%',
            y: '24.25vh',
            parents: 7,
            type: 'durability',
        },
        {
            name: 'Durability +++',
            id: 9,
            description: 'Increase your max durability',
            unlocked: false,
            skillPoints: 3,
            image: 'url(./assets/durability/durability-plus-plus-plus.png)',
            x: '66.5%',
            y: '6vh',
            parents: 8,
            type: 'durability',
        },
        {
            name: 'Stamina +',
            id: 10,
            description: 'Increase your max stamina',
            unlocked: false,
            skillPoints: 1,
            image: 'url(./assets/stamina/stamina-plus.png)',
            x: '58.75%',
            y: '59.5vh',
            parents: null,
            type: 'stamina',
        },
        {
            name: 'Stamina ++',
            id: 11,
            description: 'Increase your max stamina',
            unlocked: false,
            skillPoints: 2,
            image: 'url(./assets/stamina/stamina-plus-plus.png)',
            x: '73.25%',
            y: '59.5vh',
            parents: 10,
            type: 'stamina',
        },
        {
            name: 'Stamina +++',
            id: 12,
            description: 'Increase your max stamina',
            unlocked: false,
            skillPoints: 3,
            image: 'url(./assets/stamina/stamina-plus-plus-plus.png)',
            x: '87.75%',
            y: '59.5vh',
            parents: 11,
            type: 'stamina',
        },
        {
            name: 'Strength +',
            id: 13,
            description: 'Increase your max strength',
            unlocked: false,
            skillPoints: 1,
            image: 'url(./assets/strength/strength-plus.png)',
            x: '37%',
            y: '41.75vh',
            parents: null,
            type: 'strength',
        },
        {
            name: 'Strength ++',
            id: 14,
            description: 'Increase your max strength',
            unlocked: false,
            skillPoints: 2,
            image: 'url(./assets/strength/strength-plus-plus.png)',
            x: '29.5%',
            y: '23.75vh',
            parents: 13,
            type: 'strength',
        },
        {
            name: 'Strength +++',
            id: 15,
            description: 'Increase your max strength',
            unlocked: false,
            skillPoints: 3,
            image: 'url(./assets/strength/strength-plus-plus-plus.png)',
            x: '22%',
            y: '6vh',
            parents: 14,
            type: 'strength',
        },
        {
            name: 'Dodge',
            id: 16,
            description: 'Learn dodge ability',
            unlocked: false,
            skillPoints: 1,
            image: 'url(./assets/misc/dodge.png)',
            x: '29.5%',
            y: '59.5vh',
            parents: null,
            type: 'misc',
        },
        {
            name: 'Sixth sense',
            id: 17,
            description: 'Learn the sixth sense ability and increase your awareness',
            unlocked: false,
            skillPoints: 2,
            image: 'url(./assets/misc/sixth-sense.png)',
            x: '15%',
            y: '59.5vh',
            parents: 16,
            type: 'misc',
        },
        {
            name: 'Teleportation',
            id: 18,
            description: 'Learn the teleportation ability and unlock fast travel',
            unlocked: false,
            skillPoints: 3,
            image: 'url(./assets/misc/teleport.png)',
            x: '0.3%',
            y: '59.5vh',
            parents: 17,
            type: 'misc',
        },
    ],
    specialSkills: [
        {
            name: 'Iron Will',
            description: 'Tremendously increases strength and durability',
            unlocked: false,
            skillPoints: 5,
            image: 'url(./assets/combined/iron-will.png)',
            x: '43.75%',
            y: '6vh',
            parents: [15, 9],
            type: 'strength-durability',
        },
        {
            name: 'Arcane Vitality',
            description: 'Tremendously boosts mana regeneration and health recovery',
            unlocked: false,
            skillPoints: 5,
            image: 'url(./assets/combined/arcane-vitality.png)',
            x: '44%',
            y: '113vh',
            parents: [3, 6],
            type: 'health-mana',
        },
    ],
    starterSkill: {
        name: 'Journey Begins',
        description: 'Gain access to the skill tree',
        unlocked: true,
        skillPoints: 0,
        image: 'url(./assets/misc/rubber-man.png)',
        x: '44%',
        y: '59.5vh',
        type: 'starter',
    },
};
```

And we should not forget to initialize the model in our `index.js` file.

```js
function updateModel(modelName) {
    engine.updateWholeModel(modelName);
    engine.synchronizeModels();
}

engine.on("Ready", () => {
    engine.createJSModel("SkillsModel", SkillsModel);
    engine.synchronizeModels();
});
```

The model we created has a `skills` array with objects that contain the skill data, `specialSkills` array because we have special skills that require multiple skills to be unlocked, and a `starterSkill` object that represents the starting skill of the tree which will be unlocked by default.

Since the url for the mask image icons will come from our mocked model, we can remove the placeholder CSS we used previously.

```css
.hex-content {
    width: 70%;
    height: 70%;
    mask-size: 100%;
    mask-repeat: no-repeat;
    mask-position: center 60%;
    background-color: crimson;
    mask-image: url(./assets/strength/strength-plus.png);
   background-color: var(--shadow-color);
}

.hex path {
    stroke: crimson;
}
```

Now for every category we will create a new class with different color. And also we will add a class for when the skill is locked.

### Connecting model data to the html with HTML data-binding

Now that we have the model setup, we can start connecting the data to the HTML elements. We will loop through the `skills` array and create a hexagon element for each skill by utilizing the power of [data-binding ](https://docs.coherent-labs.com/cpp-gameface/integration/ui_scripting/htmldatabinding/#DataBinding).

Since we have 3 types of skills - starter, normal and combined we will need to handle them separately.

#### Starter skill

For the starter skill, we will need to extract from the model the `x` and `y` coordinates and the image url for the mask image.

```html
<div class="skill-container">
    <div
        class="hex hex-starter"
        data-bind-style-top='{{SkillsModel.starterSkill.y}}'
        data-bind-style-left='{{SkillsModel.starterSkill.x}}'>
        <div class="hex-content" data-bind-style-mask-image='{{SkillsModel.starterSkill.image}}'></div>
        <!-- Main -->
        <svg class="svg svg-main" viewBox="0 0 395 441" fill="none" xmlns="http://www.w3.org/2000/svg">
            <!-- Side 1 -->
            <path d="M198 3.5L392 111" stroke="#FF9F43" />
            <!-- Side 2 -->
            <path d="M392 111L392 330.5" stroke="#FFEB3B" />
            <!-- Side 3 -->
            <path class="strike-dash" d="M392 330.5L198 437.5" stroke="#4CAF50"/>
            <!-- Side 4 (with stroke-dasharray) -->
            <path d="M198 437.5L3 330.5" stroke="#42A5F5"/>
            <!-- Side 5 -->
            <path d="M3 330.5L3 111" stroke="#AB47BC" />
            <!-- Side 6 -->
            <path class="strike-dash" d="M3 111L198 3.5" stroke="crimson" />
        </svg>
    </div>
</div>
```

To make our starter skill stand out we will apply a different color to each side of the hexagon. We will use the `stroke` attribute to apply the color to the sides directly.

```css
.hex-starter > .hex-content {
    background: conic-gradient(
        #FF9F43 0%,
        #FFEB3B 20%,
        #4CAF50 40%,
        #42A5F5 60%,
        #AB47BC 80%,
        crimson 100%
      );
}
```

And apply a `conic gradient` as the background, making it all seamlessly integrate with the stroke colors.

With that, we can now see the starter skill placed in the center of our skill tree!

#### Regular skills

Since we will be looping through the `skills` array we will need to create a new hexagon element for each skill. We can easily do that with the help of the `data-bind-for` attribute.

The ohter notable difference from the starter skill logic is that here we will dynamically create the class name based on the skill type, which with the help of the CSS we set up earlier will apply different colors based on the categories of the skills.

And finally, we will use `data-bind-class-toggle` to conditionally apply the `hex-locked` class to the hexagon element if the skill is not unlocked. This will make the hexagon appear grayed out if they are locked.

```html
<div
    class="hex hex-skill"
    data-bind-for="index, skill:{{SkillsModel.skills}}"
    data-bind-style-top='{{skill.y}}'
    data-bind-style-left='{{skill.x}}'
    data-bind-class="'hex-'+{{skill.type}};'"
    data-bind-class-toggle="hex-locked:{{skill.unlocked}} === false">
    <div class="hex-content" data-bind-style-mask-image='{{skill.image}}'></div>
    <svg class="svg svg-main" viewBox="0 0 395 441" fill="none" xmlns="http://www.w3.org/2000/svg">
        <!-- Side 1 -->
        <path d="M198 3.5L392 111" />
        <!-- Side 2 -->
        <path d="M392 111L392 330.5" />
        <!-- Side 3 -->
        <path class="strike-dash" d="M392 330.5L198 437.5" />
        <!-- Side 4 (with stroke-dasharray) -->
        <path d="M198 437.5L3 330.5" />
        <!-- Side 5 -->
        <path d="M3 330.5L3 111" />
        <!-- Side 6 -->
        <path class="strike-dash" d="M3 111L198 3.5" />
    </svg>
</div>
```

#### Combined skills

Lastly, we will create a hexagon element for each combined skill. The combined skills will have a different color on both the left and right sides based on the category of the parent skill of each side.

We will achieve that by grouping the left and right sides of the hexagon svg `path` elements and applying a class to it.

```html
<div
    class="hex hex-skill"
    data-bind-for="index, skill:{{SkillsModel.specialSkills}}"
    data-bind-style-top='{{skill.y}}'
    data-bind-style-left='{{skill.x}}'
    data-bind-class="'hex-'+{{skill.type}};'"
    data-bind-class-toggle="hex-locked:{{skill.unlocked}} === false">
    <div class="hex-content" data-bind-style-mask-image='{{skill.image}}'></div>
    <!-- Main -->
    <svg class="svg svg-main" data-bind-class="'hex-'+{{skill.type}};'" viewBox="0 0 395 441" fill="none" xmlns="http://www.w3.org/2000/svg">
        <g class="path-right-side">
            <!-- Side 1 -->
            <path d="M198 3.5L392 111"  />
            <!-- Side 2 -->
            <path d="M392 111L392 330.5" />
            <!-- Side 3 -->
            <path class="strike-dash" d="M392 330.5L198 437.5" />
        </g>
        <g class="path-left-side">
            <path d="M198 437.5L3 330.5" />
            <!-- Side 5 -->
            <path d="M3 330.5L3 111"  />
            <!-- Side 6 -->
            <path class="strike-dash" d="M3 111L198 3.5"  />
        </g>
    </svg>
</div>
```

And just as we did with the starter skill, because there will be more than one color for the background, we will add separate styles for the background of the combined skills.

```css
/* Combined skills colors */
.hex-strength-durability .hex-content {
    background: linear-gradient(to right, crimson 0%, #FF9F43 80%);
}

.hex-health-mana .hex-content {
    background: linear-gradient(to right, #42A5F5 0%, #4CAF50 80%);
}

/* Locked background */
.hex-locked .hex-content{
    background: #ffffff;
}
```

And with that we have all the skills placed on the skill tree!

*If you comment out the `data-bind-class-toggle` attribute you will be able to see the color categories*

### Replacing the skill tree svg

Now that we have all the skills placed on the skill tree, we can remove the hexagons that we placed our elements upon and preserve only the lines connecting them.

Achieving this is very simple, we just need to head to Figma again and export the skill tree without the hexagons.

## Adding effects and animations

To make our colorful skill tree even more engaging we can add some effects and animations to the hexagons.

### Glow effect

To enhance the visual appeal of the skill tree we can add a glow effect to the hexagons making them more futuristic.

Doing that is very simple, we just need to add a `filter` property to the svg hexagon element.

```css
.svg-main {
    filter: drop-shadow(0 0 5px var(--shadow-color)) drop-shadow(0 0 20px var(--shadow-color));
}
```

Each hexagon will now have a glow effect that will change color based on the category of the skill.

We also need to handle the combined skills and the starter skill separately.

```css
/* Combined skills glow */
.svg-main.hex-strength-durability {
    filter: drop-shadow(0 0 5px #FF9F43) drop-shadow(0 0 5px crimson)
}

.svg-main.hex-health-mana {
    filter: drop-shadow(0 0 5px #42A5F5) drop-shadow(0 0 5px #4CAF50)
}

/* Only for specificity */
.hex-locked .hex-strength-durability.svg-main,
.hex-locked .hex-health-mana.svg-main {
    filter: drop-shadow(0 0 5px #ffffff) drop-shadow(0 0 5px #ffffff)
}
```

The starter skill will have a different glow effect. We want each side of the hexagon to have a different glow color. Unfortunately, we can’t apply filter to a path element, so the approach we will go for is to put 2 of the same SVGs in the `hex-content` element and blur them, making the effect of a glow.

In the `hex-starter` element, inside `hex-content` we will add the following html:

```html
<div>
    <svg class="svg svg-main svg-starter-glow outer" viewBox="0 0 395 441" fill="none" xmlns="http://www.w3.org/2000/svg">
        <!-- Side 1 -->
        <path d="M198 3.5L392 111" stroke="#FF9F43" />
        <!-- Side 2 -->
        <path d="M392 111L392 330.5" stroke="#FFEB3B" />
        <!-- Side 3 -->
        <path class="strike-dash" d="M392 330.5L198 437.5" stroke="#4CAF50"/>
        <!-- Side 4 (with stroke-dasharray) -->
        <path d="M198 437.5L3 330.5" stroke="#42A5F5"/>
        <!-- Side 5 -->
        <path d="M3 330.5L3 111" stroke="#AB47BC" />
        <!-- Side 6 -->
        <path class="strike-dash" d="M3 111L198 3.5" stroke="crimson" />
    </svg>
    <svg class="svg svg-main svg-starter-glow inner" viewBox="0 0 395 441" fill="none" xmlns="http://www.w3.org/2000/svg">
        <!-- Side 1 -->
        <path d="M198 3.5L392 111" stroke="#FF9F43" />
        <!-- Side 2 -->
        <path d="M392 111L392 330.5" stroke="#FFEB3B" />
        <!-- Side 3 -->
        <path class="strike-dash" d="M392 330.5L198 437.5" stroke="#4CAF50"/>
        <!-- Side 4 (with stroke-dasharray) -->
        <path d="M198 437.5L3 330.5" stroke="#42A5F5"/>
        <!-- Side 5 -->
        <path d="M3 330.5L3 111" stroke="#AB47BC" />
        <!-- Side 6 -->
        <path class="strike-dash" d="M3 111L198 3.5" stroke="crimson" />
    </svg>
</div>
```

And the following CSS:

```css
.svg-starter-glow {
    filter: blur(10px);
}

.svg-starter-glow.outer {
    transform: scale(1.02);
}

.svg-starter-glow.inner {
    transform: scale(0.98);
}
```

### Active skill effect

When a skill is hovered or selected we can add a unique animation making it stand out from the rest:

Let’s begin by firstly modifying the hex elements by adding a couple of SVGs again inside the `hex-content` element.

To decrease the amount of code, we are going to once again use `data-bind-for` to render the inner svgs that will be used for the animation.

```js
engine.on("Ready", () => {
engine.createJSModel("SkillsModel", SkillsModel);
engine.createJSModel("SvgModel", SvgModel);
```

And we should also create the model like so:

```js
const SvgModel = {
    innerSvgs: [1,2,3],
}
```

Starter hex:

```html
<!-- Outer -->
<svg class="svg svg-outer" viewBox="0 0 395 441" fill="none" xmlns="http://www.w3.org/2000/svg">
    <!-- Side 1 -->
    <path d="M198 3.5L392 111" stroke="#FF9F43" />
    <!-- Side 2 -->
    <path d="M392 111L392 330.5" stroke="#FFEB3B" />
    <!-- Side 3 -->
    <path d="M392 330.5L198 437.5" stroke="#4CAF50"/>
    <!-- Side 4 (with stroke-dasharray) -->
    <path d="M198 437.5L3 330.5" stroke="#42A5F5"/>
    <!-- Side 5 -->
    <path d="M3 330.5L3 111" stroke="#AB47BC" />
    <!-- Side 6 -->
    <path d="M3 111L198 3.5" stroke="crimson" />
</svg>

<!-- Inner -->
<div data-bind-for="svg:{{SvgModel.innerSvgs}}">
    <svg class="svg svg-inner" data-bind-class="'svg-inner-'+{{svg}}" data-bind-class-toggle="selected: {{SkillsModel.starterSkill}} === {{activeState.activeSkill}}" viewBox="0 0 395 441" fill="none" xmlns="http://www.w3.org/2000/svg">
        <path d="M198 3.5L392 111" stroke="#FF9F43" />
        <!-- Side 2 -->
        <path d="M392 111L392 330.5" stroke="#FFEB3B" />
        <!-- Side 3 -->
        <path d="M392 330.5L198 437.5" stroke="#4CAF50"/>
        <!-- Side 4 (with stroke-dasharray) -->
        <path d="M198 437.5L3 330.5" stroke="#42A5F5"/>
        <!-- Side 5 -->
        <path d="M3 330.5L3 111" stroke="#AB47BC" />
        <!-- Side 6 -->
        <path d="M3 111L198 3.5" stroke="crimson" />
    </svg>
</div>
```

Regular skills:

```html
<svg class="svg  svg-outer" viewBox="0 0 395 441" fill="none" xmlns="http://www.w3.org/2000/svg">
    <path d="M392 111L198 3.5L3 111V330.5L198 437.5L392 330.5V111Z" />
</svg>

<!-- Inner -->
<div data-bind-for="svg:{{SvgModel.innerSvgs}}">
    <svg class="svg svg-inner" data-bind-class="'svg-inner-'+{{svg}}" data-bind-class-toggle="selected: {{skill}} === {{activeState.activeSkill}}" viewBox="0 0 395 441" fill="none" xmlns="http://www.w3.org/2000/svg">
        <path d="M392 111L198 3.5L3 111V330.5L198 437.5L392 330.5V111Z"  />
    </svg>
</div>
```

Combined skills:

```html
<!-- Outer -->
<svg class="svg svg-outer" viewBox="0 0 395 441" fill="none" xmlns="http://www.w3.org/2000/svg">
    <g class="path-right-side">
        <!-- Side 1 -->
        <path  d="M198 3.5L392 111"  />
        <!-- Side 2 -->
        <path  d="M392 111L392 330.5" />
        <!-- Side 3 -->
        <path d="M392 330.5L198 437.5" />
    </g>
    <g class="path-left-side">
        <path d="M198 437.5L3 330.5" />
        <!-- Side 5 -->
        <path d="M3 330.5L3 111"  />
        <!-- Side 6 -->
        <path d="M3 111L198 3.5"  />
    </g>
</svg>

<!-- Inner -->
<div data-bind-for="svg:{{SvgModel.innerSvgs}}">
    <svg class="svg svg-inner" data-bind-class="'svg-inner-'+{{svg}}" data-bind-class-toggle="selected: {{skill}} === {{activeState.activeSkill}}" viewBox="0 0 395 441" fill="none" xmlns="http://www.w3.org/2000/svg">
        <g class="path-right-side">
            <!-- Side 1 -->
            <path  d="M198 3.5L392 111"  />
            <!-- Side 2 -->
            <path  d="M392 111L392 330.5" />
            <!-- Side 3 -->
            <path d="M392 330.5L198 437.5" />
        </g>
        <g class="path-left-side">
            <path d="M198 437.5L3 330.5" />
            <!-- Side 5 -->
            <path d="M3 330.5L3 111"  />
            <!-- Side 6 -->
            <path d="M3 111L198 3.5"  />
        </g>
    </svg>
</div>
```

We will now use the `svg-outer` and `svg-inner` classes to apply different kinds of animations for each SVG.

Let’s create a second CSS file to put our animations in as to not polute the global one.

```css
@keyframes inner-shrink-1 {
    0% {
        transform: scale(1);
    }
    100% {
        transform: scale(0.9);
    }
}

@keyframes inner-shrink-2 {
    0% {
        transform: scale(0.95);
    }
    100% {
        transform: scale(0.85);
    }
}

@keyframes inner-shrink-3 {
    0% {
        transform: scale(0.95);
    }
    100% {
        transform: scale(0.8);
    }
}

@keyframes outer-glow {
    0% {
        filter: drop-shadow(0 0 5px rgba(0, 0, 0, 0.51)) blur(5px);
        stroke-width: 10;
        opacity: 1;
    }

    25% {
        filter: drop-shadow(0 0 30px rgba(0, 0, 0, 0.51)) blur(15px);
        stroke-width: 50;
    }

    90% {
        transform: scale(1.25);
        opacity: 0.25;
        stroke-width: 20;
    }

    100% {
        opacity: 0;
        stroke-width: 0;
        transform: scale(1.3);
    }
}
```

Unfortunately, CSS variables can’t be used in keyframes, so we will have to set some default colors for the animations.

Now let’s add the animations to the hexagons:

```css
/* Outer shadow */
.selected {
    transform: scale(1.1);
}

.svg-outer {
    position: absolute;
    top: 0;
    left: 0;
    display: none;
}

.selected .svg-outer {
    animation: outer-glow 1.5s infinite ease-in;
    display: block;
}

/* Inner animation svg */
.svg-inner {
    position: absolute;
    top: 0;
    left: 0;
    stroke-width: 4;
    opacity: 0.5;
    display: none;
}

.selected.svg-inner {
    display: block;
}

.svg-inner-1 {
    animation: inner-shrink-1 0.75s infinite alternate ease-in-out;
}

.svg-inner-2 {
    stroke-width: 7;
    opacity: 0.75;
    animation: inner-shrink-2 0.75s infinite alternate ease-in-out;
}

.svg-inner-3 {
    animation: inner-shrink-3 0.75s infinite alternate ease-in-out;
}
```

We added a `selected` class to apply and run the animations only on the currently selected element.

We can now add the `selected` class to the hexagon element and its SVGs when it is hovered or clicked. We will again utilize the power of [data-binding ](https://docs.coherent-labs.com/cpp-gameface/integration/ui_scripting/htmldatabinding/#DataBinding)to easily attach events to our skill tree items.

For more seаmless state management, we are going to create [an observable model ](https://docs.coherent-labs.com/cpp-gameface/integration/ui_scripting/htmldatabinding/#observable-models)which will automatically update when its state changes.

After initialzing it we are going to set its value as our starter skill.

```js
engine.on("Ready", () => {
    engine.createJSModel("SkillsModel", SkillsModel);
    engine.createJSModel("SvgModel", SvgModel);
    engine.createObservableModel("activeState");
    engine.addSynchronizationDependency(SkillsModel, activeState);

    activeState.activeSkill = SkillsModel.starterSkill;
```

Starter skill:

```html
<div
    class="hex hex-starter"
    data-bind-style-top='{{SkillsModel.starterSkill.y}}'
    data-bind-style-left='{{SkillsModel.starterSkill.x}}'
    data-bind-focus="makeActive(this, {{SkillsModel.starterSkill}})"
    data-bind-class-toggle="selected: {{SkillsModel.starterSkill}} === {{activeState.activeSkill}}"
    data-bind-mouseenter="focusElement(this)"
>
```

Other skills:

```html
<div
    class="hex hex-skill"
    data-bind-for="index, skill:{{SkillsModel.skills}}"
    data-bind-style-top='{{skill.y}}'
    data-bind-style-left='{{skill.x}}'
   data-bind-focus="makeActive(this, {{skill}})"
   data-bind-mouseenter="focusElement(this)"
    data-bind-class="'hex-'+{{skill.type}};'"
    data-bind-class-toggle="hex-locked:{{skill.unlocked}} === false"
   data-bind-class-toggle="selected: {{skill}} === {{activeState.activeSkill}};hex-locked:{{skill.unlocked}} === false">
>
```

As you can see, we’ve added a `data-bind-focus` attribute to the hexagon element. This attribute will call the `makeActive` function when the element is focused. The `makeActive` function will expect the DOM element and the skill object from the model as arguments and there we will handle which element is active.

```js
function focusElement(element) {
    element.focus();
}

function makeActive(skillElement, skill) {
    if(skillElement.classList.contains('selected')) return

    activeState.activeSkill = skill;
    engine.synchronizeModels();
}
```

Like that, the code will almost work, what’s left is to make each of our hexagons focusable.

### Adding keyboard navigation

We are going to use Gameface’s [Interaction manager ](https://coherentlabs.github.io/GameUIComponents/en/interaction-manager)library and more specifically the [Spatial navigation ](https://coherentlabs.github.io/GameUIComponents/en/interaction-manager/spatial-navigation/)to make our hexagons focusable and to easily extend our sample with keyboard navigation.

After installing it and putting it in our project, what’s left is to initialize it in our `index.js` file.

```js
function initSpatialNavigation() {
    interactionManager.spatialNavigation.init(['.hex'], 0.3);
    interactionManager.spatialNavigation.focusFirst();
}
```

Here we say that we want to make all elements with the class `hex` focusable and that we want to allow a maximum of 30% overlap between the current item and the others when deciding which element to focus next. You can find out more about this parameter in the [documentation ](https://coherentlabs.github.io/GameUIComponents/en/interaction-manager/spatial-navigation/#overlap-optional).

The only thing left is to call this function after the model has loaded in the `Ready` event.

```js
engine.on("Ready", () => {
    engine.createJSModel("SkillsModel", SkillsModel);
    engine.synchronizeModels();
    initSpatialNavigation();
});
```

And that’s it! Now you can navigate through the hexagons using the keyboard arrows and see the active element animation respond.

## Making it all interactive

Now that we have our skill tree set up, we can start making everything interactive and input responsive.

### Adding tooltip with skill information

We are going to add a tooltip to display the skill’s information when the user hovers over a skill and a skill points counter to keep track of the available skill points the player has.

The tooltip will consist of the following elements:

- Skill name
- Skill cost
- Skill description
- Unlocked status that will show dynamically based on the skill’s unlocked state.

We are once again going to make use of the observable model that keeps track of the active element. Having this information we can use `data-binding` to fill out the `textContent` of the elements we need with the properties of the currently active element.

```html
<body>
    <div class="skill-info" data-bind-class-toggle="hex-locked:{{activeState.activeSkill.unlocked}} === false" data-bind-class="'hex-'+{{activeState.activeSkill.type}}">
        <h1 class="skill-name" data-bind-value="{{activeState.activeSkill.name}}" ></h1>
        <div class="skill-price" data-bind-value="'Skill cost'+' '+{{activeState.activeSkill.skillPoints}}" data-bind-class-toggle="hidden:!{{activeState.activeSkill.skillPoints}}"></div>
        <div class="skill-description" data-bind-value="{{activeState.activeSkill.description}}"></div>
        <div style="overflow: hidden;">
            <div class="skill-status" data-bind-class-toggle="unlocked:{{activeState.activeSkill.unlocked}}">unlocked</div>
        </div>
    </div>
    <div class="container">
```

Now to add the styles

```css
.skill-info {
    position: absolute;
    top: 2.5%;
    left: 1%;
    padding: 1.5vmax 1.25vmax;
    width: 30%;
    background: rgba(0, 0, 0, 0.8);
    color: white;
    border-radius: 10px;
    box-shadow: 0 0 20px 3px var(--shadow-color);
    transition: box-shadow 0.5s;
}

.hex-locked.skill-info {
    opacity: 0.8;
}

.hex-starter.skill-info,
.hex-strength-durability.skill-info,
.hex-health-mana.skill-info {
    background-color: black;
}

.hex-strength-durability.skill-info {
    --shadow-color: crimson;
    --shadow-color-2: #FF9F43;
}

.hex-health-mana.skill-info {
    --shadow-color: #42A5F5;
    --shadow-color-2: #4CAF50;
}

/* Reset for locked state */
.hex-locked.skill-info {
    --shadow-color: #FFF;
}

.hex-starter.skill-info::before,
.hex-strength-durability.skill-info::before,
.hex-health-mana.skill-info::before {
    content: '';
    position: absolute;
    top: -5px;
    left: -5px;
    right: -5px;
    bottom: -5px;
    z-index: -1;
    border-radius: 10px;
    filter: blur(10px);
    background-image: linear-gradient(to right, var(--shadow-color), var(--shadow-color-2));
}

.hex-starter.skill-info::before {
    background: linear-gradient(to right, #FF9F43, #FFEB3B, #4CAF50, #42A5F5, #AB47BC, crimson);
}

/* Reset for locked state */
.hex-locked.skill-info::before {
    background-image: none;
}

.skill-name {
    border-bottom: 2px solid rgba(255, 255, 255, 0.5);
    font-size: 1.6vmax;
    margin: 0;
    margin-bottom: 1vmax;
    padding-bottom: 1vmax;
    text-transform: uppercase;
}

.skill-price {
    text-transform: uppercase;
    font-size: 1.7vmax;
    margin-bottom: 1vmax;
    color: var(--shadow-color);
    filter: drop-shadow(0 0 10px var(--shadow-color));
    transition: filter 0.5s, color 0.5s;
}

.skill-price.hidden {
    display: none;
}

.skill-description {
    font-size: 0.9vmax;
}

.skill-points {
    position: absolute;
    bottom: 2.5%;
    left: 1%;
    display: flex;
    font-size: 3vmax;
}

.skill-status {
    color: white;
    letter-spacing: 10px;
    font-size: 1.2vmax;
    text-transform: uppercase;
    text-align: center;
    margin-top: 2vmax;
    position: relative;
    transform: translateX(-100%);
    opacity: 0;
    transition: transform 0.3s, opacity 0.3s;
}

.skill-status.unlocked {
    transform: translate(0);
    opacity: 1;
}

.skill-status::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: var(--shadow-color);
    opacity: 0.5;
    z-index: -1;
    border: 1px solid white;
    transition: background-color 0.5s;
}

/* starter skill status */
.hex-starter .skill-status::before {
    background: linear-gradient(to right, #FF9F43, #FFEB3B, #4CAF50, #42A5F5, #AB47BC, crimson);
}

/* Combined skills status */
.hex-strength-durability .skill-status::before,
.hex-health-mana .skill-status::before {
    background-image: linear-gradient(to right, var(--shadow-color), var(--shadow-color-2));
}
```

The idea is to have the tooltip correspond to the skill category and also have a glow effect. We achieved this by using `box shadows` for the regular skills and `linear gradients` for the starter and combined skills.

Now when you hover over a skill, you will see the tooltip with the skill’s information dynamically updating upon interaction.

### Adding skill unlock functionality

In order to make the interaction with the skill tree more engaging, we will make the paths connecting the skills fill with the corresponding color of the skill’s category when a skill is unlocked.

We will slightly modify the SVG paths by giving them category classes, as well as a class that will help us keep track of which path corresponds to which skill of the category, essentially making each path have a unique class that will help us identify when to color its stroke.

```html
<svg class="svg-paths" width="100%" height="100%" viewBox="0 0 1466 1380" fill="none" xmlns="http://www.w3.org/2000/svg">
    <path class="hex-path hex-locked hex-stamina stamina-1" d="M816.041 681.154H855.924" stroke-width="5"/>
    <path class="hex-path hex-locked hex-stamina stamina-2" d="M1030.69 680.257H1070.58" stroke-width="5"/>
    <path class="hex-path hex-locked hex-stamina stamina-3" d="M1245.35 680.257H1285.23" stroke-width="5"/>
    <path class="hex-path hex-locked hex-strength strength-1" d="M689.74 606.015L669.799 571.475" stroke-width="5"/>
    <path class="hex-path hex-locked hex-strength strength-2" d="M579.5 420.042L559.559 385.502" stroke-width="5"/>
    <path class="hex-path hex-locked hex-strength strength-3" d="M472.174 234.069L452.232 199.529" stroke-width="5"/>
    <path class="hex-path hex-locked hex-durability durability-1" d="M776.004 609.151L795.946 574.611" stroke-width="5"/>
    <path class="hex-path hex-locked hex-durability durability-2" d="M883.107 423.179L903.049 388.639" stroke-width="5"/>
    <path class="hex-path hex-locked hex-durability durability-3" d="M997.828 237.206L1017.77 202.666" stroke-width="5"/>
    <path class="hex-path hex-locked hex-misc misc-1" d="M644.05 679.362H604.167" stroke-width="5"/>
    <path class="hex-path hex-locked hex-misc misc-2" d="M429.306 680.257H389.423" stroke-width="5"/>
    <path class="hex-path hex-locked hex-misc misc-3" d="M214.653 680.257H174.769" stroke-width="5"/>
    <path class="hex-path hex-locked hex-mana mana-1" d="M684.971 752.67L665.03 787.21" stroke-width="5"/>
    <path class="hex-path hex-locked hex-mana mana-2" d="M577.948 938.603L558.007 973.143" stroke-width="5"/>
    <path class="hex-path hex-locked hex-mana mana-3" d="M470.687 1124.58L450.745 1159.12" stroke-width="5"/>
    <path class="hex-path hex-locked hex-health health-1" d="M774.518 753.526L794.459 788.066" stroke-width="5"/>
    <path class="hex-path hex-locked hex-health health-2" d="M885.14 939.5L905.081 974.04" stroke-width="5"/>
    <path class="hex-path hex-locked hex-health health-3" d="M996.34 1125.47L1016.28 1160.01" stroke-width="5"/>
    <path class="hex-path hex-locked hex-mana health-mana" d="M496.525 1244.9H640.15" stroke-width="5"/>
    <path class="hex-path hex-locked hex-health health-mana" d="M819.85 1244.9H975.798" stroke-width="5"/>
    <path class="hex-path hex-locked hex-strength strength-durability" d="M495.181 129.061H638.806" stroke-width="5"/>
    <path class="hex-path hex-locked hex-durability strength-durability" d="M818.505 129.061H974.454" stroke-width="5"/>
</svg>
```

Let’s apply the styles to the paths, as well as handle the transition for when the stroke is filled.

```css
.hex-path {
    stroke: var(--shadow-color);
    transition: all 0.3s ease-in;
}
```

With that out of the way we are now ready to combine the skill unlock functionality with the skill tree.

In the `index.js` file we will add an `unlockSkill` function that will handle all the logic.

```js
function unlockSkill(skillElement, skill, combined = false) {
    const {type, skillPoints, parents, unlocked} = skill;

    skillElement.focus();

    if (unlocked) return;

    if(skillPoints > SkillsModel.points || !isSkillValid(parents, skill)) {
        return;
    };

    skillElement.classList.remove('hex-locked');
    SkillsModel.points -= skillPoints;
    skill.unlocked = true;

    updateModel(SkillsModel);

    if(combined) {
        const paths = skillTree.querySelectorAll(`.${type}`);
        if (paths) paths.forEach((path) => path.classList.remove('hex-locked'));
    } else {
        const path = skillTree.querySelector(`.${type}-${skillPoints}`);
        if (path) path.classList.remove('hex-locked');
    }
}


function isSkillValid(parents, skill) {
    if(parents === null) return true;

    if (Array.isArray(parents)) return skill.parents.every((parentId) => SkillsModel.skills[parentId - 1].unlocked);

    return SkillsModel.skills[skill.parents - 1].unlocked;
}
```

In the `unlockSkill` function we do the following:

- Check if the skill is already unlocked
- Check if the player has enough skill points to unlock the skill
- Check if the skill’s parent is unlocked
- If all the conditions are met, we set the unlocked field in the model to true, update the model, and remove the `hex-locked` class from the skill element and the corresponding path to fill the svg’s path with color.

We also added the `isSkillValid` helper function that will help us determine if the skill’s is valid for unlocking.

- Normal skill is considered valid if the parent is unlocked
- Combined skill is considered valid if both parents are unlocked

Lastly we need to connect the `unlockSkill` function with our hexagon elements. The logic for unlocking will trigger when the element is clicked or the key ‘Enter’ is pressed.

```html
<div
    class="hex hex-skill"
    data-bind-for="index, skill:{{SkillsModel.skills}}"
    data-bind-style-top='{{skill.y}}'
    data-bind-style-left='{{skill.x}}'
    data-bind-click="unlockSkill(this, {{skill}})"
    data-bind-keypress="handleKeyPress(this, event, {{skill}})"
    data-bind-focus="makeActive(this, {{skill}})"
    data-bind-mouseenter="focusElement(this)"
    data-bind-class="'hex-'+{{skill.type}};'"
    data-bind-class-toggle="hex-locked:{{skill.unlocked}} === false">
```

For the combined skills, it’s the same as the regular skills, but we need to pass an additional argument to the `unlockSkill` function to indicate that the skill is combined.

```html
<div
    class="hex hex-skill"
    data-bind-for="index, skill:{{SkillsModel.skills}}"
    data-bind-style-top='{{skill.y}}'
    data-bind-style-left='{{skill.x}}'
   data-bind-click="unlockSkill(this, {{skill}}, true)"
   data-bind-keypress="handleKeyPress(this, event, {{skill}}, true)"
    data-bind-focus="makeActive(this, {{skill}})"
    data-bind-mouseenter="focusElement(this)"
    data-bind-class="'hex-'+{{skill.type}};'"
    data-bind-class-toggle="hex-locked:{{skill.unlocked}} === false">
```

Lastly, we need to handle the key press event for the ‘Enter’ key within the `handleKeyPress` function. Its logic is simple - if the ‘Enter’ key is pressed, we call the `unlockSkill` function.

```js
function handleKeyPress(skillElement, event, skill, combined = false) {
    if(event.charCode !== 13) return

    unlockSkill(skillElement, skill, combined)
}
```

And that’s it! Now you can interact with the skill tree by unlocking the skills and seeing the paths, skills and the tooltip fill with color.

### Adding error Handling

As you have probably noticed, clicking on a skill that is locked and doesn’t meet the requirements will not show a visual cue.

We should notify the player that the skill can’t be unlocked and why. We will add a subtle locked animation to the skill element and display an error message.

For displaying the error message, we will use [Gameface’s toast ](https://coherentlabs.github.io/GameUIComponents/en/components/toast/)component.

**Click if unsure how to add it to your project**

First, we need to install it.

```bash
npm i coherent-gameface-toast
```

After installing it, we need to add the toast component to our project. Let’s first add the toast’s styles.

We will add them before our custom styles, so they can be easily overwritten.

```html
<link rel="stylesheet" href="./node_modules/coherent-gameface-toast/coherent-gameface-components-theme.css">
<link rel="stylesheet" href="./node_modules/coherent-gameface-toast/style.css">
<link rel="stylesheet" href="styles.css">
<link rel="stylesheet" href="animations.css">
```

And the toast’s script

```html
<script src="cohtml.js"></script>
<script src="model.js"></script>
<script src="node_modules/coherent-gameface-interaction-manager/dist/interaction-manager.min.js"></script>
<script src="./node_modules/coherent-gameface-toast/dist/toast.production.min.js"></script>
<script src="index.js"></script>
```

Let’s add the toast to our `index.html` file.

```html
<body>
<gameface-toast class="toast-slide-in" position="top-right" timeout="3000">
    <div slot="message">
        <div class="error-message-header">Can't unlock</div>
        <div class="error-message"></div>
    </div>
</gameface-toast>
```

Now let’s customize the toast’s styles to fit the theme of our project.

```css
/* Error message */
.guic-toast-container {
    overflow: visible;
}

.toast-slide-in {
    animation-name: slide-in;
    animation-duration: 0.5s;
}

.toast-slide-in-retrigger {
    animation-name: slide-in-retrigger;
    animation-duration: 0.5s;
}

.guic-toast-hide {
    animation-name: guic-toast-fade-out;
}

.guic-toast {
    background: rgba(0, 0, 0, 0.8);
    color: white;
    border-radius: 10px;
    box-shadow: 0 0 20px 3px crimson;
    padding: 0.2vmax;
}

.guic-toast-message {
    padding: 0.5vmax;
}

.error-message-header {
    background-color: rgba(220, 20, 60, 0.5);
    padding: 0.3vmax 0.5vmax;
    margin-bottom: 0.3vmax;
    text-transform: uppercase;
    border: 1px solid white;
}

/* disable since we are not using it */
.guic-toast-close-btn {
    display: none;
}
```

And add a custom `slide-in` animation for the toast, overwritting the default one.

```css
@keyframes slide-in {
    0% {
        transform: translateX(50vmax);
    }
    50% {
        transform: translateX(-5vmax);
    }
    100% {
        transform: translateX(0%);
    }
}

@keyframes slide-in-retrigger {
    0% {
        transform: translateX(50vmax);
    }
    50% {
        transform: translateX(-5vmax);
    }
    100% {
        transform: translateX(0%);
    }
}
```

The reason we added 2 animation that do the same thing is because we want to be able to retrigger the animation if the toast needs to be shown again before the previous one has finished.

Now let’s add the logic for showing the error message.

```js
const toast = document.querySelector('gameface-toast');
const toastMessage = toast.querySelector('.error-message');

function showErrorMessage(skillPoints) {
    toastMessage.textContent = skillPoints > SkillsModel.points ? 'Not enough skill points' : 'Parent skill not unlocked';
    if(toast.visible) {
        toast.classList.toggle('toast-slide-in');
        toast.classList.toggle('toast-slide-in-retrigger');
    }

    toast.show();
}
```

The other visual indicator for invalid operation is the locked animation on the skill element. We will add a simple animation that will make the skill element shake when the player tries to unlock a locked skill.

```css
@keyframes shake {
    25% {
        transform: translateX(2.5%);
    }

    50% {
        transform: translateX(-2.5%);
    }
}
```

Apply it to the hex elements

```css
.hex.shake{
    animation: shake 0.5s ease-in-out;
}
```

What’s left is to add a function to trigger the animation and combine it with the toast error message in the `unlockSkill` function.

```js
if (skillPoints > SkillsModel.points || !isSkillValid(parents, skill)) {
    showErrorMessage(skillPoints);
    triggerLockedAnimation(skillElement);
    return;
};

function triggerLockedAnimation(skillElement) {
    skillElement.classList.add('shake');
    setTimeout(() => skillElement.classList.remove('shake'), 500);
}
```

Now we have a fully interactive skill tree with error handling and visual cues for the player!

### Quality of life improvements

Our skill tree is almost complete, but there is one quality of life improvement we can add to make the experience even better.

Currenly, if the player decided to navigate the tree using only the keyboard, the screen won’t follow them like it would if they were using the mouse. We can fix this by enhancing the `makeActive` function by calling the `translateSkillTreeIntoView` function and providing it with the coordinates of the active skill.

```js
function makeActive(skillElement, skill) {
    if(skillElement.classList.contains('selected')) return

    activeState.activeSkill = skill;
    engine.synchronizeModels();

    const skillRect = skillElement.getBoundingClientRect();
    const skillTreeRect = skillTree.getBoundingClientRect();

    // Coordinates relative to the skill tree
    const x = skillRect.x - skillTreeRect.x;
    const y = skillRect.y - skillTreeRect.y;

    translateSkillTreeIntoView(x, y);
}
```

In order to properly calculate the correct x and y coordinates, we have to subtract the position of the skill element from the position of the skill tree. That way we get the correct coordinates relative to the viewport and not the tree SVG.

And with that our skill tree is complete!

## On this page
