Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

JavaScript/TypeScript animation engine

License

NotificationsYou must be signed in to change notification settings

tweenjs/tween.js

Repository files navigation

JavaScript (TypeScript) tweening engine for easy animations, incorporating optimised Robert Penner's equations.

NPM VersionCDNJSNPM DownloadsBuild and Tests

More languages:English,简体中文


<divid="box"></div><style>#box {background-color: deeppink;width:100px;height:100px;}</style><scripttype="module">import{Tween,Easing}from'https://unpkg.com/@tweenjs/tween.js@23.1.3/dist/tween.esm.js'constbox=document.getElementById('box')// Get the element we want to animate.constcoords={x:0,y:0}// Start at (0, 0)consttween=newTween(coords,false)// Create a new tween that modifies 'coords'..to({x:300,y:200},1000)// Move to (300, 200) in 1 second..easing(Easing.Quadratic.InOut)// Use an easing function to make the animation smooth..onUpdate(()=>{// Called after tween.js updates 'coords'.// Move 'box' to the position described by 'coords' with a CSS translation.box.style.setProperty('transform','translate('+coords.x+'px, '+coords.y+'px)')}).start()// Start the tween immediately.// Setup the animation loop.functionanimate(time){tween.update(time)requestAnimationFrame(animate)}requestAnimationFrame(animate)</script>

Try this example on CodePen

Features

  • Does one thing only and does it well: tweens properties of an object
  • Doesn't take care of CSS units (e.g. appendingpx)
  • Doesn't interpolate colors
  • Easing functions are reusable outside of Tween
  • Can also use custom easing functions
  • Doesn't make its own animation loop, making it flexible for integration intoany animation loop.

Examples

hello worldhello world
(source)
BarsBars
(source)
Black and redBlack and red
(source)
GraphsGraphs
(source)
Simplest possible exampleSimplest possible example
(source)
Video and timeVideo and time
(source)
Array interpolationArray interpolation
(source)
Dynamic to, objectDynamic to, object
(source)
Dynamic to, interpolation arrayDynamic to, interpolation array
(source)
Dynamic to, large interpolation arrayDynamic to, large interpolation array
(source)
RepeatRepeat
(source)
Relative valuesRelative values
(source)
YoyoYoyo
(source)
Stop all chained tweensStop all chained tweens
(source)
Custom functionsCustom functions
(source)
Relative start timeRelative start time
(source)
Pause tweenPause tween
(source)
Complex propertiesComplex properties
(source)
Animate an array of valuesAnimate an array of values
(source)

Installation

The recommended method is to useimport syntax. Here we've listed variousinstall methods starting roughly with the most recommended first and leastdesirable last. Evaluate all of the following methods to pick what is mostsuitable for your project.

Withnpm install andimport fromnode_modules

You can add tween.js as an npm dependency:

npm install @tweenjs/tween.js

Without a build tool

Installed locally

You can import fromnode_modules if you servenode_modules as part of yourwebsite, using a standardimportmap script tag. First, assumingnode_modulesis at the root of your website, you can write an import map like so in your HTMLfile:

<scripttype="importmap">{"imports":{"@tweenjs/tween.js":"/node_modules/@tweenjs/tween.js/dist/tween.esm.js"}}</script>

Now in any of your module scripts you can import Tween.js by its package name:

<scripttype="module">import{Tween}from'@tweenjs/tween.js'</script>

Import from CDN

Note that, without theimportmap, you can import directly from a CDN as with the first example above, like so:

<scripttype="module">import{Tween}from'https://unpkg.com/browse/@tweenjs/tween.js@23.1.3/dist/tween.esm.js'</script>

You can also link yourimportmap to the CDN instead of a localnode_modules folder, if you prefer that:

<scripttype="importmap">{"imports":{"@tweenjs/tween.js":"https://unpkg.com/browse/@tweenjs/tween.js@23.1.3/dist/tween.esm.js"}}</script><scripttype="module">import{Tween}from'@tweenjs/tween.js'</script>

With a build tool

If you are usingNode.js,Parcel,Webpack,Rollup,Vite, or another buildtool, then you can install@tweenjs/tween.js withnpm install @tweenjs/tween.js, andimport the library into your JavaScript (orTypeScript) file, and the build tool will know how to find the source code fromnode_modules without needing to create animportmap script:

import*asTWEENfrom'@tweenjs/tween.js'

However, note that this approach requires always running a build tool for yourapp to work, while theimportmap approach will simply work without any buildtools as a simple static HTML site.

Manual build

Another approach is to download the source code with git, manually build thelibrary, then place the output in your project. Node.js is required for this.

git clone https://github.com/tweenjs/tween.jscd tween.jsnpm installnpm run build

This will create some builds in thedist directory. There are currently two different builds of the library:

  • ES6 Module in/dist/tween.esm.js (recommended)
  • UMD in/dist/tween.umd.js (deprecated, will be removed in a future major version)

You are now able to copy one of those two files into your project, and use like this (recommended):

<scripttype="module">import{Tween}from'path/to/tween.esm.js'</script>

or (deprecated, to be removed in future major):

<scriptsrc="path/to/tween.umd.js"></script><script>const{Tween}=TWEEN</script>

wherepath/to is replaced with the location where you placed the file.

Note

You can also download these files from unpkg, for example here:https://unpkg.com/browse/@tweenjs/tween.js@23.1.3/dist/

Global variable from CDN (deprecated)

Note

This method is deprecated and will be removed in a future major version!

Install a globalTWEEN variable from a content-delivery network (CDN) using the UMD file.

From cdnjs:

<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/tween.js/23.1.3/tween.umd.js"></script>

Or from unpkg.com:

<scriptsrc="https://unpkg.com/@tweenjs/tween.js@^23.1.3/dist/tween.umd.js"></script>

Then use theTWEEN variable in any script:

<script>const{Tween, Easing, Group/*, ...*/}=TWEENconsttween=newTween(someObject)// ...</script>

Note

unpkg.com supports a semver version in the URL, where the^ in theURL tells unpkg to give you the latest version 20.x.x.

CommonJS (deprecated)

Skip this section if you don't know what CommonJS is!

Note

This method is deprecated and will be removed in a future major version!

Any of the above methods work in older systems that still use CommonJS. Repeatany of the above methods but usingdist/tween.cjs instead ofdist/tween.esm.js ordist/tween.umd.js.

Documentation

Tests

You need to installnpm first--this comes with node.js, so install that one first. Then, cd totween.js's (or wherever you cloned the repo) directory and run:

npm install

To run the tests run:

npmtest

If you want to add any feature or change existing features, youmust run thetests to make sure you didn't break anything else. Any pull request (PR) needsto have updated passing tests for feature changes (or new passing tests for newfeatures or fixes) insrc/tests.ts to be accepted. Seecontributing for more information.

People

Maintainers:Joe Pea (@trusktr).

All contributors.

Projects using tween.js

LumeA-Frame VRMOMA Inventing Abstraction 1910-1925Web LabMACCHINA IMinesweeper 3DROMEWebGL GlobeAndroidifyThe Wilderness DowntownLinechart


[8]ページ先頭

©2009-2025 Movatter.jp