Skip to content

Commit 059950e

Browse files
authored
Ot_page_transition improvements (#155)
2 parents adff88b + 981b943 commit 059950e

File tree

2 files changed

+34
-30
lines changed

2 files changed

+34
-30
lines changed

css/ot_page_transition.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,16 @@
99
background-color: white;
1010
}
1111

12+
.ot-page-transition-will-change {
13+
will-change: transform;
14+
}
15+
1216
.ot-page-transition-container{
1317
width: 100%;
1418
height: 100%;
1519
background-size: contain;
1620
background-repeat: no-repeat;
21+
will-change: transform;
1722
}
1823

1924
.ot-page-transition-transform-1{

src/widgets/ot_page_transition.eliom

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ let set_transition_duration elt t =
2626
Js.Unsafe.coerce ((To_dom.of_element elt)##.style) in
2727
container_style##.transitionDuration := s
2828

29+
let cl_will_change = "ot-page-transition-will-change"
30+
2931
module Make (Conf:PAGE_TRANSITION_CONF) = struct
3032
type screenshot = Conf.screenshot
3133
let screenshot_list = Hashtbl.create 10
@@ -47,29 +49,18 @@ module Make (Conf:PAGE_TRANSITION_CONF) = struct
4749
then Hashtbl.replace screenshot_list id (screenshot,w,h)
4850
else Hashtbl.add screenshot_list id (screenshot,w,h)
4951

50-
let wait_for_screenshot max_wait_time id =
51-
let rec aux i =
52-
if (not (mem_screenshot id)) && i < max_wait_time then
53-
let%lwt () = Lwt_js.sleep 0.01 in
54-
aux (i+1)
55-
else Lwt.return_unit
56-
in
57-
aux 0
58-
5952
let wrap_screenshot screenshot transition_duration =
6053
let container = Conf.screenshot_container screenshot in
6154
let wrapper = div ~a:[a_class ["ot-page-transition-wrapper"]] [container] in
6255
set_transition_duration container transition_duration;
6356
wrapper,container
6457

6558
let forward_animation_ transition_duration id =
66-
Eliom_client.onload ( fun () ->
67-
Lwt.async (fun () ->
68-
let h =
69-
Js.Optdef.get
70-
Dom_html.window##.innerHeight
71-
(fun () -> assert false) in
59+
Eliom_client.onload @@ fun () ->
60+
Lwt.async @@ fun () ->
61+
let h = React.S.value Ot_size.height in
7262
let new_body = Of_dom.of_body Dom_html.document##.body in
63+
Manip.Class.add new_body cl_will_change;
7364
let style = Js.Unsafe.coerce Dom_html.document##.body##.style in
7465
let initial_height = style##.height in
7566
let initial_transition_duration = style##.transitionDuration in
@@ -89,33 +80,43 @@ module Make (Conf:PAGE_TRANSITION_CONF) = struct
8980
let%lwt () = Lwt_js.sleep transition_duration in
9081
Manip.removeSelf screenshot_wrapper;
9182
Manip.SetCss.height new_body (Js.to_string initial_height);
83+
Manip.Class.remove new_body cl_will_change;
9284
style##.transitionDuration := initial_transition_duration;
9385
Lwt.return_unit
94-
))
86+
87+
let wait_for ~sleep ~cycles cond =
88+
let rec loop i =
89+
if not (cond ()) && i < cycles then
90+
let%lwt () = Lwt_js.sleep sleep in
91+
loop (i+1)
92+
else Lwt.return_unit
93+
in
94+
loop 0
9595

9696
let forward_animation ?(transition_duration=0.5) take_screenshot id =
9797
try
98-
take_screenshot (push_screenshot id);
98+
let screenshot = ref None in
99+
take_screenshot (fun ss -> screenshot := Some ss);
99100
forward_animation_ transition_duration id;
100-
wait_for_screenshot 100 id
101+
wait_for ~sleep:0.01 ~cycles:100
102+
(fun () -> match !screenshot with
103+
| None -> false
104+
| Some ss -> push_screenshot id ss; true)
101105
with _ -> Lwt.return_unit
102106

103107
let backward_animation_
104108
transition_duration history_screenshot current_screenshot =
105109
let current_screenshot_container =
106110
Conf.screenshot_container current_screenshot in
107-
let h =
108-
Js.Optdef.get
109-
Dom_html.window##.innerHeight
110-
(fun () -> assert false) in
111+
let h = React.S.value Ot_size.height in
111112
Manip.SetCss.heightPx current_screenshot_container h;
112113
let history_screenshot_wrapper,history_screenshot_container =
113114
wrap_screenshot history_screenshot transition_duration in
114115
Manip.Class.add
115116
history_screenshot_container "ot-page-transition-transform-2";
116117
let temporary_body =
117-
body [current_screenshot_container;
118-
history_screenshot_wrapper] in
118+
D.body [current_screenshot_container; history_screenshot_wrapper] in
119+
Manip.Class.add temporary_body cl_will_change;
119120
Manip.replaceSelf
120121
(Of_dom.of_body Dom_html.document##.body) temporary_body;
121122
let%lwt () = Lwt_js_events.request_animation_frame () in
@@ -124,19 +125,17 @@ module Make (Conf:PAGE_TRANSITION_CONF) = struct
124125
Manip.Class.add temporary_body "ot-page-transition-transform-1";
125126
Manip.Class.remove
126127
history_screenshot_container "ot-page-transition-transform-2";
128+
Manip.Class.remove temporary_body cl_will_change;
127129
Lwt.return_unit
128130

129131
let backward_animation ?(transition_duration=0.5) take_screenshot id =
130132
try
131133
let scr_width,scr_height = Ot_size.get_screen_size () in
132-
let history_screenshot,w,h = pop_screenshot id in
134+
let hist_screenshot,w,h = pop_screenshot id in
133135
if w = scr_width && h = scr_height
134136
then begin
135-
let f current_screenshot =
136-
Lwt.async
137-
(fun () ->
138-
backward_animation_
139-
transition_duration history_screenshot current_screenshot)
137+
let f cur_screenshot = Lwt.async @@ fun () ->
138+
backward_animation_ transition_duration hist_screenshot cur_screenshot
140139
in
141140
take_screenshot f;
142141
Lwt_js.sleep transition_duration

0 commit comments

Comments
 (0)