Skip to content

Commit ab129cc

Browse files
committed
UI+Visualisations+Canvas: simpler API to request/cancel the canvas animation
1 parent 813cac2 commit ab129cc

File tree

7 files changed

+53
-39
lines changed

7 files changed

+53
-39
lines changed

api/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
var loaded = false;
66

77
window.api = {
8-
version: "0.8.11",
8+
version: "0.8.12",
99
thumbnail: {}
1010
};
1111

api/video.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ api.video = that = {
5050
that.isPlaying = false;
5151
that.isStopped = true;
5252
dom.ctrlThumbnailVideo[ 0 ].pause();
53-
ui.stop();
53+
ui
54+
.canvasToggle( false )
55+
.stop()
56+
;
5457
}
5558
return that;
5659
},

api/visualisations.js

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
var
66
that,
77
obj,
8-
enable = false,
8+
selectedVisu,
99
visu = {},
1010
jqOldVisu = dom.empty,
11-
selectedVisu = $.noop,
1211
analyser = api.audio.analyser
1312
;
1413

@@ -21,6 +20,7 @@ if ( analyser ) {
2120
}
2221

2322
api.visualisations = that = {
23+
enable: false,
2424
add: function( name, fn ) {
2525
visu[ name ] = fn;
2626
$( "<li data-name='" + name + "'>" + name + "</li>" )
@@ -31,29 +31,26 @@ api.visualisations = that = {
3131
},
3232
select: function( name ) {
3333
if ( selectedVisu !== visu[ name ] ) {
34-
selectedVisu = visu[ name ] || $.noop;
3534
jqOldVisu.removeClass( "selected" );
3635
jqOldVisu = dom.ctrlVisualList.find( "[data-name='" + name + "']" ).addClass( "selected" );
37-
if ( enable ) {
38-
that.toggle( true );
39-
}
36+
selectedVisu = visu[ name ];
37+
ui.canvasRender( selectedVisu, obj );
4038
}
4139
return that;
4240
},
4341
toggle: function( b ) {
4442
if ( typeof b !== "boolean" ) {
45-
b = !enable;
43+
b = !that.enable;
4644
}
4745
if ( b && !api.audio.ctx ) {
4846
b = false;
4947
api.error.throw( "WEBAUDIO" );
5048
}
51-
ui
52-
.canvasToggle( b )
53-
.canvasRender( b && selectedVisu, obj )
54-
.visualisationsToggle( b )
55-
;
56-
enable = b;
49+
if ( api.video.type === "audio" ) {
50+
ui.canvasToggle( b );
51+
}
52+
ui.visualisationsToggle( b );
53+
that.enable = b;
5754
return that;
5855
}
5956
};

css/player/screen.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ $backLightWidth: 35%;
110110
}
111111
.fileplayer.video.webaudio & video.webaudio,
112112
.fileplayer.video:not(.webaudio) & video:not(.webaudio),
113-
.fileplayer.audio & canvas {
113+
.fileplayer.canvas-displayed & canvas {
114114
display: block;
115115
}
116116
.seeking {

main.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ui/canvas.js

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,44 @@
44

55
var
66
requestId,
7-
jqCanvas = dom.screenCanvas,
8-
canvas = jqCanvas[ 0 ],
7+
frameObj,
8+
frameFn = $.noop,
9+
canvas = dom.screenCanvas[ 0 ],
910
ctx = canvas.getContext( "2d" )
1011
;
1112

13+
function frame( timestamp ) {
14+
frameObj.timestamp = timestamp;
15+
frameFn( frameObj );
16+
if ( ui.canvasDisplayed ) {
17+
requestId = requestAnimationFrame( frame );
18+
}
19+
}
20+
1221
$.extend( ui, {
13-
canvasDisplay: false,
22+
canvasDisplayed: false,
1423
canvasToggle: function( b ) {
1524
if ( typeof b !== "boolean" ) {
16-
b = !ui.canvasDisplay;
25+
b = !ui.canvasDisplayed;
1726
}
18-
if ( b !== ui.canvasDisplay ) {
19-
ui.canvasDisplay = b;
20-
jqCanvas.toggle( b );
27+
ui.canvasDisplayed = b;
28+
ctx.clearRect( 0, 0, canvas.width, canvas.height );
29+
dom.fileplayer.toggleClass( "canvas-displayed", b );
30+
if ( b ) {
31+
if ( !requestId ) {
32+
requestId = requestAnimationFrame( frame );
33+
}
34+
} else {
35+
cancelAnimationFrame( requestId );
36+
requestId = null;
2137
}
2238
return ui;
2339
},
2440
canvasRender: function( fn, obj ) {
25-
function frame( timestamp ) {
26-
obj.timestamp = timestamp;
27-
fn( obj );
28-
requestId = requestAnimationFrame( frame );
29-
}
30-
ctx.clearRect( 0, 0, canvas.width, canvas.height );
31-
cancelAnimationFrame( requestId );
32-
if ( fn ) {
33-
obj = $.extend( obj, {
34-
ctxCanvas: ctx
35-
});
36-
requestId = requestAnimationFrame( frame );
37-
}
41+
frameFn = fn || $.noop;
42+
frameObj = $.extend( obj, {
43+
ctxCanvas: ctx
44+
});
3845
return ui;
3946
}
4047
});

ui/fileSelect.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
$.extend( ui, {
44
jqFileSelected: dom.empty,
55
fileSelect: function( jqFile ) {
6-
ui.jqFileSelected.removeClass( "selected" );
7-
ui.jqFileSelected = jqFile.addClass( "selected" );
6+
var fwrap = jqFile[ 0 ].fileWrapper;
87

98
function txt( btn, file, str ) {
109
file = file && file.fileWrapper;
@@ -14,8 +13,16 @@ $.extend( ui, {
1413
;
1514
}
1615

16+
// Write the tooltip on the prev/next buttons.
1717
txt( dom.ctrlPrevBtn, jqFile.prev()[ 0 ] || ui.jqFiles.get( -1 ), "Previous" );
1818
txt( dom.ctrlNextBtn, jqFile.next()[ 0 ] || ui.jqFiles[ 0 ], "Next" );
19+
20+
// toggleClass "selected" on the jqFile in the playlist's list.
21+
ui.jqFileSelected.removeClass( "selected" );
22+
ui.jqFileSelected = jqFile.addClass( "selected" );
23+
24+
// Display or not the canvas.
25+
ui.canvasToggle( fwrap.type === "audio" && api.visualisations.enable );
1926
return ui;
2027
}
2128
});

0 commit comments

Comments
 (0)