package ease-caml

All checked builds pass
  1. Overview
  2. Docs
An easing/tweening library for OCaml

Install

dune-project
 Dependency

Authors

Maintainers

Sources

0.2.2.tar.gz
md5=85afaf2347ba62aaa42da99433fc473a
sha512=220f47a1bf5a0f08c4a057650a22cc98dfea53670abf7ab3c87dc7f8a346c7b1088901e43b28cf66d450151dad00f671e839c872105df34309ea1d653431f918

doc/README.html

ease-caml

ease-caml

A game framework agnostic library for easing and tweening. A tween takes a floating point value (or collection thereof) from one value continuously over time to another value via an easing function.

Example

https://github.com/user-attachments/assets/03dc5fc6-17c6-4ba7-acfe-e983be9997dc

Consider the following Raylib Ocaml example that draws a blank screen:

let setup () =
  Raylib.init_window 800 450 "raylib [core] example - basic window";
  Raylib.set_target_fps 60

let rec loop () =
  if Raylib.window_should_close () then Raylib.close_window ()
  else (
    let open Raylib in
    begin_drawing ();
    clear_background Color.raywhite;
    end_drawing ();
    loop ()
  )

let () = setup () |> loop

We will adapt this to have a ball bounce on the screen. First we create a ball just outside the screen (add this to the top of the example):

module FT = Tween.FloatTween (* For a single floating point value. *)

type circle =
{
  r: float;
  x: float;
  y: float ref;
}

let ball : circle = { r = 40.0; x = 400.0; y = ref ~-.40.0 }

This will create a ball just outside the viewport. Note that y is a float ref since we will be making the ball fall in the y direction (and thus y must also be mutable!).

Now we will create a tween to move the ball, right below let ball : circle = ... add

let ty = FT.make_tween ball.y 225.0 ~ef:Easers.bounce 1.0

~-.40.0 is the start value of y, 225.0 is the end value and it will transition from start value to end value using a pre-defined bounce function for 1.0 duration. ball.y argument indicates what value ty is targeting.

Also, we will need a tween_manager that updates all of our tweens for us. Right below add:

let tm = FT.new_manager ()

and in setup (anywhere really!) we need to add ty to the tween manager tm with:

FT.add ty tm

ty will be in charge changing the y value. To make ty do work right below let open Raylib in add

FT.update tm (get_frame_time ());

Then after the clear_background Color.raywhite; add

draw_circle (Int.of_float ball.x) (Int.of_float !(ball.y)) ball.r Color.maroon;

to actually draw the ball.

All in all you should now have:

module FT = Tween.FloatTween

type circle =
{
  r: float;
  x: float;
  y: float ref;
}

let ball : circle = { r = 40.0; x = 400.0; y = ref ~-.40.0 }
let ty = FT.make_tween ball.y 225.0 ~ef:Easers.bounce 1.0
let tm = FT.new_manager ()

let setup () =
  Raylib.init_window 800 450 "simple_tween";
  Raylib.set_target_fps 60;
  FT.add ty tm

let rec loop () =
  if Raylib.window_should_close () then Raylib.close_window ()
  else (
    let open Raylib in
    FT.update tm (get_frame_time ());
    begin_drawing ();
    clear_background Color.raywhite;
    draw_circle (Int.of_float ball.x) (Int.of_float !(ball.y)) ball.r Color.maroon;
    end_drawing ();
    loop ()
  )

let () = setup () |> loop

This will make a red ball bounce on the screen. This is examples/simple_tween.ml. It can be ran by going to the base directory and running:

dune build
dune exec -- ./examples/simple_tween.exe

Documentation

Further documentation can be found here.