-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathvisuals.js
56 lines (50 loc) · 1.68 KB
/
visuals.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
var i = {};
var funct={};
var fadeRate = 100;
var w; // width of our drawing context
var h; // height of our drawing context
var c; // a canvas context to use in live coding of visuals
var draw = function() {}; // redefined in performance, called every animation frame
var play = function(e) {}; // redefined in performance, called with events from server (i.e. /play messages from Tidal)
var play2 = function(e) {
// convert array from Tidal into a JSON object
var f = {};
for(var n=0;n<e.length;n+=2) {
f[e[n]] = e[n+1];
}
play(f);
};
function setupVisuals() {
// add a new canvas adjusted to display dimensions and store context for later use
$('<canvas>').attr({id: "gcanvas"}).css({zIndex: $('canvas').length + 3}).insertBefore('#global');
$('#global').css({zIndex: $('canvas').length + 3});
adjustCanvasDimensions();
c = document.getElementById('gcanvas').getContext('2d');
c.fillStyle = "white";
c.strokeStyle = "white";
// and activate our animation callback function 'tick'
requestAnimationFrame(tick); // activate our animation callback function 'tick'
console.log('visuals are set');
};
function adjustCanvasDimensions() {
w = $("#global").width();
h = $("#global").height();
var canvas = document.getElementById("gcanvas");
canvas.width = w;
canvas.height = h;
}
function tick() {
draw();
$.each(funct, function(k,v) { eval(v); });
requestAnimationFrame(tick);
}
function retick() { // useful if in livecoding an error crashes animation callback
console.log("retick");
requestAnimationFrame(tick);
}
function clear () {
c.save();
c.setTransform(1,0,0,1,0,0);
c.clearRect(0,0,w,h)
c.restore();
};