Skip to content

Commit

Permalink
added 'ellipse' method to canvasRenderingContext2D (#1555)
Browse files Browse the repository at this point in the history
* added 'ellipse' method to canvasRenderingContext2D
---------

Co-authored-by: Hugo Heuzard <hugo.heuzard@gmail.com>
  • Loading branch information
FayCarsons and hhugo committed Jan 18, 2024
1 parent a22a811 commit 679bb51
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 36 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Features/Changes
* Mics: fix support for OCaml 5.2
* Compiler: no longer rely on IIFE for scoping variable inside loops
* Lib: add ellipse to canvasRenderingContext2D (@FayCarsons, #1555)

# 5.6.0 (2024-01-02) - Lille

Expand Down
46 changes: 10 additions & 36 deletions examples/hyperbolic/hypertree.ml
Original file line number Diff line number Diff line change
Expand Up @@ -580,47 +580,21 @@ let from_screen canvas x y =

let pi = 4. *. atan 1.

let ellipse_arc c cx cy rx ry start fin clock_wise =
c##save;
c##translate (Js.float cx) (Js.float cy);
c##scale (Js.float rx) (Js.float ry);
c##arc
(Js.float 0.)
(Js.float 0.)
(Js.float 1.)
(Js.float start)
(Js.float fin)
clock_wise;
c##restore

let arc c (rx, ry, dx, dy) z0 z1 z2 =
let rd = norm (sub z1 z0) in
let start = atan2 (z1.y -. z0.y) (z1.x -. z0.x) in
let fin = atan2 (z2.y -. z0.y) (z2.x -. z0.x) in
c##beginPath;
let alpha = mod_float (fin -. start +. (2. *. pi)) (2. *. pi) in
(*
Firebug.console##log_4(start, fin, alpha, (alpha > pi));
*)
if rx = ry
then
c##arc
(Js.float ((z0.x *. rx) +. dx))
(Js.float ((z0.y *. rx) +. dy))
(Js.float (rd *. rx))
(Js.float start)
(Js.float fin)
(Js.bool (alpha > pi))
else
ellipse_arc
c
((z0.x *. rx) +. dx)
((z0.y *. ry) +. dy)
(rd *. rx)
(rd *. ry)
start
fin
(Js.bool (alpha > pi));
c##ellipse
((z0.x *. rx) +. dx)
((z0.y *. ry) +. dy)
(rd *. rx)
(rd *. ry)
0.
start
fin
(Js.bool (alpha > pi));
c##stroke

let line c (rx, ry, dx, dy) z1 z2 =
Expand Down Expand Up @@ -668,7 +642,7 @@ let draw canvas vertices edges nodes boxes =
(Js.float (float canvas##.height));
let padding = opt_style style##.padding 0. in
c##beginPath;
ellipse_arc c dx dy (rx +. padding) (ry +. padding) 0. 7. Js._false;
c##ellipse dx dy (rx +. padding) (ry +. padding) 0. 0. 7. Js._false;
Js.Optdef.iter style##.backgroundColor (fun color ->
c##.fillStyle := color;
c##fill);
Expand Down
11 changes: 11 additions & 0 deletions lib/js_of_ocaml/dom_html.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1845,6 +1845,17 @@ and canvasRenderingContext2D = object
method arc :
number_t -> number_t -> number_t -> number_t -> number_t -> bool t -> unit meth

method ellipse :
number_t
-> number_t
-> number_t
-> number_t
-> number_t
-> number_t
-> number_t
-> bool t
-> unit meth

method fill : unit meth

method stroke : unit meth
Expand Down
11 changes: 11 additions & 0 deletions lib/js_of_ocaml/dom_html.mli
Original file line number Diff line number Diff line change
Expand Up @@ -1671,6 +1671,17 @@ and canvasRenderingContext2D = object
method arc :
number_t -> number_t -> number_t -> number_t -> number_t -> bool t -> unit meth

method ellipse :
number_t
-> number_t
-> number_t
-> number_t
-> number_t
-> number_t
-> number_t
-> bool t
-> unit meth

method fill : unit meth

method stroke : unit meth
Expand Down

0 comments on commit 679bb51

Please sign in to comment.