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