forked from dbuenzli/brr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_c2d.ml
67 lines (59 loc) · 2.15 KB
/
test_c2d.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
(*---------------------------------------------------------------------------
Copyright (c) 2020 The brr programmers. All rights reserved.
SPDX-License-Identifier: ISC
---------------------------------------------------------------------------*)
open Brr
open Brr_canvas
open Fut.Syntax
let stripe_cnv_rect c ~x ~y ~w ~h =
let x = truncate x and y = truncate y in
let w = truncate w and h = truncate h in
let idata = C2d.get_image_data c ~x ~y ~w ~h in
let d = C2d.Image_data.data idata in
for y = 0 to h - 1 do
for x = 0 to w - 1 do
if x mod 4 <> 0 then () else
let off = 4 * (y * w + x) in
Tarray.set d (off ) 0xFF;
Tarray.set d (off + 1) 0x00;
Tarray.set d (off + 2) 0x00
done
done;
C2d.put_image_data c idata ~x ~y
let draw_brr c ~x ~y =
let size = truncate (96. *. Window.device_pixel_ratio G.window) in
C2d.set_font c Jstr.(v "bold " + of_int size + v "px SourceSansPro");
C2d.fill_text c (Jstr.v " Brr!") ~x ~y
let draw_rect c ~x ~y ~w ~h =
C2d.set_fill_style c (C2d.color (Jstr.v "#000"));
C2d.fill_rect c ~x ~y ~w ~h
let draw cnv =
let c = C2d.get_context cnv in
let w = float @@ Canvas.w cnv in
let h = float @@ Canvas.h cnv in
C2d.stroke_rect c ~x:0. ~y:0. ~w ~h;
let w = 0.5 *. w and h = 0.5 *. h in
let x = w and y = h in
draw_rect c ~x ~y ~w ~h;
stripe_cnv_rect c ~x ~y ~w ~h;
draw_brr c ~x:10. ~y:h
let set_size cnv =
let el = Canvas.to_el cnv in
let w = El.inner_w el in
let h = Jstr.(of_int (truncate ((w *. 3.) /. 4.)) + v "px") (* 4:3 *) in
El.set_inline_style El.Style.height h el;
Canvas.set_size_to_layout_size cnv
let main () =
let h1 = El.h1 [El.txt' "2D canvas"] in
let info =
let brr = El.strong [El.txt' "Brr!"] in
[ El.txt' "Draws "; brr; El.txt' " and a black and red striped corner. " ]
in
let cnv = Canvas.create [] in
let children = [h1; El.p info; Canvas.to_el cnv] in
El.set_children (Document.body G.document) children;
(* We need to wait for the stylesheet to access the font and setup the
layout *)
let* _ev = Ev.next Ev.load (Window.as_target G.window) in
set_size cnv; draw cnv; Fut.return ()
let () = ignore (main ())