1
1
import Decimal from './external/decimal.mjs' ;
2
2
3
- export const calculateCycleDurationInSeconds = frequency => 1 / frequency ;
3
+ export const calculateCycleDurationInSeconds = frequency => 1 / frequency ;
4
4
5
5
6
6
// --------------------- Refresh Rate Helpers ---------------------
@@ -13,30 +13,30 @@ export const getRefreshRateReadings = async (bufferSize = 10, samples = 10) => {
13
13
return new Promise ( resolve => {
14
14
15
15
const run = ( now ) => {
16
- rafTimestamps . unshift ( now ) ; // place current timestamp (passed each time requestAnimationFrame is invoked) at the start of the array
17
-
18
- if ( rafTimestamps . length > bufferSize ) { // once 'timestamps' array contains 11 elements
19
-
20
- var firstTime = rafTimestamps . pop ( ) ;
21
- var refreshRate = 1000 * bufferSize / ( now - firstTime ) ;
22
- refreshRates . unshift ( refreshRate ) ;
23
-
24
- if ( refreshRates . length == samples ) {
25
- cancelAnimationFrame ( id ) ;
26
- resolve ( refreshRates )
27
- return ;
28
- }
16
+ rafTimestamps . unshift ( now ) ; // place current timestamp (passed each time requestAnimationFrame is invoked) at the start of the array
17
+
18
+ if ( rafTimestamps . length > bufferSize ) { // once 'timestamps' array contains 11 elements
19
+
20
+ var firstTime = rafTimestamps . pop ( ) ;
21
+ var refreshRate = 1000 * bufferSize / ( now - firstTime ) ;
22
+ refreshRates . unshift ( refreshRate ) ;
23
+
24
+ if ( refreshRates . length == samples . length ) {
25
+ cancelAnimationFrame ( id ) ;
26
+ resolve ( refreshRates )
27
+ return ;
29
28
}
30
-
31
- var id = window . requestAnimationFrame ( run ) ;
32
- }
29
+ }
33
30
34
31
var id = window . requestAnimationFrame ( run ) ;
32
+ }
33
+
34
+ var id = window . requestAnimationFrame ( run ) ;
35
35
} )
36
36
37
37
} ;
38
38
39
- export const calculateRefreshRate = async ( bufferSize = 10 , samples = 10 ) => {
39
+ export const calculateRefreshRate = async ( bufferSize = 10 , samples = 10 ) => {
40
40
41
41
const refreshRates = await getRefreshRateReadings ( bufferSize , samples )
42
42
@@ -54,77 +54,69 @@ export const calculateRefreshRate = async (bufferSize=10, samples=10) => {
54
54
55
55
if ( maxCount . length > 1 ) maxCount = [ maxCount . reduce ( ( a , b ) => a + b ) / maxCount . length ] ;
56
56
57
- const val = Number ( maxCount . shift ( ) . toFixed ( 7 ) )
58
- return val
57
+ const val = Number ( maxCount . shift ( ) . toFixed ( 7 ) )
58
+ return val
59
59
}
60
60
61
61
62
62
// --------------------- Decimal.js Helpers ---------------------
63
63
export const period = new Decimal ( 2 ) . times ( Decimal . acos ( - 1 ) ) ;
64
64
65
- export function countDecimals ( stimulusFrequency )
66
- {
67
- if ( Math . floor ( stimulusFrequency ) === Number ( stimulusFrequency ) )
68
- return 0 ;
65
+ export function countDecimals ( stimulusFrequency ) {
66
+ if ( Math . floor ( stimulusFrequency ) === Number ( stimulusFrequency ) )
67
+ return 0 ;
69
68
70
- var noOfDecimalPlaces = stimulusFrequency . toString ( ) . split ( "." ) [ 1 ] . length ;
69
+ var noOfDecimalPlaces = stimulusFrequency . toString ( ) . split ( "." ) [ 1 ] . length ;
71
70
72
- if ( noOfDecimalPlaces < 3 )
73
- return noOfDecimalPlaces ;
71
+ if ( noOfDecimalPlaces < 3 )
72
+ return noOfDecimalPlaces ;
74
73
75
- throw "Stimuli frequencies must have less than 3 decimal places."
74
+ throw "Stimuli frequencies must have less than 3 decimal places."
76
75
}
77
76
78
- export function calculateNumberOfSeconds ( stimulusFrequency )
79
- {
80
- var maxSeconds = Math . pow ( 10 , countDecimals ( stimulusFrequency ) ) ;
77
+ export function calculateNumberOfSeconds ( stimulusFrequency ) {
78
+ var maxSeconds = Math . pow ( 10 , countDecimals ( stimulusFrequency ) ) ;
81
79
82
- for ( var noOfSecs = 1 ; noOfSecs <= maxSeconds ; noOfSecs += 1 )
83
- {
84
-
85
- var noOfCycles = new Decimal ( noOfSecs ) . times ( stimulusFrequency ) ;
80
+ for ( var noOfSecs = 1 ; noOfSecs <= maxSeconds ; noOfSecs += 1 ) {
86
81
87
- if ( noOfCycles . mod ( 1 ) . toNumber ( ) === 0 )
88
- return noOfSecs ;
89
- }
82
+ var noOfCycles = new Decimal ( noOfSecs ) . times ( stimulusFrequency ) ;
90
83
91
- throw "Failed to calculate the required number of seconds." ;
84
+ if ( noOfCycles . mod ( 1 ) . toNumber ( ) === 0 )
85
+ return noOfSecs ;
86
+ }
87
+
88
+ throw "Failed to calculate the required number of seconds." ;
92
89
}
93
90
94
- export function modulus ( x , y )
95
- {
96
- var quotient = x . div ( y ) ;
91
+ export function modulus ( x , y ) {
92
+ var quotient = x . div ( y ) ;
97
93
98
- var quotientInt = new Decimal ( quotient . toFixed ( 0 ) ) ;
94
+ var quotientInt = new Decimal ( quotient . toFixed ( 0 ) ) ;
99
95
100
- if ( ( quotient . minus ( quotientInt ) ) . div ( quotientInt ) . abs ( ) . toNumber ( ) < Number . EPSILON )
101
- {
102
- return new Decimal ( 0 ) ;
103
- }
104
- else
105
- {
106
- var n = quotient . floor ( ) ;
107
- var temp = y . times ( n ) ;
108
-
109
- var signY = Math . sign ( y . toNumber ( ) ) ;
110
- var result = x . minus ( temp ) ;
111
-
112
- if ( ! x . equals ( y ) && ! result . equals ( 0 ) )
113
- {
114
- if ( Math . sign ( result . toNumber ( ) ) != signY )
115
- result = result . abs ( ) . times ( signY ) ;
116
- }
96
+ if ( ( quotient . minus ( quotientInt ) ) . div ( quotientInt ) . abs ( ) . toNumber ( ) < Number . EPSILON ) {
97
+ return new Decimal ( 0 ) ;
98
+ }
99
+ else {
100
+ var n = quotient . floor ( ) ;
101
+ var temp = y . times ( n ) ;
117
102
118
- return result ;
103
+ var signY = Math . sign ( y . toNumber ( ) ) ;
104
+ var result = x . minus ( temp ) ;
105
+
106
+ if ( ! x . equals ( y ) && ! result . equals ( 0 ) ) {
107
+ if ( Math . sign ( result . toNumber ( ) ) != signY )
108
+ result = result . abs ( ) . times ( signY ) ;
119
109
}
110
+
111
+ return result ;
112
+ }
120
113
}
121
114
122
- export function generateSquareWave ( timeInstant )
123
- {
124
- const dutyCycle = new Decimal ( 50 ) ,
125
- halfPeriod = period . times ( dutyCycle . div ( 100 ) ) ;
126
-
127
- var modulusValue = modulus ( timeInstant , period ) ;
115
+ export function generateSquareWave ( timeInstant ) {
116
+ const dutyCycle = new Decimal ( 50 ) ,
117
+ halfPeriod = period . times ( dutyCycle . div ( 100 ) ) ;
118
+
119
+ var modulusValue = modulus ( timeInstant , period ) ;
128
120
129
- return 2 * ( modulusValue . toNumber ( ) < halfPeriod . toNumber ( ) ? 1 : 0 ) - 1 ;
121
+ return 2 * ( modulusValue . toNumber ( ) < halfPeriod . toNumber ( ) ? 1 : 0 ) - 1 ;
130
122
}
0 commit comments