Mercurial > hg > mood-conductor
comparison mcserver/static/app.js~ @ 12:9d9169751aba
new server
author | gyorgyf |
---|---|
date | Fri, 22 Jun 2012 11:48:28 +0100 |
parents | 180274c3b48a |
children | 9d57fcc848c0 |
comparison
equal
deleted
inserted
replaced
11:2c3fe4b24640 | 12:9d9169751aba |
---|---|
2 init: function() { | 2 init: function() { |
3 this.canvas = document.getElementById('canvas'); | 3 this.canvas = document.getElementById('canvas'); |
4 this.marker = document.getElementById('marker'); | 4 this.marker = document.getElementById('marker'); |
5 this.label = document.getElementById('label'); | 5 this.label = document.getElementById('label'); |
6 | 6 |
7 this.canvas.addEventListener('mousedown', this.onMouseDown.bind(this)); | 7 this.canvas.addEventListener('click', this.onMouseUp.bind(this)); |
8 this.canvas.addEventListener('mousemove', this.onMouseMove.bind(this)); | |
9 this.canvas.addEventListener('mouseup', this.onMouseUp.bind(this)); | |
10 | 8 |
11 this.request("/moods.csv", this.loadMoods.bind(this)); | 9 this.request("/moodconductor/moods.csv", this.loadMoods.bind(this)); |
10 <<<<<<< local | |
12 this.background(); | 11 this.background(); |
12 ======= | |
13 this.draw(); | |
14 >>>>>>> other | |
13 }, | 15 }, |
14 | 16 |
15 tl: { r: 200, g: 0, b: 0 }, | 17 tl: { r: 200, g: 0, b: 0 }, |
16 tr: { r: 200, g: 150, b: 0 }, | 18 tr: { r: 200, g: 150, b: 0 }, |
17 bl: { r: 0, g: 50, b: 100 }, | 19 bl: { r: 0, g: 50, b: 100 }, |
23 g: Math.floor(a.g + (b.g - a.g) * x), | 25 g: Math.floor(a.g + (b.g - a.g) * x), |
24 b: Math.floor(a.b + (b.b - a.b) * x) | 26 b: Math.floor(a.b + (b.b - a.b) * x) |
25 }; | 27 }; |
26 }, | 28 }, |
27 | 29 |
28 background: function() { | 30 draw: function() { |
29 var step = 20; | 31 var step = 20; |
30 var ctx = this.canvas.getContext("2d"); | 32 var ctx = this.canvas.getContext("2d"); |
31 ctx.clearRect(0, 0, 320, 320); | 33 ctx.clearRect(0, 0, 320, 320); |
32 | 34 |
33 for (var y = 0; y < 320; y += step) { | 35 for (var y = 0; y < 320; y += step) { |
37 var color = this.interpolateColor(left, right, x / 320); | 39 var color = this.interpolateColor(left, right, x / 320); |
38 ctx.fillStyle = "rgb(" + color.r + "," + color.g + "," + color.b + ")"; | 40 ctx.fillStyle = "rgb(" + color.r + "," + color.g + "," + color.b + ")"; |
39 ctx.fillRect(x, y, step, step); | 41 ctx.fillRect(x, y, step, step); |
40 } | 42 } |
41 } | 43 } |
44 | |
45 ctx.beginPath(); | |
42 ctx.strokeStyle = "rgb(0,0,0)"; | 46 ctx.strokeStyle = "rgb(0,0,0)"; |
43 ctx.moveTo(0, 160); | 47 ctx.moveTo(0, 160); |
44 ctx.lineTo(320, 160); | 48 ctx.lineTo(320, 160); |
45 ctx.stroke(); | 49 ctx.stroke(); |
46 | 50 |
70 ctx.translate(158, 120); | 74 ctx.translate(158, 120); |
71 ctx.rotate(Math.PI * 1.5); | 75 ctx.rotate(Math.PI * 1.5); |
72 ctx.font = "16px Arial"; | 76 ctx.font = "16px Arial"; |
73 ctx.fillText("Arousal", 0, 0); | 77 ctx.fillText("Arousal", 0, 0); |
74 ctx.restore(); | 78 ctx.restore(); |
79 | |
80 if (this.marker) { | |
81 ctx.fillStyle = "rgba(0, 0, 0, 0.5)"; | |
82 ctx.beginPath(); | |
83 ctx.arc(this.marker.x, this.marker.y, 20, 0, Math.PI*2, true); | |
84 ctx.fill(); | |
85 } | |
75 }, | 86 }, |
76 | 87 |
77 loadMoods: function(text) { | 88 loadMoods: function(text) { |
78 this.moods = []; | 89 this.moods = []; |
79 | 90 |
100 }.bind(this); | 111 }.bind(this); |
101 request.open("GET", url); | 112 request.open("GET", url); |
102 request.send(null); | 113 request.send(null); |
103 }, | 114 }, |
104 | 115 |
105 onMouseDown: function(event) { | 116 onMouseUp: function(event) { |
106 this.mouseDown = true; | 117 event.preventDefault(); |
107 this.setMarker(event.pageX, event.pageY); | 118 this.setMarker(event); |
119 this.sendPosition(event); | |
120 this.draw(); | |
108 }, | 121 }, |
109 | 122 |
110 onMouseMove: function(event) { | 123 sendPosition: function(event) { |
111 if (this.mouseDown) { | 124 var x = event.pageX / 320; |
112 this.setMarker(event.pageX, event.pageY); | 125 var y = 1 - event.pageY / 320; |
113 } | 126 |
127 this.request("/moodconductor/mood?x=" + x + "&y=" + y); | |
114 }, | 128 }, |
115 | 129 |
116 onMouseUp: function(event) { | 130 setMarker: function(event) { |
117 this.mouseDown = false; | 131 this.marker = { |
118 this.setMarker(event.pageX, event.pageY); | 132 x: event.pageX, |
133 y: event.pageY | |
134 }; | |
135 | |
136 <<<<<<< local | |
137 sendPosition: function(pageX, pageY) { | |
138 var x = pageX / 320; | |
139 var y = 1 - pageY / 320; | |
140 this.request("/moodconductor/mood?x=" + x + "&y=" + y); | |
119 }, | 141 }, |
120 | 142 |
121 setMarker: function(pageX, pageY) { | 143 setMarker: function(pageX, pageY) { |
122 this.background(); | 144 this.background(); |
123 this.drawMarker(pageX, pageY); | 145 this.drawMarker(pageX, pageY); |
124 | 146 |
125 var x = pageX / 320; | 147 var x = pageX / 320; |
126 var y = 1 - pageY / 320; | 148 var y = 1 - pageY / 320; |
149 ======= | |
150 var x = event.pageX / 320; | |
151 var y = 1 - event.pageY / 320; | |
152 >>>>>>> other | |
127 | 153 |
128 var mood = this.findMood(x, y); | 154 var mood = this.findMood(x, y); |
129 | 155 |
130 if (mood != this.mood) { | 156 this.label.innerHTML = mood.label; |
131 this.label.innerHTML = mood.label; | 157 this.mood = mood; |
132 this.mood = mood; | |
133 this.request("/mood?x=" + x + "&y=" + y); | |
134 } | |
135 }, | |
136 | |
137 drawMarker: function(x, y) { | |
138 var ctx = this.canvas.getContext("2d"); | |
139 ctx.fillStyle = "rgba(0, 0, 0, 0.5)"; | |
140 ctx.beginPath(); | |
141 ctx.arc(x, y, 20, 0, Math.PI*2, true); | |
142 ctx.fill(); | |
143 }, | 158 }, |
144 | 159 |
145 findMood: function(x, y) { | 160 findMood: function(x, y) { |
146 var distance = 1; | 161 var distance = 1; |
147 var index = null; | 162 var index = null; |