# ui/uisnapscrollview (vanilla.js 0.4.0)

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

Snap scroll view component.

## UISnapScrollView extends UIScrollView

Snap scroll view component.
- Inherits ScrollView, and snaps to the center of an item when a drag is released.
- Even when items differ in size, each item center snaps to the viewport center.
- Horizontal snap: setHorizontal(true), setVertical(false) (default)
- Vertical snap: setHorizontal(false), setVertical(true)

[Snap behavior]
- setSnapToNearest(false) : moves only one item per swipe. (default)
  While dragging, the offset never goes beyond one item on either side of the current one. On release, if the drag velocity is at least setVelocityThreshold
  it moves one item in that direction (fling), otherwise it moves when the dragged distance is at least the setPageChangeThreshold ratio of the neighbor item size.
- setSnapToNearest(true)  : snaps to the item nearest the dragged position. (The drag is not clamped to neighbor items, so it can pass several items)
- setVelocityThreshold(px/s): minimum velocity that decides the direction in one-item mode. (default: 200, pixels per second the scroll offset moved)

[Paging]
- pageTo(index, smooth): moves to the given index. smooth=true tweens, false moves immediately.

Example: Five pages swiped horizontally

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

class PagerScene extends UIScene {
	create() {
		super.create();
		const pageWidth = 360;
		const pageHeight = 200;
		const pageCount = 5;
		const viewportNode = new WorldNode();
		viewportNode.setPivot(Pivot.topLeft.clone());
		viewportNode.setAnchor(Pivot.topLeft.clone());
		viewportNode.setContentSize(Vector2.create(pageWidth, pageHeight));
		viewportNode.setLocalPosition(Vector2.create(140, 80));
		const snapScrollView = viewportNode.addComponent(UISnapScrollView);
		snapScrollView.setPageChangeThreshold(0.25);

		const content = snapScrollView.getContent();
		for (let pageIndex = 0; pageIndex < pageCount; ++pageIndex) {
			const pageNode = new WorldNode();
			pageNode.setPivot(Pivot.topLeft.clone());
			pageNode.setAnchor(Pivot.topLeft.clone());
			pageNode.setContentSize(Vector2.create(pageWidth, pageHeight));
			pageNode.setLocalPosition(Vector2.create(pageIndex * pageWidth, 0));
			const pagePaint = pageNode.addComponent(Paint);
			pagePaint.setColor(new Color(0.2 + pageIndex * 0.15, 0.3, 0.6, 1));
			const pageLabel = pageNode.addComponent(UILabel);
			pageLabel.setText("Page " + (pageIndex + 1));
			pageLabel.setFontSize(28);
			content.addChild(pageNode);
		}
		snapScrollView.setScrollContentSize(Vector2.create(pageWidth * pageCount, pageHeight));
		snapScrollView.pageTo(2, false);
		const root = this.getRoot();
		root.addChild(viewportNode);
	}
}

const engineConfiguration = new EngineConfiguration();
const engine = new Engine(engineConfiguration);
const scene = new PagerScene();
scene.setLoadingMinDurationMs(0);
engine.run(scene);
```

- `constructor()` (constructor, since 0.2.0): Constructor.
- `setSnapToNearest(isSnapToNearest)` (method, since 0.2.0): Sets the snap mode.
  - false: moves only one item per swipe. (default)
  - true : snaps to the item nearest the drag position.
  - `isSnapToNearest` boolean: True selects nearest item mode. (The drag is not clamped to neighbor items, and on release it goes to the item whose snap offset is nearest)

  Example: Drag across several items and snap to the nearest one on release

  ```js
  import { UISnapScrollView, WorldNode, Pivot, Vector2 } from "./build/vanilla.js";
  
  const viewportNode = new WorldNode();
  viewportNode.setContentSize(Vector2.create(200, 120));
  const snapScrollView = viewportNode.addComponent(UISnapScrollView);
  snapScrollView.setSnapToNearest(true);
  const content = snapScrollView.getContent();
  for (let pageIndex = 0; pageIndex < 5; ++pageIndex) {
  	const pageNode = new WorldNode();
  	pageNode.setPivot(Pivot.topLeft.clone());
  	pageNode.setAnchor(Pivot.topLeft.clone());
  	pageNode.setContentSize(Vector2.create(200, 120));
  	pageNode.setLocalPosition(Vector2.create(pageIndex * 200, 0));
  	content.addChild(pageNode);
  }
  snapScrollView.setScrollContentSize(Vector2.create(1000, 120));
  snapScrollView.touchPress(Vector2.create(190, 60));
  snapScrollView.tick(0.016);
  snapScrollView.touchMove(Vector2.create(-300, 60));
  snapScrollView.tick(0.016);
  snapScrollView.touchRelease(Vector2.create(-300, 60));
  snapScrollView.tick(0.5);
  const scrollOffset = snapScrollView.getScrollOffset();
  console.log(snapScrollView.getSnapCurrentIndex(), scrollOffset.x);
  ```

- `getSnapToNearest()` (method, since 0.2.0): Returns the snap mode.
  - returns boolean: True if in nearest item mode. (default false)
- `setVelocityThreshold(velocityThreshold)` (method, since 0.2.0): Sets the minimum speed that triggers a directional snap. (px/s, used only in one item mode)
  - `velocityThreshold` number: Minimum velocity. (Pixels per second the scroll offset moved, in one-item mode a release at or above this velocity moves one item in that direction regardless of the dragged distance)
- `getVelocityThreshold()` (method, since 0.2.0): Returns the minimum speed that triggers a directional snap.
  - returns number: Minimum velocity. (pixels per second, default 200)
- `setPageChangeThreshold(pageChangeThreshold)` (method, since 0.2.0): Sets the page change threshold for slow swipes. (ratio to the item size, default: 0.35)
  - `pageChangeThreshold` number: Ratio to the neighboring item's size. (on release, moves to that item if dragged further than this)
- `getPageChangeThreshold()` (method, since 0.2.0): Returns the page change threshold for slow swipes.
  - returns number: Ratio to the neighboring item's size. (default 0.35)
- `getDragVelocity()` (method, since 0.4.0): Returns the drag velocity.
  - returns number: Velocity of the scroll axis offset while dragging. (Pixels per second, negative toward the next item, the value of the last dragging tick, 0 when a drag starts)
- `tick(timeDelta)` (method, since 0.2.0): Update.
  - `timeDelta` number: Time since the last frame. (seconds, the snap tween also advances by this much)
- `clampScrollOffsetToAdjacentPages()` (method, since 0.2.0): Limits the scroll offset during drag to the range of pages adjacent to the current page.
- `computeSnapOffsets()` (method, since 0.2.0): Computes the snap offset list per child node.
  - Returns an array of scroll offsets at which each item's center lands at the viewport center.
  - Computed correctly even if item sizes are uneven.
  - returns number[]: Scroll offsets in the order of the content node's children. (values on the scrolling axis, empty array if there are no children or before attaching to a node)
- `playSnapTween()` (method, since 0.2.0): Decides the snap index from the current offset and velocity and starts the tween.
  - Nearest item mode: the item whose snap offset is nearest the current offset.
  - One-item mode: one item in the velocity direction if the drag velocity is at least setVelocityThreshold, otherwise one item in the drag direction when the dragged distance is at least the ratio of the neighbor item size.
- `pageTo(index, smooth)` (method, since 0.2.0): Moves to the given index.
  - smooth: if true, smoothly with a tween, if false, moves immediately.
  - `index` number: Item index. (child index of the content node, clamped to 0 ~ child count - 1)
  - `smooth` boolean: If true, moves with a 0.3 second tween, if false, moves immediately.

  Example: Moving immediately and moving with a tween

  ```js
  import { UISnapScrollView, WorldNode, Pivot, Vector2 } from "./build/vanilla.js";
  
  const viewportNode = new WorldNode();
  viewportNode.setContentSize(Vector2.create(200, 120));
  const snapScrollView = viewportNode.addComponent(UISnapScrollView);
  const content = snapScrollView.getContent();
  for (let pageIndex = 0; pageIndex < 3; ++pageIndex) {
  	const pageNode = new WorldNode();
  	pageNode.setPivot(Pivot.topLeft.clone());
  	pageNode.setAnchor(Pivot.topLeft.clone());
  	pageNode.setContentSize(Vector2.create(200, 120));
  	pageNode.setLocalPosition(Vector2.create(pageIndex * 200, 0));
  	content.addChild(pageNode);
  }
  snapScrollView.setScrollContentSize(Vector2.create(600, 120));
  snapScrollView.pageTo(2, false);
  const jumpedOffset = snapScrollView.getScrollOffset();
  console.log(jumpedOffset.x);
  snapScrollView.pageTo(1, true);
  snapScrollView.tick(0.5);
  const tweenedOffset = snapScrollView.getScrollOffset();
  console.log(tweenedOffset.x);
  ```

- `setSnapCurrentIndex(index)` (method, since 0.2.0): Snap tween move to a specific index.
  - `index` number: Item index. (clamped to 0 ~ child count - 1, does nothing if there are no children)
- `getSnapCurrentIndex()` (method, since 0.2.0): Returns the current snap index.
  - returns number: Index of the item currently snapped to (or being tweened to).
