# core/scene (vanilla.js 0.4.0)

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

Scene.

## Scene extends Object

Scene.
- new() ==> create() ==> async load(engine) ==> initialize(engine)
- finalize(engine) ==> async unload(engine) ==> destroy()
- resize(canvasNativeSize)
- tick(timeDelta)
- preDraw(graphic) ==> draw(graphic) ==> postDraw(graphic)
- touchPress(viewInputPosition) ==> touchMove(viewInputPosition) ==> touchRelease(viewInputPosition)
- reset()
- Widgets on nodes receive touches whatever the kind of scene. tickTouch first passes the touch through the scene's touch raycaster (a TouchRaycaster at first, replaced with setTouchRaycaster)
  to the frontmost pressable node, then calls the scene's touch hooks. (like Unity's EventSystem, same as SceneBase.tick in the Rust version)

Example: Load, update, draw and receive presses

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

class MainScene extends Scene {
	async load(engine) {
		await super.load(engine);
		this.boxPosition = Vector2.create(100, 100);
	}

	initialize(engine) {
		console.log("initialized");
	}

	tick(timeDelta) {
		super.tick(timeDelta);
		this.boxPosition.x += 60 * timeDelta;
	}

	draw(graphic) {
		super.draw(graphic);
		graphic.setFillColor(Colors.white);
		graphic.drawRect(Rect.create(this.boxPosition.x, this.boxPosition.y, 80, 80));
	}

	touchPress(viewInputPosition) {
		this.boxPosition = viewInputPosition.clone();
	}
}

const engineConfiguration = new EngineConfiguration();
const engine = new Engine(engineConfiguration);
engine.run(new MainScene());
```

- `constructor()` (constructor, since 0.0.8): Constructor.
- `create()` (method, since 0.0.8): Constructor.
- `setEngine(engine)` (method, since 0.2.0): Sets the engine. (called by SceneManager before load() so it is accessible in drawOnLoad)
  - `engine` Engine: The engine that loaded this scene.
- `setLoaded(isLoaded)` (method, since 0.2.0): Sets whether loading is complete. (SceneManager switches it to true after load+initialize)
  - `isLoaded` boolean: Whether loading has finished.
- `isLoaded()` (method, since 0.2.0): Returns whether loading is complete.
  - returns boolean: True if loading has finished. (becomes true right before initialize)
- `drawOnLoad(graphic)` (method, since 0.2.0): Draws while loading. (called only while async load() is in progress, the scene's regular tick/draw is not called)
  - `graphic` Graphic: Graphic to draw with.

  Example: Show text while loading

  ```js
  import { Engine, EngineConfiguration, Scene, Colors } from "./build/vanilla.js";
  
  class MainScene extends Scene {
  	async load(engine) {
  		await super.load(engine);
  		await new Promise((resolve) => {
  			setTimeout(resolve, 500);
  		});
  	}
  
  	drawOnLoad(graphic) {
  		graphic.setFillColor(Colors.white);
  		graphic.setFontString("24px sans-serif");
  		graphic.drawFillText("Loading", 40, 40);
  	}
  }
  
  const engineConfiguration = new EngineConfiguration();
  const engine = new Engine(engineConfiguration);
  engine.run(new MainScene());
  ```

- `destroy()` (method, since 0.0.8): Destroy.
- `async load(engine)` (method, since 0.0.8): Async loading.
  - `engine` Engine: The engine loading the scene.
- `initialize(engine)` (method, since 0.0.8): Initialization.
  - `engine` Engine: The engine that loaded the scene.
- `finalize(engine)` (method, since 0.0.8): Finalization.
  - `engine` Engine: The engine unloading the scene.
- `onPageProcessRestored()` (method, since 0.2.0): Detects restoration after the page process was discarded (freeze/resume, bfcache restore, etc.).
  The engine detects the browser event and calls this on all loaded scenes.
- `onPageVisibilityRestored()` (method, since 0.2.0): Detects page visibility restoration (background → foreground).
  The engine detects the visibilitychange event and calls this on all loaded scenes.
- `async unload(engine)` (method, since 0.0.8): Async unloading.
  - `engine` Engine: The engine unloading the scene.
- `resize(canvasNativeSize)` (method, since 0.0.8): Screen size changed.
  - `canvasNativeSize` Vector2: New size of the canvas element. (CSS pixels)
- `tick(timeDelta)` (method, since 0.0.8): Periodic update.
  - `timeDelta` number: Time since the last frame. (Seconds)
- `preDraw(graphic)` (method, since 0.0.8): Pre-draw.
  - `graphic` Graphic: Graphic to draw with.
- `draw(graphic)` (method, since 0.0.8): Draw.
  - `graphic` Graphic: Graphic to draw with.
- `postDraw(graphic)` (method, since 0.0.8): Post-draw.
  - `graphic` Graphic: Graphic to draw with.
- `drawGizmos(graphic)` (method, since 0.0.11): Gizmo draw.
  - `graphic` Graphic: Graphic to draw with.
- `tickTouch(timeDelta)` (method, since 0.0.8): Touch update.
  - `timeDelta` number: Time since the last frame. (Seconds)
- `touchPress(viewInputPosition)` (method, since 0.0.8): Touch press.
  - `viewInputPosition` Vector2: Input position in view coordinates.
- `touchMove(viewInputPosition)` (method, since 0.0.8): Touch move.
  - `viewInputPosition` Vector2: Input position in view coordinates.
- `touchRelease(viewInputPosition)` (method, since 0.0.8): Touch release.
  - `viewInputPosition` Vector2: Input position in view coordinates.
- `touchCancel(viewInputPosition)` (method, since 0.1.0): Touch cancelled.
  - `viewInputPosition` Vector2: Input position in view coordinates.
- `touchWheel(viewInputPosition, wheelDelta)` (method, since 0.2.1): Mouse wheel.
  - `viewInputPosition` Vector2: Input position in view coordinates.
  - `wheelDelta` Vector2: Wheel amount accumulated this frame. (sum of WheelEvent deltaX, deltaY)
- `startTween(tween)` (method, since 0.0.8): Starts a tween.
  - `tween` Tween: Tween to start. (does nothing if it is already running)

  Example: Fade a node in over 0.5 seconds

  ```js
  import { Engine, EngineConfiguration, Scene, WorldNode, Tween, TweenProperty, Vector2 } from "./build/vanilla.js";
  
  class MainScene extends Scene {
  	initialize(engine) {
  		const root = this.getRoot();
  		const node = new WorldNode();
  		node.setContentSize(Vector2.create(100, 100));
  		root.addChild(node);
  		const tween = Tween.createNodeTween(node, TweenProperty.localOpacity, 0, 1, 0.5);
  		this.startTween(tween);
  		const tweenCount = this.getTweenCount();
  		console.log(tweenCount);
  	}
  }
  
  const engineConfiguration = new EngineConfiguration();
  const engine = new Engine(engineConfiguration);
  engine.run(new MainScene());
  ```

- `stopTween(tween)` (method, since 0.0.8): Stops a tween.
  - `tween` Tween: Tween to stop. (does nothing if it is not running in this scene)
- `getTweenCount()` (method, since 0.4.0): Returns the number of running tweens.
  - returns number: Number of running tweens.
- `stopAllTweens()` (method, since 0.0.8): Stops all tweens.
- `getEngine()` (method, since 0.0.8): Returns the engine.
  - returns Engine: The engine that loaded this scene. (undefined before loading)
- `getRoot()` (method, since 0.0.8): Returns the root node.
  - returns WorldNode: The root node. (created in create, sized to the view)
- `setTouchRaycaster(touchRaycaster)` (method, since 0.4.0): Sets the touch raycaster.
  - tickTouch passes touches to the frontmost pressable node through this raycaster. Its root is set to the scene root.
  - Given a TouchRecognizer, drag gestures inside scroll views (delayed press, a drag goes to the scroll view) are passed too.
  - null means touches are not passed to nodes. (when the scene passes them itself from its touch hooks, as UIScene and GameScene do)
  - `touchRaycaster` TouchRaycaster | null: Raycaster that passes touches to nodes. (null means touches are not passed)

  Example: Passing scroll view drag gestures too

  ```js
  import { Engine, EngineConfiguration, Scene, TouchRecognizer } from "./build/vanilla.js";
  
  class MainScene extends Scene {
  	initialize(engine) {
  		this.setTouchRaycaster(new TouchRecognizer());
  	}
  }
  
  const engineConfiguration = new EngineConfiguration();
  const engine = new Engine(engineConfiguration);
  engine.run(new MainScene());
  ```

- `setTouchDispatchEnabled(isEnabled)` (method, since 0.4.0): Turns passing touches to nodes on or off. (same as the Rust SceneBase.set_touch_dispatch_enabled)
  - When off, tickTouch does not pass to nodes through the raycaster and only calls the scene's touch hooks. (for intercepting in the hooks and passing directly)
  - The raycaster is kept, so turning it back on resumes passing. Same effect as setTouchRaycaster(null).
  - `isEnabled` boolean: true to pass to nodes. (true by default)

  Example: Turn off the scene's default passing and receive in the hooks directly

  ```js
  import { Scene } from "./build/vanilla.js";
  const scene = new Scene();
  scene.setTouchDispatchEnabled(false);
  console.log(scene.isTouchDispatchEnabled()); // false
  ```

- `isTouchDispatchEnabled()` (method, since 0.4.0): Returns whether touches are passed to nodes.
  - returns boolean: true if it passes.
- `getTouchRaycaster()` (method, since 0.4.0): Returns the touch raycaster.
  - returns TouchRaycaster | null: The raycaster that passes touches to nodes. (create makes a TouchRaycaster, null if touches are not passed)
- `setGizmoVisible(isVisible)` (method, since 0.0.9): Sets gizmo drawing.
  - `isVisible` boolean: If true, gizmos are drawn.
- `isGizmoVisible()` (method, since 0.0.9): Returns whether gizmos are drawn.
  - returns boolean: True if gizmos are drawn.
- `setSceneBackgroundColor(color)` (method, since 0.4.0): Sets the background color. (drawSceneBackground fills it, a Color or color text such as "#202020")
  - `color` Color | string: Background color. (Color text is read with Color.from, default black)
- `getSceneBackgroundColor()` (method, since 0.4.0): Returns the background color.
  - returns Color: Background color. (default black)
- `drawSceneBackground(graphic)` (method, since 0.4.0): Draws the background. (Fills the whole canvas with the background color, including outside the safe area, and applies the view coordinate system)
  - GameScene and UIScene call it in preDraw. A scene that extends Scene directly calls it in its own preDraw.
  - `graphic` Graphic: Graphic to draw with.

  Example: Filling the background of a scene that extends Scene directly

  ```js
  import { Engine, EngineConfiguration, Scene, Vector2 } from "./build/vanilla.js";
  
  class MainScene extends Scene {
  	create() {
  		super.create();
  		this.setSceneBackgroundColor("#203040");
  	}
  
  	preDraw(graphic) {
  		super.preDraw(graphic);
  		this.drawSceneBackground(graphic);
  	}
  }
  const engineConfiguration = new EngineConfiguration();
  engineConfiguration.referenceResolutionSize = Vector2.create(1280, 720);
  const engine = new Engine(engineConfiguration);
  engine.run(new MainScene());
  ```

