# core/pointercursor (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/core/pointercursor/

Mouse cursor.

## PointerCursor extends Singleton

Mouse cursor.
- When on (setEnabled(true)), the engine sets the canvas cursor at the end of every frame. When off (default), it does nothing, same as before.
- If the mouse is over a UIButton, the button's cursor (initially the pointing-finger pointer), otherwise the cursor the screen set with setScreenCursor.
- The value is a CSS cursor name or the name of an image cursor added with registerImageCursor. Written to the canvas only when it changes.
- default is the default cursor (initially the browser's default arrow), and an unknown name clears the canvas's cursor value so the browser's default arrow shows.
- Image cursors give 1x and 2x images together (-webkit-image-set), browsers that cannot read that get the 1x image, and if the image cannot be loaded, the CSS cursor given with it.
- On finger devices (pointer: coarse), the canvas is not touched.

Example: Set the screen cursor and an image cursor over buttons

```js
import { Engine, EngineConfiguration, Scene, PointerCursor } from "./build/vanilla.js";

const engineConfiguration = new EngineConfiguration();
const engine = new Engine(engineConfiguration);
const pointerCursor = PointerCursor.getInstance();
pointerCursor.registerImageCursor("hand", "assets/icons/icon.png", "", 4, 4, "pointer");
pointerCursor.setCursorNames("hand", "default");
pointerCursor.setScreenCursor("crosshair");
pointerCursor.setEnabled(true);
engine.run(new Scene());
const cursorValue = pointerCursor.readCursorValue("crosshair");
console.log(cursorValue);
```

- `constructor()` (constructor, since 0.4.0): Constructor.
- `setEnabled(isEnabled)` (method, since 0.4.0): Sets enabled. (Turning it off clears the canvas's cursor)
  - `isEnabled` boolean: Enables it if true.
- `isEnabled()` (method, since 0.4.0): Returns whether enabled.
  - returns boolean: True if enabled.
- `markOverButton()` (method, since 0.4.0): Notifies that the mouse is over a button. (The hovered button calls it every frame)
- `setScreenCursor(cursorName)` (method, since 0.4.0): Sets the screen cursor. (The cursor shown when not over a button)
  - `cursorName` string: A CSS cursor name or the name of an added image cursor.
- `getScreenCursor()` (method, since 0.4.0): Returns the screen cursor.
  - returns string: The screen cursor name.
- `setCursorNames(buttonCursorName, defaultCursorName)` (method, since 0.4.0): Sets the button-hover and default cursors. (A CSS cursor name or the name of an added image cursor)
  - `buttonCursorName` string: Cursor over a button.
  - `defaultCursorName` string: Cursor used when the screen cursor is default.
- `getButtonCursorName()` (method, since 0.4.0): Returns the button-hover cursor.
  - returns string: The cursor name over buttons.
- `getDefaultCursorName()` (method, since 0.4.0): Returns the default cursor.
  - returns string: The cursor name used when the screen cursor is default.
- `registerImageCursor(name, url, url2x, hotspotX, hotspotY, fallbackCursorName)` (method, since 0.4.0): Adds an image cursor. (1x and 2x images, hotspot relative to the 1x image, CSS cursor for when the image cannot be used)
  - On devices with a mouse, preloads the images so it does not flicker on the first hover.
  - `name` string: Cursor name.
  - `url` string: URL of the 1x image.
  - `url2x` string: URL of the 2x image. ("" if none)
  - `hotspotX` number: Hotspot of the 1x image. (pixels)
  - `hotspotY` number: Hotspot Y. (pixels, based on the 1x image, rounded)
  - `fallbackCursorName` string: CSS cursor name used when the image cannot be used.
- `readCursorValue(cursorName)` (method, since 0.4.0): Converts a cursor name to the value to write to the canvas. (The CSS value for an image cursor, a usable CSS name as is, otherwise "")
  - `cursorName` string: Cursor name.
  - returns string: Value to write to the canvas style.cursor. ("" for an unknown name)
- `apply()` (method, since 0.4.0): Applies the cursor. (The engine calls it at the end of every frame, clears the button mark)
- `static isCoarsePointer()` (static, since 0.4.0): Returns whether the primary pointer is a finger.
  - returns boolean: True if the primary pointer is a finger.
- `static readEngineCanvas()` (static, since 0.4.0): Reads the engine's canvas. (Obtained through the engine's global accessor to avoid circular references, null if none)
  - returns HTMLCanvasElement | null: The engine's canvas. (null if there is no engine)
