### Running Tests with Busted Framework Source: https://github.com/kikito/tween.lua/blob/master/README.md Shows the command to run the project's specifications using the busted testing framework. Requires busted to be installed on the system. ```bash busted ``` -------------------------------- ### Installing and Requiring Tween Library in Lua Source: https://github.com/kikito/tween.lua/blob/master/README.md Demonstrates how to install and require the tween.lua library in a Lua project. The library should be copied to a project folder and required using Lua's require function. ```lua local tween = require 'tween' ``` -------------------------------- ### Reversing Animations with Negative Time in Lua Source: https://context7.com/kikito/tween.lua/llms.txt This example uses negative delta time in tween.lua to play animations backward from a current point. It depends on creating a tween object and calling update with negative values; inputs include duration, subject, target, and easing. Purpose is to rewind animations, clamping at start (clock=0); outputs interpolated values in reverse, with limitation that further negative updates do not go below zero. ```lua local tween = require 'tween' local slider = {value = 0} local sliderTween = tween.new(2, slider, {value = 100}, 'inOutQuad') -- Forward animation sliderTween:update(1) print(slider.value) -- Output: 50 (approximately) -- Reverse animation (negative dt) sliderTween:update(-0.5) print(slider.value) -- Output: 25 (approximately) -- Continue reversing sliderTween:update(-0.5) print(slider.value) -- Output: 0 (back to start) -- Clock stops at 0 when going backward sliderTween:update(-1) print(slider.value) -- Output: 0 (clamped at start) print(sliderTween.clock) -- Output: 0 ``` -------------------------------- ### Creating Custom Easing Functions in Lua Source: https://github.com/kikito/tween.lua/blob/master/README.md Shows how to create custom easing functions for use with tween.lua. The example uses LÖVE's Bezier Curve to create a custom easing function that takes four parameters (time, begin, change, duration) and returns the interpolated value. ```lua local cubicbezier = function (x1, y1, x2, y2) local curve = love.math.newBezierCurve(0, 0, x1, y1, x2, y2, 1, 1) return function (t, b, c, d) return c * curve:evaluate(t/d) + b end end local label = { x=200, y=0, text = "hello" } local labelTween = tween.new(4, label, {y=300}, cubicbezier(.35, .97, .58, .61)) ``` -------------------------------- ### Animating Nested Tables in Lua Source: https://context7.com/kikito/tween.lua/llms.txt This example shows tweening complex nested tables in Lua using tween.lua, targeting numeric values in deep structures. It depends on the 'tween' module and updates via the tween object's update method with delta time. Purpose is to smoothly interpolate multi-level object properties like positions and colors; outputs partial or full transitions based on time elapsed, with limitations on non-numeric leaves. ```lua local tween = require 'tween' -- Complex nested structure local character = { position = {x = 0, y = 0}, color = {r = 255, g = 255, b = 255, a = 255}, stats = {health = 100, energy = 50} } local target = { position = {x = 500, y = 300}, color = {r = 0, g = 128, b = 255, a = 128}, stats = {health = 80, energy = 100} } local characterTween = tween.new(3, character, target, 'inOutCubic') -- Update over time characterTween:update(1.5) -- Halfway through print(character.position.x) -- Output: 250 print(character.position.y) -- Output: 150 print(character.color.r) -- Output: 127.5 print(character.stats.energy) -- Output: 75 characterTween:update(1.5) -- Complete print(character.position.x) -- Output: 500 print(character.color.a) -- Output: 128 print(character.stats.health) -- Output: 80 ``` -------------------------------- ### Create Tween with Linear and Easing Functions Source: https://context7.com/kikito/tween.lua/llms.txt Demonstrates how to create tween instances with different easing functions to animate numeric properties of tables. Supports linear, bounce, and custom easing functions. ```lua local tween = require 'tween' -- Animate music volume from 0 to 5 over 10 seconds with linear easing local music = { volume = 0, path = "path/to/file.mp3" } local musicTween = tween.new(10, music, {volume = 5}) -- Animate position with bounce easing local label = { x = 200, y = 0, text = "hello" } local labelTween = tween.new(4, label, {y = 300}, 'outBounce') -- Animate nested tables (color values) local properties = {bgcolor = {255, 255, 255}, fgcolor = {0, 0, 0}} local fadeTween = tween.new(2, properties, {bgcolor = {0, 0, 0}, fgcolor = {255, 0, 0}}, 'linear') -- Custom easing function (t=time, b=begin, c=change, d=duration) local customEasing = function(t, b, c, d) return c * (t / d) ^ 2 + b end local customTween = tween.new(5, {x = 0}, {x = 100}, customEasing) ``` -------------------------------- ### Creating Sequential Tweens in Lua Source: https://context7.com/kikito/tween.lua/llms.txt This snippet demonstrates chaining multiple tweens in sequence using tween.lua by switching tweens upon completion. It requires the 'tween' module and an update loop with delta time; inputs are multiple tween objects with targets and easing. Outputs coordinated animations across stages, printing progress; limitation is manual handling of completion checks without built-in chaining. ```lua local tween = require 'tween' local box = {x = 0, y = 0} -- Create multiple tween stages local tween1 = tween.new(2, box, {x = 100, y = 0}, 'linear') local tween2 = tween.new(2, box, {x = 100, y = 100}, 'outBounce') local tween3 = tween.new(2, box, {x = 0, y = 100}, 'inOutQuad') local currentTween = tween1 local stage = 1 function update(dt) local complete = currentTween:update(dt) print("Stage " .. stage .. ": x=" .. box.x .. ", y=" .. box.y) if complete then stage = stage + 1 if stage == 2 then currentTween = tween2 print("Moving to stage 2") elseif stage == 3 then currentTween = tween3 print("Moving to stage 3") elseif stage == 4 then print("All animations complete!") end end end -- Simulate animation for i = 1, 10 do update(0.6) end -- Output shows smooth transition through all three stages ``` -------------------------------- ### Create Color Fade Tween in Lua Source: https://github.com/kikito/tween.lua/blob/master/README.md Shows how to fade background from white to black and foreground from black to red over 2 seconds using nested tables. Requires tween library; subject and target use tables for colors (arrays of RGB values), with linear easing. Supports subtables for complex properties, updated incrementally with the update method. ```lua local tween = require 'tween' -- fade background from white to black and foregrond from black to red in 2 seconds -- Notice that you can use subtables with tween local properties = {bgcolor = {255,255,255}, fgcolor = {0,0,0}} local fadeTween = tween.new(2, properties, {bgcolor = {0,0,0}, fgcolor={255,0,0}}, 'linear') ... fadeTween:update(dt) ``` -------------------------------- ### Using Built-in Easing Functions in Lua Source: https://github.com/kikito/tween.lua/blob/master/README.md Demonstrates how to use tween.lua's built-in easing functions by specifying them as string names or function references. The linear easing function is used as the default when none is specified. ```lua local t1 = tween.new(10, subject, {x=10}, tween.easing.linear) local t2 = tween.new(10, subject, {x=10}, 'linear') local t3 = tween.new(10, subject, {x=10}) ``` -------------------------------- ### Create Bouncing Text Tween in Lua Source: https://github.com/kikito/tween.lua/blob/master/README.md Demonstrates creating a tween to animate text falling from the top of the screen with a bouncing effect on y=300 over 4 seconds. Depends on the tween library; subject is a table with numeric positions, target specifies the end position, and easing like 'outBounce' controls the animation. Updates via the update method apply the changes gradually. ```lua local tween = require 'tween' -- make some text fall from the top of the screen, bouncing on y=300, in 4 seconds local label = { x=200, y=0, text = "hello" } local labelTween = tween.new(4, label, {y=300}, 'outBounce') ... labelTween:update(dt) ``` -------------------------------- ### Applying Built-in Easing Functions in Lua Source: https://context7.com/kikito/tween.lua/llms.txt This snippet illustrates using predefined easing functions in tween.lua to control animation pacing. It requires the 'tween' module and specifies easing by string name or function reference. Inputs include duration, target object, goal values, and easing type; outputs modify the object properties over time, with available easings listed for various families like Quad, Cubic, and Bounce. ```lua local tween = require 'tween' -- Linear (constant speed) local obj1 = {x = 0} local t1 = tween.new(1, obj1, {x = 100}, 'linear') -- Quadratic ease-in (starts slow, ends fast) local obj2 = {x = 0} local t2 = tween.new(1, obj2, {x = 100}, 'inQuad') -- Bounce ease-out (bouncing effect at the end) local obj3 = {x = 0} local t3 = tween.new(1, obj3, {x = 100}, 'outBounce') -- Elastic ease-in-out (overshoots at both ends) local obj4 = {x = 0} local t4 = tween.new(1, obj4, {x = 100}, 'inOutElastic') -- Back ease-out (slight overshoot at the end) local obj5 = {x = 0} local t5 = tween.new(1, obj5, {x = 100}, 'outBack') -- Using function reference instead of string local obj6 = {x = 0} local t6 = tween.new(1, obj6, {x = 100}, tween.easing.inOutQuad) -- All available easing families and variants: -- linear -- inQuad, outQuad, inOutQuad, outInQuad -- inCubic, outCubic, inOutCubic, outInCubic -- inQuart, outQuart, inOutQuart, outInQuart -- inQuint, outQuint, inOutQuint, outInQuint -- inSine, outSine, inOutSine, outInSine -- inExpo, outExpo, inOutExpo, outInExpo -- inCirc, outCirc, inOutCirc, outInCirc -- inElastic, outElastic, inOutElastic, outInElastic -- inBack, outBack, inOutBack, outInBack -- inBounce, outBounce, inOutBounce, outInBounce ``` -------------------------------- ### Reset Tween to Initial State Source: https://context7.com/kikito/tween.lua/llms.txt Demonstrates how to reset a tween to its initial state, restoring the subject table to original values and resetting the internal clock. ```lua local tween = require 'tween' local character = { health = 100, x = 0, y = 0 } local moveTween = tween.new(2, character, {x = 500, y = 300}) -- Advance the tween moveTween:update(1) print(character.x, character.y) -- Output: 250 150 -- Reset to initial state moveTween:reset() print(character.x, character.y) -- Output: 0 0 print(moveTween.clock) -- Output: 0 -- Can now run the animation again moveTween:update(2) print(character.x, character.y) -- Output: 500 300 ``` -------------------------------- ### Animate Multiple Properties Simultaneously using tween.lua (Lua) Source: https://context7.com/kikito/tween.lua/llms.txt This snippet creates a tween that smoothly animates six separate properties of a player table over three seconds using theinOutQuad' easing function. It requires the tween.lua library demonstrates updating the tween in a game loop, printing the interpolated values each frame. The code is limited to Lua environments compatible with the library, such as LÖVE or Corona. ```Lua local tween = require 'tween' local player = { x = 0, y = 0, rotation = 0, scale = 1, opacity = 0, speed = 10 } local playerTween = tween.new(3, player, { x = 500, y = 300, rotation = 360, scale = 2, opacity = 255, speed = 50 }, 'inOutQuad') -- Single update affects all properties function update(dt) local complete = playerTween:update(dt) print(string.format( "x=%.2f, y=%.2f, rot=%.2f, scale=%.2f, opacity=%.2f, speed=%.2f", player.x, player.y, player.rotation, player.scale, player.opacity, player.speed )) return complete end -- Simulate animation frames for i = 1, 6 do update(0.5) end -- All properties animate smoothly toward their targets ``` -------------------------------- ### Create Volume Tween in Lua Source: https://github.com/kikito/tween.lua/blob/master/README.md This snippet shows how to create a tween that increases music volume from 0 to 5 over 10 seconds. It requires the tween library to be required, and involves a subject table with numeric values and a target table. The tween is updated in a loop with dt, typically in a game engine like LÖVE, to gradually change the values. ```lua local tween = require 'tween' -- increase the volume of music from 0 to 5 in 10 seconds local music = { volume = 0, path = "path/to/file.mp3" } local musicTween = tween.new(10, music, {volume = 5}) ... musicTween:update(dt) ``` -------------------------------- ### Set Tween to Specific Time Source: https://context7.com/kikito/tween.lua/llms.txt Illustrates how to jump to any point in a tween's timeline by directly setting its internal clock. Values are clamped to the tween duration. ```lua local tween = require 'tween' local sprite = { opacity = 0, scale = 1 } local fadeTween = tween.new(5, sprite, {opacity = 255, scale = 2}) -- Jump to 50% completion (2.5 seconds into a 5-second tween) fadeTween:set(2.5) print(sprite.opacity) -- Output: 127.5 print(sprite.scale) -- Output: 1.5 -- Jump to end fadeTween:set(5) print(sprite.opacity) -- Output: 255 print(sprite.scale) -- Output: 2 -- Jump past the end (clamped to duration) fadeTween:set(10) print(sprite.opacity) -- Output: 255 print(sprite.scale) -- Output: 2 -- Rewind to start fadeTween:set(0) print(sprite.opacity) -- Output: 0 print(sprite.scale) -- Output: 1 ``` -------------------------------- ### Tween.new Function in Lua Source: https://github.com/kikito/tween.lua/blob/master/README.md Creates a new tween object for gradual changes. Takes duration (positive number), subject (table with numeric values), target (matching table), and optional easing function or name. Returns a tween that needs updating; values must be numbers or tables, and extra target keys are ignored. ```lua local t = tween.new(duration, subject, target, [easing]) ``` -------------------------------- ### Update Tween in Game Loop Source: https://context7.com/kikito/tween.lua/llms.txt Shows how to update a tween within a game loop using delta time. The update method returns true when the animation completes and interpolates subject properties each frame. ```lua local tween = require 'tween' local ball = { x = 0, y = 0 } local ballTween = tween.new(3, ball, {x = 100, y = 200}, 'inOutQuad') -- In your game loop or update function function update(dt) -- dt is delta time in seconds (e.g., 0.016 for 60 FPS) local complete = ballTween:update(dt) if complete then print("Animation finished!") print("Final position: x=" .. ball.x .. ", y=" .. ball.y) -- Output: Final position: x=100, y=200 else -- Animation still running, ball.x and ball.y are being updated print("Current position: x=" .. ball.x .. ", y=" .. ball.y) end end -- Simulate 5 frames at 60 FPS (0.016 seconds each) for i = 1, 5 do update(0.016) -- Frame 1: Current position: x=1.02, y=2.04 -- Frame 2: Current position: x=4.10, y=8.20 -- Frame 3: Current position: x=9.23, y=18.46 -- Frame 4: Current position: x=16.41, y=32.82 -- Frame 5: Current position: x=25.64, y=51.28 end ``` -------------------------------- ### Updating Dynamic Targets in Lua Tweens Source: https://context7.com/kikito/tween.lua/llms.txt This snippet shows modifying tween targets mid-animation in tween.lua, which automatically adjusts the interpolation path. Requires the 'tween' module; inputs are initial subject, mutable target table, duration, and easing. Purpose is to create responsive animations that adapt to changes; outputs progressive values toward the updated target, with no explicit limitations beyond standard tween behavior. ```lua local tween = require 'tween' local obj = {x = 0} local target = {x = 100} local dynamicTween = tween.new(4, obj, target, 'linear') -- First update dynamicTween:update(1) print(obj.x) -- Output: 25 -- Change the target mid-animation target.x = 200 -- Continue updating - tween adapts to new target dynamicTween:update(1) print(obj.x) -- Output: 125 (adjusts trajectory toward new target) dynamicTween:update(2) print(obj.x) -- Output: 200 (reaches new target) ``` -------------------------------- ### Tween.update Method in Lua Source: https://github.com/kikito/tween.lua/blob/master/README.md Updates the tween by advancing its internal clock by dt, applying easing to approach the target. Input dt must be positive (or negative for reverse); returns true when complete. Changes subject values gradually until matching target; equivalent to set(clock + dt). ```lua local complete = t:update(dt) ``` -------------------------------- ### Tween.reset Method in Lua Source: https://github.com/kikito/tween.lua/blob/master/README.md Resets the tween's internal clock to 0, restoring subject to initial state. Equivalent to set(0). Useful for restarting animations without recreating the tween object. ```lua t:reset() ``` -------------------------------- ### Tween.set Method in Lua Source: https://github.com/kikito/tween.lua/blob/master/README.md Sets the tween's internal clock to a specific time, updating the subject accordingly. Clock is a positive number or 0; returns true if at or beyond duration. Useful for jumping to specific animation states, capping at duration. ```lua local complete = t:set(clock) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.