blob: 1fcd31720a091f34e7a5ad8a9839776727c1c0ee (
plain)
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
|
open Js_of_ocaml
let (pixelRatio : float) =
2 * Js.Unsafe.get Dom_html.window (Js.string "devicePixelRatio")
|> float_of_int
class type xmlSerializer = object
method serializeToString : Dom.element Js.t -> Js.js_string Js.t Js.meth
end
let (xmlSerializer : xmlSerializer Js.t Js.constr) =
Js.Unsafe.global##._XMLSerializer
(* Extract an image from a svg element *)
let generate_png (svg_image : Dom_svg.svgElement Js.t) callback =
let image = Dom_html.createImg Dom_html.document in
image##.onload :=
Dom_html.handler (fun _ev ->
let canvas = Dom_html.createCanvas Dom_html.document in
let context = canvas##getContext Dom_html._2d_ in
let width = svg_image##.width##.baseVal##.value |> Js.to_float
and height = svg_image##.height##.baseVal##.value |> Js.to_float in
image##.width := Float.to_int @@ (pixelRatio *. width);
image##.height := Float.to_int @@ (pixelRatio *. height);
canvas##.width := Float.to_int @@ (pixelRatio *. width);
canvas##.height := Float.to_int @@ (pixelRatio *. height);
context##drawImage_withSize
image (Js.number_of_float 0.0) (Js.number_of_float 0.0)
(Js.number_of_float (pixelRatio *. width))
(Js.number_of_float (pixelRatio *. height));
callback @@ canvas##toDataURL_type (Js.string "image/png");
Js._false);
let xml = new%js xmlSerializer in
let svg_xml = xml##serializeToString (svg_image :> Dom.element Js.t) in
let svg_src =
(Js.string "data:image/svg+xml;base64,")##concat
(Dom_html.window##btoa (Js.unescape (Js.encodeURIComponent svg_xml)))
in
image##.src := svg_src
|