Methods
The Web Cube element has a few methods that you can use to interact with the cube.
setState
Sets the state of the cube. You can use the Face enum to represent face values.
Parameters
newState
The new state of the cube.
- Type:
FlatState - Required: Yes
Example
import { WebCube, Face } from "@web-cube/web-cube";const $cube = document.querySelector("web-cube") as WebCube;$cube.setState({ [Face.Up]: [ [Face.Up, Face.Up], [Face.Up, Face.Down], ], [Face.Down]: [ [Face.Down, Face.Down], [Face.Down, Face.Up], ], [Face.Left]: [ [Face.Left, Face.Left], [Face.Left, Face.Right], ], [Face.Right]: [ [Face.Right, Face.Right], [Face.Right, Face.Left], ], [Face.Front]: [ [Face.Front, Face.Front], [Face.Front, Face.Back], ], [Face.Back]: [ [Face.Back, Face.Back], [Face.Back, Face.Front],});When the state changes, the element emits web-cube:state-changed.
getState
Gets the state of the cube.
Returns
- Type:
FlatState
Example
import { WebCube } from "@web-cube/web-cube";
const $cube = document.querySelector("web-cube") as WebCube;const currentState = $cube.getState();console.log(currentState);rotateCube
Rotates the entire cube with an animation.
Parameters
options
The options for the rotation.
- Type:
CubeRotationOptions - Required: Yes
Returns
- Type:
Promise<void>
Validation rules
axismust be"x","y", or"z".anglemust be90,180,270, or360.speedmust be greater than0.
Example
import { WebCube } from "@web-cube/web-cube";
const $cube = document.querySelector("web-cube") as WebCube;
$cube.rotateCube({ axis: "x", angle: 90, backwards: false, speed: 500 });This method emits web-cube:before-rotate, web-cube:before-cube-rotate, web-cube:after-rotate, and web-cube:after-cube-rotate.
rotateLayer
Rotates a layer of the cube.
Parameters
options
The options for the rotation.
- Type:
LayerRotationOptions - Required: Yes
Returns
- Type:
Promise<void>
Validation rules
axismust be"x","y", or"z".layermust be in range0tosize - 1.anglemust be90,180,270, or360.speedmust be greater than0.
Example
import { WebCube } from "@web-cube/web-cube";
const $cube = document.querySelector("web-cube") as WebCube;
$cube.rotateLayer({ axis: "x", layer: 0, angle: 90, backwards: false, speed: 500,});This method emits web-cube:before-rotate, web-cube:before-layer-rotate, web-cube:after-rotate, and web-cube:after-layer-rotate.
Utility wrapper methods
The package also exports utility functions that wrap these methods, including rotateCube, rotateLayer, and rotate.
See Utilities for details.