Giter Site home page Giter Site logo

linecode / three-plain-animator Goto Github PK

View Code? Open in Web Editor NEW

This project forked from maciejwwojcik/three-plain-animator

0.0 1.0 0.0 21.99 MB

Three-Plain-Animator is a package for threejs developers to support 2D animations.

License: MIT License

JavaScript 99.70% TypeScript 0.29% HTML 0.01%

three-plain-animator's Introduction

Welcome to three-plain-animator

Three-Plain-Animator is a package for threejs developers to support 2D animations.

npm version TypeScript

Example

Installation

The package is available via npm: three-plain-animator

npm i three-plain-animator

Usage

There are two main classes to work with:

  • PlainAnimator
  • PlainSingularAnimator

The first one is for continuous animations like walking. The second one is for animation that should run only once and then stop on the last frame of the animation.

Example

This is a simple example with Homer Simpson animation gif for reference. I assume that creating a basic scene using threejs is not a part of this example. So I will show only unnecessary code just to not mess around.

I converted gif to sprite and uploaded this on imgur.

The first step is to create texture just like every texture using threejs.

const texturePath =  'https://i.imgur.com/Oj6RJV9.png';
const spriteTexture = new  THREE.TextureLoader().load(texturePath)

Next step is about creating the animator object:

 const animator =  new  PlainAnimator(spriteTexture, 4, 4, 10, 15);

These magic numbers are the follows: |value| description | |--|--| | 4 | number of frames horizontally | | 4 | number of frames vertically | | 10 | total number of frames | | 15 | frames per second (fps) |

Then the final texture could be get using init() method:

const texture = animator.init();

To animate texture it's required to animate texture in the main loop of rendering

animator.animate();

So for instance, you can use it as follows:

function animate() {
  renderer.render(scene, camera);
  animator.animate(); // update the animation
  requestAnimationFrame(animate);
}

animate();

Full code:

const texturePath =  'https://i.imgur.com/Oj6RJV9.png';
const spriteTexture = new  THREE.TextureLoader().load(texturePath)
const animator =  new  PlainAnimator(spriteTexture, 4, 4, 10, 15);

const texture = animator.init();    

const geometry =  new  THREE.PlaneGeometry(512,  512);
const material =  new  THREE.MeshBasicMaterial({ map: texture, transparent:  true  });

let mesh =  new  THREE.Mesh(geometry, material)

There is working code on stackblitz with this example:

Docs

Read docs here.

Requierments

The package requires threejs library

Support

The package supports TypeScript and contains typescript definitions.

Future work & TODO

  1. GIF files support
  2. Tests

Feel free to ask any questions. Post on GitHub or write to me: [email protected]

three-plain-animator's People

Contributors

dependabot[bot] avatar maciejwwojcik avatar

Watchers

 avatar

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.