GithubHelp home page GithubHelp logo

dok11 / easy-prop-animation Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 0.0 80 KB

EasyPropAnimation is a helpful class for Babylon.js that allows you easy to runs a property animations

License: MIT License

TypeScript 100.00%
animation babylonjs css 3d cubic-bezier

easy-prop-animation's Introduction

EasyPropAnimation

EasyPropAnimation is a simple and powerful utility class for BabylonJS that makes animating properties more intuitive and developer-friendly. It simplifies the process of creating animations using a syntax similar to CSS transitions. With EasyPropAnimation, you can quickly and easily animate various properties of your 3D objects.

Features

  • Animate multiple properties at once with a single function call
  • Specify easing functions and durations similar to CSS transitions
  • Use simple arrays instead of Vector3 when desired
  • No need to manually create keyframes or animation functions

Installation

npm install easy-prop-animation

Usage

Here's an example of how to use EasyPropAnimation:

import { EasyPropAnimation } from 'easy-prop-animation';

// Run the animation
EasyPropAnimation.run(camera, {
  position: new Vector3(0, 4, 2),
  transition: 'all 1s ease-in-out',
});

You can animate multiple properties at once:

EasyPropAnimation.run(camera, {
  position: new Vector3(0, 4, 2),
  target: new Vector3(1, 4, 6),
  transition: 'all 500ms linear',
});

And split them by transition property as in CSS:

EasyPropAnimation.run(camera, {
  position: new Vector3(0, 4, 2),
  target: new Vector3(1, 4, 6),
  transition: 'position 1s ease-in-out, target 5s ease-in',
});

And you don't need to use Vector3 class, if you want to use a simple array

EasyPropAnimation.run(camera, {
  position: [-1, 4, 3],
  target: [1.2, 1.47, 5.8],
  transition: 'all 500ms linear',
});

And also you can the only specified property of a vector:

EasyPropAnimation.run(camera, {
  'position.y': 10,
  transition: 'all 300ms ease-in-out',
});

And you can use Bézier curve for easing function:

EasyPropAnimation.run(camera, {
  position: [-1, 4, 3],
  transition: 'all 1s cubic-bezier(0.42, 0, 0.58, 1)',
});

For manual control of created animation you can use returned AnimationGroup:

const animationGroup = EasyPropAnimation.run(camera, {
  'position.y': 10,
  transition: 'all 300ms ease-in-out',
});
animationGroup.onAnimationGroupEndObservable.add(() => {
  EasyPropAnimation.run(camera, {
    'position.y': 0,
    transition: 'all 300ms ease-in-out',
  });
});

With this library we can animate Nodes, Cameras and even Scene, here is an example:

scene.imageProcessingConfiguration.exposure = 0;

function onSceneReady() {
  EasyPropAnimation.run(this.scene, {
    'imageProcessingConfiguration.exposure': 1,
    transition: 'all 1s ease-in-out',
  });
}

Contributing

Contributions are welcome! If you'd like to contribute to this project, please follow the standard Gitflow workflow and submit a pull request.

Relative resources

easy-prop-animation's People

Contributors

dok11 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

easy-prop-animation's Issues

Feature request: animation abort controller

Hi, when animation a camera position I would love to have an abort controller support so I can relinquish camera control to the user if they start trying to move the view.

Currently I use my own camera moving scripts and do this

const controller = new AbortController();
const canvas = scene.getEngine().getRenderingCanvas();

const onFocus = () => controller.abort();

canvas?.addEventListener('focus', onFocus);
canvas?.addEventListener('wheel', onFocus);

// move camera
await Promise.all([
  animateCameraTo(camera, scene, controller.signal),
  animateCameraTo({ ...camTarget, prop: 'target', duration: 500 }, scene, controller.signal),
]);

canvas?.removeEventListener('focus', onFocus);
canvas?.removeEventListener('wheel', onFocus);

and I think it would be a great addition to the run function.

Multiple animation snapping to end

Hi, when I have multiple animations with different durations all animations seem to snap to their final frame when the first duration is reached.

EasyPropAnimation.run(scene.activeCamera, {
  position: [camera.x, camera.y, camera.z],
  target: [camTarget.x, camTarget.y, camTarget.z],
  transition: 'target 0.5s cubic-bezier(0.4, 0, 0.2, 1), position 1.5s cubic-bezier(0.4, 0, 0.2, 1)',
});

I've fixed this in a local version of your code by converting all animations to an animation group and then playing, and returning, that animation group at the end of the run functions. I'm happy to do a pull request if that's the route you want to take.

An added bonus of using animationGroup is you gain observables on the entire group like so

console.log('animation start');

const anim = EasyPropAnimation.run(scene.activeCamera, {
  position: [camera.x, camera.y, camera.z],
  target: [camTarget.x, camTarget.y, camTarget.z],
  transition: 'target 0.5s cubic-bezier(0.4, 0, 0.2, 1), position 1.5s cubic-bezier(0.4, 0, 0.2, 1)',
});

anim.onAnimationGroupEndObservable.add(() => console.log('animation end'));

Thanks 😊

Update Babylon Version

Is there any particular reason why Babylon v5 is listed as peer dependancy? From what I know, your library is working just fine with latest Babylon version(6.27.1), but I needed to import it manually without npm.

Both --legacy-peer-deps and --force is something I prefer to avoid if possible.

Thx

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.