-
Notifications
You must be signed in to change notification settings - Fork 4
/
WebVfx.js
1444 lines (1271 loc) · 61.3 KB
/
WebVfx.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
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
=====================================================================
Elusien's WebVfx framework for Shotcut (http://elusien.co.uk/shotcut)
=====================================================================
This framework enables Shotcut HTML Overlay filters to be developed quickly using a modern browser,
with all its development tools (e.g. using function key F12) at your disposal. Shotcut does not have any
such tools, other than a basic console.log, and in many cases of error you just end up with a blank screen.
You can style the HTML elements as normal using CSS then modify the properties you want to animate
using this framework.
When you apply this filter to a clip in Shotcut, you need to tick the box that says
'Use WebVfx javascript extension' and confirm that you know how to use it.
There are 3 parts to this framework two of which are exposed to the user via HTML tag parameters:
1) Animation effects:
This enables you to animate CSS properties for any of the HTML elements in the HTML Overlay filter.
It also enables the use of "keyframes" for fine control of the animation.
Format:
class='webvfx'
This identifies the HTML element as one that is under the control of the framework.
data-control='<duration>:<fps>'
This is only used for the animation in the browser, not in shotcut. It informs the browser
as to how many seconds to run the animation and what the "frames per second" is. It is not
required by Shotcut as the length is determined by the length of the clip to which it is
applied, as is the 'fps'.
Default: data-control='8:30'
data-animate='<animation structure>'
This provides the animation parameters to both the browser and Shotcut. It is in a form
similar to what would be supplied to CSS to do the animation. All the parameters apart from
the "0%" and the "100%" are optional. Times are "normalised times", where 0.0 means the
first fram of the clip and 1.0 means the last frame of the clip. For a list of the easings
see the "Easing_Funs()" function below.
These parameters are:
start: <time to start the animation> Default: start:0.0
end: <time to end the animation> Default: end:1.0
ease: <an "easing" to describe how the property changes during the duration of the effect>
Default: ease: "linearTween"
0%: <structure detailing the CSS property/properties to animate and their initial value(s)>
100%: <structure detailing THE SAME CSS property/properties and their final value(s)>
The 0% and 100% are "keyframes" that specify the values at the start and end times. Other keyframes
can be used to specify the values at other specific times. e.g "50%" for the values halfway through
or "25%" for the value a quarter of the way into the animation. These values donot have to be whole
numbers, e.g. you could have "16.125%". These are best illustrated by examples:
a) Fadeout an element:
<div class='webvfx' data-animate='{0%: {opacity: 1;}, 100%: {opacity: 0%;}}'>
FADING TEXT
</div>
b) Fadeout an element, then fade it back in:
<div class='webvfx' data-animate='{0%: {opacity: 1;}, 50%: {opacity: 0%;}, 100%: {opacity: 1;}}'>
TEXT FADING OUT THEN BACK IN
</div>
c) Change an element's colour, move it around, change it from a square shape to a circle.
<div class='webvfx' data-animate=
'{start: 0.2, end: 1.0, ease: "easeOutSine",
0%: {backgroundColor: #f00; left: 0px; top: 0px; borderRadius: 0%;},
25%: {backgroundColor: #00f; left:100px; top: 0px; borderRadius: 50%;},
50%: {backgroundColor: #0f0; left:200px; top:200px; borderRadius: 0%;},
75%: {backgroundColor: #f0f; left: 0px; top:200px; borderRadius: 50%;},
100%: {backgroundColor: #ff0; left:100px; top: 0px; borderRadius: 0%;}
}'>
HI
</div>
common mistakes:
Forgetting that the "ease" function name is case-sensitive;
Forgetting to put a semi-colon (;) after the last CSS property value in a keyframe;
Adding a comma (,) after the last keyframe;
Not having the same property names in each keyframe;
Not using the "camelCase" names for properties
(e.g. you must use "borderRadius", NOT "border-radius");
Using some of the more recent CSS constructs
(e.g. transform-style: preserve-3d;)
Some CSS properties have to be the "webkit" version
(e.g. you must use "webkitTransform", NOT "transform"
2) Stopwatch effects:
This enables you to have 1 or more stopwatches in the HTML Overlay filter. It also enables the
use of "keyframes" for fine control of the stopwatches.
A stopwatch consists of 4 elements specified using HTML '<span></span>' tags.
<span> number 1 is the frame number;
<span> number 2 is the hour number;
<span> number 3 is the minute number;
<span> number 4 is the millisecond number
Normally you would initialise each of the <span> elements by placing a zero (0) in it. Placing any
other number will initialise it to that value. Leaving the <span> empty will hide it (see examples).
Format:
class='webvfx'
This identifies the HTML element as one that is under the control of the framework.
data-control='<duration>:<fps>'
This is only used for the stopwatch in the browser, not in shotcut. It informs the browser
as to how many seconds to run the stopwatch and what the "frames per second" is. It is not
required by Shotcut as the length is determined by the length of the clip to which it is
applied, as is the 'fps'.
Default: data-control='8:30'
data-animate='<stopwatch structure>'
This provides the stopwatch parameters to both the browser and Shotcut.
All the parameters are optional. Times are "normalised times", where 0.0 means the
first fram of the clip and 1.0 means the last frame of the clip.
These parameters are:
start: <time to start the animation> Default: start:0.0
end: <time to end the animation> Default: end:1.0
Keyframes can be used to specify when to pause and resume the stopwatch. e.g "50%" for halfway
through or "25%" for a quarter of the way into the animation. These values do not have to be whole
numbers, e.g. you could have "16.125%". If you "pause" the stopwatch, then the next keyframe would
"resume" it. When you resume it you have the option of:
"normal" (the default) at which point the times will start up where they left off;
"skip" at which point the times will skip to the value they would have had if the stopwatch
had not been paused;
<nnn> a number representing a time in milliseconds to at which the times will start
(eg resume: 0 will resume the stopwatch times from the beginning,
resume: 50000 will resume the stopwatch times from 50 seconds).
These are best illustrated by examples:
a) Stopwatch showing frame-number, minutes and seconds:
<div class="webvfx" data-stopwatch=''>
Frame <span>0</span> => <span></span><span>00</span>m <span>00</span>s <span></span>
</div>
b) Stopwatch showing minutes and seconds, running for 120 seconds with various pauses and resumes:
<div class="webvfx" data-control='120:24' data-stopwatch=
'{10%: {pause;},
20%: {resume;},
40%: {pause;},
60%: {resume: skip;},
80%: {pause;},
90%: {resume: 60000;}
}'>
<span></span></span><span></span><span>00</span>m <span>00</span>s <span></span>
</div>
3) User-supplied effects:
This enables you to provide your own javascript function to do something to the HTML. This function
will be called for each frame with the parameters:
time: the "normalised time" of this frame (0.0 to 1.0);
frame_number: the number of this frame
frame_rate : the frame-rate in frames per second
To do this you need to create the javascript function, then add it to a GLOBAL-scope array called
"webvfx_add_to_frame" e.g.
<script>
function flash(time, frame_number, frame_rate) {
bulb = document.getElementById("bulb");
bulb.style.opacity = ((frame_number % 10) < 5) ? 0.1 : 1.0;
}
webvfx_add_to_frame = [flash];
<script>
A COMPLETE EXAMPLE
==================
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link href="https://fonts.googleapis.com/css?family=Audiowide|Iceland" rel="stylesheet">
<!-- WebVfx does not recognise Google web fonts in "<link>", so make sure you also have the font downloaded on your computer. -->
<!-- WebVfx does not recognise some recent CSS constructs, try using the -webkit- prefix for these -->
<title>WebVfx Example</title>
<style>
body * {
color:#888; font-family: 'Iceland', monospace; font-size: 30px;
}
#wanderer {
width: 150px; height: 150px;
background-color: #f00;
position: relative;
z-index: -1;
}
[data-stopwatch]>span {
display: inline-block;
border: solid 1px #f00;
padding: 2px; margin: 2px 0;
}
#middle {
position: absolute;
top: 50%; left: 50%;
transform: translate(-50%, -50%);
-webkit-transform: translate(-50%, -50%0);
}
#bulb {
width: 2em; height: 2em; margin: auto;
background-color: #f00;
border-radius: 50%;
-webkit-border-radius: 50%;
}
</style>
</head>
<body>
<div id='wanderer' class='webvfx' data-control='20:30'
data-animate='{start: 0.1, end: 1.0, ease: "easeOutSine",
0%: {backgroundColor: #f00; left: 0px; top: 0px; borderRadius: 0%;},
25%: {backgroundColor: #00f; left:100px; top: 0px; borderRadius: 50%;},
50%: {backgroundColor: #0f0; left:200px; top:200px; borderRadius: 0%;},
75%: {backgroundColor: #f0f; left: 0px; top:200px; borderRadius: 50%;},
100%: {backgroundColor: #ff0; left:100px; top: 0px; borderRadius: 0%;}
}'>
</div>
<div class='webvfx'
data-stopwatch='{start: 0.2, end: 0.8,
10%: {pause;},
20%: {resume;},
40%: {pause;},
60%: {resume: skip;},
80%: {pause;},
90%: {resume: 100000;}
}'>
Frame <span>0</span> => </span><span>00</span>h <span>00</span>m <span>00</span>s <span>000</span>ms
</div>
<div class='webvfx' data-stopwatch=''>
<span>0</span> => <span></span><span>00</span>:<span>00</span>.<span>000</span>
</div>
<div id='middle' class="webvfx" data-stopwatch=''>
<span>9000</span> => <span></span><span>08</span>:<span>10</span><span></span>
</div>
<p> Assuming a framerate of 30fps. </p>
<div id='bulb'> </div>
</body>
<script>
webvfx_add_to_frame = [flash];
function flash(time, frame_number, frame_rate) {
bulb = document.getElementById("bulb");
if ((frame_number % 10) < 5) {
bulb.style.opacity = 0.1;
} else {
bulb.style.opacity = 1.0
}
}
</script>
<script src="WebVfx.js"></script>
</html>
* The MIT License (MIT)
*
* Copyright (c) 2018 Elusien Entertainment
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @version 1.0
*/
var Elusien = {
iter : 0,
niters : 0,
delay : 0,
frame_number: 0,
frame_length: 0
};
function Easing_Funs(){
/*
USERS SHOULD NOT MODIFY THIS FUNCTION
#####################################
Easing Equations v1.3, Oct. 29, 2002, Open source under the BSD License.
Copyright © 2001-2002 Robert Penner. All rights reserved.
These tweening functions provide different flavors of math-based motion under a consistent API.
Math.easingType(t,b,c,d)
t: current time,
b: beginning value (i.e value at time_start),
c: change in value (end_value - b), can be negative
d: duration (i.e. time_end - time_start)
Type of easing HTML data-easing value Description
-------------- ---------------------- -----------
Linear "linearTween" linear tweening - no easing is performed
Quadratic "easeInQuad" t^2 acceleration from zero velocity
"easeOutQuad" t^2 deceleration to zero velocity
"easeInOutQuad" t^2 acceleration until halfway, then deceleration
Cubic "easeInCubic" t^3 acceleration from zero velocity
"easeOutCubic" t^3 deceleration to zero velocity
"easeInOutCubic" t^3 acceleration until halfway, then deceleration
Quartic "easeInQuartic" t^4 acceleration from zero velocity
"easeOutQuartic" t^4 deceleration to zero velocity
"easeInOutQuartic" t^4 acceleration until halfway, then deceleration
Quintic "easeInQuintic" t^5 acceleration from zero velocity
"easeOutQuintic" t^5 deceleration to zero velocity
"easeInOutQuintic" t^5 acceleration until halfway, then deceleration
Sinusoidal "easeInSine" sinusoidal acceleration from zero velocity
"easeOutSine" sinusoidal deceleration to zero velocity
"easeInOutSine" sinusoidal acceleration until halfway, then deceleration
Exponential "easeInExpo" exponential acceleration from zero velocity
"easeOutExpo" exponential deceleration to zero velocity
"easeInOutExpo" exponential acceleration until halfway, then deceleration
Circular "easeInCirc" circular acceleration from zero velocity
"easeOutCirc" circular deceleration to zero velocity
"easeInOutCirc" circular acceleration until halfway, then deceleration
Bounce "easeInBounce" elastic bounce, then acceleration from zero velocity
"easeOutBounce" elastic deceleration to zero velocity, then bounce
"easeInOutBounce" elastic bounce, then acceleration until halfway, then deceleration, then bounce
Changes:
1.3 - tweaked the exponential easing functions to make endpoints exact
1.2 - inline optimizations (changing t and multiplying in one step)--thanks to Tatsuo Kato for the idea
Discussed in Chapter 7 of Robert Penner's Programming Macromedia Flash MX (including graphs of the easing equations)
http://www.robertpenner.com/profmx
http://www.amazon.com/exec/obidos/ASIN/0072223561/robertpennerc-20
*/
this.linearTween = function (t, b, c, d) {return c*t/d + b;};
this.easeInQuad = function (t, b, c, d) {return c*(t/=d)*t + b;};
this.easeOutQuad = function (t, b, c, d) {return -c *(t/=d)*(t-2) + b;};
this.easeInOutQuad = function (t, b, c, d) {return ((t/=d/2) < 1) ? c/2*t*t + b : -c/2*((--t)*(t-2) - 1) + b;};
this.easeInCubic = function (t, b, c, d) {return c*(t/=d)*t*t + b;};
this.easeOutCubic = function (t, b, c, d) {return c*((t=t/d-1)*t*t + 1) + b;};
this.easeInOutCubic = function (t, b, c, d) {return ((t/=d/2) < 1) ? c/2*t*t*t + b : c/2*((t-=2)*t*t + 2) + b;};
this.easeInQuart = function (t, b, c, d) {return c*(t/=d)*t*t*t + b;};
this.easeOutQuart = function (t, b, c, d) {return -c * ((t=t/d-1)*t*t*t - 1) + b;};
this.easeInOutQuart = function (t, b, c, d) {return ((t/=d/2) < 1) ? c/2*t*t*t*t + b : -c/2*((t-=2)*t*t*t - 2) + b;};
this.easeInQuint = function (t, b, c, d) {return c*(t/=d)*t*t*t*t + b;};
this.easeOutQuint = function (t, b, c, d) {return c*((t=t/d-1)*t*t*t*t + 1) + b;};
this.easeInOutQuint = function (t, b, c, d) {return ((t/=d/2) < 1) ? c/2*t*t*t*t*t + b : c/2*((t-=2)*t*t*t*t + 2) + b;};
this.easeInSine = function (t, b, c, d) {return -c * Math.cos(t/d * (Math.PI/2)) + c + b;};
this.easeOutSine = function (t, b, c, d) {return c * Math.sin(t/d * (Math.PI/2)) + b;};
this.easeInOutSine = function (t, b, c, d) {return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;};
this.easeInExpo = function (t, b, c, d) {return (t===0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;};
this.easeOutExpo = function (t, b, c, d) {return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;};
this.easeInOutExpo = function (t, b, c, d) {return (t===0) ? b : ((t==d) ? b+c : (((t/=d/2) < 1) ? c/2 * Math.pow(2, 10 * (t - 1)) + b : c/2*(-Math.pow(2, -10 * --t) + 2) + b));};
this.easeInCirc = function (t, b, c, d) {return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;};
this.easeOutCirc = function (t, b, c, d) {return c * Math.sqrt(1 - (t=t/d-1)*t) + b;};
this.easeInOutCirc = function (t, b, c, d) {return ((t/=d/2) < 1) ? -c/2 * (Math.sqrt(1 - t*t) - 1) + b : c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;};
this.easeInBounce = function (t, b, c, d) {return (t===0) ? b : (((t/=d) == 1) ? b + c : -(c * Math.pow(2, 10 * (t -= 1)) * Math.sin((t * d - (d*0.3 /4)) * (2 * Math.PI) / (d*0.3 ))) + b);};
this.easeOutBounce = function (t, b, c, d) {return (t===0) ? b : (((t/=d/2) == 2) ? b + c : (c * Math.pow(2, -10 * t ) * Math.sin((t * d - (d*0.3 /4)) * (2 * Math.PI) / (d*0.3 ))) + c + b);};
this.easeInOutBounce= function (t, b, c, d) {return (t===0) ? b : (((t/=d/2) == 2) ? b + c : ((t<1) ? -0.5 * (c * Math.pow(2, 10 * (t -= 1)) * Math.sin((t * d - (d*0.3*1.5/4)) * (2 * Math.PI) / (d*0.3*1.5))) + b :
(c * Math.pow(2, -10 * (t -= 1)) * Math.sin((t * d - (d*0.3*1.5/4)) * (2 * Math.PI) / (d*0.3*1.5))) * 0.5 + c + b));};
} // #################### END OF Easing_Funs CONSTRUCTOR ####################
var exif_orientations = [
'scale(1,1) rotate(0deg)',
'scale(1,1) rotate(0deg)',
'scale(-1,1) rotate(0deg)',
'scale(1,1) rotate(180deg)',
'scale(1,-1) rotate(0deg)',
'scale(-1,1) rotate(90deg)',
'scale(1,1) rotate(90deg)',
'scale(-1,1) rotate(-90deg)',
'scale(1,1) rotate(-90deg)'
];
function _arrayBufferToBase64( buffer ) {
var binary = '';
var bytes = new Uint8Array( buffer );
var len = bytes.byteLength;
for (var i = 0; i < len; i++) {
binary += String.fromCharCode( bytes[ i ] );
}
return window.btoa( binary );
}
function exif_orientation(filename) {
if (window.File && window.FileReader && window.FileList && window.Blob) {
// Great success! All the File APIs are supported.
} else {
alert('The File APIs are not fully supported in this browser.');
}
var file = new File([""], filename);
console.log('WHOOPS:' + filename + ': ' + file.name);
var file_reader = new FileReader();
file_reader.onload = function() {
console.log('OK:' + this.readyState + ', ' + this.error + '; ' + this.result.length);
var scanner = new DataView(this.result);
var idx = 0;
var value = 1; // Non-rotated is the default
if(this.result.length < 2 || scanner.getUint16(idx) != 0xFFD8) {
// Not a JPEG
return 0;
}
idx += 2;
var maxBytes = scanner.byteLength;
while(idx < maxBytes - 2) {
var uint16 = scanner.getUint16(idx);
idx += 2;
switch(uint16) {
case 0xFFE1: // Start of EXIF
var exifLength = scanner.getUint16(idx);
maxBytes = exifLength - idx;
idx += 2;
break;
case 0x0112: // Orientation tag
// Read the value, its 6 bytes further out
// See page 102 at the following URL
// http://www.kodak.com/global/plugins/acrobat/en/service/digCam/exifStandard2.pdf
value = scanner.getUint16(idx + 6, false);
maxBytes = 0; // Stop scanning
break;
}
}
};
file_reader.readAsArrayBuffer(file);
}
/*
From the EXIF standard documentation
(http://web.archive.org/web/20131018091152/http://exif.org/Exif2-2.PDF)
Table 18 Tag Support Levels (5)
Orientation:
The image orientation viewed in terms of rows and columns.
Tag = 274 (112 Hex)
Type = SHORT
Count = 1
Default = 1
1 = The 0th row is at the visual top of the image, and the 0th column is the visual left-hand side.
2 = The 0th row is at the visual top of the image, and the 0th column is the visual right-hand side.
3 = The 0th row is at the visual bottom of the image, and the 0th column is the visual right-hand side.
4 = The 0th row is at the visual bottom of the image, and the 0th column is the visual left-hand side.
5 = The 0th row is the visual left-hand side of the image, and the 0th column is the visual top.
6 = The 0th row is the visual right-hand side of the image, and the 0th column is the visual top.
7 = The 0th row is the visual right-hand side of the image, and the 0th column is the visual bottom.
8 = The 0th row is the visual left-hand side of the image, and the 0th column is the visual bottom.
Other = reserved
*/
function Producer(params) {
/*
USERS SHOULD NOT MODIFY THIS FUNCTION
#####################################
==============================================================================================================
Description
-----------
This function is called to save its parameters as its properties. These are required by the
render function in order to animate the objects on the screen by manipulating certain of their CSS properties.
Parameters Description
---------- -----------
params An object containing all the parameters
==============================================================================================================
*/
this.params = params; // Add params objects as a property of the Producer class
} // #################### END OF Producer CONSTRUCTOR ####################
Producer.prototype.render = function(time) {
/*
USERS SHOULD NOT MODIFY THIS FUNCTION
#####################################
==============================================================================================================
Description
-----------
This function is called once for each frame to manipulate the various objects' CSS properties by 1 increment.
By using "prototype" it is stored in the Producer object as a method called "render".
This is the main function, since it is where the animated frames are produced.
Parameters Description
---------- -----------
time The current normalised (between 0.0 and 1.0) time. This is incremented on each call by an
amount equal to 1/(duration*framerate), where duration = the length of the clip in seconds
and framerate is the number of frames per second.
==============================================================================================================
*/
var property;
var startval;
var changeval;
var propertyval;
var i;
var j;
var k;
var t = time;
var duration;
var animate;
var start;
var end;
var ease;
var keyftime;
var keystart;
var keyend;
var kt;
var kd;
var prop;
var fprop;
var tprop;
var fp = [];
var tp = [];
var aprops = [];
var from = {};
var to = {};
Elusien.frame_length = t / (++Elusien.frame_number);
if (typeof webvfx_add_to_frame !=='undefined') {for (i = 0; i < webvfx_add_to_frame.length; i++) {webvfx_add_to_frame[i](time, this.params.browser, Elusien.frame_number, Elusien.frame_rate);}}
for (var param in this.params) {
if (typeof this.params[param].animate != "undefined"){
animate = this.params[param].animate;
start = animate.start;
end = animate.end;
ease = animate.ease;
duration = end - start;
aprops = Object.getOwnPropertyNames(this.params[param].animate).sort();
if ((t >= start) && (t <= end)) {
// The next statement is to get over the problem that the time never reaches 1.0. the last time is for the last frame
// which can appear up to 1/frame-rate seconds before 1.0. Since we do not know the framerate, assume it is 24fps.
if ((end-t) < 2*Elusien.frame_length){t = end;}
j = 0;
for (k = 0; k < aprops.length; k++) {
if (aprops[k].match(/^\d+(.\d)*$/) !== null) {
prop = +aprops[k];
keyftime = (prop * duration / 100) + start;
if (keyftime <= t) {
kt = t - keyftime;
keystart = k;
keyend = keystart+1;
if (aprops[keyend].match(/^\d+(.\d)*$/) === null) {
keyend = keystart;
kt = 1.0;
kd = kt;
} else {
kd = (+aprops[keyend] * duration / 100) - (+aprops[keystart] * duration / 100);
}
}
}
}
//console.log('DEBUG: prop=' + prop + ', keystart=' + keystart + ', keyend=' + keyend);
from = animate[aprops[keystart]];
to = animate[aprops[keyend]];
fp = Object.getOwnPropertyNames(from);
tp = Object.getOwnPropertyNames( to );
for (j=0; j<fp.length; j++) {
property = fp[j];
propertyval = " ";
for (i = 0; i < from[property].length; i++) {
fprop = from[property][i];
if(typeof fprop == "string"){fprop = fprop.trim();}
tprop = to[property][i];
if(typeof tprop == "string"){tprop = tprop.trim();}
if (typeof fprop == "string" && fprop[0] != '#') {
propertyval += fprop;
} else if ((typeof fprop == "string") && (fprop[0] == '#')) {
propertyval += "rgb(";
startval = parseInt(fprop.substring(1,3),16);
changeval = parseInt(tprop.substring(1,3),16) - startval;
propertyval += Math.floor(this.params[param].easing[ease](kt, +startval, +changeval, kd)) + ',';
startval = parseInt(fprop.substring(3,5),16);
changeval = parseInt(tprop.substring(3,5),16) - startval;
propertyval += Math.floor(this.params[param].easing[ease](kt, +startval, +changeval, kd)) + ',';
startval = parseInt(fprop.substring(5,7),16);
changeval = parseInt(tprop.substring(5,7),16) - startval;
propertyval += Math.floor(this.params[param].easing[ease](kt, +startval, +changeval, kd)) + ')';
} else {
startval = fprop;
changeval = tprop - startval;
propertyval += this.params[param].easing[ease](kt, +startval, +changeval, kd);
}
}
//console.log(time + "|DEBUG: property=" + property + ": " + propertyval);
this.params[param].style[property] = propertyval;
}
}
} else if (typeof this.params[param].stopwatch != "undefined"){
run_stopwatch(this.params[param].stopwatch, this.params[param].sw_params, time, Elusien.frame_number, Elusien.frame_rate);
} else if (typeof this.params[param].slideshow != "undefined"){
run_slideshow(this.params[param].slideshow, this.params[param].ss_params, time, Elusien.frame_number, Elusien.frame_rate);
}
}
}; // #################### END OF render ####################
function slideshow_to_JSON_string(data_slideshow) {
/*
USERS SHOULD NOT MODIFY THIS FUNCTION
#####################################
==============================================================================================================
Description
-----------
This function is called once to create the JSON string from the user's animation request..
Parameters Description
---------- -----------
data_animate The string the user provided on the "data-animate" attribute in the HTML.
==============================================================================================================
*/
return data_slideshow.replace( /\n/g, ' ')
.replace( /\s+:/g, ': ')
.replace( /\s{2,}/g, ' ')
.replace( /([a-z0-9_.]+):/ig, '"$1":')
.replace( /:([^:]*)\s*;/g, ':"$1",')
.replace( /[;,]\s*}/g, '}');
}
function animate_to_JSON_string(data_animate) {
/*
USERS SHOULD NOT MODIFY THIS FUNCTION
#####################################
==============================================================================================================
Description
-----------
This function is called once to create the JSON string from the user's animation request..
Parameters Description
---------- -----------
data_animate The string the user provided on the "data-animate" attribute in the HTML.
==============================================================================================================
*/
return data_animate.replace( /\n/g, ' ')
.replace( /\s+:/g, ': ')
.replace( /\s{2,}/g, ' ')
.replace( /(\d\d\d)(.\d*){0,1}%:/g, '$1$2:')
.replace( /(\D)(\d\d)(.\d*){0,1}%:/g, '$10$2$3:')
.replace( /(\D\D)(\d)(.\d*){0,1}%:/g, '$100$2$3:')
.replace( /([a-z0-9_.]+):/ig, '"$1":')
.replace( /:([^:]*)\s*;/g, ':"$1",')
.replace( /[;,]\s*}/g, '}');
}
function stopwatch_to_JSON_string(data_stopwatch) {
/*
USERS SHOULD NOT MODIFY THIS FUNCTION
#####################################
==============================================================================================================
Description
-----------
This function is called once to create the JSON string from the user's animation request..
Parameters Description
---------- -----------
data_animate The string the user provided on the "data-animate" attribute in the HTML.
==============================================================================================================
*/
return data_stopwatch.replace( /\n/g, ' ')
.replace( /\s+:/g, ': ')
.replace( /\s{2,}/g, ' ')
.replace( /pause\s*;/g, 'pause: normal;')
.replace( /resume\s*;/g, 'resume: normal;')
.replace( /(\d\d\d)(.\d*){0,1}%:/g, '$1$2:')
.replace(/(\D)(\d\d)(.\d*){0,1}%:/g, '$10$2$3:')
.replace(/(\D\D)(\d)(.\d*){0,1}%:/g, '$100$2$3:')
.replace( /([a-z0-9_.]+):/ig, '"$1":')
.replace( /:([^:]*)\s*;/g, ':"$1",')
.replace( /[;,]\s*}/g, '}');
}
function format_animate(animate) {
/*
USERS SHOULD NOT MODIFY THIS FUNCTION
#####################################
==============================================================================================================
Description
-----------
This function is called to modify the "key-frames£ from the user's animation request
to get them in a format that can be easily manipulated in the "render" function.
Parameters Description
---------- -----------
animate The animation object that contains all the info including the key-frames.
==============================================================================================================
*/
var aprops = [];
var bprops = [];
var anprops = [];
var aval = {};
var j = 0;
var k = 0;
aprops = Object.getOwnPropertyNames(animate).sort();
for (j=0; j<aprops.length; j++) {
if ((aprops[j] !='start') && (aprops[j] != 'end') && (aprops[j] != 'ease')) {anprops[k++] = aprops[j];}
}
// process the key-frames animation data
for (j=0; j < anprops.length; j++) {
aval = animate[anprops[j]];
bprops = Object.getOwnPropertyNames(aval).sort();
for (k = 0; k < bprops.length; k++) {
if (typeof aval[bprops[k]] == 'string') {aval[bprops[k]] = aval[bprops[k]].trim();}
animate[anprops[j]][bprops[k]] = format_css(aval[bprops[k]]);
}
}
} // #################### END OF format_animate ####################
function format_css(str){
/*
USERS SHOULD NOT MODIFY THIS FUNCTION
#####################################
==============================================================================================================
Description
-----------
Yet another function that is called to modify the "key-frames£ from the user's animation request
to get them in a format that can be easily manipulated in the "render" function.
Parameters Description
---------- -----------
str The css property to be animated.
==============================================================================================================
*/
var numRegexp = /(([+-]{0,1}\d+\.{0,1}\d*)|#([0-9abcdef]+[0-9abcdef]))/ig;
var hex3Regexp = /([a-z0-9])([a-z0-9])([a-z0-9])/i;
var result = [];
var atemp = [];
var strtemp = String(str);
var j;
var k = 0;
if (strtemp === "") {
result[0] = "";
return result;
}
atemp = strtemp.replace(numRegexp, '|!$1|').split('|');
for (j=0; j < atemp.length; j++){
if (atemp[j][0] == '!') {
if (atemp[j][1] != '#') {
result[k++] = +atemp[j].substr(1);
} else {
if (atemp[j].length == 5) {atemp[j] =atemp[j].replace(hex3Regexp, "$1$1$2$2$3$3");}
result[k++] = atemp[j].substr(1);
}
} else if (atemp[j] !== "") {
result[k++] = atemp[j];
}
}
return result;
} // #################### END OF format_css ####################
function pad(number, length) {
var str = '' + number;
while (str.length < length) {str = '0' + str;}
return str;
}
function error(arr, throw_err){
var err = 'ERROR: ';
for (var i = 0; i < arr.length; i++){err += arr[i];}
console.log(err);
if (throw_err) {throw(err);} else {
if (confirm(err + ' DO YOU WISH TO ABORT?')){throw(err);}
}
}
function run_slideshow(slideshow, ss_params, time, frame_number, frame_rate){
var slide_no;
if (ss_params.last_time == time) {return;}
ss_params.last_time = time;
function all_images_loaded(report){
/*
Simple test to see if all images are loaded in the DOM. If not:
Ask user if he/she wants to abort or wait a bit.
If the "report" argument is true, print a list of missing files to the console-log.
*/
for (var i = 0; i < ss_params.images.length; i++){
//console.log('EXIF: ' + exif_orientation(ss_params.images[i].getAttributeNode('src').value));
if (!(ss_params.images[i].complete && (ss_params.images[i].naturalWidth !== 0))) {
ss_params.images_loaded = false;
if (report) {console.log('MISSING image: ' + ss_params.images[i].getAttributeNode('src').value);}
}
}
if (!ss_params.images_loaded && report){alert('A list of missing images is on the console-log.');}
return ss_params.images_loaded;
}
// ======= End of function all_images_loaded(report) =======
if (time === 0.0){
while (!all_images_loaded(false)){
err = "WARNING: Not all images loaded yet.\n";
if (!confirm(err + 'DO YOU WANT TO CARRY ON?' )){if (!all_images_loaded(true)){throw(err);}}
if (!confirm('DO YOU WANT TO WAIT FOR MISSING IMAGE(s)?')){if (!all_images_loaded(true)){break ;}}
}
ss_params.slide_len = 1.0 / ss_params.images.length;
ss_params.trans_len = ss_params.trans_pcnt * ss_params.slide_len / 100;
ss_params.parent_cstyle = window.getComputedStyle(slideshow, null);
ss_params.parent_width = slideshow.offsetWidth;
ss_params.parent_height = slideshow.offsetHeight;
ss_params.parent_ratio = ss_params.parent_width / ss_params.parent_height;
set_image_size();
transition_image(0.0);
transition_image(1.0);
return;
}
if (frame_number == 2) {
ss_params.frame_len = time;
ss_params.slide_frames = ss_params.slide_len / ss_params.frame_len;
ss_params.trans_frames = Math.floor(ss_params.trans_pcnt * ss_params.slide_frames / 100) + 1;
}
slide_no = Math.floor(time / ss_params.slide_len); // (first slide is slide_no = 0)
if (slide_no === 0) {
return ;
}
if (slide_no != ss_params.slide_this ) { // we have a new slide
ss_params.slide_prev = ss_params.slide_this;
ss_params.slide_this = slide_no;
ss_params.slide_start_time = time;
ss_params.trans_end_time = time + ss_params.trans_len;
set_image_size();
}
if (time < ss_params.trans_end_time) {
transition_image((time-ss_params.slide_start_time)/ss_params.trans_len);
} else if ((time == ss_params.trans_end_time) || (ss_params.trans_end_time == ss_params.slide_start_time)){
transition_image(0.0);
transition_image(1.0);
} else {
transition_image(1.0 + ss_params.magnify * (time - ss_params.trans_end_time) / (ss_params.slide_len - ss_params.trans_len));
}
function set_image_size() {
var reverse = false;
var exif = ss_params.images[ss_params.slide_this].getAttribute('data-exif');
if (exif !== null) {
switch(exif){
case '180': orient = 3;
break;
case '90': orient = 6;
reverse = true;
break;
case '-90': orient = 8;
reverse = true;
break;
default : orient = 1;
}
reverse = false;
}
if (ss_params.slide_this !== 0) {
ss_params.slide_prev_width = ss_params.slide_this_width;
ss_params.slide_prev_height = ss_params.slide_this_height;
ss_params.slide_prev_move_distance = ss_params.slide_this_move_distance;
ss_params.slide_prev_offset = ss_params.slide_this_offset;
}
ss_params.slide_this_width = reverse ? ss_params.images[ss_params.slide_this].naturalHeight : ss_params.images[ss_params.slide_this].naturalWidth;
ss_params.slide_this_height = reverse ? ss_params.images[ss_params.slide_this].naturalWidth : ss_params.images[ss_params.slide_this].naturalHeight;
ss_params.slide_this_ratio = ss_params.slide_this_width / ss_params.slide_this_height;
if (ss_params.slide_this_ratio >= ss_params.parent_ratio){
ss_params.slide_this_width = ss_params.parent_width;
ss_params.slide_this_height = ss_params.slide_this_width / ss_params.slide_this_ratio;
} else {
ss_params.slide_this_height = ss_params.parent_height;
ss_params.slide_this_width = ss_params.slide_this_height * ss_params.slide_this_ratio;
}
ss_params.images[ss_params.slide_this].style.height = (reverse ? ss_params.slide_this_width : ss_params.slide_this_height) + 'px';
ss_params.images[ss_params.slide_this].style.width = (reverse ? ss_params.slide_this_height : ss_params.slide_this_width ) + 'px';
if (reverse) {
// this code will not work, the problem is width is still width, even when rotated 90%.
ss_params.images[ss_params.slide_this].style.webkitTransform = exif_orientations[orient] + ' translateX(-' + ss_params.slide_this_height/2 +'px)';
}
ss_params.slide_this_move_distance = (ss_params.horv == 'hriz') ? ss_params.parent_width : ss_params.parent_height;
ss_params.slide_this_offset = (ss_params.horv == 'hriz') ? 0.5 * (ss_params.parent_width - ss_params.slide_this_width) : 0.5 * (ss_params.parent_height - ss_params.slide_this_height);
return;
}
function transition_image(fraction) {
var p = (ss_params.slide_prev != -1);
var style_this = ss_params.images[ss_params.slide_this].style;
if (p) {style_prev = ss_params.images[ss_params.slide_prev].style;}
if (fraction > 1.0) {
style_this.webkitTransform = style_this.webkitTransform + ' scale(' + fraction +',' + fraction +')';
return;
}
if (fraction === 0) {
style_this.display = "block";
ss_params.transition_complete = false;
}
if (ss_params.type == 'sliding') {
if (!ss_params.transition_complete) {
style_this.opacity = fraction;
style_this[ss_params.dirn] = ( ss_params.sign * (1-fraction) * ss_params.slide_this_move_distance + ss_params.slide_this_offset) + 'px';
if (p) {
style_prev.opacity = 1.0 - fraction;
style_prev[ss_params.dirn] = (-ss_params.sign * fraction * ss_params.slide_prev_move_distance + ss_params.slide_prev_offset) + 'px';
}
ss_params.transition_complete = (fraction == 1.0);
if (p && ss_params.transition_complete){
style_prev.display = "none";
}
}
}
return;
}
}
function run_stopwatch(watch, sw_params, time, frame_number, frame_rate) {
var i=0, hh = 0, mm = 0, ss = 0, mmm = 0;
var current_time = 0;
var text = '';
var t = time;
var duration;
var start;
var end;
var prop;
var sprop;
var aprops = [];
if (time === 0.0) {
sw_params.last_time = -1;
sw_params.frame_len = 1000.0 / frame_rate; // Length of a frame in msecs.
sw_params.running_time = 0; // running_time includes time spent pausing
sw_params.current_time = 0; // current_time excludes time spent pausing
sw_params.running_frame = 0; // running_frame includes frames spent pausing
sw_params.current_frame = 0; // current_frame excludes frames spent pausing
sw_params.pausing = false;
//console.log(frame_rate);
// for (watch = 0; watch < SW.watches.length; watch++) {
sw_params.current_time0 = 0;
sw_params.frame_number0 = 0;
sw_params.spans = watch.children;
for (i = 0; i < sw_params.spans.length; i++) { // Hide those empty spans that the user doesn't want to see.
text = sw_params.spans[i].textContent;
if (text === "") {
sw_params.spans[i].style.display = "none";
} else {
if (i === 0) { sw_params.frame_number0 = +text;}
if (i === 1) { sw_params.current_time0 = +(text.replace(/^0/ , '')) * 1000 * 60 * 60;}
if (i === 2) { sw_params.current_time0 += +(text.replace(/^0/ , '')) * 1000 * 60 ;}
if (i === 3) { sw_params.current_time0 += +(text.replace(/^0/ , '')) * 1000 ;}
if (i === 4) { sw_params.current_time0 += +(text.replace(/^0{1,2}/, '')) ;}
}
}
}
if (sw_params.last_time == time) {return -1;} // This frame is the same as the last one!
start = sw_params.start;
end = sw_params.end;
duration = end - start;
sw_params.last_time = time;
aprops = Object.getOwnPropertyNames(sw_params).sort();
//console.log('DEBUG: aprops.length=' + aprops.length + ', aprops[0]=' + aprops[0]);
if ((t >= start) && (t <= end)) {
//console.log('DEBUG: Off we go');
// The next statement is to get over the problem that the time never reaches 1.0. the last time is for the last frame
// which can appear up to 1/frame-rate seconds before 1.0. Since we do not know the framerate, assume it is 24fps.
if ((end-t) < 2*Elusien.frame_length){t = end;}
if ((aprops.length > 0) && (aprops[0].match(/^\d+(.\d)*$/) !== null)){
sprop = aprops[0];
prop = +aprops[0];
//console.log('DEBUG: t=' + t + ', aprops[0]=' + aprops[0], ' + struct=' + JSON.stringify(sw_params[sprop], null, 4));
if (((prop * duration / 100) + start) <= t) {
if (typeof sw_params[sprop].pause != "undefined") {
sw_params.pausing = true;
} else if (typeof sw_params[sprop].resume != "undefined") {
sw_params[sprop].resume = sw_params[sprop].resume.trim();
sw_params.pausing = false;
if(sw_params[sprop].resume == 'skip') {