Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate Easings Code #139

Merged
merged 6 commits into from
Apr 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions examples/others/easings_testbed.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
********************************************************************************************/

const r = require('raylib')
const easing = require('raylib/easings')

const FONT_SIZE = 20

Expand All @@ -39,10 +38,15 @@ let easingY = 0 // Easing selected for y axis

r.SetTargetFPS(60)

const easingTypes = Object.values(easing)
// Easing functions reference data
const easingTypes = Object.keys(r)
.filter(name => name.startsWith('Ease'))
.map(function (name) {
return { name: name, func: r[name] }
})

// NoEase function, used when "no easing" is selected for any axis. It just ignores all parameters besides b.
easingTypes.shift((t, b, c, d) => b)
// NoEase function, used when "no easing" is selected for any axis.
easingTypes.push({ name: 'None', func: (t, b, c, d) => b })

while (!r.WindowShouldClose()) {
// Update
Expand Down Expand Up @@ -91,8 +95,8 @@ while (!r.WindowShouldClose()) {

// Movement computation
if (!paused && ((boundedT && t < d) || !boundedT)) {
ballPosition.x = easingTypes[easingX](t, 100.0, 700.0 - 100.0, d)
ballPosition.y = easingTypes[easingY](t, 100.0, 400.0 - 100.0, d)
ballPosition.x = easingTypes[easingX].func(t, 100.0, 700.0 - 100.0, d)
ballPosition.y = easingTypes[easingY].func(t, 100.0, 400.0 - 100.0, d)
t += 1.0
}
r.BeginDrawing()
Expand Down
96 changes: 96 additions & 0 deletions examples/shapes/shapes_easings_ball_anim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*******************************************************************************************
*
* raylib [shapes] example - easings ball anim
*
* This example has been created using raylib 2.5 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Copyright (c) 2014-2019 Ramon Santamaria (@raysan5)
*
********************************************************************************************/

const r = require('raylib')

// Initialization
// --------------------------------------------------------------------------------------
const screenWidth = 800
const screenHeight = 450

r.InitWindow(screenWidth, screenHeight, 'raylib [shapes] example - easings ball anim')

// Ball variable value to be animated with easings
let ballPositionX = -100
let ballRadius = 20
let ballAlpha = 0

let state = 0
let framesCounter = 0

r.SetTargetFPS(60) // Set our game to run at 60 frames-per-second
// --------------------------------------------------------------------------------------

// Main game loop
while (!r.WindowShouldClose()) { // Detect window close button or ESC key
// Update
// ----------------------------------------------------------------------------------
if (state === 0) { // Move ball position X with easing
framesCounter++
ballPositionX = r.EaseElasticOut(framesCounter, -100, screenWidth / 2.0 + 100, 120)

if (framesCounter >= 120) {
framesCounter = 0
state = 1
}
} else if (state === 1) { // Increase ball radius with easing
framesCounter++
ballRadius = r.EaseElasticIn(framesCounter, 20, 500, 200)

if (framesCounter >= 200) {
framesCounter = 0
state = 2
}
} else if (state === 2) { // Change ball alpha with easing (background color blending)
framesCounter++
ballAlpha = r.EaseCubicOut(framesCounter, 0.0, 1.0, 200)

if (framesCounter >= 200) {
framesCounter = 0
state = 3
}
} else if (state === 3) { // Reset state to play again
if (r.IsKeyPressed(r.KEY_ENTER)) {
// Reset required variables to play again
ballPositionX = -100
ballRadius = 20
ballAlpha = 0.0
state = 0
}
}

if (r.IsKeyPressed(r.KEY_R)) {
framesCounter = 0
}
// ----------------------------------------------------------------------------------

// Draw
// ----------------------------------------------------------------------------------
r.BeginDrawing()
r.ClearBackground(r.RAYWHITE)

if (state >= 2) {
r.DrawRectangle(0, 0, screenWidth, screenHeight, r.GREEN)
}

r.DrawCircle(ballPositionX, 200, ballRadius, r.Fade(r.RED, 1.0 - ballAlpha))

if (state === 3) {
r.DrawText('PRESS [ENTER] TO PLAY AGAIN!', 240, 200, 20, r.BLACK)
}
r.EndDrawing()
// ----------------------------------------------------------------------------------
}

// De-Initialization
// --------------------------------------------------------------------------------------
r.CloseWindow() // Close window and OpenGL context
// --------------------------------------------------------------------------------------
6 changes: 2 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
* https://github.com/robloach/node-raylib
*/

const raylib = require('./src/generated/node-raylib.js')
const raylib = require('./src/generated/node-raylib')
const { format } = require('util')
const easings = require('./src/easings.js')

// Constants
raylib.MAX_GAMEPADS = 4
raylib.MAX_GAMEPAD_AXIS = 8
raylib.MAX_GAMEPAD_BUTTONS = 32
Expand All @@ -21,7 +21,5 @@ raylib.MAX_KEY_PRESSED_QUEUE = 16
*/
raylib.TextFormat = format

raylib.Easings = easings

// Export the bindings for the module.
module.exports = raylib
Loading