Something went wrong. Try again.
Fork of dvui, the immediate-mode Zig GUI, with a font face index for Pipit
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241//! Easing functions mainly used for animations. Controls the rate of change of a value,//! used to turn a linearly changing value into a smooth, styalized change.//!//! Note that some easing functions can return values outside 0 and 1 for values of t//! between 0 and 1.//!//! See [easings.net](https://easings.net/) for examples and visualizations
// Adapted from https://gist.github.com/Kryzarel/bba64622057f21a1d6d44879f9cd7bd4
const std = @import("std");const dvui = @import("dvui.zig");
pub const EasingFn = fn (t: f32) f32;
/// pub fn linear(t: f32) f32 { return t;}
/// pub fn inQuad(t: f32) f32 { return t * t;}/// pub fn outQuad(t: f32) f32 { return 1 - inQuad(1 - t);}/// pub fn inOutQuad(t: f32) f32 { if (t < 0.5) return inQuad(t * 2) / 2; return 1 - inQuad((1 - t) * 2) / 2;}
/// pub fn inCubic(t: f32) f32 { return t * t * t;}/// pub fn outCubic(t: f32) f32 { return 1 - inCubic(1 - t);}/// pub fn inOutCubic(t: f32) f32 { if (t < 0.5) return inCubic(t * 2) / 2; return 1 - inCubic((1 - t) * 2) / 2;}
/// pub fn inQuart(t: f32) f32 { return t * t * t * t;}/// pub fn outQuart(t: f32) f32 { return 1 - inQuart(1 - t);}/// pub fn inOutQuart(t: f32) f32 { if (t < 0.5) return inQuart(t * 2) / 2; return 1 - inQuart((1 - t) * 2) / 2;}
/// pub fn inQuint(t: f32) f32 { return t * t * t * t * t;}/// pub fn outQuint(t: f32) f32 { return 1 - inQuint(1 - t);}/// pub fn inOutQuint(t: f32) f32 { if (t < 0.5) return inQuint(t * 2) / 2; return 1 - inQuint((1 - t) * 2) / 2;}
/// pub fn inSine(t: f32) f32 { return 1 - std.math.cos(t * std.math.pi / 2);}/// pub fn outSine(t: f32) f32 { return std.math.sin(t * std.math.pi / 2);}/// pub fn inOutSine(t: f32) f32 { return (std.math.cos(t * std.math.pi) - 1) / -2;}
/// pub fn inExpo(t: f32) f32 { return std.math.pow(f32, 2, 10 * (t - 1));}/// pub fn outExpo(t: f32) f32 { return 1 - inExpo(1 - t);}/// pub fn inOutExpo(t: f32) f32 { if (t < 0.5) return inExpo(t * 2) / 2; return 1 - inExpo((1 - t) * 2) / 2;}
/// pub fn inCirc(t: f32) f32 { return -(std.math.sqrt(1 - t * t) - 1);}/// pub fn outCirc(t: f32) f32 { return 1 - inCirc(1 - t);}/// pub fn inOutCirc(t: f32) f32 { if (t < 0.5) return inCirc(t * 2) / 2; return 1 - inCirc((1 - t) * 2) / 2;}
/// ////// This function extents past 0 and 1 for values of t between 0 and 1pub fn inElastic(t: f32) f32 { return 1 - outElastic(1 - t);}/// ////// This function extents past 0 and 1 for values of t between 0 and 1pub fn outElastic(t: f32) f32 { const p: f32 = 0.3; return std.math.pow(f32, 2, -10 * t) * std.math.sin((t - p / 4) * (2 * std.math.pi) / p) + 1;}/// ////// This function extents past 0 and 1 for values of t between 0 and 1pub fn inOutElastic(t: f32) f32 { if (t < 0.5) return inElastic(t * 2) / 2; return 1 - inElastic((1 - t) * 2) / 2;}
/// ////// This function extents past 0 and 1 for values of t between 0 and 1pub fn inBack(t: f32) f32 { const s: f32 = 1.70158; return t * t * ((s + 1) * t - s);}/// ////// This function extents past 0 and 1 for values of t between 0 and 1pub fn outBack(t: f32) f32 { return 1 - inBack(1 - t);}/// ////// This function extents past 0 and 1 for values of t between 0 and 1pub fn inOutBack(t: f32) f32 { if (t < 0.5) return inBack(t * 2) / 2; return 1 - inBack((1 - t) * 2) / 2;}
/// pub fn inBounce(t: f32) f32 { return 1 - outBounce(1 - t);}/// pub fn outBounce(t: f32) f32 { const div: f32 = 2.75; const mult: f32 = 7.5625;
if (t < 1 / div) { return mult * t * t; } else if (t < 2 / div) { const x = t - 1.5 / div; return mult * x * x + 0.75; } else if (t < 2.5 / div) { const x = t - 2.25 / div; return mult * x * x + 0.9375; } else { const x = t - 2.625 / div; return mult * x * x + 0.984375; }}/// pub fn inOutBounce(t: f32) f32 { if (t < 0.5) return inBounce(t * 2) / 2; return 1 - inBounce((1 - t) * 2) / 2;}
test { @import("std").testing.refAllDecls(@This());}
test "DOCIMG easing plots" { var t = try dvui.testing.init(.{ .window_size = .{ .w = 200, .h = 100 } }); defer t.deinit();
const plot = struct { var easing: *const EasingFn = linear;
const resolution = 100;
fn frame() !dvui.App.Result { var y_axis = dvui.PlotWidget.Axis{ .min = -0.5, .max = 1.5 }; var x_axis = dvui.PlotWidget.Axis{ .min = -0.25, .max = 1.25 }; var plot = dvui.plot(@src(), .{ .x_axis = &x_axis, .y_axis = &y_axis }, .{ .expand = .both }); defer plot.deinit();
var x_line = plot.line(); defer x_line.deinit(); x_line.point(0, 0); x_line.point(1, 0); x_line.stroke(1, dvui.Color.black);
var y_line = plot.line(); defer y_line.deinit(); y_line.point(0, 0); y_line.point(0, 1); y_line.stroke(1, dvui.Color.black);
var line = plot.line(); defer line.deinit(); for (0..resolution) |i| { const x = @as(f64, @floatFromInt(i)) / @as(f64, @floatFromInt(resolution)); const y = easing(@floatCast(x)); line.point(x, y); } line.stroke(1, dvui.themeGet().color(.highlight, .fill)); return .ok; } };
try dvui.testing.settle(plot.frame); try t.saveImage(plot.frame, null, "easing-plot-linear.png");
inline for (@typeInfo(@This()).@"struct".decls) |decl| { if (comptime std.mem.startsWith(u8, decl.name, "in") or std.mem.startsWith(u8, decl.name, "out")) { plot.easing = @field(@This(), decl.name); try t.saveImage(plot.frame, null, "easing-plot-" ++ decl.name ++ ".png"); } }}