Compare commits
2 Commits
main
...
49bddb6365
| Author | SHA1 | Date | |
|---|---|---|---|
| 49bddb6365 | |||
| b5d846b35d |
119
boot_js.ml
119
boot_js.ml
@ -27,23 +27,20 @@ let scale_canvas (canvas : Dom_html.canvasElement Js.t) =
|
|||||||
canvas##.style##.width := width;
|
canvas##.style##.width := width;
|
||||||
canvas##.style##.height := height
|
canvas##.style##.height := height
|
||||||
|
|
||||||
let _ =
|
let webgl_initialize canvas =
|
||||||
let canvas =
|
|
||||||
Js.Unsafe.coerce (Dom_html.getElementById_exn "canvas")
|
|
||||||
in
|
|
||||||
scale_canvas canvas;
|
scale_canvas canvas;
|
||||||
let webgl_ctx =
|
(* Graphv requires a stencil buffer to work properly *)
|
||||||
(* Graphv requires a stencil buffer to work properly *)
|
let attrs = WebGL.defaultContextAttributes in
|
||||||
let attrs = WebGL.defaultContextAttributes in
|
attrs##.stencil := Js._true;
|
||||||
attrs##.stencil := Js._true;
|
match
|
||||||
match
|
WebGL.getContextWithAttributes canvas attrs |> Js.Opt.to_option
|
||||||
WebGL.getContextWithAttributes canvas attrs |> Js.Opt.to_option
|
with
|
||||||
with
|
| None ->
|
||||||
| None ->
|
print_endline "Sorry your browser does not support WebGL";
|
||||||
print_endline "Sorry your browser does not support WebGL";
|
raise Exit
|
||||||
raise Exit
|
| Some ctx -> ctx
|
||||||
| Some ctx -> ctx
|
|
||||||
in
|
let graphv_initialize webgl_ctx =
|
||||||
let open NVG in
|
let open NVG in
|
||||||
let vg =
|
let vg =
|
||||||
create
|
create
|
||||||
@ -53,55 +50,55 @@ let _ =
|
|||||||
(* File in this case is actually the CSS font name *)
|
(* File in this case is actually the CSS font name *)
|
||||||
Text.create vg ~name:"sans" ~file:"sans" |> ignore;
|
Text.create vg ~name:"sans" ~file:"sans" |> ignore;
|
||||||
webgl_ctx##clearColor 0.3 0.3 0.32 1.;
|
webgl_ctx##clearColor 0.3 0.3 0.32 1.;
|
||||||
|
vg
|
||||||
|
|
||||||
(*
|
let request_animation_frame () =
|
||||||
let render ev =
|
let t, s = Lwt.wait () in
|
||||||
webgl_ctx##clear
|
let (_ : Dom_html.animation_frame_request_id) =
|
||||||
(webgl_ctx##._COLOR_BUFFER_BIT_
|
Dom_html.window##requestAnimationFrame
|
||||||
lor webgl_ctx##._DEPTH_BUFFER_BIT_
|
(Js.wrap_callback (fun (time : float) -> Lwt.wakeup s time))
|
||||||
lor webgl_ctx##._STENCIL_BUFFER_BIT_);
|
|
||||||
let device_ratio = Dom_html.window##.devicePixelRatio in
|
|
||||||
begin_frame vg ~width:canvas##.width ~height:canvas##.height
|
|
||||||
~device_ratio;
|
|
||||||
Transform.scale vg ~x:device_ratio ~y:device_ratio;
|
|
||||||
ignore Human.Panel.Ui.(panel vg Gg.P2.o test ev);
|
|
||||||
(*
|
|
||||||
Path.begin_ vg ;
|
|
||||||
Path.rect vg ~x:40. ~y:40. ~w:320. ~h:320. ;
|
|
||||||
set_fill_color vg ~color:Color.(rgba ~r:154 ~g:203 ~b:255 ~a:200) ;
|
|
||||||
fill vg ;
|
|
||||||
Transform.translate vg ~x:200. ~y:200. ;
|
|
||||||
Transform.rotate vg ~angle:(time *. 0.0005) ;
|
|
||||||
Text.set_font_face vg ~name:"sans" ;
|
|
||||||
Text.set_size vg ~size:48. ;
|
|
||||||
Text.set_align vg ~align:Align.(center lor middle) ;
|
|
||||||
set_fill_color vg ~color:Color.white ;
|
|
||||||
Text.text vg ~x:0. ~y:0. "Hello World!" ; *)
|
|
||||||
NVG.end_frame vg
|
|
||||||
in
|
in
|
||||||
Dom_html.window##requestAnimationFrame
|
t
|
||||||
(Js.wrap_callback (fun _ -> render Human.Event.empty))
|
|
||||||
|> ignore;*)
|
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 open Js_of_ocaml_lwt.Lwt_js_events in
|
||||||
|
let page_var = Lwd.var Human.Panel.Ui.empty in
|
||||||
|
|
||||||
async (fun () ->
|
async (fun () ->
|
||||||
buffered_loop (make_event Dom_html.Event.keydown)
|
Human.Panel.Ui.boot_page >>= fun page ->
|
||||||
Dom_html.document (fun ev _ ->
|
Lwd.set page_var page;
|
||||||
webgl_ctx##clear
|
let render = Human.Panel.Ui.renderer page_var in
|
||||||
(webgl_ctx##._COLOR_BUFFER_BIT_
|
request_render canvas webgl_ctx vg render >>= fun () ->
|
||||||
lor webgl_ctx##._DEPTH_BUFFER_BIT_
|
buffered_loop
|
||||||
lor webgl_ctx##._STENCIL_BUFFER_BIT_);
|
(make_event Dom_html.Event.keydown)
|
||||||
let device_ratio = Dom_html.window##.devicePixelRatio in
|
Dom_html.document
|
||||||
begin_frame vg ~width:canvas##.width ~height:canvas##.height
|
Human.(
|
||||||
~device_ratio;
|
fun ev _ ->
|
||||||
Transform.scale vg ~x:device_ratio ~y:device_ratio;
|
Lwd.set page_var
|
||||||
Human.Panel.Ui.(
|
(Panel.Ui.handle_event (Lwd.peek page_var)
|
||||||
render_lwt vg Gg.P2.o
|
(Event_js.evt_of_jskey `Press ev));
|
||||||
(Human.Event_js.evt_of_jskey `Press ev))
|
request_render canvas webgl_ctx vg render))
|
||||||
>>= fun p ->
|
|
||||||
Logs.debug (fun m ->
|
|
||||||
m "Drawing finished at point: %a" Gg.V2.pp p);
|
|
||||||
NVG.end_frame vg;
|
|
||||||
Lwt.return_unit))
|
|
||||||
|
|
||||||
(* Dom_html.document##.onkeydown
|
(* Dom_html.document##.onkeydown
|
||||||
:= Dom.handler (fun (evt : Dom_html.keyboardEvent Js.t) ->
|
:= Dom.handler (fun (evt : Dom_html.keyboardEvent Js.t) ->
|
||||||
|
|||||||
Reference in New Issue
Block a user