Skip to content

Commit 9f24ff1

Browse files
committed
Better slide_switch animation
1 parent 5a88c58 commit 9f24ff1

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

lib/include/elements/element/gallery/slide_switch.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace cycfi { namespace elements
2323

2424
private:
2525

26-
float _xpos = full_extent;
26+
float _val = 0.0f;
2727
};
2828

2929
inline auto slide_switch()

lib/src/element/gallery/slide_switch.cpp

+8-10
Original file line numberDiff line numberDiff line change
@@ -48,25 +48,23 @@ namespace cycfi { namespace elements
4848
color = color.opacity(color.alpha * theme_.disabled_opacity);
4949

5050
// Animate sliding
51-
auto target = value? bounds.right-radius : bounds.left+radius;
52-
if (enabled && _xpos > bounds.left && _xpos < bounds.right)
51+
auto diff = value - _val;
52+
if (std::abs(diff) > 0.1)
5353
{
54-
auto diff = target - _xpos;
5554
constexpr auto alpha = 0.3;
56-
_xpos += alpha * diff;
57-
if (std::abs(diff) > 1.0f)
58-
ctx.view.refresh(ctx);
59-
else
60-
_xpos = target;
55+
_val += alpha * diff;
56+
ctx.view.refresh(ctx);
6157
}
6258
else
6359
{
64-
_xpos = target;
60+
_val = value;
6561
}
62+
auto span = (bounds.right-bounds.left)-height; // height == diameter of thumb
63+
auto xpos = bounds.left + radius + (span * _val);
6664

6765
// Draw the thumb
6866
canvas_.begin_path();
69-
canvas_.add_circle(_xpos, bounds.top+radius, radius-1.5);
67+
canvas_.add_circle(xpos, bounds.top+radius, radius-1.5);
7068
canvas_.fill_style(color);
7169
canvas_.fill();
7270
}

0 commit comments

Comments
 (0)