animation

This is a simple example of animation of jumping ball. The animation can be stopped by "restart kernel" button on the above menu.


In [1]:
#require "core" ;;
#require "async" ;;
#require "cairo2" ;;
#require "iocaml-kernel" ;;


/home/opam/.opam/4.04.1/lib/base/caml: added to search path
/home/opam/.opam/4.04.1/lib/base/caml/caml.cma: loaded
/home/opam/.opam/4.04.1/lib/base/shadow_stdlib: added to search path
/home/opam/.opam/4.04.1/lib/base/shadow_stdlib/shadow_stdlib.cma: loaded
/home/opam/.opam/4.04.1/lib/sexplib/0: added to search path
/home/opam/.opam/4.04.1/lib/sexplib/0/sexplib0.cma: loaded
/home/opam/.opam/4.04.1/lib/base: added to search path
/home/opam/.opam/4.04.1/lib/base/base.cma: loaded
/home/opam/.opam/4.04.1/lib/ocaml/unix.cma: loaded
/home/opam/.opam/4.04.1/lib/ocaml/bigarray.cma: loaded
/home/opam/.opam/4.04.1/lib/fieldslib: added to search path
/home/opam/.opam/4.04.1/lib/fieldslib/fieldslib.cma: loaded
/home/opam/.opam/4.04.1/lib/ppx_compare/runtime-lib: added to search path
/home/opam/.opam/4.04.1/lib/ppx_compare/runtime-lib/ppx_compare_lib.cma: loaded
/home/opam/.opam/4.04.1/lib/sexplib: added to search path
/home/opam/.opam/4.04.1/lib/sexplib/sexplib.cma: loaded
/home/opam/.opam/4.04.1/lib/variantslib: added to search path
/home/opam/.opam/4.04.1/lib/variantslib/variantslib.cma: loaded
/home/opam/.opam/4.04.1/lib/bin_prot/shape: added to search path
/home/opam/.opam/4.04.1/lib/bin_prot/shape/bin_shape_lib.cma: loaded
/home/opam/.opam/4.04.1/lib/bin_prot: added to search path
/home/opam/.opam/4.04.1/lib/bin_prot/bin_prot.cma: loaded
/home/opam/.opam/4.04.1/lib/ppx_hash/runtime-lib: added to search path
/home/opam/.opam/4.04.1/lib/ppx_hash/runtime-lib/ppx_hash_lib.cma: loaded
/home/opam/.opam/4.04.1/lib/ppx_inline_test/config: added to search path
/home/opam/.opam/4.04.1/lib/ppx_inline_test/config/inline_test_config.cma: loaded
/home/opam/.opam/4.04.1/lib/ppx_inline_test/runtime-lib: added to search path
/home/opam/.opam/4.04.1/lib/ppx_inline_test/runtime-lib/ppx_inline_test_lib.cma: loaded
/home/opam/.opam/4.04.1/lib/core_kernel/base_for_tests: added to search path
/home/opam/.opam/4.04.1/lib/core_kernel/base_for_tests/base_for_tests.cma: loaded
/home/opam/.opam/4.04.1/lib/jane-street-headers: added to search path
/home/opam/.opam/4.04.1/lib/jane-street-headers/jane_street_headers.cma: loaded
/home/opam/.opam/4.04.1/lib/ocaml/nums.cma: loaded
/home/opam/.opam/4.04.1/lib/num-top: added to search path
/home/opam/.opam/4.04.1/lib/num-top/num_top.cma: loaded
/home/opam/.opam/4.04.1/lib/num: added to search path
/home/opam/.opam/4.04.1/lib/ppx_assert/runtime-lib: added to search path
/home/opam/.opam/4.04.1/lib/ppx_assert/runtime-lib/ppx_assert_lib.cma: loaded
/home/opam/.opam/4.04.1/lib/ppx_bench/runtime-lib: added to search path
/home/opam/.opam/4.04.1/lib/ppx_bench/runtime-lib/ppx_bench_lib.cma: loaded
/home/opam/.opam/4.04.1/lib/ppx_expect/common: added to search path
/home/opam/.opam/4.04.1/lib/ppx_expect/common/expect_test_common.cma: loaded
/home/opam/.opam/4.04.1/lib/ppx_expect/config: added to search path
/home/opam/.opam/4.04.1/lib/ppx_expect/config/expect_test_config.cma: loaded
/home/opam/.opam/4.04.1/lib/ppx_expect/collector: added to search path
/home/opam/.opam/4.04.1/lib/ppx_expect/collector/expect_test_collector.cma: loaded
/home/opam/.opam/4.04.1/lib/stdio: added to search path
/home/opam/.opam/4.04.1/lib/stdio/stdio.cma: loaded
/home/opam/.opam/4.04.1/lib/typerep: added to search path
/home/opam/.opam/4.04.1/lib/typerep/typerep_lib.cma: loaded
/home/opam/.opam/4.04.1/lib/core_kernel: added to search path
/home/opam/.opam/4.04.1/lib/core_kernel/core_kernel.cma: loaded
/home/opam/.opam/4.04.1/lib/sexplib/unix: added to search path
/home/opam/.opam/4.04.1/lib/sexplib/unix/sexplib_unix.cma: loaded
/home/opam/.opam/4.04.1/lib/spawn: added to search path
/home/opam/.opam/4.04.1/lib/spawn/spawn.cma: loaded
/home/opam/.opam/4.04.1/lib/ocaml/threads: added to search path
/home/opam/.opam/4.04.1/lib/core: added to search path
/home/opam/.opam/4.04.1/lib/core/core.cma: loaded
/home/opam/.opam/4.04.1/lib/async_kernel: added to search path
/home/opam/.opam/4.04.1/lib/async_kernel/async_kernel.cma: loaded
/home/opam/.opam/4.04.1/lib/async_rpc_kernel: added to search path
/home/opam/.opam/4.04.1/lib/async_rpc_kernel/async_rpc_kernel.cma: loaded
/home/opam/.opam/4.04.1/lib/async_unix: added to search path
/home/opam/.opam/4.04.1/lib/async_unix/async_unix.cma: loaded
/home/opam/.opam/4.04.1/lib/async_extra: added to search path
/home/opam/.opam/4.04.1/lib/async_extra/async_extra.cma: loaded
/home/opam/.opam/4.04.1/lib/async: added to search path
/home/opam/.opam/4.04.1/lib/async/async.cma: loaded
/home/opam/.opam/4.04.1/lib/cairo2: added to search path
/home/opam/.opam/4.04.1/lib/cairo2/cairo2.cma: loaded
/home/opam/.opam/4.04.1/lib/easy-format: added to search path
/home/opam/.opam/4.04.1/lib/easy-format/easy_format.cmo: loaded
/home/opam/.opam/4.04.1/lib/biniou: added to search path
/home/opam/.opam/4.04.1/lib/biniou/biniou.cma: loaded
/home/opam/.opam/4.04.1/lib/yojson: added to search path
/home/opam/.opam/4.04.1/lib/yojson/yojson.cmo: loaded
/home/opam/.opam/4.04.1/lib/ocaml/str.cma: loaded
/home/opam/.opam/4.04.1/lib/atd: added to search path
/home/opam/.opam/4.04.1/lib/atd/atd.cma: loaded
/home/opam/.opam/4.04.1/lib/atdgen: added to search path
/home/opam/.opam/4.04.1/lib/atdgen/atdgen.cma: loaded
/home/opam/.opam/4.04.1/lib/bytes: added to search path
/home/opam/.opam/4.04.1/lib/result: added to search path
/home/opam/.opam/4.04.1/lib/result/result.cma: loaded
/home/opam/.opam/4.04.1/lib/lwt: added to search path
/home/opam/.opam/4.04.1/lib/lwt/lwt.cma: loaded
/home/opam/.opam/4.04.1/lib/lwt/lwt-log.cma: loaded
/home/opam/.opam/4.04.1/lib/lwt/lwt-unix.cma: loaded
/home/opam/.opam/4.04.1/lib/integers: added to search path
/home/opam/.opam/4.04.1/lib/integers/integers.cma: loaded
/home/opam/.opam/4.04.1/lib/integers/integer_printers.cma: loaded
/home/opam/.opam/4.04.1/lib/ctypes: added to search path
/home/opam/.opam/4.04.1/lib/ctypes/ctypes.cma: loaded
/home/opam/.opam/4.04.1/lib/ctypes/ctypes-top.cma: loaded
/home/opam/.opam/4.04.1/lib/ctypes/ctypes-foreign-base.cma: loaded
/home/opam/.opam/4.04.1/lib/ctypes/ctypes-foreign-unthreaded.cma: loaded
/home/opam/.opam/4.04.1/lib/iocaml-kernel: added to search path
/home/opam/.opam/4.04.1/lib/iocaml-kernel/iocaml_lib.cma: loaded

In [2]:
open Core ;;
open Async ;;

In [3]:
Thread.create (fun () -> never_returns (Scheduler.go ())) () ;; (* Start the Async scheduler. *)


Out[3]:
- : Core.Thread.t = <abstr>

In [4]:
let pi2 = 2.0 *. 3.14159265358979 ;;
let w = 800. ;;
let h = 400. ;;
let r = 20.0 ;;
let e = -0.9 ;;
let g = 1.0 ;; (* gravity *)
let m = 1.0 ;; (* mass *)

let plot_ball ~context x y =
  let surface = Cairo.Image.create Cairo.Image.ARGB32 ~width:800 ~height:400 in
  let ctx = Cairo.create surface in
  Cairo.arc ctx ~x ~y ~r ~a1:0.0 ~a2:pi2 ;
  Cairo.fill ctx ;
  Cairo.PNG.write_to_stream ~output:(Caml.output_string Iocaml.mime) surface  ;
  Iocaml.send_clear ~context () ;
  Iocaml.send_mime ~context ~base64:true "image/png"
;;

let context = Iocaml.cell_context() ;;
let _ =
  let rec aux x y vx vy () =
    let x, vx = if x < 0.0 then (0.0, e *. vx) else if x > w then (w, e *. vx) else (x +. vx, vx) in
    let y, vy = if y < 0.0 then (0.0, e *. vy) else if y > h then (h, e *. vy) else (y +. vy, vy +. m *. g) in
    plot_ball ~context x y ;
    Async.after (Time.Span.of_ms 10.0) >>= aux x y vx vy
  in
  aux 0.0 0.0 10.0 0.0 ()



In [ ]: