gyorgyf@0: var Application = { gyorgyf@0: init: function() { gyorgyf@0: this.canvas = document.getElementById('canvas'); gyorgyf@0: this.marker = document.getElementById('marker'); gyorgyf@0: this.label = document.getElementById('label'); gyorgyf@0: mgeorgi@10: this.canvas.addEventListener('click', this.onMouseUp.bind(this)); gyorgyf@0: mgeorgi@6: this.request("/moods.csv", this.loadMoods.bind(this)); mgeorgi@10: this.draw(); gyorgyf@0: }, gyorgyf@0: mgeorgi@6: tl: { r: 200, g: 0, b: 0 }, mgeorgi@6: tr: { r: 200, g: 150, b: 0 }, mgeorgi@6: bl: { r: 0, g: 50, b: 100 }, mgeorgi@6: br: { r: 200, g: 230, b: 80 }, mgeorgi@6: mgeorgi@6: interpolateColor: function(a, b, x) { mgeorgi@6: return { mgeorgi@6: r: Math.floor(a.r + (b.r - a.r) * x), mgeorgi@6: g: Math.floor(a.g + (b.g - a.g) * x), mgeorgi@6: b: Math.floor(a.b + (b.b - a.b) * x) mgeorgi@6: }; mgeorgi@6: }, mgeorgi@6: mgeorgi@10: draw: function() { mgeorgi@6: var step = 20; mgeorgi@6: var ctx = this.canvas.getContext("2d"); mgeorgi@6: ctx.clearRect(0, 0, 320, 320); mgeorgi@6: mgeorgi@6: for (var y = 0; y < 320; y += step) { mgeorgi@6: var left = this.interpolateColor(this.tl, this.bl, y / 320); mgeorgi@6: var right = this.interpolateColor(this.tr, this.br, y / 320); mgeorgi@6: for (var x = 0; x < 320; x += step) { mgeorgi@6: var color = this.interpolateColor(left, right, x / 320); mgeorgi@6: ctx.fillStyle = "rgb(" + color.r + "," + color.g + "," + color.b + ")"; mgeorgi@6: ctx.fillRect(x, y, step, step); mgeorgi@6: } mgeorgi@6: } mgeorgi@10: mgeorgi@10: ctx.beginPath(); mgeorgi@6: ctx.strokeStyle = "rgb(0,0,0)"; mgeorgi@6: ctx.moveTo(0, 160); mgeorgi@6: ctx.lineTo(320, 160); mgeorgi@6: ctx.stroke(); mgeorgi@6: mgeorgi@6: ctx.fillStyle = "rgb(0,0,0)"; mgeorgi@6: ctx.beginPath(); mgeorgi@6: ctx.moveTo(320, 160); mgeorgi@6: ctx.lineTo(310, 150); mgeorgi@6: ctx.lineTo(310, 170); mgeorgi@6: ctx.fill(); mgeorgi@6: mgeorgi@6: ctx.strokeStyle = "rgb(0,0,0)"; mgeorgi@6: ctx.moveTo(160, 320); mgeorgi@6: ctx.lineTo(160, 0); mgeorgi@6: ctx.stroke(); mgeorgi@6: mgeorgi@6: ctx.fillStyle = "rgb(0,0,0)"; mgeorgi@6: ctx.beginPath(); mgeorgi@6: ctx.moveTo(160, 0); mgeorgi@6: ctx.lineTo(150, 10); mgeorgi@6: ctx.lineTo(170, 10); mgeorgi@6: ctx.fill(); mgeorgi@6: mgeorgi@6: ctx.font = "16px Arial"; mgeorgi@6: ctx.fillText("Valence", 200, 158); mgeorgi@6: mgeorgi@6: ctx.save(); mgeorgi@6: ctx.translate(158, 120); mgeorgi@6: ctx.rotate(Math.PI * 1.5); mgeorgi@6: ctx.font = "16px Arial"; mgeorgi@6: ctx.fillText("Arousal", 0, 0); mgeorgi@6: ctx.restore(); mgeorgi@10: mgeorgi@10: if (this.marker) { mgeorgi@10: ctx.fillStyle = "rgba(0, 0, 0, 0.5)"; mgeorgi@10: ctx.beginPath(); mgeorgi@10: ctx.arc(this.marker.x, this.marker.y, 20, 0, Math.PI*2, true); mgeorgi@10: ctx.fill(); mgeorgi@10: } mgeorgi@6: }, mgeorgi@6: mgeorgi@6: loadMoods: function(text) { gyorgyf@0: this.moods = []; gyorgyf@0: gyorgyf@0: var lines = text.split("\n"); mgeorgi@6: gyorgyf@0: for (var i = 1; i < lines.length; i++) { gyorgyf@0: var row = lines[i].split(","); mgeorgi@6: var mood = { mgeorgi@6: label: row[0], mgeorgi@6: val: Number(row[1]), mgeorgi@6: aro: Number(row[2]), mgeorgi@6: dom: Number(row[3]) mgeorgi@6: }; mgeorgi@6: this.moods.push(mood); gyorgyf@0: } gyorgyf@0: }, mgeorgi@6: gyorgyf@0: request: function(url, callback) { gyorgyf@0: var request = new XMLHttpRequest(); gyorgyf@0: request.onreadystatechange = function() { mgeorgi@6: if (callback) { mgeorgi@6: callback(request.responseText); mgeorgi@6: } gyorgyf@0: }.bind(this); gyorgyf@0: request.open("GET", url); gyorgyf@0: request.send(null); gyorgyf@0: }, gyorgyf@0: mgeorgi@10: onMouseUp: function(event) { mgeorgi@10: event.preventDefault(); mgeorgi@10: this.setMarker(event); mgeorgi@10: this.sendPosition(event); mgeorgi@10: this.draw(); gyorgyf@0: }, gyorgyf@0: mgeorgi@10: sendPosition: function(event) { mgeorgi@10: var x = event.pageX / 320; mgeorgi@10: var y = 1 - event.pageY / 320; gyorgyf@0: mgeorgi@8: this.request("/mood?x=" + x + "&y=" + y); gyorgyf@0: }, gyorgyf@0: mgeorgi@10: setMarker: function(event) { mgeorgi@10: this.marker = { mgeorgi@10: x: event.pageX, mgeorgi@10: y: event.pageY mgeorgi@10: }; mgeorgi@6: mgeorgi@10: var x = event.pageX / 320; mgeorgi@10: var y = 1 - event.pageY / 320; gyorgyf@0: gyorgyf@0: var mood = this.findMood(x, y); gyorgyf@0: mgeorgi@8: this.label.innerHTML = mood.label; mgeorgi@8: this.mood = mood; gyorgyf@0: }, gyorgyf@0: gyorgyf@0: findMood: function(x, y) { gyorgyf@0: var distance = 1; gyorgyf@0: var index = null; gyorgyf@0: gyorgyf@0: for (var i = 0; i < this.moods.length; i++) { gyorgyf@0: var mood = this.moods[i]; mgeorgi@6: var dx = Math.abs(mood.val - x); mgeorgi@6: var dy = Math.abs(mood.aro - y); gyorgyf@0: var d = Math.sqrt(dx * dx + dy * dy); gyorgyf@0: gyorgyf@0: if (d < distance) { gyorgyf@0: distance = d; gyorgyf@0: index = i; gyorgyf@0: } gyorgyf@0: } gyorgyf@0: gyorgyf@0: return this.moods[index]; gyorgyf@0: } gyorgyf@0: };