-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
138 lines (96 loc) · 3.56 KB
/
script.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
function rotatePivot(input) {
var el = document.querySelector('.control ' + input.dataset.pivot);
el.style.transform = 'rotate(' + input.value + 'deg)';
}
var animation = {};
animation.frames = [];
var selectedFrame;
var selectedFrameNumber;
function deleteFrame() {
if (selectedFrameNumber === undefined)
return;
animation.frames.splice(selectedFrameNumber, 1);
selectedFrame = undefined;
selectedFrameNumber = selectedFrameNumber == animation.frames.length ? selectedFrameNumber - 1 : selectedFrameNumber;
if (animation.frames.length > 0) {
selectFrame(animation.frames[selectedFrameNumber], selectedFrameNumber);
saveAnimation();
}
showAllKeys();
}
function addFrame() {
var obj = {};
obj.keys = {};
obj.percent = document.querySelector('input[name="persent"]').value.split(',').join('.');
[].forEach.call(document.querySelectorAll('input[data-pivot]'), function(item) {
obj.keys[item.dataset.pivot] = item.value;
});
if (!selectedFrame)
animation.frames.push(obj);
else
animation.frames[selectedFrameNumber] = obj;
selectedFrame = undefined;
document.querySelector('button[onclick="addFrame()"]').innerHTML = 'Add cadr';
showAllKeys();
saveAnimation();
}
function focusOnInput(event) {
if (event.target.tagName == 'INPUT')
event.target.focus();
}
function saveAnimation() {
animation.name = document.querySelector('input[name="name"]').value;
animation.duration = document.querySelector('input[name="duration"]').value;
document.getElementById('css').innerHTML = createAnimation(animation);
document.querySelector('.preview').className = 'preview ' + animation.name;
document.getElementById('preview-style').innerHTML = createAnimation(animation, '.preview');
}
function selectFrame(anim, i) {
[].forEach.call(document.querySelectorAll('input[data-pivot]'), function(item) {
item.value = anim.keys[item.dataset.pivot];
rotatePivot(item);
});
selectedFrame = anim;
selectedFrameNumber = i;
document.querySelector('button[onclick="addFrame()"]').innerHTML = 'Save cadr';
document.querySelector('input[name="persent"]').value = anim.percent;
};
function showAllKeys() {
document.getElementById('keys').innerHTML = '';
animation.frames.sort(function(a, b) {
return a.percent - b.percent;
});
animation.frames.forEach(function(item, i) {
var k = document.createElement('button');
k.innerHTML = i;
k.addEventListener('click', (function(i) { var item = this; return function() { selectFrame(item, i) } }).call(item, i));
document.getElementById('keys').appendChild(k);
});
};
function createAnimation(obj, prefix) {
var strings = [];
if (!prefix)
prefix = '';
var duration = obj.duration;
var animationName = obj.name;
for (let i = 0; i < obj.frames.length; i++) {
let j = 0;
let percent = obj.frames[i].percent;
for (key in obj.frames[i].keys) {
let strAttrAnim = key.split(/[\.:\(\)]/).join('');
if (!strings[j]) {
strings[j] = ' ';
strings[j] += '@keyframes ' + animationName + strAttrAnim + '{';
}
if (obj.frames[i].keys[key] === null || obj.frames[i].keys[key] === undefined || obj.frames[i].keys[key].trim() === '') {
obj.frames[i].keys[key] = 0;
}
strings[j] += percent + '% { transform: rotate(' + obj.frames[i].keys[key] + 'deg);}';
if (i + 1 == obj.frames.length) {
strings[j] += '}' + prefix + '.' + animationName + ' ' + key + '{ animation: ' + animationName + strAttrAnim + ' ' + duration + 's infinite; }';
}
j++;
}
}
return strings.join(' ');
}