-
Notifications
You must be signed in to change notification settings - Fork 141
/
videoAnimator.js
175 lines (159 loc) · 5.73 KB
/
videoAnimator.js
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**
* Created by amandaghassaei on 7/2/17.
*/
function initVideoAnimator(globals){
var foldAngleSequence = [];
function compile(){
var lastAngle = globals.creasePercent*100;
var t = 0;
for (var i=0;i<foldAngleSequence.length;i++){
var item = foldAngleSequence[i];
item.t = t;
item.from = lastAngle;
if (item.type == "delay") item.to = lastAngle;
lastAngle = item.to;
t += item.dur;
}
}
function nextFoldAngle(stepNum){
var fps = globals.currentFPS;
var frame = globals.capturerFrames + stepNum/globals.numSteps;
var t = frame/fps;
var item = getItemForT(t);
if (item === null) {
globals.shouldAnimateFoldPercent = false;
return globals.creasePercent;
}
if (item.dur == 0) return item.to/100;
t -= item.t;
t /= item.dur;
var angle = item.from*(1-t) + item.to*t;
return angle/100;
}
function getItemForT(t){
for (var i=0;i<foldAngleSequence.length;i++){
var item = foldAngleSequence[i];
if (t <= item.t + item.dur) return item;
}
return null;
}
function addItem(){
foldAngleSequence.push({
type:"animation",
dur: null,
from: null,
to: null
});
render();
}
function addDelay(){
foldAngleSequence.push({
type:"delay",
dur:null,
from: null,
to: null
});
render();
}
function isValid(){
if (foldAngleSequence.length == 0) return false;
for (var i=0;i<foldAngleSequence.length;i++){
var item = foldAngleSequence[i];
if (item.type == "delay" && item.dur !== null) continue;
if (item.type == "animation" && item.dur !== null && item.to != null) continue;
return false
}
return true;
}
function setAnimationStatus(){
if (foldAngleSequence.length == 0) {
$("#foldPercentAnimationStatus").removeClass("warning");
$("#foldPercentAnimationStatus").html("no animation configured");
return;
}
if (isValid()) {
$("#foldPercentAnimationStatus").removeClass("warning");
$("#foldPercentAnimationStatus").html("animation configured");
} else {
$("#foldPercentAnimationStatus").addClass("warning");
$("#foldPercentAnimationStatus").html("incomplete config, will be ignored");
}
}
function render(){
setAnimationStatus();
var html = "";
if (foldAngleSequence.length == 0){
$("#animationSetupHelp").html("No animation items in sequence.");
$("#animationHTML").html("");
return;
}
for (var i=0;i<foldAngleSequence.length;i++){
if (foldAngleSequence[i].type == "delay") html += renderDelay(foldAngleSequence[i], i);
else html += renderItem(foldAngleSequence[i], i);
}
$("#animationSetupHelp").html("Configure automatic <b>Fold Percent</b> control:");
$("#animationHTML").html(html);
//bind events
$(".deleteItem").click(function(e){
e.preventDefault();
var $target = $(e.target);
var index = $target.data("index");
if (index === undefined) index = $target.parent(".deleteItem").data("index");
foldAngleSequence.splice(index, 1);
render();
});
$(".durVal").change(function(e){
var $input = $(e.target);
var val = $input.val();
if (isNaN(parseFloat(val))) return;
val = parseFloat(val);
if (val < 0) val = 0;
$input.val(val);
var index = $input.data("index");
foldAngleSequence[index].dur = val;
render();
});
$(".toVal").change(function(e){
var $input = $(e.target);
var val = $input.val();
if (isNaN(parseFloat(val))) return;
val = parseFloat(val);
if (val < -100) val = -100;
if (val > 100) val = 100;
$input.val(val);
var index = $input.data("index");
foldAngleSequence[index].to = val;
render();
});
}
function renderItem(item, index){
var dur = "";
if (item.dur !== null) dur = item.dur;
var to = "";
if (item.to !== null) to = item.to;
return '<li class="animationStep">Animate to ' +
'<input value="' + to + '" placeholder="Target" data-index="'+index+'" class="toVal float form-control" type="text">' +
' % for ' +
'<input value="' + dur + '" placeholder="Duration" data-index="'+index+'" class="durVal float form-control" type="text">' +
' seconds ' +
'<a href="#" data-index="'+index+'" class="red deleteItem"><span data-index="'+index+'" class="fui-cross"></span></a>' +
'</li>';
}
function renderDelay(delay, index){
var dur = "";
if (delay.dur !== null) dur = delay.dur;
return '<li class="animationStep">Wait for ' +
'<input value="' + dur + '" placeholder="Duration" data-index="'+index+'" class="durVal float form-control" type="text">' +
' seconds ' +
'<a href="#" data-index="'+index+'" class="red deleteItem"><span class="fui-cross"></span></a>' +
'</li>';
}
render();
return {
addItem: addItem,
addDelay: addDelay,
isValid: isValid,
compile: compile,
nextFoldAngle: nextFoldAngle
}
}