open Js_of_ocaml open Lwt.Infix module NVG = Graphv_webgl let _ = Logs.set_reporter (Human.Logs_reporter.console_reporter ()); Logs.set_level (Some Debug); Logs.debug (fun m -> m "hello") module Log = (val Logs.src_log Logs.default : Logs.LOG) (* This scales the canvas to match the DPI of the window, it prevents blurriness when rendering to the canvas *) let scale_canvas (canvas : Dom_html.canvasElement Js.t) = let dpr = Dom_html.window##.devicePixelRatio in let rect = canvas##getBoundingClientRect in let width = rect##.right -. rect##.left in let height = rect##.bottom -. rect##.top in canvas##.width := width *. dpr |> int_of_float; canvas##.height := height *. dpr |> int_of_float; let width = Printf.sprintf "%dpx" (int_of_float width) |> Js.string in let height = Printf.sprintf "%dpx" (int_of_float height) |> Js.string in canvas##.style##.width := width; canvas##.style##.height := height let webgl_initialize canvas = scale_canvas canvas; (* Graphv requires a stencil buffer to work properly *) let attrs = WebGL.defaultContextAttributes in attrs##.stencil := Js._true; match WebGL.getContextWithAttributes canvas attrs |> Js.Opt.to_option with | None -> print_endline "Sorry your browser does not support WebGL"; raise Exit | Some ctx -> ctx let graphv_initialize webgl_ctx = let open NVG in let vg = create ~flags:CreateFlags.(antialias lor stencil_strokes) webgl_ctx in (* File in this case is actually the CSS font name *) Text.create vg ~name:"sans" ~file:"sans" |> ignore; webgl_ctx##clearColor 0.3 0.3 0.32 1.; vg let request_animation_frame () = let t, s = Lwt.wait () in let (_ : Dom_html.animation_frame_request_id) = Dom_html.window##requestAnimationFrame (Js.wrap_callback (fun (time : float) -> Lwt.wakeup s time)) in t let request_render canvas webgl_ctx vg (render : NVG.t -> ?time:float -> Gg.p2 -> Gg.p2 Lwt.t) = request_animation_frame () >>= fun time -> webgl_ctx##clear (webgl_ctx##._COLOR_BUFFER_BIT_ lor webgl_ctx##._DEPTH_BUFFER_BIT_ lor webgl_ctx##._STENCIL_BUFFER_BIT_); let device_ratio = Dom_html.window##.devicePixelRatio in NVG.begin_frame vg ~width:canvas##.width ~height:canvas##.height ~device_ratio; NVG.Transform.scale vg ~x:device_ratio ~y:device_ratio; render vg ~time Gg.P2.o >>= fun _p -> (* Logs.debug (fun m -> m "Drawing finished at point: %a" Gg.V2.pp p); *) NVG.end_frame vg; Lwt.return_unit let _ = let canvas = Js.Unsafe.coerce (Dom_html.getElementById_exn "canvas") in let webgl_ctx = webgl_initialize canvas in let vg = graphv_initialize webgl_ctx in let open Js_of_ocaml_lwt.Lwt_js_events in let page_var = Lwd.var Human.Panel.Ui.empty in async (fun () -> Human.Panel.Ui.boot_page >>= fun page -> Lwd.set page_var page; let render = Human.Panel.Ui.renderer page_var in request_render canvas webgl_ctx vg render >>= fun () -> buffered_loop (make_event Dom_html.Event.keydown) Dom_html.document Human.( fun ev _ -> Lwd.set page_var (Panel.Ui.handle_event (Lwd.peek page_var) (Event_js.evt_of_jskey `Press ev)); request_render canvas webgl_ctx vg render)) (* Dom_html.document##.onkeydown := Dom.handler (fun (evt : Dom_html.keyboardEvent Js.t) -> render (Human.Event_js.evt_of_jskey `Press evt) ; Js._false ) *)