-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
12_graphs_custom_functions.html
104 lines (85 loc) · 2.87 KB
/
12_graphs_custom_functions.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<!doctype html>
<html lang="en">
<head>
<title>Tween.js / graphs for custom easing functions</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style>
body {
margin: 0px;
}
#target {
font-size: 13px;
padding: 0px 32px;
}
</style>
<link href="css/style.css" media="screen" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="info" style="position: relative">
<h1><a href="http://github.com/tweenjs/tween.js">tween.js</a></h1>
<h2>12 _ graphs for custom easing functions</h2>
<p>
A version of the <a href="03_graphs.html">graphs</a> example, but using custom easing functions (not included in
<a href="https://github.com/tween.js/tween.js">tween.js</a> by default).
</p>
</div>
<div id="target"></div>
<script type="module">
import {createGraph} from './js/createGraph.js'
import * as TWEEN from '../dist/tween.esm.js'
const group = new TWEEN.Group()
window.addEventListener('load', function () {
init()
animate()
})
function init() {
const target = document.getElementById('target')
target.appendChild(createGraph(group, 'Ten Steps', tenStepsEasing))
target.appendChild(document.createElement('br'))
for (let i = 0; i < 4; i++) {
const numSteps = (i + 1) * 4
target.appendChild(createGraph(group, numSteps + ' steps', createStepFunction(numSteps)))
}
target.appendChild(document.createElement('br'))
target.appendChild(createGraph(group, 'Random', randomEasing))
target.appendChild(document.createElement('br'))
target.appendChild(
createGraph(group, 'Noisy Exponential.InOut', createNoisyEasing(0.1, TWEEN.Easing.Exponential.InOut)),
)
target.appendChild(
createGraph(group, 'Noisy Elastic.InOut', createNoisyEasing(0.1, TWEEN.Easing.Elastic.InOut)),
)
target.appendChild(
createGraph(group, 'Noisy Circular.InOut', createNoisyEasing(0.1, TWEEN.Easing.Circular.InOut)),
)
}
function animate() {
requestAnimationFrame(animate)
group.update()
}
function tenStepsEasing(k) {
return Math.floor(k * 10) / 10
}
// This is where we get very meta and write a function for
// generating functions! The returned function is similar to
// the tenStepsEasing function above, but the number of steps
// is passed as a parameter
function createStepFunction(numSteps) {
return function (k) {
return Math.floor(k * numSteps) / numSteps
}
}
function randomEasing(k) {
return Math.random()
}
// Getting meta again: why not use existing functions as the
// base for new easing functions?
function createNoisyEasing(randomProportion, easingFunction) {
const normalProportion = 1.0 - randomProportion
return function (k) {
return randomProportion * Math.random() + normalProportion * easingFunction(k)
}
}
</script>
</body>
</html>