-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.js
310 lines (261 loc) · 11.7 KB
/
main.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
var relWidth = 80 / document.querySelector('.canvas').clientWidth;
var relHeight = 80 / document.querySelector('.canvas').clientHeight;
window.onresize = function() {
relWidth = 80 / document.querySelector('.canvas').clientWidth;
relHeight = 80 / document.querySelector('.canvas').clientHeight;
}
function moveElem(e) {
window.onbeforeunload = function() {
return 'Close window?';
};
if (e.type == 'touchmove') {
e.clientX = e.touches[0].clientX;
e.clientY = e.touches[0].clientY;
}
var currentX = e.clientX - 55;
var currentY = e.clientY - 105;
document.querySelector('.draggable').style.left = currentX + 'px';
document.querySelector('.draggable').style.top = currentY + 'px';
x[currentIndex] = currentX;
y[currentIndex] = currentY;
document.querySelectorAll('.frame')[currentIndex].children[0].children[0].style.left = relWidth * currentX + 'px';
document.querySelectorAll('.frame')[currentIndex].children[0].children[0].style.top = relHeight * currentY + 'px';
}
var x = [20];
var y = [20];
var currentIndex = 0;
function openFrame(index) {
currentIndex = index;
document.querySelector('.draggable').style.left = x[currentIndex] + 'px';
document.querySelector('.draggable').style.top = y[currentIndex] + 'px';
if (!document.querySelector('.play').classList.contains('playing')) {
document.querySelector('.draggable').style.transition = '.2s';
window.setTimeout(function() {
document.querySelector('.draggable').style.transition = '';
}, 200);
}
document.querySelectorAll('.frame').forEach(el => {
el.classList.remove('open');
})
document.querySelectorAll('.frame')[currentIndex].classList.add('open');
}
function create(htmlStr) {
var frag = document.createDocumentFragment(),
temp = document.createElement('div');
temp.innerHTML = htmlStr;
while (temp.firstChild) {
frag.appendChild(temp.firstChild);
}
return frag;
}
function insertAfter(referenceNode, newNode) {
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}
function playFrame() {
if (settings.replay) {
document.querySelector('.draggable').style.transition = settings.delay + 's linear';
if (currentIndex == x.length - 1) {
currentIndex = -1;
}
if (document.querySelector('.play').classList.contains('playing')) {
window.setTimeout(function() {
openFrame(currentIndex + 1);
playFrame();
}, settings.delay * 1000)
} else {
window.setTimeout(function() {
document.querySelector('.draggable').style.transition = '';
openFrame(startFrame);
}, settings.delay * 1000)
}
} else {
document.querySelector('.draggable').style.transition = settings.delay + 's linear';
if (currentIndex < x.length - 1) {
if (document.querySelector('.play').classList.contains('playing')) {
window.setTimeout(function() {
openFrame(currentIndex + 1);
playFrame();
}, settings.delay * 1000)
} else {
window.setTimeout(function() {
document.querySelector('.draggable').style.transition = '';
openFrame(startFrame);
}, settings.delay * 1000)
}
} else {
window.setTimeout(function() {
document.querySelector('.draggable').style.transition = '';
document.querySelector('.play').classList.remove('playing');
}, settings.delay * 1000)
}
}
}
function exportAnimation() {
var out = `.` + settings.name.replaceAll(' ', '-') + ` {
animation: ` + settings.name.replaceAll(' ', '-') + ` ` + settings.delay * x.length + `s linear forwards;
position: absolute;
}
@keyframes ` + settings.name.replaceAll(' ', '-') + ` {`;
for (var i = 0; i < x.length; i++) {
var percent = x.length > 1 ? (Math.round((100 / (x.length - 1) * i + Number.EPSILON) * 100) / 100) + `%` : `to`;
out += `
` + percent + ` {
top: ` + y[i] + `px;
left: ` + x[i] + `px;
}`;
}
return (out + `
}`);
}
function deleteAnimation() {
document.querySelector('.sidebar').innerHTML = '<div class="frame open" onclick="openFrame(0)"><div class="frame-wrapper"><div class="frame-el"></div></div>';
settings.name = 'Untitled Animation';
document.querySelector('.logo span').innerHTML = settings.name;
x = [20];
y = [20];
openFrame(0);
}
function importAnimation() {
deleteAnimation();
var code = document.querySelector('.main .code').innerText;
document.querySelector('.main .code').innerText = '';
var notabs = code.replaceAll(/ /g, '');
var lineArray = notabs.match(/[^\r\n]+/g);
settings.name = lineArray[0].replace('@keyframes ', '').replace(' {', '').replaceAll('-', ' ');
document.querySelector('.logo span').innerHTML = settings.name;
for (var i = 0; i < lineArray.length; i++) {
lineArray[i] = lineArray[i].replace(/\s/g, '');
}
var topPos = [];
var leftPos = [];
for (var i = 0; i < lineArray.length; i++) {
var property = lineArray[i].substr(0, lineArray[i].indexOf(';'));
if (property != '') {
if (property.startsWith('top:')) {
topPos.push(Number(property.replace('top:', '').replace('px', '')));
}
if (property.startsWith('left:')) {
leftPos.push(Number(property.replace('left:', '').replace('px', '')));
}
}
}
x = leftPos;
y = topPos;
document.querySelector('.sidebar').innerHTML = '';
for (var i = 0; i < x.length; i++) {
document.querySelector('.sidebar').innerHTML += '<div class="frame"><div class="frame-wrapper"><div class="frame-el"></div></div></div>';
document.querySelectorAll('.frame')[i].children[0].children[0].style.left = relWidth * x[i] + 'px';
document.querySelectorAll('.frame')[i].children[0].children[0].style.top = relHeight * y[i] + 'px';
}
document.querySelectorAll('.frame').forEach(function(el, i) {
el.addEventListener('click', e => {
openFrame(i);
})
})
openFrame(0);
document.querySelector('.main').classList.add('hidden');
document.querySelector('.upload-wrapper').classList.add('hidden');
document.querySelector('.title-wrapper').classList.remove('hidden');
}
function copy(text) {
var textArea = document.createElement("textarea");
textArea.value = text;
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
document.execCommand('copy');
document.body.removeChild(textArea);
}
function select(node) {
node.focus();
if (document.body.createTextRange) {
const range = document.body.createTextRange();
range.moveToElementText(node);
range.select();
} else if (window.getSelection) {
const selection = window.getSelection();
const range = document.createRange();
range.selectNodeContents(node);
selection.removeAllRanges();
selection.addRange(range);
}
}
document.querySelector('.add').addEventListener('click', e => {
currentIndex += 1;
x.splice(currentIndex, 0, x[currentIndex - 1]);
y.splice(currentIndex, 0, y[currentIndex - 1]);
var frame = create('<div class="frame open" style="opacity:0" onclick="openFrame(' + currentIndex + ')"><div class="frame-wrapper"><div class="frame-el"></div></div></div>');
insertAfter(document.querySelector('.sidebar').children[currentIndex - 1], frame);
window.setTimeout(function() {
document.querySelectorAll('.frame')[currentIndex].style.opacity = 1;
document.querySelectorAll('.frame')[currentIndex].scrollIntoView({
behavior: "smooth"
});
document.querySelectorAll('.frame')[currentIndex].children[0].children[0].style.left = relWidth * x[currentIndex - 1] + 'px';
document.querySelectorAll('.frame')[currentIndex].children[0].children[0].style.top = relHeight * y[currentIndex - 1] + 'px';
openFrame(currentIndex);
}, 0);
})
document.querySelector('.remove').addEventListener('click', e => {
if (currentIndex != 0) {
x.splice(currentIndex, 1);
y.splice(currentIndex, 1);
openFrame(currentIndex - 1);
document.querySelectorAll('.frame')[currentIndex + 1].remove();
document.querySelectorAll('.frame')[currentIndex].scrollIntoView({
behavior: "smooth"
});
}
})
var startFrame = 0;
document.querySelector('.play').addEventListener('click', e => {
document.querySelector('.play').classList.toggle('playing');
if (document.querySelector('.play').classList.contains('playing')) {
if (currentIndex == x.length - 1) {
currentIndex = -1;
}
startFrame = currentIndex;
playFrame();
}
})
document.querySelector('.save').addEventListener('click', e => {
window.onbeforeunload = null;
document.querySelector('.prompt').innerHTML = '<div class="title"> <p>Export</p> <svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 0 24 24" width="24" class="close" onclick="document.querySelector(\'.prompt-wrapper\').classList.toggle(\'open\')"><path d="M0 0h24v24H0z" fill="none"></path><path d="M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z" fill="currentColor"></path></svg> </div> <a>Place this code somewhere in your website for a fully fledged animation.</a> <br> <span>HTML (or any element with class <span class="code name" style="padding: 2px">---</span>)</span> <div class="code" onclick="copy(this.innerText);this.classList.toggle(\'copy\')"><svg xmlns="http://www.w3.org/2000/svg" height="20" viewBox="0 0 24 24" width="20" class="copy"><path d="M0 0h24v24H0z" fill="none"></path><path d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z" fill="currentColor"></path></svg><div class="<span class="name">---</span>"></div></div> <span>CSS</span> <div class="code export" onclick="copy(this.innerText);this.classList.toggle(\'copy\')"><svg xmlns="http://www.w3.org/2000/svg" height="20" viewBox="0 0 24 24" width="20" class="copy"><path d="M0 0h24v24H0z" fill="none"></path><path d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z" fill="currentColor"></path></svg></div> </div>'
document.querySelector('.prompt-wrapper').classList.add('open');
document.querySelectorAll('.name').forEach(name => {
name.innerHTML = settings.name.replaceAll(' ', '-');
});
document.querySelector('.export').innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" height="20" viewBox="0 0 24 24" width="20" class="copy"><path d="M0 0h24v24H0z" fill="none"></path><path d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z" fill="currentColor"></path></svg>' + exportAnimation();
})
document.querySelector('.new').addEventListener('click', e => {
document.querySelector('.prompt').innerHTML = '<div class="content-wrapper"> <div class="title"> <p>Are you sure?</p> </div> <a>This will delete the current animation.</a><div class="buttons"><div class="button second" onclick="document.querySelector(\'.prompt-wrapper\').classList.toggle(\'open\')">No</div><div class="button" onclick="deleteAnimation();document.querySelector(\'.prompt-wrapper\').classList.toggle(\'open\')">Yes</div></div></div>';
document.querySelector('.prompt-wrapper').classList.add('open');
})
var settings = {
name: 'Untitled Animation',
delay: 0.25,
replay: false
};
var down = false;
document.querySelector('.canvas').addEventListener('mousedown', e => {
down = true
});
document.querySelector('.canvas').addEventListener('mouseup', e => {
down = false
});
document.querySelector('.canvas').addEventListener('mousemove', e => {
if (down) {
moveElem(e)
}
});
document.querySelector('.canvas').addEventListener('touchstart', e => {
down = true
});
document.querySelector('.canvas').addEventListener('touchend', e => {
down = false
});
document.querySelector('.canvas').addEventListener('touchmove', e => {
if (down) {
moveElem(e)
}
});