-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
514 lines (475 loc) · 22.6 KB
/
Copy pathindex.html
File metadata and controls
514 lines (475 loc) · 22.6 KB
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<link rel='icon' href='logo.ico' type='image/x-icon' />
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<meta name="description" content="Animated Charts">
<meta name="keywords" content="Animation, Visualization, Chart">
<meta name="author" content="tge">
<title>Canis Online Editor</title>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<link rel='stylesheet' type='text/css' media='screen' href='css/style.css'>
<script src='js/lib/jquery-3.3.1.min.js'></script>
<script src='js/lib/canis_toolkit.min.js'></script>
<!-- <script src='js/lib/canis_toolkit.js'></script> -->
<script src='js/util/status.js'></script>
<script src='js/util/Util.js'></script>
<!-- <script src="js/lib/lottie.js" type="text/javascript"></script> -->
<script src="js/lib/lottie.min.js" type="text/javascript"></script>
</head>
<body>
<div id='loadingCover' class='loading-mask'>
<div class='loader'></div>
</div>
<div id='statusFooter' class='status-footer'></div>
<div>
<section class='nav-container'>
<div class='menu-bar-left'>
<input type='file' ref='openSVG' id='browsefile' style='display:none' />
<span class='title-span' onClick='window.location.href="https://canisjs.github.io"'>
<span class='cont-text'>
canis
</span>
</span>
<div class='circle-button'>
<span class='tool-button'>
<span id='renderBtn' class='tool-icon play-icon' title='play' />
</span>
</div>
<div class='circle-button'>
<span class='tool-button'>
<span id='openBtn' class='tool-icon open-icon' title='import Canis spec'
onclick='browsefile.click()' />
</span>
</div>
<div class='circle-button'>
<span class='tool-button'>
<span id='exportBtn' class='tool-icon export-icon' title='export' />
</span>
</div>
</div>
<div class='menu-bar-right'>
<div class='circle-button'>
<span class='tool-button'>
<span id='help' class='question-icon tool-icon' title='help' />
</span>
</div>
</div>
</section>
<section class='main-wrapper'>
<div class='left-panel'>
<!-- editor -->
<div class='editor-outter-wrapper'>
<div class='upper-panel'>
<div class='editor-header'>
<div class='select-wrapper'>
<select id='dataSelection' class='spec-selector' title='select example'>
<option value='groupedBar_1'>0. Fruit Sale</option>
<option selected value='mushroom_1'>1. Characteristics of Mushrooms</option>
<option value='gantt_1'>2. Event Planning</option>
<option value='purchases_1'>3. Doughnut Purchases</option>
<option value='os_1'>4. Mobile Operating System Market Share</option>
<option value='ranking_1'>5. Top 10 Best Global Brands Ranking</option>
<option value='drill_1'>6. Ebay Motorcycle Listings</option>
<option value='nightingale_1'>7. Causes of Mortality over Time</option>
<option value='polioTrans_1'>8. Polio Incidence Rates of the United States</option>
<option value='polio_1'>9. Polio Incidence Rates of the United States in 1950s
</option>
<option value='gapminder_1'>10. Trend of Life expectancy with the Development of
Fertility
</option>
<option value='gapminder_2'>11. Trend of Life expectancy with the Changing of GDP
</option>
</select>
</div>
</div>
<div id='specContainer' class='editor-inner-wrapper'></div>
</div>
<div class='resizer v-resizer'>
<div class='resize-bar'></div>
</div>
<div class='below-panel'>
<div class='console-wrapper'>
<div class='window-title'>
<span class='window-title-content'>console</span>
</div>
<div id='consoleContainerId' class='console-container'></div>
</div>
</div>
</div>
</div>
<div class='resizer h-resizer'>
<div class='resize-bar'></div>
</div>
<div class='right-panel'>
<!-- result -->
<div class='result-wrapper'>
<div class='upper-panel'>
<div id='chartWrapper' class='chart-wrapper'>
<div class='window-title'>
<span class='window-title-content'>Chart</span>
<!-- <span id='chartFoldBtn' class='rect-button fold-icon' title='fold visualization chart'></span> -->
</div>
<div id='chartContainer' class='chart-container'></div>
</div>
</div>
<div class='resizer v-resizer'>
<div class='resize-bar'></div>
</div>
<div class='below-panel'>
<div class='window-title'>
<span class='window-title-content'>Result Animation</span>
<!-- <span id='videoFoldBtn' class='rect-button fold-icon' title='fold result animation'></span> -->
</div>
<div id='lottieRender' class='video-container'></div>
</div>
</div>
<div id='controlContainer' class='control-container'>
<div id='timelineContainer' class='timeline-container'>
<div id='timeline' class='timeline'>
<div id='track' class='track'></div>
<div id='pastTime' class='past-time'></div>
<div id='slider' class='video-slider'></div>
</div>
</div>
<div id='btnContainer' class='btn-container'>
<span id='currentTime'>00.00</span><span>/</span><span id='endTime'>00.00</span>
</div>
</div>
</div>
</div>
</section>
</div>
<script src='js/lib/monaco-editor/min/vs/loader.js'></script>
<script>
let editor, status;
window.onload = () => {
$('#loadingCover').css('display', 'none');
[].forEach.call(document.getElementsByClassName('resizer'), r => {
const resizeType = r.className.split(' ')[1];
r.onmousedown = (downEvt) => {
const wrapperBBox = {
width: r.parentElement.offsetWidth,
height: r.parentElement.offsetHeight
}
downEvt.preventDefault();
let downPosi = {
x: downEvt.pageX,
y: downEvt.pageY
}
document.onmousemove = (moveEvt) => {
const movePosi = {
x: moveEvt.pageX,
y: moveEvt.pageY
}
const dis = {
xDiff: movePosi.x - downPosi.x,
yDiff: movePosi.y - downPosi.y
}
if (resizeType === 'h-resizer') {
const disPx = dis.xDiff
const sty = r.previousElementSibling.currentStyle || window.getComputedStyle(r.previousElementSibling, null);
const tmpWidth = parseFloat(sty.width.substring(0, sty.width.length - 2));
r.previousElementSibling.style.width = (tmpWidth + disPx) + 'px';
editor.layout();
} else {
const disPx = dis.yDiff;
const sty = r.previousElementSibling.currentStyle || window.getComputedStyle(r.previousElementSibling, null);
const tmpHeight = parseFloat(sty.height.substring(0, sty.height.length - 2));
r.previousElementSibling.style.height = (tmpHeight + disPx) + 'px';
editor.layout();
}
downPosi = movePosi;
}
document.onmouseup = (upEvt) => {
document.onmousemove = null;
document.onmouseup = null;
}
}
})
}
require.config({
paths: {
'vs': 'js/lib/monaco-editor/min/vs'
}
});
require(['vs/editor/editor.main'], function () {
monaco.languages.registerCompletionItemProvider('json', {
provideCompletionItems: function (model, position) {
const textUntilPosition = model.getValueInRange({
startLineNumber: 1,
startColumn: 1,
endLineNumber: position.lineNumber,
endColumn: position.column
});
const {
match,
autocompleteType
} = Util.checkAutoComplete(textUntilPosition);
if (!match) {
return {
suggestions: []
};
}
const word = model.getWordUntilPosition(position);
const range = {
startLineNumber: position.lineNumber,
endLineNumber: position.lineNumber,
startColumn: word.startColumn,
endColumn: word.endColumn
};
return {
suggestions: Util.autoComplete(range, autocompleteType)
};
}
});
editorValue = '';
editor = monaco.editor.create(document.getElementById('specContainer'), {
value: '',
language: 'json',
tabSize: 2,
scrollBeyondLastLine: false,
quickSuggestions: {
strings: true
}
});
status = new Status('consoleContainerId', editor);
$(window).resize(() => {
editor.layout();
})
let dataName = '';
//play or stop animation
let playing = false;
//current time
let time = 0;
const zeroTime = '00.00';
let lottieAni;
let gif = null;
$(function () {
dataName = 'mushroom', specIdx = 1;
//check whether the user came from the project page
let thisURL = document.URL;
let getval = thisURL.split('?')[1];
if (typeof getval !== 'undefined') {
let exmp = getval.split("=")[1].split('_');
dataName = exmp[0];
specIdx = exmp[1];
$("#dataSelection option[value='" + exmp[0] + "_" + exmp[1] + "']").attr("selected", true);
}
//use the existing json to initialize the editor
loadJson(dataName, specIdx);
})
$('#browsefile').change((e) => {
let file = $(e.target).prop('files')[0];
let reader = new FileReader();
let that = this;
reader.onloadend = function (evt) {
if (evt.target.readyState == FileReader.DONE) {
let result = evt.target.result;
updateEditorAndLoadAni(JSON.parse(result));
}
};
reader.readAsText(file, 'utf-8');
})
function loadJson(dataName, animationIdx, callback) {
$('#loadingCover').css('display', 'block');
$.getJSON('specs/' + dataName + animationIdx + '.ac', function (data) {
updateEditorAndLoadAni(data);
}).fail(function (jqxhr, textStatus, error) {
const err = textStatus + ", " + error;
console.log("Request Failed: " + err);
})
}
async function updateEditorAndLoadAni(data) {
let jsonStr = JSON.stringify(data, null, 2);
editor.setValue(jsonStr);
// editor.setSelection(new monaco.Selection(1, 2, 2, 10));
// editor.focus();
editorValue = jsonStr;
let canisSpec = await canis.renderSpec(data, status, loadEnd);
//render lottie
lottie.destroy();
lottieAni = lottie.loadAnimation({
container: document.getElementById('lottieRender'),
renderer: 'svg',
loop: false,
autoplay: false,
animationData: canisSpec // the animation data
});
}
function loadEnd() {
$('#loadingCover').css('display', 'none');
}
//data selection
$('#dataSelection').change(function () {
let selection = $(this).children('option:selected').val().split('_');
dataName = selection[0];
let animationIdx = selection[1];
loadJson(dataName, animationIdx);
pauseAnimation();
resetPlayerWidgets();
canis.reset();
})
// $('#chartFoldBtn').click((e) => {
// if ($(e.target).hasClass('fold-icon')) {
// $(e.target).removeClass('fold-icon');
// $(e.target).addClass('unfold-icon');
// $(e.target).attr('title', 'unfold visualization chart');
// $('#chartContainer').css('display', 'none');
// $('#videoWrapper').css('flex', 300 + ' 1 0%');
// } else {
// $(e.target).addClass('fold-icon');
// $(e.target).removeClass('unfold-icon');
// $(e.target).attr('title', 'fold visualization chart');
// $('#videoWrapper').css('flex', 1 + ' 1 0%');
// $('#chartContainer').css('display', '');
// }
// })
// $('#videoFoldBtn').click((e) => {
// if ($(e.target).hasClass('fold-icon')) {
// $(e.target).removeClass('fold-icon');
// $(e.target).addClass('unfold-icon');
// $(e.target).attr('title', 'unfold result animation');
// $('#lottieRender').css('display', 'none');
// $('#chartWrapper').css('flex', 300 + ' 1 0%');
// $('#controlContainer').css('display', 'none');
// } else {
// $(e.target).addClass('fold-icon');
// $(e.target).removeClass('unfold-icon');
// $(e.target).attr('title', 'fold result animation');
// $('#chartWrapper').css('flex', 1 + ' 1 0%');
// $('#lottieRender').css('display', '');
// $('#controlContainer').css('display', '');
// }
// })
//render function
$('#renderBtn').click(async function () {
const currentValue = editor.getValue();
let charIdx = 0, lineNum = 1, colNum = 1;
status.specWithoutSpace = currentValue.replace(/[\s\r\n]/g, '');
status.charCheckMap.clear();
for (let i = 0, len = currentValue.length, tmpChar; i < len | (tmpChar = currentValue.charAt(i)); i++) {
let isNul = tmpChar.match(/[\s\r\n]/) ? true : false;
let isEnter = tmpChar.match(/[\r\n]/) ? true : false;
if (!isNul) {//this char is not space or \n
status.charCheckMap.set(charIdx, [lineNum, colNum]);
charIdx++;
}
if (isEnter) {
lineNum++;
colNum = 1;
} else {
colNum++;
}
}
if (playing) {
pauseAnimation();
} else if (editorValue === currentValue) {
playAnimation();
} else {
editorValue = currentValue;
//reset vars
resetPlayerWidgets();
canis.reset();
let canisSpec = await canis.renderSpec(JSON.parse(currentValue), status, loadEnd);
//render lottie
lottie.destroy();
lottieAni = lottie.loadAnimation({
container: document.getElementById('lottieRender'),
renderer: 'svg',
loop: false,
autoplay: false,
// path: 'like.json' // the path to the animation json
animationData: canisSpec, // the animation data
});
playAnimation();
}
})
//timeline slider
$('#slider').mousedown(function () {
pauseAnimation();
$(document).mousemove(function (moveEvt) {
let targetX = moveEvt.pageX - $('#track').offset().left;
if (targetX >= 0 && targetX <= $('#track').width()) {
$('#slider').css('margin-left', targetX - 8 + 'px');
$('#pastTime').width(targetX);
let ratio = targetX / $('#track').width();
time = parseInt(canis.duration() * ratio / (1000 / canis.frameRate)) * (1000 / canis.frameRate);
$('#currentTime').text(Util.formatTime(time));
currentFrame = Math.ceil(time / (1000 / canis.frameRate));
lottie.goToAndStop(currentFrame, true);
}
})
$(document).mouseup(function (moveEvt) {
$(document).unbind('mousemove');
$(document).unbind('mouseup');
})
})
$('#track, #pastTime').click(function (downEvt) {
pauseAnimation();
let targetX = downEvt.pageX - $('#track').offset().left;
$('#slider').css('margin-left', targetX - 8 + 'px');
$('#pastTime').width(targetX);
let ratio = targetX / $('#track').width();
time = parseInt(canis.duration() * ratio / (1000 / canis.frameRate)) * (1000 / canis.frameRate);
$('#currentTime').text(Util.formatTime(time));
currentFrame = Math.ceil(time / (1000 / canis.frameRate));
lottieAni.goToAndStop(currentFrame, true);
})
let animationInterval;
function playAnimation() {
if ($('#endTime').text() === zeroTime) {
$('#endTime').text(Util.formatTime(canis.duration()));
}
if (time === 0) {
lottieAni.stop();
}
$('#renderBtn').attr('title', 'pause');
$('#renderBtn').removeClass('play-icon');
$('#renderBtn').addClass('stop-icon');
playing = true;
animationInterval = setInterval(() => {
let ratio = time / canis.duration();
$('#pastTime').width($('#track').width() * ratio);
$('#slider').css('margin-left', $('#track').width() * ratio - 8 + 'px');
$('#currentTime').text(Util.formatTime(time));
time += 1000 / canis.frameRate;
if (time > canis.duration()) {
pauseAnimation();
time = 0;
}
}, 1000 / canis.frameRate);
lottieAni.play();
}
function pauseAnimation() {
$('#renderBtn').attr('title', 'play');
$('#renderBtn').removeClass('stop-icon');
$('#renderBtn').addClass('play-icon');
playing = false;
lottieAni.pause();
clearInterval(animationInterval);
animationInterval = 'undefined';
}
$('#exportBtn').click(function () {
const lottieJSON = canis.exportJSON();
const outputFileName = dataName + '.json';
Util.saveJSON(JSON.parse(lottieJSON), outputFileName);
});
/**
* @param {boolean} endTime: whether to reset endtime or not
*/
function resetPlayerWidgets(endTime = true) {
time = 0;
$('#slider').css('margin-left', '-8px');
$('#pastTime').width(0);
$('#currentTime').text(zeroTime);
if (endTime) {
$('#endTime').text(zeroTime);
}
}
$('#help').click(() => window.location.href = 'https://canisjs.github.io/tutorial.html');
});
</script>
</body>
</html>