From b0cd9d6ebcb86b8fa53889558070d5234f43e149 Mon Sep 17 00:00:00 2001 From: Juan Pablo Date: Thu, 5 Dec 2024 22:19:35 -0500 Subject: [PATCH] added seq_smooth and helper functions --- .Rhistory | 512 +++++++++++++++++++++++++++++++++++++++++++++++++++++ R/seq_nl.R | 208 ++++++++++++++++++++++ R/utils.R | 27 +++ 3 files changed, 747 insertions(+) create mode 100644 R/seq_nl.R create mode 100644 R/utils.R diff --git a/.Rhistory b/.Rhistory index e69de29..a2c90e9 100644 --- a/.Rhistory +++ b/.Rhistory @@ -0,0 +1,512 @@ +quad_in_out = ifelse(t < 0.5, +2*t^2, +1 - 0.5*(-2*t+2)^2), +#---- --- ---- --- ---- --- ---- --- ----- --- ----# +cubic_in = t^3, +cubic_out = 1 - (1-t)^3, +cubic_in_out = ifelse(t < 0.5, +4*t^3, +1- 0.5*(-2*t+2)^3), +#---- --- ---- --- ---- --- ---- --- ----- --- ----# +quart_in = t^4, +quart_out = 1 - (1-t)^4, +quart_in_out = ifelse(t < 0.5, +8*t^4, +1 - 0.5*(-2*t+2)^4), +#---- --- ---- --- ---- --- ---- --- ----- --- ----# +quint_in = t^5, +quint_out = 1 - (1-t)^5, +quint_in_out = ifelse(t < 0.5, +16*t^5, +1 - 0.5*(-2*t+2)^5), +#---- --- ---- --- ---- --- ---- --- ----- --- ----# +exp_in = 2^(10*t - 10), +exp_out = 1 - 2^(-10*t), +exp_in_out = ifelse(t == 0,0, +ifelse(t == 1,1, +ifelse(t < 0.5, +2^(20*t-10)/2, +(2 - 2^(-20*t+10))/2 +))), +#---- --- ---- --- ---- --- ---- --- ----- --- ----# +circle_in = 1 - sqrt(1-t)^2, +circle_out = sqrt(1 - (t - 1)^2), +circle_in_out = ifelse(t < 0.5, +(1 - sqrt(1 - (2 * t)^2)) / 2, +0.5 * (sqrt(1 - (-2 * t + 2)^2) + 1)), +#---- --- ---- --- ---- --- ---- --- ----- --- ----# +back_in = 2.70158*t^3 - 1.70158*t^2, +back_out = 1 + 2.70158* (t-1)^3 + 1.70158*(t-1)^2, +back_in_out = { +k <- 1.70158 +k2 <- k * 1.525 +ifelse(t < 0.5, +(2*t)^2 * ((k2 + 1) * 2 * t - k2) / 2, +((2*t-2)^2 * ((k2 + 1) * (t * 2 - 2) + k2) + 2) / 2 +) +}, +#---- --- ---- --- ---- --- ---- --- ----- --- ----# +elastic_in = ifelse(t == 0,0, +ifelse(t == 1,1 +-(2^(10*t-10))*sin((t*10-10.75)*2*pi/3)) +), +elastic_out = ifelse(t == 0,0, +ifelse(t ==1,1, +2^(-10*t)*sin((t*10-0.75)*2*pi/3)+1) +), +elastic_in_out = ifelse( +t == 0, 0, +ifelse(t == 1, 1, +ifelse(t < 0.5, +-(2^( 20*t - 10) * sin((20 * t - 11.125) * 2 * pi/4.5)) / 2, +(2^(-20*t + 10) * sin((20 * t - 11.125) * 2 * pi/4.5)) / 2 + 1 +)) +), +#---- --- ---- --- ---- --- ---- --- ----- --- ----# +sine_in = 1 - cos((t*pi)/2), +sine_out = sin((t*pi)/2), +sine_in_out = -(cos(t*pi)-1)/2, +#---- --- ---- --- ---- --- ---- --- ----- --- ----# +bounce_in = ifelse(t < 0.3636, +1 - 7.5625 * (1 - t)^2, +ifelse(t >= 0.3636 & t < 0.7273, +1 - 7.5625 * ((1 - t) * (1.5 / 2.75))^2 + 0.75, +ifelse(t >= 0.7273 & t < 0.9091, +1 - 7.5625 * (1 - (t - (2.25 / 2.75)))^2 + 0.9375, +1 - 7.5625 * (1 - (t - (2.625 / 2.75)))^2 + 0.984375))), +bounce_out = ifelse(t < 0.3636, +7.5625 * t^2, +ifelse(t >= 0.3636 & t < 0.7273, +7.5625 * (t - 1.5 / 2.75)^2 + 0.75, +ifelse(t >= 0.7273 & t < 0.9091, +7.5625 * (t - 2.25 / 2.75)^2 + 0.9375, +7.5625 * (t - 2.625 / 2.75)^2 + 0.984375))), +bounce_in_out = ifelse(t < 0.5, +0.5 * (7.5625 * (2 * t)^2), +0.5 * (7.5625 * (2 * t - 1)^2 + 0.75) + 0.5), +stop("The specified ease type doesn't exist:", type) +) +smooth_seq <- from + smooth_fashion * (to-from) +return(smooth_seq) +} +test <- seq_smooth(1,50,100,type = "step",ease = "out") +test +plot(t,test, type = "l", col = "gray20") +test <- seq_smooth(1,50,100,type = "linear",ease = "out") +test +plot(t,test, type = "l", col = "gray20") +seq_smooth <- function(from = 1, to = 1, +n = 100, +type = "linear", +step_count = NULL, +ease = NULL){ +stopifnot( +is.character(type), +is.character(ease) +) +type <- match.arg(type,c("linear","quad","cubic","quart", +"quint","exp","circle","back", +"elastic","sine","bounce","step")) +# `in` curves it at the start +#`out` will curve the line at the end +#`in_out` will curve the line at both ends +# Keep in mind there are n - 1 critical points +# as polynomials of size n increases. +ease <- match.arg(ease,c("in","out","in_out")) +# Compute normalized time (t) as the y-component +# Time could be any range, but it complicates comparison if +# time range is not bounded. However, you can always +# normalize it to be bounded from [0,1] +t <- seq(0,1,length.out = n) +# Default sequence +if(type == "linear") { +seq <- from + t*(to-from) +return(seq) +} +# Issue warning if 'eae' not set to NULL when type is linear +if(type == "linear" & !is.null(ease)) { +warning("No easing applied to linear sequence.") +seq <- from + t*(to-from) +return(seq) +} +if(type == "step" & is.null(step_count)){ +warning("Step count set to 'NULL'. Using default steps = 4") +step_count = 4 +smooth_seq <- from + (to - from)*round(step_count*t) +return(smooth_seq) +} else{ +smooth_seq <- from + (to - from)*round(step_count*t) +return(smooth_seq) +} +if(type == "step" && !is.null(ease)){ +warning("Step function is not continuous and cannot be smoothed using an easing function.\nUsing default steps = 4") +if(is.null(step_count)){step_count = 4 +smooth_seq <- from + (to - from)*round(step_count*t) +return(smooth_seq) +} else { +smooth_seq <- from + (to - from)*round(step_count*t) +return(smooth_seq) +} +} +# What type of sequence and direction to compute +smooth_fashion <- join_char(type,"_",ease) +smooth_fashion <- switch( +smooth_fashion, +#---- --- ---- --- ---- --- ---- --- ----- --- ----# +# Notice how we use base '2' and exponentiate as +# polynomial increases by n. You can use any base you +# like, I chose 2 because that's what I've seen others +# do, and it's the standard as far as I know. +quad_in = t^2, +quad_out = 1-(1 - t)^2, +quad_in_out = ifelse(t < 0.5, +2*t^2, +1 - 0.5*(-2*t+2)^2), +#---- --- ---- --- ---- --- ---- --- ----- --- ----# +cubic_in = t^3, +cubic_out = 1 - (1-t)^3, +cubic_in_out = ifelse(t < 0.5, +4*t^3, +1- 0.5*(-2*t+2)^3), +#---- --- ---- --- ---- --- ---- --- ----- --- ----# +quart_in = t^4, +quart_out = 1 - (1-t)^4, +quart_in_out = ifelse(t < 0.5, +8*t^4, +1 - 0.5*(-2*t+2)^4), +#---- --- ---- --- ---- --- ---- --- ----- --- ----# +quint_in = t^5, +quint_out = 1 - (1-t)^5, +quint_in_out = ifelse(t < 0.5, +16*t^5, +1 - 0.5*(-2*t+2)^5), +#---- --- ---- --- ---- --- ---- --- ----- --- ----# +exp_in = 2^(10*t - 10), +exp_out = 1 - 2^(-10*t), +exp_in_out = ifelse(t == 0,0, +ifelse(t == 1,1, +ifelse(t < 0.5, +2^(20*t-10)/2, +(2 - 2^(-20*t+10))/2 +))), +#---- --- ---- --- ---- --- ---- --- ----- --- ----# +circle_in = 1 - sqrt(1-t)^2, +circle_out = sqrt(1 - (t - 1)^2), +circle_in_out = ifelse(t < 0.5, +(1 - sqrt(1 - (2 * t)^2)) / 2, +0.5 * (sqrt(1 - (-2 * t + 2)^2) + 1)), +#---- --- ---- --- ---- --- ---- --- ----- --- ----# +back_in = 2.70158*t^3 - 1.70158*t^2, +back_out = 1 + 2.70158* (t-1)^3 + 1.70158*(t-1)^2, +back_in_out = { +k <- 1.70158 +k2 <- k * 1.525 +ifelse(t < 0.5, +(2*t)^2 * ((k2 + 1) * 2 * t - k2) / 2, +((2*t-2)^2 * ((k2 + 1) * (t * 2 - 2) + k2) + 2) / 2 +) +}, +#---- --- ---- --- ---- --- ---- --- ----- --- ----# +elastic_in = ifelse(t == 0,0, +ifelse(t == 1,1 +-(2^(10*t-10))*sin((t*10-10.75)*2*pi/3)) +), +elastic_out = ifelse(t == 0,0, +ifelse(t ==1,1, +2^(-10*t)*sin((t*10-0.75)*2*pi/3)+1) +), +elastic_in_out = ifelse( +t == 0, 0, +ifelse(t == 1, 1, +ifelse(t < 0.5, +-(2^( 20*t - 10) * sin((20 * t - 11.125) * 2 * pi/4.5)) / 2, +(2^(-20*t + 10) * sin((20 * t - 11.125) * 2 * pi/4.5)) / 2 + 1 +)) +), +#---- --- ---- --- ---- --- ---- --- ----- --- ----# +sine_in = 1 - cos((t*pi)/2), +sine_out = sin((t*pi)/2), +sine_in_out = -(cos(t*pi)-1)/2, +#---- --- ---- --- ---- --- ---- --- ----- --- ----# +bounce_in = ifelse(t < 0.3636, +1 - 7.5625 * (1 - t)^2, +ifelse(t >= 0.3636 & t < 0.7273, +1 - 7.5625 * ((1 - t) * (1.5 / 2.75))^2 + 0.75, +ifelse(t >= 0.7273 & t < 0.9091, +1 - 7.5625 * (1 - (t - (2.25 / 2.75)))^2 + 0.9375, +1 - 7.5625 * (1 - (t - (2.625 / 2.75)))^2 + 0.984375))), +bounce_out = ifelse(t < 0.3636, +7.5625 * t^2, +ifelse(t >= 0.3636 & t < 0.7273, +7.5625 * (t - 1.5 / 2.75)^2 + 0.75, +ifelse(t >= 0.7273 & t < 0.9091, +7.5625 * (t - 2.25 / 2.75)^2 + 0.9375, +7.5625 * (t - 2.625 / 2.75)^2 + 0.984375))), +bounce_in_out = ifelse(t < 0.5, +0.5 * (7.5625 * (2 * t)^2), +0.5 * (7.5625 * (2 * t - 1)^2 + 0.75) + 0.5), +stop("The specified ease type doesn't exist:", type) +) +smooth_seq <- from + smooth_fashion * (to-from) +return(smooth_seq) +} +plot(t,test, type = "l", col = "gray20") +test <- seq_smooth(1,50,100,type = "linear",ease = "out") +test +plot(t,test, type = "l", col = "gray20") +test <- seq_smooth(1,50,100,type = "bounce",ease = "out") +plot(t,test, type = "l", col = "gray20") +test <- seq_smooth(1,50,100,type = "bounce",ease = "out") +0.3636*2.75 +.7273*2.75 +0.9091*2.75 +bounce_in <- 1 - ifelse( +(1 - t) < 0.3636, +7.5625 * (1 - t)^2, +ifelse( +(1 - t) < 0.7273, +7.5625 * ((1 - t) - 1.5 / 2.75)^2 + 0.75, +ifelse( +(1 - t) < 0.9091, +7.5625 * ((1 - t) - 2.25 / 2.75)^2 + 0.9375, +7.5625 * ((1 - t) - 2.625 / 2.75)^2 + 0.984375 +) +) +) +bounce_in_out <- ifelse( +t < 0.5, +0.5 * ifelse( +t * 2 < 0.3636, +7.5625 * (2 * t)^2, +ifelse( +t * 2 < 0.7273, +7.5625 * ((2 * t) - 1.5 / 2.75)^2 + 0.75, +ifelse( +t * 2 < 0.9091, +7.5625 * ((2 * t) - 2.25 / 2.75)^2 + 0.9375, +7.5625 * ((2 * t) - 2.625 / 2.75)^2 + 0.984375 +) +) +), +0.5 * ifelse( +(2 * t - 1) < 0.3636, +7.5625 * (2 * t - 1)^2, +ifelse( +(2 * t - 1) < 0.7273, +7.5625 * ((2 * t - 1) - 1.5 / 2.75)^2 + 0.75, +ifelse( +(2 * t - 1) < 0.9091, +7.5625 * ((2 * t - 1) - 2.25 / 2.75)^2 + 0.9375, +7.5625 * ((2 * t - 1) - 2.625 / 2.75)^2 + 0.984375 +) +) +) + 0.5 +) +# Generate sequences for bounce functions +t <- seq(0, 1, length.out = 100) +bounce_out_seq <- bounce_out +bounce_in_seq <- bounce_in +bounce_in_out_seq <- bounce_in_out +# Plot +plot(t, bounce_out_seq, type = "l", col = "red", main = "Bounce Functions") +lines(t, bounce_in_seq, col = "blue") +lines(t, bounce_in_out_seq, col = "green") +legend("topright", legend = c("Bounce Out", "Bounce In", "Bounce In-Out"), +col = c("red", "blue", "green"), lty = 1) +seq_smooth <- function(from = 1, to = 1, +n = 100, +type = "linear", +step_count = NULL, +ease = NULL){ +stopifnot( +is.character(type), +is.character(ease) +) +type <- match.arg(type,c("linear","quad","cubic","quart", +"quint","exp","circle","back", +"elastic","sine","bounce","step")) +# `in` curves it at the start +#`out` will curve the line at the end +#`in_out` will curve the line at both ends +# Keep in mind there are n - 1 critical points +# as polynomials of size n increases. +ease <- match.arg(ease,c("in","out","in_out")) +# Compute normalized time (t) as the y-component +# Time could be any range, but it complicates comparison if +# time range is not bounded. However, you can always +# normalize it to be bounded from [0,1] +t <- seq(0,1,length.out = n) +# Default sequence +if(type == "linear") { +seq <- from + t*(to-from) +return(seq) +} +# Issue warning if 'eae' not set to NULL when type is linear +if(type == "linear" & !is.null(ease)) { +warning("No easing applied to linear sequence.") +seq <- from + t*(to-from) +return(seq) +} +if(type == "step" & is.null(step_count)){ +warning("Step count set to 'NULL'. Using default steps = 4") +step_count = 4 +smooth_seq <- from + (to - from)*round(step_count*t) +return(smooth_seq) +} else{ +smooth_seq <- from + (to - from)*round(step_count*t) +return(smooth_seq) +} +if(type == "step" && !is.null(ease)){ +warning("Step function is not continuous and cannot be smoothed using an easing function.\nUsing default steps = 4") +if(is.null(step_count)){step_count = 4 +smooth_seq <- from + (to - from)*round(step_count*t) +return(smooth_seq) +} else { +smooth_seq <- from + (to - from)*round(step_count*t) +return(smooth_seq) +} +} +# What type of sequence and direction to compute +smooth_fashion <- join_char(type,"_",ease) +smooth_fashion <- switch( +smooth_fashion, +#---- --- ---- --- ---- --- ---- --- ----- --- ----# +# Notice how we use base '2' and exponentiate as +# polynomial increases by n. You can use any base you +# like, I chose 2 because that's what I've seen others +# do, and it's the standard as far as I know. +quad_in = t^2, +quad_out = 1-(1 - t)^2, +quad_in_out = ifelse(t < 0.5, +2*t^2, +1 - 0.5*(-2*t+2)^2), +#---- --- ---- --- ---- --- ---- --- ----- --- ----# +cubic_in = t^3, +cubic_out = 1 - (1-t)^3, +cubic_in_out = ifelse(t < 0.5, +4*t^3, +1- 0.5*(-2*t+2)^3), +#---- --- ---- --- ---- --- ---- --- ----- --- ----# +quart_in = t^4, +quart_out = 1 - (1-t)^4, +quart_in_out = ifelse(t < 0.5, +8*t^4, +1 - 0.5*(-2*t+2)^4), +#---- --- ---- --- ---- --- ---- --- ----- --- ----# +quint_in = t^5, +quint_out = 1 - (1-t)^5, +quint_in_out = ifelse(t < 0.5, +16*t^5, +1 - 0.5*(-2*t+2)^5), +#---- --- ---- --- ---- --- ---- --- ----- --- ----# +exp_in = 2^(10*t - 10), +exp_out = 1 - 2^(-10*t), +exp_in_out = ifelse(t == 0,0, +ifelse(t == 1,1, +ifelse(t < 0.5, +2^(20*t-10)/2, +(2 - 2^(-20*t+10))/2 +))), +#---- --- ---- --- ---- --- ---- --- ----- --- ----# +circle_in = 1 - sqrt(1-t)^2, +circle_out = sqrt(1 - (t - 1)^2), +circle_in_out = ifelse(t < 0.5, +(1 - sqrt(1 - (2 * t)^2)) / 2, +0.5 * (sqrt(1 - (-2 * t + 2)^2) + 1)), +#---- --- ---- --- ---- --- ---- --- ----- --- ----# +back_in = 2.70158*t^3 - 1.70158*t^2, +back_out = 1 + 2.70158* (t-1)^3 + 1.70158*(t-1)^2, +back_in_out = { +k <- 1.70158 +k2 <- k * 1.525 +ifelse(t < 0.5, +(2*t)^2 * ((k2 + 1) * 2 * t - k2) / 2, +((2*t-2)^2 * ((k2 + 1) * (t * 2 - 2) + k2) + 2) / 2 +) +}, +#---- --- ---- --- ---- --- ---- --- ----- --- ----# +elastic_in = ifelse(t == 0,0, +ifelse(t == 1,1 +-(2^(10*t-10))*sin((t*10-10.75)*2*pi/3)) +), +elastic_out = ifelse(t == 0,0, +ifelse(t ==1,1, +2^(-10*t)*sin((t*10-0.75)*2*pi/3)+1) +), +elastic_in_out = ifelse( +t == 0, 0, +ifelse(t == 1, 1, +ifelse(t < 0.5, +-(2^( 20*t - 10) * sin((20 * t - 11.125) * 2 * pi/4.5)) / 2, +(2^(-20*t + 10) * sin((20 * t - 11.125) * 2 * pi/4.5)) / 2 + 1 +)) +), +#---- --- ---- --- ---- --- ---- --- ----- --- ----# +sine_in = 1 - cos((t*pi)/2), +sine_out = sin((t*pi)/2), +sine_in_out = -(cos(t*pi)-1)/2, +#---- --- ---- --- ---- --- ---- --- ----- --- ----# +bounce_in = 1 - ifelse( +(1 - t) < 0.3636, +7.5625 * (1 - t)^2, +ifelse( +(1 - t) < 0.7273, +7.5625 * ((1 - t) - 1.5 / 2.75)^2 + 0.75, +ifelse( +(1 - t) < 0.9091, +7.5625 * ((1 - t) - 2.25 / 2.75)^2 + 0.9375, +7.5625 * ((1 - t) - 2.625 / 2.75)^2 + 0.984375 +) +) +), +bounce_out = ifelse( +t < 0.3636, +7.5625 * t^2, +ifelse( +t < 0.7273, +7.5625 * (t - 1.5 / 2.75)^2 + 0.75, +ifelse( +t < 0.9091, +7.5625 * (t - 2.25 / 2.75)^2 + 0.9375, +7.5625 * (t - 2.625 / 2.75)^2 + 0.984375 +) +) +) +, +bounce_in_out = ifelse( +t < 0.5, +0.5 * ifelse( +t * 2 < 0.3636, +7.5625 * (2 * t)^2, +ifelse( +t * 2 < 0.7273, +7.5625 * ((2 * t) - 1.5 / 2.75)^2 + 0.75, +ifelse( +t * 2 < 0.9091, +7.5625 * ((2 * t) - 2.25 / 2.75)^2 + 0.9375, +7.5625 * ((2 * t) - 2.625 / 2.75)^2 + 0.984375 +) +) +), +0.5 * ifelse( +(2 * t - 1) < 0.3636, +7.5625 * (2 * t - 1)^2, +ifelse( +(2 * t - 1) < 0.7273, +7.5625 * ((2 * t - 1) - 1.5 / 2.75)^2 + 0.75, +ifelse( +(2 * t - 1) < 0.9091, +7.5625 * ((2 * t - 1) - 2.25 / 2.75)^2 + 0.9375, +7.5625 * ((2 * t - 1) - 2.625 / 2.75)^2 + 0.984375 +) +) +) + 0.5 +), +stop("The specified ease type doesn't exist:", type) +) +smooth_seq <- from + smooth_fashion * (to-from) +return(smooth_seq) +} +test <- seq_smooth(1,50,100,type = "bounce",ease = "out") +test <- seq_smooth(1,50,100,type = "bounce",ease = "in") +test <- seq_smooth(1,50,100,type = "quint",ease = "in") diff --git a/R/seq_nl.R b/R/seq_nl.R new file mode 100644 index 0000000..2d37909 --- /dev/null +++ b/R/seq_nl.R @@ -0,0 +1,208 @@ +seq_smooth <- function(from = 1, to = 1, + n = 100, + type = "linear", + step_count = NULL, + ease = NULL){ + +stopifnot( + is.character(type), + is.character(ease) +) + + type <- match.arg(type,c("linear","quad","cubic","quart", + "quint","exp","circle","back", + "elastic","sine","bounce","step")) + + # `in` curves it at the start + #`out` will curve the line at the end + #`in_out` will curve the line at both ends + # Keep in mind there are n - 1 critical points + # as polynomials of size n increases. + ease <- match.arg(ease,c("in","out","in_out")) + + # Compute normalized time (t) as the y-component + # Time could be any range, but it complicates comparison if + # time range is not bounded. However, you can always + # normalize it to be bounded from [0,1] + t <- seq(0,1,length.out = n) + + # Default sequence + if(type == "linear") { + seq <- from + t*(to-from) + return(seq) + } + + # Issue warning if 'eae' not set to NULL when type is linear + if(type == "linear" & !is.null(ease)) { + warning("No easing applied to linear sequence.") + seq <- from + t*(to-from) + return(seq) + } + + if(type == "step" & is.null(step_count)){ + warning("Step count set to 'NULL'. Using default steps = 4") + step_count = 4 + smooth_seq <- from + (to - from)*round(step_count*t) + return(smooth_seq) + } else{ + smooth_seq <- from + (to - from)*round(step_count*t) + return(smooth_seq) + } + + if(type == "step" && !is.null(ease)){ + warning("Step function is not continuous and cannot be smoothed using an easing function.\nUsing default steps = 4") + if(is.null(step_count)){step_count = 4 + smooth_seq <- from + (to - from)*round(step_count*t) + return(smooth_seq) + } else { + smooth_seq <- from + (to - from)*round(step_count*t) + return(smooth_seq) + } + } + + # What type of sequence and direction to compute + smooth_fashion <- join_char(type,"_",ease) + + + smooth_fashion <- switch( + smooth_fashion, + #---- --- ---- --- ---- --- ---- --- ----- --- ----# + # Notice how we use base '2' and exponentiate as + # polynomial increases by n. You can use any base you + # like, I chose 2 because that's what I've seen others + # do, and it's the standard as far as I know. + quad_in = t^2, + quad_out = 1-(1 - t)^2, + quad_in_out = ifelse(t < 0.5, + 2*t^2, + 1 - 0.5*(-2*t+2)^2), + #---- --- ---- --- ---- --- ---- --- ----- --- ----# + cubic_in = t^3, + cubic_out = 1 - (1-t)^3, + cubic_in_out = ifelse(t < 0.5, + 4*t^3, + 1- 0.5*(-2*t+2)^3), + #---- --- ---- --- ---- --- ---- --- ----- --- ----# + quart_in = t^4, + quart_out = 1 - (1-t)^4, + quart_in_out = ifelse(t < 0.5, + 8*t^4, + 1 - 0.5*(-2*t+2)^4), + #---- --- ---- --- ---- --- ---- --- ----- --- ----# + quint_in = t^5, + quint_out = 1 - (1-t)^5, + quint_in_out = ifelse(t < 0.5, + 16*t^5, + 1 - 0.5*(-2*t+2)^5), + #---- --- ---- --- ---- --- ---- --- ----- --- ----# + exp_in = 2^(10*t - 10), + exp_out = 1 - 2^(-10*t), + exp_in_out = ifelse(t == 0,0, + ifelse(t == 1,1, + ifelse(t < 0.5, + 2^(20*t-10)/2, + (2 - 2^(-20*t+10))/2 + ))), + #---- --- ---- --- ---- --- ---- --- ----- --- ----# + circle_in = 1 - sqrt(1-t)^2, + circle_out = sqrt(1 - (t - 1)^2), + circle_in_out = ifelse(t < 0.5, + (1 - sqrt(1 - (2 * t)^2)) / 2, + 0.5 * (sqrt(1 - (-2 * t + 2)^2) + 1)), + #---- --- ---- --- ---- --- ---- --- ----- --- ----# + back_in = 2.70158*t^3 - 1.70158*t^2, + back_out = 1 + 2.70158* (t-1)^3 + 1.70158*(t-1)^2, + back_in_out = { + k <- 1.70158 + k2 <- k * 1.525 + ifelse(t < 0.5, + (2*t)^2 * ((k2 + 1) * 2 * t - k2) / 2, + ((2*t-2)^2 * ((k2 + 1) * (t * 2 - 2) + k2) + 2) / 2 + ) + }, + #---- --- ---- --- ---- --- ---- --- ----- --- ----# + elastic_in = ifelse(t == 0,0, + ifelse(t == 1,1 + -(2^(10*t-10))*sin((t*10-10.75)*2*pi/3)) + ), + elastic_out = ifelse(t == 0,0, + ifelse(t ==1,1, + 2^(-10*t)*sin((t*10-0.75)*2*pi/3)+1) + ), + elastic_in_out = ifelse( + t == 0, 0, + ifelse(t == 1, 1, + ifelse(t < 0.5, + -(2^( 20*t - 10) * sin((20 * t - 11.125) * 2 * pi/4.5)) / 2, + (2^(-20*t + 10) * sin((20 * t - 11.125) * 2 * pi/4.5)) / 2 + 1 + )) + ), + #---- --- ---- --- ---- --- ---- --- ----- --- ----# + sine_in = 1 - cos((t*pi)/2), + sine_out = sin((t*pi)/2), + sine_in_out = -(cos(t*pi)-1)/2, + #---- --- ---- --- ---- --- ---- --- ----- --- ----# + bounce_in = 1 - ifelse( + (1 - t) < 0.3636, + 7.5625 * (1 - t)^2, + ifelse( + (1 - t) < 0.7273, + 7.5625 * ((1 - t) - 1.5 / 2.75)^2 + 0.75, + ifelse( + (1 - t) < 0.9091, + 7.5625 * ((1 - t) - 2.25 / 2.75)^2 + 0.9375, + 7.5625 * ((1 - t) - 2.625 / 2.75)^2 + 0.984375 + ) + ) + ), + bounce_out = ifelse( + t < 0.3636, + 7.5625 * t^2, + ifelse( + t < 0.7273, + 7.5625 * (t - 1.5 / 2.75)^2 + 0.75, + ifelse( + t < 0.9091, + 7.5625 * (t - 2.25 / 2.75)^2 + 0.9375, + 7.5625 * (t - 2.625 / 2.75)^2 + 0.984375 + ) + ) + ) + , + bounce_in_out = ifelse( + t < 0.5, + 0.5 * ifelse( + t * 2 < 0.3636, + 7.5625 * (2 * t)^2, + ifelse( + t * 2 < 0.7273, + 7.5625 * ((2 * t) - 1.5 / 2.75)^2 + 0.75, + ifelse( + t * 2 < 0.9091, + 7.5625 * ((2 * t) - 2.25 / 2.75)^2 + 0.9375, + 7.5625 * ((2 * t) - 2.625 / 2.75)^2 + 0.984375 + ) + ) + ), + 0.5 * ifelse( + (2 * t - 1) < 0.3636, + 7.5625 * (2 * t - 1)^2, + ifelse( + (2 * t - 1) < 0.7273, + 7.5625 * ((2 * t - 1) - 1.5 / 2.75)^2 + 0.75, + ifelse( + (2 * t - 1) < 0.9091, + 7.5625 * ((2 * t - 1) - 2.25 / 2.75)^2 + 0.9375, + 7.5625 * ((2 * t - 1) - 2.625 / 2.75)^2 + 0.984375 + ) + ) + ) + 0.5 + ), + + stop("The specified ease type doesn't exist:", type) + + ) + smooth_seq <- from + smooth_fashion * (to-from) + + return(smooth_seq) +} diff --git a/R/utils.R b/R/utils.R new file mode 100644 index 0000000..98a50b0 --- /dev/null +++ b/R/utils.R @@ -0,0 +1,27 @@ +replace_char <- function(.input_string, + old_char, + new_char, + use_regex = FALSE) { + + if (old_char == "" || is.null(old_char)) { + stop("Error: The character to replace cannot be empty.") + } + + gsub(old_char, + new_char, + .input_string, + fixed = !use_regex) + +} + +join_char <- function(first, + join_with = "_", + second){ + + stopifnot(is.character(first), + is.character(second)) + + new_string <- paste0(first,join_with,second) + + return(new_string) +}