gyorgyf@0: var Application = { mgeorgi@17: moods: [ mgeorgi@17: ['power',0.69,0.71,0.79,1], mgeorgi@17: ['bright',0.81,0.55,0.67,2], mgeorgi@17: ['brutal',0.23,0.7,0.45,3], mgeorgi@17: ['confused',0.28,0.63,0.41,4], mgeorgi@17: ['rock',0.57,0.44,0.52,5], mgeorgi@17: ['serious',0.51,0.38,0.52,6], mgeorgi@17: ['relaxed',0.75,0.17,0.57,7], mgeorgi@17: ['calm',0.72,0.33,0.67,8], mgeorgi@17: ['dark',0.46,0.41,0.48,9], mgeorgi@17: ['dirty',0.26,0.49,0.46,10], mgeorgi@17: ['energy',0.78,0.74,0.74,11], mgeorgi@17: ['fun',0.92,0.78,0.73,12], mgeorgi@17: ['aggressive',0.51,0.6,0.57,13], mgeorgi@17: ['scary',0.28,0.71,0.33,14], mgeorgi@17: ['positive',0.88,0.57,0.65,15], mgeorgi@17: ['sad',0.08,0.39,0.31,16] mgeorgi@17: ], mgeorgi@17: gyorgyf@0: init: function() { mgeorgi@23: this.is_touch_device = 'ontouchstart' in document.documentElement; gyorgyf@0: this.canvas = document.getElementById('canvas'); gyorgyf@0: this.label = document.getElementById('label'); mgeorgi@17: this.draw(); mgeorgi@26: this.lastClick = new Date(); gyorgyf@0: mgeorgi@23: if (this.is_touch_device) { mgeorgi@23: this.canvas.addEventListener('touchstart', function(event) { mgeorgi@23: Application.onMouseUp(event.targetTouches[0]); mgeorgi@23: }); mgeorgi@23: } mgeorgi@23: else { mgeorgi@23: this.canvas.addEventListener('click', function(event) { mgeorgi@23: Application.onMouseUp(event); mgeorgi@23: }); mgeorgi@23: } 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@11: 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@23: var list = []; mgeorgi@23: 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@11: mgeorgi@11: 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"; gyorgyf@29: ctx.fillText("Positive", 245, 158); gyorgyf@29: ctx.fillText("Negative", 15, 158); gyorgyf@29: mgeorgi@6: mgeorgi@6: ctx.save(); gyorgyf@29: ctx.translate(158, 75); mgeorgi@6: ctx.rotate(Math.PI * 1.5); mgeorgi@6: ctx.font = "16px Arial"; gyorgyf@29: ctx.fillText("Excited", 0, 0); mgeorgi@6: ctx.restore(); gyorgyf@29: gyorgyf@29: ctx.save(); gyorgyf@29: ctx.translate(158, 305); gyorgyf@29: ctx.rotate(Math.PI * 1.5); gyorgyf@29: ctx.font = "16px Arial"; gyorgyf@29: ctx.fillText("Calm", 0, 0); gyorgyf@29: ctx.restore(); gyorgyf@29: mgeorgi@11: mgeorgi@11: if (this.marker) { mgeorgi@11: ctx.fillStyle = "rgba(0, 0, 0, 0.5)"; mgeorgi@11: ctx.beginPath(); mgeorgi@11: ctx.arc(this.marker.x, this.marker.y, 20, 0, Math.PI*2, true); mgeorgi@11: ctx.fill(); mgeorgi@11: } mgeorgi@6: }, mgeorgi@6: gyorgyf@0: request: function(url, callback) { gyorgyf@0: var request = new XMLHttpRequest(); gyorgyf@0: request.open("GET", url); gyorgyf@0: request.send(null); gyorgyf@0: }, gyorgyf@0: mgeorgi@11: onMouseUp: function(event) { mgeorgi@26: if ((new Date() - this.lastClick) > 1000) { mgeorgi@26: this.setMarker(event); mgeorgi@26: this.sendPosition(event); mgeorgi@26: this.draw(); mgeorgi@26: this.lastClick = new Date(); mgeorgi@26: } gyorgyf@0: }, gyorgyf@0: mgeorgi@11: sendPosition: function(event) { mgeorgi@11: var x = event.pageX / 320; mgeorgi@11: var y = 1 - event.pageY / 320; mgeorgi@11: mgeorgi@11: this.request("/moodconductor/mood?x=" + x + "&y=" + y); gyorgyf@0: }, gyorgyf@0: mgeorgi@11: setMarker: function(event) { mgeorgi@11: this.marker = { mgeorgi@11: x: event.pageX, mgeorgi@11: y: event.pageY mgeorgi@11: }; mgeorgi@9: mgeorgi@11: var x = event.pageX / 320; mgeorgi@11: var y = 1 - event.pageY / 320; gyorgyf@0: mgeorgi@17: this.label.innerHTML = this.findMood(x, y); 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@17: var dx = Math.abs(mood[1] - x); mgeorgi@17: var dy = Math.abs(mood[2] - 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: mgeorgi@17: return this.moods[index][0]; gyorgyf@0: } gyorgyf@0: };