# Custom CSS Cursor

> A custom cursor is a must-have for every game, it brings forward the vibe and atmosphere of the 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/custom-cursor/

## Custom CSS Cursor

ui tutorials

3/28/2024

Tony Gruev

1. Mouse Movement Using attributeStyleMap
2. Limitations and Peculiarities
   1. Remove the body margin
   2. Scrollable Page
   3. Frame rate dependency
3. OS-controller Cursor (Recommended)

A custom cursor is a must-have for every game, it brings forward the vibe and atmosphere of the game.

We will use only CSS and JavaScript to hide the default cursor and move a custom one with an image.

**It is recommended** to use the OS-controller Cursor .

The [CSS Typed Object Model ](https://docs.coherent-labs.com/cpp-gameface/content_development/csstypedobjectmodel/#using-the-css-typed-object-model)will be utilized for a better performance.

A sample showing changing the mouse cursor image and increasing its size can be found in the Gameface package in folder **Samples\uiresources\UITutorials\CustomCSSCursor**.

Fist of all we hide the default cursor with:

```css
html {
    cursor: none;
}
```

Now the element for that cursor is:

```html
<div id="cursor" class="cursor"></div>
```

The styles for the cursor:

```css
.cursor {
    position: absolute;
    top: 0;
    left: 0;
    width: 55px;
    height: 55px;
    background-image: url('./assets/mouse1.svg');
    background-position: 50% 0;
    background-repeat: no-repeat;
    /* pass events throught the cursor element */
    pointer-events: none;
    /*
        should always be on top of every element but can be
        omitted if top level elements don't have z-index and
        the cursor element is at the end of the <body>
    */
    z-index: 1;
}
```

Although the default cursor may not be visible, it remains present. Therefore, when we click on the document, the `.cursor` element will capture the pointer events. To prevent this and allow the event to propagate to the element beneath the `.cursor` element, we must disable its pointer-events using `pointer-events: none`.

**Important:** Make sure the `.cursor` element has the highest `z-index` value over all the elements on the page. In case you don’t change the `z-index` - placing the `.cursor` element at the end of the `<body>` without `z-index` will suffice.

## Mouse Movement Using attributeStyleMap

And finally the JavaScript we need:

```js
const cursor = document.getElementById('cursor');
// Cache the attributeStyleMap for the cursor element
const cursorStyleMap = cursor.attributeStyleMap;
// Save the new CSSTransformValue to reuse it in cursorStyleMap.set()
const mouseTransform = new CSSTransformValue([new CSSTranslate(CSS.px(0), CSS.px(0))]);
// Save the properties from CSSTransformValue that are going to be changed
const mouseTransformTranslateX = mouseTransform[0].x;
const mouseTransformTranslateY = mouseTransform[0].y;

document.addEventListener('mousemove', (e) => {
    const {x, y} = e;
    mouseTransformTranslateX.value = x;
    mouseTransformTranslateY.value = y;
    cursorStyleMap.set('transform', mouseTransform);
});
```

The cursor icon can be changed easily also.

For example, by entering another element:

```js
changeBox.addEventListener('mouseover', () => {
    cursor.classList.toggle('cursor2');
});
```

And this requires only for the background image to be changed:

```css
.cursor2 {
    background-image: url('./assets/mouse2.svg');
}
```

## Limitations and Peculiarities

There are some particularities that need to be taken into account.

### Remove the body margin

Disabling the `margin` on the body prevents the cursor element from being offset from the OS cursor position.

```css
body {
    margin: 0;
}
```

### Scrollable Page

In case the page is scrollable, you will need to take that into account in the `mousemove` EventListener:

```js
// ...
mouseTransformTranslateX.value = x + window.scrollX;
mouseTransformTranslateY.value = y + window.scrollY;
// ...
```

Also the cursor element is updated only on `mousemove` and there is a need to watch for scrolling with the `scroll` event:

```js
let cacheMousePositions = {x: 0, y: 0};

document.addEventListener('scroll', (e) => {
    mouseTransformTranslateX.value = cacheMousePositions.x + window.scrollX;
    mouseTransformTranslateY.value = cacheMousePositions.y + window.scrollY;
    cursorStyleMap.set('transform', mouseTransform);
});
```

and the coordinates can be saved in the `mousemove` EventListener :

```js
cacheMousePositions = {x, y};
```

### Frame rate dependency

Since this front-end approach is relying on how fast the application is running, there will be a delay after moving the mouse and when the cursor element catches on. The lower the frame rate, the bigger the delay.

For information how to use the OS-controlled cursor, check the section below.

## OS-controller Cursor (Recommended)

It is recommended to use the OS-controlled cursor - read the [Cursor control ](https://docs.coherent-labs.com/cpp-gameface/integration/optional_features/inputhandlingcpp_native/#CursorControl)in the Gameface documentation.

## On this page
