annotate mcserver/static/app.js @ 54:c0b34039917a tip

Server: added an exposed function to log the start time of a performance (for log-to-audio sync)
author Mathieu Barthet <mathieu.barthet@eecs.qmul.ac.uk>
date Wed, 14 Oct 2015 19:20:08 +0100
parents f5ca9dbabe83
children
rev   line source
gyorgyf@0 1 var Application = {
mgeorgi@17 2 moods: [
mgeorgi@17 3 ['power',0.69,0.71,0.79,1],
mgeorgi@17 4 ['bright',0.81,0.55,0.67,2],
mgeorgi@17 5 ['brutal',0.23,0.7,0.45,3],
mgeorgi@17 6 ['confused',0.28,0.63,0.41,4],
mgeorgi@17 7 ['rock',0.57,0.44,0.52,5],
mgeorgi@17 8 ['serious',0.51,0.38,0.52,6],
mgeorgi@17 9 ['relaxed',0.75,0.17,0.57,7],
mgeorgi@17 10 ['calm',0.72,0.33,0.67,8],
mgeorgi@17 11 ['dark',0.46,0.41,0.48,9],
mgeorgi@17 12 ['dirty',0.26,0.49,0.46,10],
mgeorgi@17 13 ['energy',0.78,0.74,0.74,11],
mgeorgi@17 14 ['fun',0.92,0.78,0.73,12],
mgeorgi@17 15 ['aggressive',0.51,0.6,0.57,13],
mgeorgi@17 16 ['scary',0.28,0.71,0.33,14],
mgeorgi@17 17 ['positive',0.88,0.57,0.65,15],
mgeorgi@17 18 ['sad',0.08,0.39,0.31,16]
mgeorgi@17 19 ],
mgeorgi@17 20
gyorgyf@0 21 init: function() {
mgeorgi@23 22 this.is_touch_device = 'ontouchstart' in document.documentElement;
gyorgyf@0 23 this.canvas = document.getElementById('canvas');
gyorgyf@0 24 this.label = document.getElementById('label');
mgeorgi@17 25 this.draw();
mgeorgi@26 26 this.lastClick = new Date();
gyorgyf@0 27
mgeorgi@23 28 if (this.is_touch_device) {
mgeorgi@23 29 this.canvas.addEventListener('touchstart', function(event) {
mgeorgi@23 30 Application.onMouseUp(event.targetTouches[0]);
mgeorgi@23 31 });
mgeorgi@23 32 }
mgeorgi@23 33 else {
mgeorgi@23 34 this.canvas.addEventListener('click', function(event) {
mgeorgi@23 35 Application.onMouseUp(event);
mgeorgi@23 36 });
mgeorgi@23 37 }
gyorgyf@0 38 },
gyorgyf@0 39
mgeorgi@6 40 tl: { r: 200, g: 0, b: 0 },
mgeorgi@6 41 tr: { r: 200, g: 150, b: 0 },
mgeorgi@6 42 bl: { r: 0, g: 50, b: 100 },
mgeorgi@6 43 br: { r: 200, g: 230, b: 80 },
mgeorgi@6 44
mgeorgi@6 45 interpolateColor: function(a, b, x) {
mgeorgi@6 46 return {
mgeorgi@6 47 r: Math.floor(a.r + (b.r - a.r) * x),
mgeorgi@6 48 g: Math.floor(a.g + (b.g - a.g) * x),
mgeorgi@6 49 b: Math.floor(a.b + (b.b - a.b) * x)
mgeorgi@6 50 };
mgeorgi@6 51 },
mgeorgi@6 52
mgeorgi@11 53 draw: function() {
mgeorgi@6 54 var step = 20;
mgeorgi@6 55 var ctx = this.canvas.getContext("2d");
mgeorgi@6 56 ctx.clearRect(0, 0, 320, 320);
mgeorgi@6 57
mgeorgi@23 58 var list = [];
mgeorgi@23 59
mgeorgi@6 60 for (var y = 0; y < 320; y += step) {
mgeorgi@6 61 var left = this.interpolateColor(this.tl, this.bl, y / 320);
mgeorgi@6 62 var right = this.interpolateColor(this.tr, this.br, y / 320);
mgeorgi@6 63 for (var x = 0; x < 320; x += step) {
mgeorgi@6 64 var color = this.interpolateColor(left, right, x / 320);
mgeorgi@6 65 ctx.fillStyle = "rgb(" + color.r + "," + color.g + "," + color.b + ")";
mgeorgi@6 66 ctx.fillRect(x, y, step, step);
mgeorgi@6 67 }
mgeorgi@6 68 }
mgeorgi@11 69
mgeorgi@11 70 ctx.beginPath();
mgeorgi@6 71 ctx.strokeStyle = "rgb(0,0,0)";
mgeorgi@6 72 ctx.moveTo(0, 160);
mgeorgi@6 73 ctx.lineTo(320, 160);
mgeorgi@6 74 ctx.stroke();
mgeorgi@6 75
mgeorgi@6 76 ctx.fillStyle = "rgb(0,0,0)";
mgeorgi@6 77 ctx.beginPath();
mgeorgi@6 78 ctx.moveTo(320, 160);
mgeorgi@6 79 ctx.lineTo(310, 150);
mgeorgi@6 80 ctx.lineTo(310, 170);
mgeorgi@6 81 ctx.fill();
mgeorgi@6 82
mgeorgi@6 83 ctx.strokeStyle = "rgb(0,0,0)";
mgeorgi@6 84 ctx.moveTo(160, 320);
mgeorgi@6 85 ctx.lineTo(160, 0);
mgeorgi@6 86 ctx.stroke();
mgeorgi@6 87
mgeorgi@6 88 ctx.fillStyle = "rgb(0,0,0)";
mgeorgi@6 89 ctx.beginPath();
mgeorgi@6 90 ctx.moveTo(160, 0);
mgeorgi@6 91 ctx.lineTo(150, 10);
mgeorgi@6 92 ctx.lineTo(170, 10);
mgeorgi@6 93 ctx.fill();
mgeorgi@6 94
mgeorgi@6 95 ctx.font = "16px Arial";
gyorgyf@29 96 ctx.fillText("Positive", 245, 158);
gyorgyf@29 97 ctx.fillText("Negative", 15, 158);
gyorgyf@29 98
mgeorgi@6 99
mgeorgi@6 100 ctx.save();
gyorgyf@29 101 ctx.translate(158, 75);
mgeorgi@6 102 ctx.rotate(Math.PI * 1.5);
mgeorgi@6 103 ctx.font = "16px Arial";
gyorgyf@29 104 ctx.fillText("Excited", 0, 0);
mgeorgi@6 105 ctx.restore();
gyorgyf@29 106
gyorgyf@29 107 ctx.save();
gyorgyf@29 108 ctx.translate(158, 305);
gyorgyf@29 109 ctx.rotate(Math.PI * 1.5);
gyorgyf@29 110 ctx.font = "16px Arial";
gyorgyf@29 111 ctx.fillText("Calm", 0, 0);
gyorgyf@29 112 ctx.restore();
gyorgyf@29 113
mgeorgi@11 114
mgeorgi@11 115 if (this.marker) {
mgeorgi@11 116 ctx.fillStyle = "rgba(0, 0, 0, 0.5)";
mgeorgi@11 117 ctx.beginPath();
mgeorgi@11 118 ctx.arc(this.marker.x, this.marker.y, 20, 0, Math.PI*2, true);
mgeorgi@11 119 ctx.fill();
mgeorgi@11 120 }
mgeorgi@6 121 },
mgeorgi@6 122
gyorgyf@0 123 request: function(url, callback) {
gyorgyf@0 124 var request = new XMLHttpRequest();
gyorgyf@0 125 request.open("GET", url);
gyorgyf@0 126 request.send(null);
gyorgyf@0 127 },
gyorgyf@0 128
mgeorgi@11 129 onMouseUp: function(event) {
mgeorgi@26 130 if ((new Date() - this.lastClick) > 1000) {
mgeorgi@26 131 this.setMarker(event);
mgeorgi@26 132 this.sendPosition(event);
mgeorgi@26 133 this.draw();
mgeorgi@26 134 this.lastClick = new Date();
mgeorgi@26 135 }
gyorgyf@0 136 },
gyorgyf@0 137
mgeorgi@11 138 sendPosition: function(event) {
mgeorgi@11 139 var x = event.pageX / 320;
mgeorgi@11 140 var y = 1 - event.pageY / 320;
mgeorgi@11 141
mgeorgi@11 142 this.request("/moodconductor/mood?x=" + x + "&y=" + y);
gyorgyf@0 143 },
gyorgyf@0 144
mgeorgi@11 145 setMarker: function(event) {
mgeorgi@11 146 this.marker = {
mgeorgi@11 147 x: event.pageX,
mgeorgi@11 148 y: event.pageY
mgeorgi@11 149 };
mgeorgi@9 150
mgeorgi@11 151 var x = event.pageX / 320;
mgeorgi@11 152 var y = 1 - event.pageY / 320;
gyorgyf@0 153
mgeorgi@17 154 this.label.innerHTML = this.findMood(x, y);
gyorgyf@0 155 },
gyorgyf@0 156
gyorgyf@0 157 findMood: function(x, y) {
gyorgyf@0 158 var distance = 1;
gyorgyf@0 159 var index = null;
gyorgyf@0 160
gyorgyf@0 161 for (var i = 0; i < this.moods.length; i++) {
gyorgyf@0 162 var mood = this.moods[i];
mgeorgi@17 163 var dx = Math.abs(mood[1] - x);
mgeorgi@17 164 var dy = Math.abs(mood[2] - y);
gyorgyf@0 165 var d = Math.sqrt(dx * dx + dy * dy);
gyorgyf@0 166
gyorgyf@0 167 if (d < distance) {
gyorgyf@0 168 distance = d;
gyorgyf@0 169 index = i;
gyorgyf@0 170 }
gyorgyf@0 171 }
gyorgyf@0 172
mgeorgi@17 173 return this.moods[index][0];
gyorgyf@0 174 }
gyorgyf@0 175 };