浏览代码

code cleanup

j8
isymchych 9 年前
父节点
当前提交
cb522eadd8
共有 2 个文件被更改,包括 144 次插入151 次删除
  1. 137
    141
      modules/UI/audio_levels/AudioLevels.js
  2. 7
    10
      modules/UI/audio_levels/CanvasUtils.js

+ 137
- 141
modules/UI/audio_levels/AudioLevels.js 查看文件

@@ -1,12 +1,16 @@
1 1
 /* global APP, interfaceConfig, $ */
2 2
 /* jshint -W101 */
3
+
3 4
 import CanvasUtil from './CanvasUtils';
4 5
 
5
-var ASDrawContext = null;
6
+const LOCAL_LEVEL = 'local';
7
+
8
+let ASDrawContext = null;
9
+let audioLevelCanvasCache = {};
6 10
 
7 11
 function initActiveSpeakerAudioLevels() {
8
-    var ASRadius = interfaceConfig.ACTIVE_SPEAKER_AVATAR_SIZE / 2;
9
-    var ASCenter = (interfaceConfig.ACTIVE_SPEAKER_AVATAR_SIZE + ASRadius) / 2;
12
+    let ASRadius = interfaceConfig.ACTIVE_SPEAKER_AVATAR_SIZE / 2;
13
+    let ASCenter = (interfaceConfig.ACTIVE_SPEAKER_AVATAR_SIZE + ASRadius) / 2;
10 14
 
11 15
     // Draw a circle.
12 16
     ASDrawContext.arc(ASCenter, ASCenter, ASRadius, 0, 2 * Math.PI);
@@ -18,23 +22,138 @@ function initActiveSpeakerAudioLevels() {
18 22
 }
19 23
 
20 24
 /**
21
- * The audio Levels plugin.
25
+ * Resizes the given audio level canvas to match the given thumbnail size.
26
+ */
27
+function resizeAudioLevelCanvas(audioLevelCanvas, thumbnailWidth, thumbnailHeight) {
28
+    audioLevelCanvas.width = thumbnailWidth + interfaceConfig.CANVAS_EXTRA;
29
+    audioLevelCanvas.height = thumbnailHeight + interfaceConfig.CANVAS_EXTRA;
30
+}
31
+
32
+/**
33
+ * Draws the audio level canvas into the cached canvas object.
34
+ *
35
+ * @param id of the user for whom we draw the audio level
36
+ * @param audioLevel the newAudio level to render
22 37
  */
23
-var AudioLevels = (function(my) {
24
-    var audioLevelCanvasCache = {};
38
+function drawAudioLevelCanvas(id, audioLevel) {
39
+    if (!audioLevelCanvasCache[id]) {
40
+
41
+        let videoSpanId = getVideoSpanId(id);
42
+
43
+        let audioLevelCanvasOrig = $(`#${videoSpanId}>canvas`).get(0);
44
+
45
+        /*
46
+         * FIXME Testing has shown that audioLevelCanvasOrig may not exist.
47
+         * In such a case, the method CanvasUtil.cloneCanvas may throw an
48
+         * error. Since audio levels are frequently updated, the errors have
49
+         * been observed to pile into the console, strain the CPU.
50
+         */
51
+        if (audioLevelCanvasOrig) {
52
+            audioLevelCanvasCache[id] = CanvasUtil.cloneCanvas(audioLevelCanvasOrig);
53
+        }
54
+    }
25 55
 
26
-    my.LOCAL_LEVEL = 'local';
56
+    let canvas = audioLevelCanvasCache[id];
27 57
 
28
-    my.init = function () {
58
+    if (!canvas) {
59
+        return;
60
+    }
61
+
62
+    let drawContext = canvas.getContext('2d');
63
+
64
+    drawContext.clearRect(0, 0, canvas.width, canvas.height);
65
+
66
+    let shadowLevel = getShadowLevel(audioLevel);
67
+
68
+    if (shadowLevel > 0) {
69
+        // drawContext, x, y, w, h, r, shadowColor, shadowLevel
70
+        CanvasUtil.drawRoundRectGlow(drawContext,
71
+                                     interfaceConfig.CANVAS_EXTRA / 2, interfaceConfig.CANVAS_EXTRA / 2,
72
+                                     canvas.width - interfaceConfig.CANVAS_EXTRA,
73
+                                     canvas.height - interfaceConfig.CANVAS_EXTRA,
74
+                                     interfaceConfig.CANVAS_RADIUS,
75
+                                     interfaceConfig.SHADOW_COLOR,
76
+                                     shadowLevel);
77
+    }
78
+}
79
+
80
+/**
81
+ * Returns the shadow/glow level for the given audio level.
82
+ *
83
+ * @param audioLevel the audio level from which we determine the shadow
84
+ * level
85
+ */
86
+function getShadowLevel (audioLevel) {
87
+    let shadowLevel = 0;
88
+
89
+    if (audioLevel <= 0.3) {
90
+        shadowLevel = Math.round(interfaceConfig.CANVAS_EXTRA/2*(audioLevel/0.3));
91
+    } else if (audioLevel <= 0.6) {
92
+        shadowLevel = Math.round(interfaceConfig.CANVAS_EXTRA/2*((audioLevel - 0.3) / 0.3));
93
+    } else {
94
+        shadowLevel = Math.round(interfaceConfig.CANVAS_EXTRA/2*((audioLevel - 0.6) / 0.4));
95
+    }
96
+
97
+    return shadowLevel;
98
+}
99
+
100
+/**
101
+ * Returns the video span id corresponding to the given user id
102
+ */
103
+function getVideoSpanId(id) {
104
+    let videoSpanId = null;
105
+
106
+    if (id === LOCAL_LEVEL || APP.conference.isLocalId(id)) {
107
+        videoSpanId = 'localVideoContainer';
108
+    } else {
109
+        videoSpanId = `participant_${id}`;
110
+    }
111
+
112
+    return videoSpanId;
113
+}
114
+
115
+/**
116
+ * Indicates that the remote video has been resized.
117
+ */
118
+$(document).bind('remotevideo.resized', function (event, width, height) {
119
+    let resized = false;
120
+
121
+    $('#remoteVideos>span>canvas').each(function() {
122
+        let canvas = $(this).get(0);
123
+        if (canvas.width !== width + interfaceConfig.CANVAS_EXTRA) {
124
+            canvas.width = width + interfaceConfig.CANVAS_EXTRA;
125
+            resized = true;
126
+        }
127
+
128
+        if (canvas.height !== height + interfaceConfig.CANVAS_EXTRA) {
129
+            canvas.height = height + interfaceConfig.CANVAS_EXTRA;
130
+            resized = true;
131
+        }
132
+    });
133
+
134
+    if (resized) {
135
+        Object.keys(audioLevelCanvasCache).forEach(function (id) {
136
+            audioLevelCanvasCache[id].width = width + interfaceConfig.CANVAS_EXTRA;
137
+            audioLevelCanvasCache[id].height = height + interfaceConfig.CANVAS_EXTRA;
138
+        });
139
+    }
140
+});
141
+
142
+/**
143
+ * The audio Levels plugin.
144
+ */
145
+const AudioLevels = {
146
+
147
+    init () {
29 148
         ASDrawContext = $('#activeSpeakerAudioLevel')[0].getContext('2d');
30 149
         initActiveSpeakerAudioLevels();
31
-    };
150
+    },
32 151
 
33 152
     /**
34 153
      * Updates the audio level canvas for the given id. If the canvas
35 154
      * didn't exist we create it.
36 155
      */
37
-    my.updateAudioLevelCanvas = function (id, VideoLayout) {
156
+    updateAudioLevelCanvas (id, VideoLayout) {
38 157
         let videoSpanId = 'localVideoContainer';
39 158
         if (id) {
40 159
             videoSpanId = `participant_${id}`;
@@ -72,7 +191,7 @@ var AudioLevels = (function(my) {
72 191
 
73 192
             resizeAudioLevelCanvas(audioLevelCanvas, thumbnailWidth, thumbnailHeight);
74 193
         }
75
-    };
194
+    },
76 195
 
77 196
     /**
78 197
      * Updates the audio level UI for the given id.
@@ -80,7 +199,7 @@ var AudioLevels = (function(my) {
80 199
      * @param id id of the user for whom we draw the audio level
81 200
      * @param audioLevel the newAudio level to render
82 201
      */
83
-    my.updateAudioLevel = function (id, audioLevel, largeVideoId) {
202
+    updateAudioLevel (id, audioLevel, largeVideoId) {
84 203
         drawAudioLevelCanvas(id, audioLevel);
85 204
 
86 205
         let videoSpanId = getVideoSpanId(id);
@@ -100,7 +219,7 @@ var AudioLevels = (function(my) {
100 219
         );
101 220
         drawContext.drawImage(canvasCache, 0, 0);
102 221
 
103
-        if (id === AudioLevels.LOCAL_LEVEL) {
222
+        if (id === LOCAL_LEVEL) {
104 223
             id = APP.conference.localId;
105 224
             if (!id) {
106 225
                 return;
@@ -112,9 +231,9 @@ var AudioLevels = (function(my) {
112 231
                 AudioLevels.updateActiveSpeakerAudioLevel(audioLevel);
113 232
             });
114 233
         }
115
-    };
234
+    },
116 235
 
117
-    my.updateActiveSpeakerAudioLevel = function(audioLevel) {
236
+    updateActiveSpeakerAudioLevel (audioLevel) {
118 237
         if($("#activeSpeaker").css("visibility") == "hidden" || ASDrawContext === null) {
119 238
             return;
120 239
         }
@@ -129,131 +248,8 @@ var AudioLevels = (function(my) {
129 248
 
130 249
         // Fill the shape.
131 250
         ASDrawContext.fill();
132
-    };
133
-
134
-    /**
135
-     * Resizes the given audio level canvas to match the given thumbnail size.
136
-     */
137
-    function resizeAudioLevelCanvas(audioLevelCanvas,
138
-                                    thumbnailWidth,
139
-                                    thumbnailHeight) {
140
-        audioLevelCanvas.width = thumbnailWidth + interfaceConfig.CANVAS_EXTRA;
141
-        audioLevelCanvas.height = thumbnailHeight + interfaceConfig.CANVAS_EXTRA;
142
-    }
143
-
144
-    /**
145
-     * Draws the audio level canvas into the cached canvas object.
146
-     *
147
-     * @param id of the user for whom we draw the audio level
148
-     * @param audioLevel the newAudio level to render
149
-     */
150
-    function drawAudioLevelCanvas(id, audioLevel) {
151
-        if (!audioLevelCanvasCache[id]) {
152
-
153
-            let videoSpanId = getVideoSpanId(id);
154
-
155
-            let audioLevelCanvasOrig = $(`#${videoSpanId}>canvas`).get(0);
156
-
157
-            /*
158
-             * FIXME Testing has shown that audioLevelCanvasOrig may not exist.
159
-             * In such a case, the method CanvasUtil.cloneCanvas may throw an
160
-             * error. Since audio levels are frequently updated, the errors have
161
-             * been observed to pile into the console, strain the CPU.
162
-             */
163
-            if (audioLevelCanvasOrig) {
164
-                audioLevelCanvasCache[id] = CanvasUtil.cloneCanvas(audioLevelCanvasOrig);
165
-            }
166
-        }
167
-
168
-        let canvas = audioLevelCanvasCache[id];
169
-
170
-        if (!canvas) {
171
-            return;
172
-        }
173
-
174
-        let drawContext = canvas.getContext('2d');
175
-
176
-        drawContext.clearRect(0, 0, canvas.width, canvas.height);
177
-
178
-        let shadowLevel = getShadowLevel(audioLevel);
179
-
180
-        if (shadowLevel > 0) {
181
-            // drawContext, x, y, w, h, r, shadowColor, shadowLevel
182
-            CanvasUtil.drawRoundRectGlow(drawContext,
183
-                interfaceConfig.CANVAS_EXTRA / 2, interfaceConfig.CANVAS_EXTRA / 2,
184
-                canvas.width - interfaceConfig.CANVAS_EXTRA,
185
-                canvas.height - interfaceConfig.CANVAS_EXTRA,
186
-                interfaceConfig.CANVAS_RADIUS,
187
-                interfaceConfig.SHADOW_COLOR,
188
-                shadowLevel);
189
-        }
190
-    }
191
-
192
-    /**
193
-     * Returns the shadow/glow level for the given audio level.
194
-     *
195
-     * @param audioLevel the audio level from which we determine the shadow
196
-     * level
197
-     */
198
-    function getShadowLevel (audioLevel) {
199
-        var shadowLevel = 0;
200
-
201
-        if (audioLevel <= 0.3) {
202
-            shadowLevel = Math.round(interfaceConfig.CANVAS_EXTRA/2*(audioLevel/0.3));
203
-        }
204
-        else if (audioLevel <= 0.6) {
205
-            shadowLevel = Math.round(interfaceConfig.CANVAS_EXTRA/2*((audioLevel - 0.3) / 0.3));
206
-        }
207
-        else {
208
-            shadowLevel = Math.round(interfaceConfig.CANVAS_EXTRA/2*((audioLevel - 0.6) / 0.4));
209
-        }
210
-        return shadowLevel;
211
-    }
212
-
213
-    /**
214
-     * Returns the video span id corresponding to the given user id
215
-     */
216
-    function getVideoSpanId(id) {
217
-        var videoSpanId = null;
218
-        if (id === AudioLevels.LOCAL_LEVEL || APP.conference.isLocalId(id)) {
219
-            videoSpanId = 'localVideoContainer';
220
-        } else {
221
-            videoSpanId = `participant_${id}`;
222
-        }
223
-
224
-        return videoSpanId;
225
-    }
226
-
227
-    /**
228
-     * Indicates that the remote video has been resized.
229
-     */
230
-    $(document).bind('remotevideo.resized', function (event, width, height) {
231
-        var resized = false;
232
-        $('#remoteVideos>span>canvas').each(function() {
233
-            var canvas = $(this).get(0);
234
-            if (canvas.width !== width + interfaceConfig.CANVAS_EXTRA) {
235
-                canvas.width = width + interfaceConfig.CANVAS_EXTRA;
236
-                resized = true;
237
-            }
238
-
239
-            if (canvas.height !== height + interfaceConfig.CANVAS_EXTRA) {
240
-                canvas.height = height + interfaceConfig.CANVAS_EXTRA;
241
-                resized = true;
242
-            }
243
-        });
244
-
245
-        if (resized) {
246
-            Object.keys(audioLevelCanvasCache).forEach(function (id) {
247
-                audioLevelCanvasCache[id].width =
248
-                    width + interfaceConfig.CANVAS_EXTRA;
249
-                audioLevelCanvasCache[id].height =
250
-                    height + interfaceConfig.CANVAS_EXTRA;
251
-            });
252
-        }
253
-    });
254
-
255
-    return my;
251
+    },
256 252
 
257
-})(AudioLevels || {});
253
+};
258 254
 
259
-module.exports = AudioLevels;
255
+export default AudioLevels;

+ 7
- 10
modules/UI/audio_levels/CanvasUtils.js 查看文件

@@ -1,7 +1,7 @@
1 1
 /**
2 2
  * Utility class for drawing canvas shapes.
3 3
  */
4
-var CanvasUtil = (function(my) {
4
+const CanvasUtil = {
5 5
 
6 6
     /**
7 7
      * Draws a round rectangle with a glow. The glowWidth indicates the depth
@@ -15,8 +15,7 @@ var CanvasUtil = (function(my) {
15 15
      * @param glowColor the color of the glow
16 16
      * @param glowWidth the width of the glow
17 17
      */
18
-    my.drawRoundRectGlow
19
-        = function(drawContext, x, y, w, h, r, glowColor, glowWidth) {
18
+    drawRoundRectGlow (drawContext, x, y, w, h, r, glowColor, glowWidth) {
20 19
 
21 20
         // Save the previous state of the context.
22 21
         drawContext.save();
@@ -73,14 +72,14 @@ var CanvasUtil = (function(my) {
73 72
 
74 73
         // Restore the previous context state.
75 74
         drawContext.restore();
76
-    };
75
+    },
77 76
 
78 77
     /**
79 78
      * Clones the given canvas.
80 79
      *
81 80
      * @return the new cloned canvas.
82 81
      */
83
-    my.cloneCanvas = function (oldCanvas) {
82
+    cloneCanvas (oldCanvas) {
84 83
         /*
85 84
          * FIXME Testing has shown that oldCanvas may not exist. In such a case,
86 85
          * the method CanvasUtil.cloneCanvas may throw an error. Since audio
@@ -103,9 +102,7 @@ var CanvasUtil = (function(my) {
103 102
 
104 103
         //return the new canvas
105 104
         return newCanvas;
106
-    };
105
+    }
106
+};
107 107
 
108
-    return my;
109
-})(CanvasUtil || {});
110
-
111
-module.exports = CanvasUtil;
108
+export default CanvasUtil;

正在加载...
取消
保存