Skip to main content

animato_js/
lib.rs

1//! JavaScript and TypeScript bindings for Animato.
2//!
3//! This crate is compiled with `wasm-pack` and published as `@animato/core`.
4//! It exposes the renderer-agnostic Animato engines to JavaScript while keeping
5//! framework adapters in examples and application code.
6
7#![deny(missing_docs)]
8#![deny(missing_debug_implementations)]
9
10mod batch;
11mod color;
12mod driver;
13mod easing;
14mod error;
15mod keyframe;
16mod path;
17mod physics;
18mod spring;
19mod timeline;
20mod tween;
21mod types;
22mod wasm_dom;
23
24pub use batch::TweenBatch;
25pub use color::{ColorTween, interpolate_color};
26pub use driver::{RafDriver, ScrollDriver};
27pub use easing::{available_easings, ease, parse_easing_for_js};
28pub use keyframe::{KeyframeTrack, KeyframeTrack2D, KeyframeTrack3D, KeyframeTrack4D};
29pub use path::{MorphPath, MotionPath};
30pub use physics::{DragState, GestureRecognizer, Inertia, Inertia2D};
31pub use spring::{Spring, Spring2D, Spring3D, Spring4D};
32pub use tween::{Tween, Tween2D, Tween3D, Tween4D};
33pub use wasm_dom::{Draggable, FlipAnimation, LayoutAnimator, Observer, ScrollSmoother, SplitText};
34
35use wasm_bindgen::prelude::*;
36
37/// Initialize Animato's optional browser runtime hooks.
38///
39/// This is safe to call more than once. The current implementation installs a
40/// panic hook when the crate is built with the `console_error_panic_hook`
41/// dependency enabled by downstream builds.
42#[wasm_bindgen(js_name = initAnimato)]
43pub fn init_animato() {
44    #[cfg(all(target_arch = "wasm32", feature = "panic-hook"))]
45    console_error_panic_hook::set_once();
46}
47
48/// Return the Animato package version.
49#[wasm_bindgen]
50pub fn version() -> String {
51    env!("CARGO_PKG_VERSION").to_owned()
52}
53
54/// Snap a value to a grid size.
55#[wasm_bindgen(js_name = snapTo)]
56pub fn snap_to(value: f32, grid: f32) -> f32 {
57    animato_tween::snap_to(value, grid)
58}
59
60/// Round a value to a fixed number of decimal places.
61#[wasm_bindgen(js_name = roundTo)]
62pub fn round_to(value: f32, decimals: u32) -> f32 {
63    animato_tween::round_to(value, decimals)
64}