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@23: this.canvas.addEventListener('mousedown', this.onMouseDown.bind(this)); mgeorgi@23: this.canvas.addEventListener('mousemove', this.onMouseMove.bind(this)); mgeorgi@23: this.canvas.addEventListener('mouseup', this.onMouseUp.bind(this)); gyorgyf@0: mgeorgi@23: this.request("/moods.csv", this.loadMoods.bind(this)); mgeorgi@9: this.background(); mgeorgi@9: }, mgeorgi@9: mgeorgi@9: tl: { r: 200, g: 0, b: 0 }, mgeorgi@9: tr: { r: 200, g: 150, b: 0 }, mgeorgi@9: bl: { r: 0, g: 50, b: 100 }, mgeorgi@9: br: { r: 200, g: 230, b: 80 }, mgeorgi@9: mgeorgi@9: interpolateColor: function(a, b, x) { mgeorgi@9: return { mgeorgi@9: r: Math.floor(a.r + (b.r - a.r) * x), mgeorgi@9: g: Math.floor(a.g + (b.g - a.g) * x), mgeorgi@9: b: Math.floor(a.b + (b.b - a.b) * x) mgeorgi@9: }; mgeorgi@9: }, mgeorgi@9: mgeorgi@23: background: function() { mgeorgi@9: var step = 20; mgeorgi@9: var ctx = this.canvas.getContext("2d"); mgeorgi@9: ctx.clearRect(0, 0, 320, 320); mgeorgi@9: mgeorgi@9: for (var y = 0; y < 320; y += step) { mgeorgi@9: var left = this.interpolateColor(this.tl, this.bl, y / 320); mgeorgi@9: var right = this.interpolateColor(this.tr, this.br, y / 320); mgeorgi@9: for (var x = 0; x < 320; x += step) { mgeorgi@9: var color = this.interpolateColor(left, right, x / 320); mgeorgi@9: ctx.fillStyle = "rgb(" + color.r + "," + color.g + "," + color.b + ")"; mgeorgi@9: ctx.fillRect(x, y, step, step); mgeorgi@9: } mgeorgi@9: } mgeorgi@9: ctx.strokeStyle = "rgb(0,0,0)"; mgeorgi@9: ctx.moveTo(0, 160); mgeorgi@9: ctx.lineTo(320, 160); mgeorgi@9: ctx.stroke(); mgeorgi@9: mgeorgi@9: ctx.fillStyle = "rgb(0,0,0)"; mgeorgi@9: ctx.beginPath(); mgeorgi@9: ctx.moveTo(320, 160); mgeorgi@9: ctx.lineTo(310, 150); mgeorgi@9: ctx.lineTo(310, 170); mgeorgi@9: ctx.fill(); mgeorgi@9: mgeorgi@9: ctx.strokeStyle = "rgb(0,0,0)"; mgeorgi@9: ctx.moveTo(160, 320); mgeorgi@9: ctx.lineTo(160, 0); mgeorgi@9: ctx.stroke(); mgeorgi@9: mgeorgi@9: ctx.fillStyle = "rgb(0,0,0)"; mgeorgi@9: ctx.beginPath(); mgeorgi@9: ctx.moveTo(160, 0); mgeorgi@9: ctx.lineTo(150, 10); mgeorgi@9: ctx.lineTo(170, 10); mgeorgi@9: ctx.fill(); mgeorgi@9: mgeorgi@9: ctx.font = "16px Arial"; mgeorgi@9: ctx.fillText("Valence", 200, 158); mgeorgi@9: mgeorgi@9: ctx.save(); mgeorgi@9: ctx.translate(158, 120); mgeorgi@9: ctx.rotate(Math.PI * 1.5); mgeorgi@9: ctx.font = "16px Arial"; mgeorgi@9: ctx.fillText("Arousal", 0, 0); mgeorgi@9: ctx.restore(); gyorgyf@0: }, gyorgyf@0: mgeorgi@6: loadMoods: function(text) { gyorgyf@0: this.moods = []; gyorgyf@0: gyorgyf@0: var lines = text.split("\n"); mgeorgi@6: mgeorgi@6: for (var i = 1; i < lines.length; i++) { mgeorgi@6: var row = lines[i].split(","); mgeorgi@6: var mood = { mgeorgi@9: label: row[0], mgeorgi@9: val: Number(row[1]), mgeorgi@9: aro: Number(row[2]), mgeorgi@9: dom: Number(row[3]) mgeorgi@6: }; mgeorgi@6: this.moods.push(mood); mgeorgi@6: } mgeorgi@6: }, mgeorgi@9: 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@23: onMouseDown: function(event) { mgeorgi@23: this.mouseDown = true; mgeorgi@23: this.setMarker(event.pageX, event.pageY); gyorgyf@0: }, gyorgyf@0: mgeorgi@23: onMouseMove: function(event) { mgeorgi@23: if (this.mouseDown) { mgeorgi@23: this.setMarker(event.pageX, event.pageY); mgeorgi@23: } gyorgyf@0: }, gyorgyf@0: mgeorgi@23: onMouseUp: function(event) { mgeorgi@23: this.mouseDown = false; mgeorgi@23: this.setMarker(event.pageX, event.pageY); gyorgyf@0: }, gyorgyf@0: mgeorgi@6: setMarker: function(pageX, pageY) { mgeorgi@9: this.background(); mgeorgi@9: this.drawMarker(pageX, pageY); mgeorgi@9: mgeorgi@6: var x = pageX / 320; mgeorgi@6: var y = 1 - pageY / 320; mgeorgi@6: mgeorgi@6: var mood = this.findMood(x, y); mgeorgi@6: mgeorgi@23: if (mood != this.mood) { mgeorgi@23: this.label.innerHTML = mood.label; mgeorgi@23: this.mood = mood; mgeorgi@23: this.request("/mood?x=" + x + "&y=" + y); mgeorgi@23: } mgeorgi@23: }, mgeorgi@23: mgeorgi@23: drawMarker: function(x, y) { mgeorgi@23: var ctx = this.canvas.getContext("2d"); mgeorgi@23: ctx.fillStyle = "rgba(0, 0, 0, 0.5)"; mgeorgi@23: ctx.beginPath(); mgeorgi@23: ctx.arc(x, y, 20, 0, Math.PI*2, true); mgeorgi@23: ctx.fill(); mgeorgi@9: }, mgeorgi@6: mgeorgi@6: findMood: function(x, y) { mgeorgi@6: var distance = 1; mgeorgi@6: var index = null; gyorgyf@0: gyorgyf@0: for (var i = 0; i < this.moods.length; i++) { gyorgyf@0: var mood = this.moods[i]; mgeorgi@9: var dx = Math.abs(mood.val - x); mgeorgi@9: var dy = Math.abs(mood.aro - y); mgeorgi@6: var d = Math.sqrt(dx * dx + dy * dy); gyorgyf@0: mgeorgi@6: if (d < distance) { mgeorgi@6: distance = d; mgeorgi@6: index = i; gyorgyf@0: } gyorgyf@0: } gyorgyf@0: mgeorgi@6: return this.moods[index]; gyorgyf@0: } mgeorgi@23: };