To integrate Flipper animations with Roact, create a Flipper.SingleMotor in your component's init method. Use Roact.createBinding to track the motor's value and use self.motor:onStep(setBinding) to update the binding on every animation frame. This allows you to use binding:map() within your render function to derive properties like Size or Position from the animation state.
To trigger animations, use self.motor:setGoal() with a goal type like Flipper.Spring.new(targetValue, options).
local Example = Roact.Component:extend("Example")
function Example:init()
self.motor = Flipper.SingleMotor.new(0)
local binding, setBinding = Roact.createBinding(self.motor:getValue())
self.binding = binding
self.motor:onStep(setBinding)
end
function Example:render()
return Roact.createElement("ImageButton", {
Size = self.binding:map(function(value)
return UDim2.new(0, 100, 0, 100):Lerp(UDim2.new(0, 80, 0, 80), value)
end),
Position = UDim2.new(0.5, 0, 0.5, 0),
AnchorPoint = Vector2.new(0.5, 0.5),
[Roact.Event.MouseButton1Down] = function()
self.motor:setGoal(Flipper.Spring.new(1, {
frequency = 5,
dampingRatio = 1
}))
end,
[Roact.Event.MouseButton1Up] = function()
self.motor:setGoal(Flipper.Spring.new(0, {
frequency = 4,
dampingRatio = 0.75
}))
end
})
end
return Example