Git Product home page Git Product logo

flux's Introduction

flux

A fast, lightweight tweening library for Lua.

Installation

The flux.lua file should be dropped into an existing project and required by it.

flux = require "flux"

The flux.update() function should be called at the start of each frame. As its only argument It should be given the time in seconds that has passed since the last call.

flux.update(deltatime)

Usage

Any number of numerical values in a table can be tweened simultaneously. Tweens are started by using the flux.to() function. This function requires 3 arguments:

  • obj The object which contains the variables to tween
  • time The amount of time the tween should take to complete
  • vars A table where the keys correspond to the keys in obj which should be tweened, and their values correspond to the destination
-- Moves the ball object to the position 200, 300 over 4 seconds
flux.to(ball, 4, { x = 200, y = 300 })

If you try to tween a variable which is already being tweened, the original tween stops tweening the variable and the new tween begins from the current value.

Additional options

Additional options when creating a tween can be set through the use of chained functions provided by the tween object which flux.to() returns.

flux.to(t, 4, { x = 10 }):ease("linear"):delay(1)

:ease(type)

The easing type which should be used by the tween; type should be a string containing the name of the easing to be used. The library provides the following easing types:

linear quadin quadout quadinout cubicin cubicout cubicinout quartin quartout quartinout quintin quintout quintinout expoin expoout expoinout sinein sineout sineinout circin circout circinout backin backout backinout elasticin elasticout elasticinout

The default easing type is quadout. Examples of the different easing types can be found here.

:delay(time)

The amount of time flux should wait before starting the tween; time should be a number of seconds. The default delay time is 0.

:onstart(fn)

Sets the function fn to be called when the tween starts (once the delay has finished). :onstart() can be called multiple times to add more than one function.

:onupdate(fn)

Sets the function fn to be called each frame the tween updates a value. onupdate() can be called multiple times to add more than one function.

:oncomplete(fn)

Sets the function fn to be called once the tween has finished and reached its destination values. oncomplete() can be called multiple times to add more than one function.

:after([obj,] time, vars)

Creates a new tween and chains it to the end of the existing tween; the chained tween will be called after the original one has finished. Any additional chained function used after :after() will effect the chained tween. There is no limit to how many times :after() can be used in a chain, allowing the creation of long tween sequences. If obj is not specified the obj argument from the original tween is used.

-- Tweens t.x to 10 over 2 seconds, then to 20 over 1 second
flux.to(t, 2, { x = 10 }):after(t, 1, { x = 20 })

Stopping a tween

If you want the ability to stop a tween before it has finished, the tween should be assigned to a variable when it is created.

local tween = flux.to(x, 2, { y = 20 }):delay(1)

The tween can then be stopped at any point by calling its :stop() method.

tween:stop()

This will cause the tween to immediatly be removed from its parent group and will leave its tweened variables at their current values. The tween's oncomplete() callback is not called.

Groups

flux provides the ability to create tween groups; these are objects which can have tweens added to them, and who are in charge of updating and handling their contained tweens. A group is created by calling the flux.group() function.

group = flux.group()

Once a group is created it acts independently of the flux object, and must be updated each frame using its own update method.

group:update(deltatime)

To add a tween to a group, the group's to() method should be used.

group:to(t, 3, { x = 10, y = 20 })

A good example of where groups are useful is for games where you may have a set of tweens which effect objects in the game world and which you want to pause when the game is paused. A group's tweens can be paused by simply neglecting to call its update() method; when a group is destroyed its tweens are also destroyed.

License

This library is free software; you can redistribute it and/or modify it under the terms of the MIT license. See LICENSE for details.

flux's People

Contributors

rxi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

flux's Issues

Check if a tween is active

Is there a possibility to check whether a tween is active or not? Do I have to do it with :oncomplete ? Or can I manually check it if I store the tween as a variable like this:

myTween=flux.to(self, 4, {x=300})
...
if myTween:isActive() then
...
end

Repeat

It would be nice to have a repeat functionality. Something to repeat the whole transformation with options to reverse it "in time" as well.

:oncomplete functions don't want to accept an iterator as an argument

I'm not sure if this is a bug,an intended feature, or an error on my part, but I felt it'd be worth posting regardless.

Whenever you have a tween inside of an iteration and try to pass the iterator as an argument to the function within :oncomplete, it gives the error "expected function or callable". A (barebones) example of what I'm attempting to do is below:

for i, player in ipairs(playerList) do
flux.to(player, moveSpeed, {x =xvalue, y = yvalue}):delay(.5):oncomplete(resetAction(player))
end

A temporary solution I've found is that by calling a mediating function with :oncomplete to then call the main function with the arguments, you can get by without an issue.

If you'd like more code to find the issue, I can provide it as soon as possible.

make flux.after a top-level method

Sometimes I want to make chains of tweens, like so

local f = flux.to(...)
f = f:after(...)
f = f:after(...)

this works pretty nice, except for the fact that the callsite for the first tween is different from the other tweens. this can be inconvenient, for example, when doing this in a loop:

local f 
for _, obj in ipairs(objects) do
    if not f then
        f = flux.to(...)
    else
        f = f:after(...)
     end
end

I'm wondering if it's possible to move this logic up, so for example there could be a flux:after() method that, because it's called on the flux object and not a tween object, behaves like flux.to(). that would look like this in a loop:

local f = flux
for _, obj in ipairs(objects) do
    f = f:after(...)
end

OutBounce

I need an OutBounce ease, is it possible to add?

tween:stop() isn't interrupting chains

Huge fan of this library! One issue/question:

If I chain together multiple tweens in one line of code using flux.to(...):after(...):after(...) and call tween:stop(), it appears that the first tween is stopped and removed from flux.tweens but the others remain and carry out their operations.

Is this the intended behavior? Is there a preferred method for interrupting multiple tweens this way? For the moment I'm having to use oncomplete() for each new tween so I can safely interrupt any complex events.

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.