1+ /*
2+ * Utils
3+ *
4+ * globals window performance
5+ */
6+
7+ 'use strict' ;
8+
9+ Object . defineProperty ( exports , '__esModule' , {
10+ value : true
11+ } ) ;
12+ function now ( ) {
13+ if ( typeof performance !== 'undefined' && typeof performance . now !== 'undefined' ) {
14+ return performance . now ( ) ;
15+ }
16+ return + new Date ( ) ;
17+ }
18+
19+ var requestAnimationFrame = undefined ;
20+ var cancelAnimationFrame = undefined ;
21+ if ( typeof window !== 'undefined' ) {
22+ requestAnimationFrame = window . requestAnimationFrame || window . mozRequestAnimationFrame || window . webkitRequestAnimationFrame || window . msRequestAnimationFrame ;
23+ cancelAnimationFrame = window . cancelAnimationFrame || window . mozCancelAnimationFrame ;
24+ }
25+
26+ if ( typeof requestAnimationFrame === 'undefined' ) {
27+ requestAnimationFrame = function ( fn ) {
28+ return setTimeout ( fn , 50 ) ;
29+ } ;
30+ cancelAnimationFrame = function ( id ) {
31+ return clearTimeout ( id ) ;
32+ } ;
33+ }
34+
35+ var runAnimation = function runAnimation ( fn ) {
36+ var last = now ( ) ;
37+ var tick = function tick ( ) {
38+ var diff = now ( ) - last ;
39+ if ( diff >= 33 ) {
40+ last = now ( ) ; // Don't run faster than 30 fps
41+ return fn ( diff , function ( ) {
42+ return requestAnimationFrame ( tick ) ;
43+ } ) ;
44+ }
45+ return setTimeout ( tick , 33 - diff ) ;
46+ } ;
47+ return tick ( ) ;
48+ } ;
49+
50+ function result ( obj , key ) {
51+ for ( var _len = arguments . length , args = Array ( _len > 2 ? _len - 2 : 0 ) , _key = 2 ; _key < _len ; _key ++ ) {
52+ args [ _key - 2 ] = arguments [ _key ] ;
53+ }
54+
55+ if ( typeof obj [ key ] === 'function' ) {
56+ return obj [ key ] . apply ( obj , args ) ;
57+ }
58+ return obj [ key ] ;
59+ }
60+
61+ function extend ( out ) {
62+ for ( var _len2 = arguments . length , sources = Array ( _len2 > 1 ? _len2 - 1 : 0 ) , _key2 = 1 ; _key2 < _len2 ; _key2 ++ ) {
63+ sources [ _key2 - 1 ] = arguments [ _key2 ] ;
64+ }
65+
66+ sources . forEach ( function ( source ) {
67+ if ( source ) {
68+ for ( var key in source ) {
69+ if ( ( { } ) . hasOwnProperty . call ( source , key ) ) {
70+ var val = source [ key ] ;
71+ // NOTE: Checking for object may be sufficient...
72+ if ( typeof out [ key ] !== 'undefined' && typeof out [ key ] === 'object' && typeof val !== 'undefined' && typeof val === 'object' ) {
73+ extend ( out [ key ] , val ) ;
74+ } else {
75+ out [ key ] = val ;
76+ }
77+ }
78+ }
79+ }
80+ } ) ;
81+
82+ return out ;
83+ }
84+
85+ function average ( arr ) {
86+ var sum = 0 ;
87+ var count = 0 ;
88+ arr . forEach ( function ( v ) {
89+ sum += Math . abs ( v ) ;
90+ ++ count ;
91+ } ) ;
92+ return sum / count ;
93+ }
94+
95+ function getFromDOM ( ) {
96+ var key = arguments [ 0 ] === undefined ? 'options' : arguments [ 0 ] ;
97+ var json = arguments [ 1 ] === undefined ? true : arguments [ 1 ] ;
98+
99+ var el = document . querySelector ( '[data-pace-#{ key }]' ) ;
100+
101+ if ( ! el ) {
102+ return ;
103+ }
104+
105+ var data = el . getAttribute ( 'data-pace-#{ key }' ) ;
106+
107+ if ( ! json ) {
108+ return data ;
109+ }
110+
111+ try {
112+ return JSON . parse ( data ) ;
113+ } catch ( e ) {
114+ if ( typeof console !== 'undefined' ) {
115+ console . error ( 'Error parsing inline pace options' , e ) ;
116+ }
117+ }
118+ }
119+
120+ exports [ 'default' ] = {
121+ now : now ,
122+ requestAnimationFrame : requestAnimationFrame ,
123+ cancelAnimationFrame : cancelAnimationFrame ,
124+ runAnimation : runAnimation ,
125+ result : result ,
126+ extend : extend ,
127+ average : average ,
128+ getFromDOM : getFromDOM
129+ } ;
130+ module . exports = exports [ 'default' ] ;
0 commit comments