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

Easing.generateExponential() #619

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
76 changes: 38 additions & 38 deletions examples/00_hello_world.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,73 +4,73 @@
<title>Tween.js / hello world!</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="css/style.css" media="screen" rel="stylesheet" type="text/css" />
<style>
.threedee {
position: absolute;
transform-style: preserve-3d;
transform: translate3d(0, 0, 0.1px);
}
.box {
width: 100px;
height: 100px;
padding: 1em;
box-sizing: border-box;
}
</style>
</head>
<body>
<body style="transform-style: preserve-3d; perspective: 800px;">
<div id="info">
<h1><a href="http://github.com/tweenjs/tween.js">tween.js</a></h1>
<h2>00 _ hello world</h2>
<p>Simple example for illustrating the creation and chaining of tweens.</p>
</div>
<div
id="target"
style="
position: absolute;
top: 100px;
left: 100px;
width: 100px;
height: 100px;
background: #a0dde9;
padding: 1em;
"
>
hello world!

<div class="threedee" style="transform: translate3d(0, 0, 0.1px);">
<div id="target" class="box" style="background: #a0dde9;">hello world!</div>
</div>

<div class="threedee" style="transform: translate3d(0, 120px, 0.1px);">
<div id="target2" class="box" style="background: #b0a0e9;">hello world again!</div>
</div>

<script src="../dist/tween.umd.js"></script>
<script src="js/RequestAnimationFrame.js"></script>
<script>
var position
var target
var tween, tweenBack

init()
init(document.getElementById('target'))
init(document.getElementById('target2'))
// init2()
animate()

function init() {
position = {x: 100, y: 100, rotation: 0}
target = document.getElementById('target')
tween = new TWEEN.Tween(position)
.to({x: 700, y: 200, rotation: 359}, 2000)
.delay(1000)
.easing(TWEEN.Easing.Elastic.InOut)
function init(target) {
const position = {x: 0, y: 0, rotation: 0}
const tween = new TWEEN.Tween(position)
.to({x: 700, y: 200, rotation: 360}, 2000)
.easing(TWEEN.Easing.Exponential.InOut)
.onUpdate(update)

tweenBack = new TWEEN.Tween(position)
.to({x: 100, y: 100, rotation: 0}, 3000)
.easing(TWEEN.Easing.Elastic.InOut)
const tweenBack = new TWEEN.Tween(position)
.to({x: 0, y: 0, rotation: 0}, 2000)
.easing(TWEEN.Easing.Exponential.InOut)
.onUpdate(update)

tween.chain(tweenBack)
tweenBack.chain(tween)

tween.start()

function update() {
target.style.transform = `translate3d(${position.x}px, ${position.y}px, 0.01px) rotateY(${position.rotation}deg)`
}
}

//If we register the callback animate, but the TWEEN.update(time) returns false,
//cancel/unregister the handler
function animate(time) {
var id = requestAnimationFrame(animate)

var result = TWEEN.update(time)

if (!result) cancelAnimationFrame(id)
}
var keepGoing = TWEEN.update(time)

function update() {
target.style.left = position.x + 'px'
target.style.top = position.y + 'px'
target.style.webkitTransform = 'rotate(' + Math.floor(position.rotation) + 'deg)'
target.style.MozTransform = 'rotate(' + Math.floor(position.rotation) + 'deg)'
if (!keepGoing) cancelAnimationFrame(id)
}
</script>
</body>
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
"tween",
"interpolation"
],
"dependencies": {},
"scripts": {
"dev": "(npm run tsc-watch -- --preserveWatchOutput & p1=$!; npm run rollup-build -- --watch & p2=$!; wait $p1 $p2)",
"build": "rimraf dist .tmp && node scripts/write-version.js && npm run tsc && npm run rollup-build",
Expand Down
60 changes: 60 additions & 0 deletions src/Easing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,66 @@ const Easing = {
},
}
},
generateExponential: function (
// A value of 300 is makes the curve very close to Exponential with its value of 1024.
base = 300,
): {
In(amount: number): number
Out(amount: number): number
InOut(amount: number): number
} {
// https://www.desmos.com/calculator/pioplwo3zq

if (base <= 0) {
console.warn(
'base should be larger than 0. You might like to try between 20 and 400, maybe more. Setting a default value of 300.',
)
base = 300
}

function In(amount: number): number {
// Start similar to Exponential
let expo = base ** (amount - 1)

// adjust so it hits 0 when amount is 0
expo = expo - expo * (1 - amount)

return expo
}

function Out(amount: number): number {
// translate X by 1
amount = amount + 1

// Similar to In, but negate power to make the graph have a negative slope instead of positive.
let expo = base ** -(amount - 1)

// adjust so it hits 1 when amount is 1
expo = expo - expo * -(1 - amount)

// Flip it upside down, move it up by 1.
return -expo + 1
}

function InOut(amount: number): number {
amount *= 2

if (amount < 1) return In(amount) / 2

// Similar to In, but negate power to make the graph have a negative slope instead of positive.
let expo = base ** -(amount - 1)

// adjust so it hits 1 when amount is 1
expo = expo - expo * -(1 - amount)

// Flip it upside down, move it up by 2.
expo = -expo + 2

return expo / 2
}

return {In, Out, InOut}
},
}

export default Easing