-
Notifications
You must be signed in to change notification settings - Fork 314
Expand file tree
/
Copy pathview-step-manual.R
More file actions
78 lines (76 loc) · 2.69 KB
/
Copy pathview-step-manual.R
File metadata and controls
78 lines (76 loc) · 2.69 KB
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
68
69
70
71
72
73
74
75
76
77
78
#' @include view-step.R
NULL
#' @rdname view_step
#'
#' @param xmin,xmax,ymin,ymax Vectors of even length defining the boundaries of
#' the different views to go through
#'
#' @export
#' @importFrom ggplot2 ggproto
view_step_manual <- function(pause_length = 1, step_length = 1, xmin, xmax, ymin, ymax,
delay = 0, ease = 'cubic-in-out', wrap = TRUE,
pause_first = FALSE, fixed_x = FALSE, fixed_y = FALSE,
exclude_layer = NULL, aspect_ratio = 1) {
ggproto(NULL, ViewStepManual,
fixed_lim = list(x = fixed_x, y = fixed_y),
exclude_layer = exclude_layer,
aspect_ratio = aspect_ratio,
params = list(
pause_length = pause_length,
step_length = step_length,
windows = data_frame0(
xmin = xmin,
xmax = xmax,
ymin = ymin,
ymax = ymax
),
delay = delay,
ease = ease,
wrap = wrap,
pause_first = pause_first
)
)
}
#' @rdname gganimate-ggproto
#' @format NULL
#' @usage NULL
#' @export
#' @importFrom ggplot2 ggproto
#' @importFrom tweenr keep_state
ViewStepManual <- ggproto('ViewStepManual', ViewStep,
setup_params = function(self, data, params) {
nsteps <- nrow(params$windows)
step_length <- rep(params$step_length, length.out = nsteps)
pause_length <- rep(params$pause_length, length.out = nsteps)
if (!params$pause_first) {
pause_length <- c(0, head(pause_length, -1))
}
if (!params$wrap) {
step_length[length(step_length)] <- 0
}
params$step_length <- step_length
params$pause_length <- pause_length
params
},
train = function(self, data, params) {
frames <- distribute_frames(params$pause_length, params$step_length, params$nframes)
params$windows <- vec_rbind0(
params$windows,
params$windows[rep(1, length(frames$static_length) - nrow(params$windows) + 1), , drop = FALSE]
)
frame_ranges <- params$windows[1, , drop = FALSE]
for (i in seq_len(nrow(params$windows) - 1)) {
if (frames$static_length[i] != 0) {
frame_ranges <- keep_state(frame_ranges, frames$static_length[i])
}
if (frames$transition_length[i] != 0) {
frame_ranges <- self$window_transition(frame_ranges, params$windows[i + 1, , drop = FALSE], frames$transition_length[i], params)
}
}
frame_ranges <- frame_ranges[seq_len(params$nframes), , drop = FALSE]
frame_ranges$.frame <- (seq_len(nrow(frame_ranges)) + round(params$delay * frames$mod)) %% params$nframes
frame_ranges <- frame_ranges[order(frame_ranges$.frame), ]
params$frame_ranges <- frame_ranges
params
}
)