11"use strict" ;
22
3- function FlightLogAnalyser ( flightLog , graphConfig , canvas , craftCanvas , options ) {
3+ function FlightLogAnalyser ( flightLog , graphConfig , canvas , analyserCanvas , options ) {
44
5- var canvasCtx = canvas . getContext ( "2d" ) ;
5+ var
6+ ANALYSER_LEFT_PROPORTION = 0.05 , // 5% from left
7+ ANALYSER_TOP_PROPORTION = 0.55 , // 55% from top
8+ ANALYSER_HEIGHT_PROPORTION = 0.40 , // 40% high
9+ ANALYSER_WIDTH_PROPORTION = 0.40 , // 40% wide
10+
11+ ANALYSER_LARGE_LEFT_PROPORTION = 0.05 , // 5% from left
12+ ANALYSER_LARGE_TOP_PROPORTION = 0.05 , // 55% from top
13+ ANALYSER_LARGE_HEIGHT_PROPORTION = 0.90 , // 40% high
14+ ANALYSER_LARGE_WIDTH_PROPORTION = 0.90 ; // 40% wide
15+
16+ var canvasCtx = analyserCanvas . getContext ( "2d" ) ;
617
718var // inefficient; copied from grapher.js
819
4859
4960 var audioIterations = 0 ; // variable to monitor spectrum processing
5061
62+ var isFullscreen = false ;
63+
64+ this . setFullscreen = function ( size ) {
65+ isFullscreen = ( size == true ) ;
66+ that . resize ( ) ;
67+ }
68+
69+ function getSize ( ) {
70+ if ( isFullscreen ) {
71+ return {
72+ height : ANALYSER_LARGE_HEIGHT_PROPORTION ,
73+ width : ANALYSER_LARGE_WIDTH_PROPORTION ,
74+ left : ANALYSER_LARGE_LEFT_PROPORTION ,
75+ top : ANALYSER_LARGE_TOP_PROPORTION ,
76+ }
77+ } else {
78+ return {
79+ height : ANALYSER_HEIGHT_PROPORTION ,
80+ width : ANALYSER_WIDTH_PROPORTION ,
81+ left : ANALYSER_LEFT_PROPORTION ,
82+ top : ANALYSER_TOP_PROPORTION ,
83+ }
84+ }
85+
86+ }
87+
88+ this . resize = function ( ) {
89+
90+ // Determine the analyserCanvas location
91+ canvasCtx . canvas . height = ( canvas . height * getSize ( ) . height ) ;
92+ canvasCtx . canvas . width = ( canvas . width * getSize ( ) . width ) ;
93+
94+ // Recenter the analyser canvas in the bottom left corner
95+ $ ( analyserCanvas ) . css ( {
96+ left : ( canvas . width * getSize ( ) . left ) + "px" ,
97+ top : ( canvas . height * getSize ( ) . top ) + "px" ,
98+ } ) ;
99+
100+ }
101+
51102 function dataLoad ( dataBuffer , audioBuffer ) {
52103
53104 var chunkIndex , frameIndex ;
@@ -94,17 +145,24 @@ try {
94145
95146 canvasCtx . save ( ) ;
96147
148+ canvasCtx . lineWidth = 1 ;
149+
97150 var bufferLength = spectrumAnalyser . frequencyBinCount ;
98151 var dataArray = new Uint8Array ( bufferLength ) ;
99152
100- var HEIGHT = canvasCtx . canvas . height * 0.4 ; /* trial and error values to put box in right place */
101- var WIDTH = canvasCtx . canvas . width * 0.4 ;
102- var LEFT = canvasCtx . canvas . width * 0.05 ;
103- var TOP = canvasCtx . canvas . height * 0.55 ;
153+
154+
155+ canvasCtx . clearRect ( 0 , 0 , canvasCtx . canvas . width , canvasCtx . canvas . height ) ;
156+
157+ var MARGIN = 10 ; // pixels
158+ var HEIGHT = canvasCtx . canvas . height - MARGIN ;
159+ var WIDTH = canvasCtx . canvas . width ;
160+ var LEFT = canvasCtx . canvas . left ;
161+ var TOP = canvasCtx . canvas . top ;
104162
105163 /* only plot the lower half of the FFT, as the top half
106164 never seems to have any values in it - too high frequency perhaps. */
107- var PLOTTED_BUFFER_LENGTH = bufferLength / 2 ;
165+ var PLOTTED_BUFFER_LENGTH = bufferLength ; // / 2;
108166
109167 canvasCtx . translate ( LEFT , TOP ) ;
110168
@@ -118,19 +176,19 @@ try {
118176 var x = 0 ;
119177
120178 for ( var i = 0 ; i < ( PLOTTED_BUFFER_LENGTH ) ; i ++ ) {
121- barHeight = ( dataArray [ i ] / 255 * HEIGHT ) ;
179+ barHeight = ( dataArray [ i ] / 255 * ( HEIGHT ) ) ;
122180
123181 canvasCtx . fillStyle = 'rgba(0,255,0,0.3)' ; /* green */
124- canvasCtx . fillRect ( x , HEIGHT - barHeight , barWidth , barHeight ) ;
182+ canvasCtx . fillRect ( x , ( HEIGHT ) - barHeight , barWidth , barHeight ) ;
125183
126184 x += barWidth + 1 ;
127185 }
128- drawGridLines ( options . analyserSampleRate , LEFT , TOP , WIDTH , HEIGHT ) ;
129- drawAxisLabel ( analyserFieldName + ' ' + analyserSampleRange , WIDTH - 8 , HEIGHT - 10 , 'right' ) ;
186+ drawAxisLabel ( analyserFieldName + ' ' + analyserSampleRange , WIDTH - 4 , HEIGHT - 6 , 'right' ) ;
187+ drawGridLines ( options . analyserSampleRate , LEFT , TOP , WIDTH , HEIGHT , MARGIN ) ;
130188 canvasCtx . restore ( ) ;
131189 }
132190
133- function drawGridLines ( sampleRate , LEFT , TOP , WIDTH , HEIGHT ) {
191+ function drawGridLines ( sampleRate , LEFT , TOP , WIDTH , HEIGHT , MARGIN ) {
134192
135193 var ticks = 5 ;
136194 var frequencyInterval = ( sampleRate / ticks ) / 4 ;
@@ -145,7 +203,8 @@ try {
145203 canvasCtx . lineTo ( i * ( WIDTH / ticks ) , HEIGHT ) ;
146204
147205 canvasCtx . stroke ( ) ;
148- drawAxisLabel ( ( frequency ) + "Hz" , i * ( WIDTH / ticks ) , HEIGHT * 1.05 , 'center' ) ;
206+ var textAlign = ( i == 0 ) ?'left' :( ( i == ticks ) ?'right' :'center' ) ;
207+ drawAxisLabel ( ( frequency ) + "Hz" , i * ( WIDTH / ticks ) , HEIGHT + MARGIN , textAlign ) ;
149208 frequency += frequencyInterval ;
150209 }
151210 }
@@ -186,8 +245,8 @@ try {
186245 }
187246 draw ( ) ; // draw the analyser on the canvas....
188247 }
189- } catch ( e ) {
190- console . log ( 'Failed to create analyser' ) ;
191- } ;
248+ } catch ( e ) {
249+ console . log ( 'Failed to create analyser' ) ;
250+ } ;
192251
193252}
0 commit comments