# ui/uihitarea (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/ui/uihitarea/

Expands the press area.

## UIHitArea extends Component

Expands the press area.
- Keeps the visible button as is, and places a transparent press area as the sibling right behind the button to widen its width and height to at least the minimum size. (easier to press with a finger)
- The node later in draw order receives the press, so presses on the button are received by the button and only the widened part around the button is received by the press area.
- Pressing the press area calls the button's click notification as is. If the button is turned off or cannot be pressed, the press area follows.
- Follows the button's position, size and pivot every frame, so it stays matched even when the screen re-places the button.
- If a clipping node is set, clips the widened part so it does not go outside that node's screen rectangle. (the button's own area stays as is, so that areas outside the window or the scroll box are not pressed)
  Clipping is correct when the parent does not rotate.
- Usage: UIHitArea.attach(buttonNode, minimumSize) (call after the button is attached to its parent)

Example: Enlarge the press area of a small close button

```js
import { Engine, EngineConfiguration, UIScene, UIButton, UIHitArea, Paint, WorldNode, Pivot, Vector2, Color } from "./build/vanilla.js";

class MainScene extends UIScene {
	constructor() {
		super();
		this.setLoadingMinDurationMs(0);
	}

	initialize(engine) {
		super.initialize(engine);
		const buttonNode = new WorldNode();
		buttonNode.setName("CloseButton");
		buttonNode.setPivot(Pivot.topLeft);
		buttonNode.setAnchor(Pivot.topLeft);
		buttonNode.setLocalPosition(Vector2.create(300, 20));
		buttonNode.setContentSize(Vector2.create(24, 24));
		const paint = buttonNode.addComponent(Paint);
		paint.setColor(new Color(0.85, 0.3, 0.3, 1));
		const button = buttonNode.addComponent(UIButton);
		button.setClickedEvent(() => {
			console.log("close");
		});
		const root = this.getRoot();
		root.addChild(buttonNode);

		const hitNode = UIHitArea.attach(buttonNode, 48);
		const hitArea = hitNode.getComponent(UIHitArea);
		hitArea.setClipNode(root);
		console.log(hitNode.getName(), hitArea.getMinimumSize());
	}
}

const engineConfiguration = new EngineConfiguration();
engineConfiguration.canvasId = "mainCanvas";
engineConfiguration.referenceResolutionSize = Vector2.create(640, 360);
engineConfiguration.autoResizeOnWindowResize = true;
const engine = new Engine(engineConfiguration);
engine.run(new MainScene());
```

- `constructor()` (constructor, since 0.4.0): Constructor.
- `tick(timeDelta)` (method, since 0.4.0): Follows the button. (Aligns the center and widens only the short side, turns off together when the button is off)
  - `timeDelta` number: Time since the last frame. (Seconds)
- `setTarget(targetNode, targetButton)` (method, since 0.4.0): Sets the button to widen.
  - `targetNode` WorldNode: Node of the button to enlarge.
  - `targetButton` UIButton: The button attached to that node.
- `getTargetNode()` (method, since 0.4.0): Returns the node of the button to widen.
  - returns WorldNode | null: Node of the button to enlarge. (null if not set)
- `getTargetButton()` (method, since 0.4.0): Returns the button to widen.
  - returns UIButton | null: The button to enlarge. (null if not set)
- `setMinimumSize(minimumSize)` (method, since 0.4.0): Sets the minimum width and height of the press area.
  - `minimumSize` number: Minimum width and height of the press area. (units within the parent, axes where the button is larger are not enlarged)
- `getMinimumSize()` (method, since 0.4.0): Returns the minimum width and height of the press area.
  - returns number: Minimum width and height of the press area. (default 0)
- `setClipNode(clipNode)` (method, since 0.4.0): Sets the node that clips the widened portion. (null means no clipping)
  - `clipNode` WorldNode | null: Node that clips the enlarged part. (no clipping if null)
- `getClipNode()` (method, since 0.4.0): Returns the node that clips the widened portion.
  - returns WorldNode | null: Node that clips the enlarged part. (null if none)
- `static attach(buttonNode, minimumSize)` (static, since 0.4.0): Attaches a widened press area to the button. (Call after the button is attached to its parent, inserts it as the button's next sibling and returns the press area node)
  - `buttonNode` WorldNode: Node with UIButton attached.
  - `minimumSize` number: Minimum width and height of the press area. (units within the parent)
  - returns WorldNode | null: The created press area node. (null if the button node has no parent or no UIButton)
