-
Notifications
You must be signed in to change notification settings - Fork 0
/
jspsych-draw-and-mouse-tracking.js
282 lines (215 loc) · 7.26 KB
/
jspsych-draw-and-mouse-tracking.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// 01/2021
// Author: Gustavo Juantorena
// https://github.com/GEJ1
jsPsych.plugins['draw-and-mouse-tracking'] = (function(){
var plugin = {};
plugin.info = {
name: 'draw-and-mouse-tracking',
parameters: {
stimulus: {
type: jsPsych.plugins.parameterType.IMAGE,
pretty_name: "Stimulus",
default: undefined,
description: "The image to be displayed",
},
canvas_width: {
type: jsPsych.plugins.parameterType.INT,
default: window.innerWidth,
description: 'The width of the canvas.'
},
canvas_height: {
type: jsPsych.plugins.parameterType.INT,
default: window.innerHeight,
description: 'The height of the canvas.'
},
canvas_background_color: {
type: jsPsych.plugins.parameterType.STRING,
default: 'grey',
description: 'The background color of the canvas.'
},
content_wrapper_color: {
type: jsPsych.plugins.parameterType.STRING,
default: 'grey',
description: 'The color of the content wrapper.'
},
drawline_color: {
type: jsPsych.plugins.parameterType.STRING,
default: 'green',
description: 'The color of the drawline.'
},
lineWidth: {
type: jsPsych.plugins.parameterType.INT,
default: 2,
description: 'The thickness of the drawline.'
},
response_ends_trial: {
type: jsPsych.plugins.parameterType.BOOL,
default: true,
description: 'If true, trial will end when subject makes a response.'
},
trial_duration: {
type: jsPsych.plugins.parameterType.INT,
default: null,
description: 'How long to show trial before it ends.'
}
}
}
let default_maxWidth;
plugin.trial = function(display_element, trial){
//global variables
let x = 0;
let y = 0;
isDrawing = false;
// data
let pos_tracking = [];
let cursor_time = [];
//background color //I need to improve this
document.getElementsByClassName("jspsych-content-wrapper")[0].style.backgroundColor = trial.content_wrapper_color; //Background color
let new_html =
'<canvas id="myCanvas" class="jspsych-canvas" width=' +
trial.canvas_width +
" height=" +
trial.canvas_height +
' style="background-color:' +
trial.canvas_background_color +
';"></canvas>';
imageObj = new Image(); // declare globally
imageObj.onload = function() {
// now set up handler when image is actually loaded
// - else drawImage will fail (width, height is not available and no data)
window.addEventListener('resize', resizeCanvas, false);
// initial call to draw image first time
resizeCanvas();
};
imageObj.src = trial.stimulus;
function resizeCanvas() { //I need to look the size
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
drawStuff();
}
function drawStuff() {
var x = (canvas.width - imageObj.width ) * 0.5,
y = (canvas.height - imageObj.height) * 0.5;
ctx.drawImage(imageObj, x, y);
}
// draw
display_element.innerHTML = new_html;
var canvas = document.getElementById('myCanvas');
const ctx = canvas.getContext('2d');
let start_time;
// trial time init
start_time = performance.now();
canvas.addEventListener("mouseup", mouseUpFunc);
canvas.addEventListener('mousedown', e => {
// console.log('entro a mousedown');
x = e.offsetX;
y = e.offsetY;
isDrawing = true;
});
canvas.addEventListener( "mousemove", function(e){
// console.log('entro a mouseMove');
if (isDrawing === true) {
// console.log('entro al IF de mouseMove');
drawLine(ctx, x, y, e.offsetX, e.offsetY);
x = e.offsetX;
y = e.offsetY;
}
mouseMove(e);
});
canvas.addEventListener('mouseup', e => {
if (isDrawing === true) {
drawLine(ctx, x, y, e.offsetX, e.offsetY);
x = 0;
y = 0;
isDrawing = false;
}
});
function mouseMove(e){
var x = e.clientX;
var y = e.clientY;
var coor = "(" + x + "," + y + ")";
console.log(coor);
pos_tracking.push(coor); //Save coor in array pos_tracking
//timer for cursor
// var startTime = Date.now();
let startTime = Math.round(performance.now()) ;
//start_time was declared at begining of the trial
let time_in_trial = Math.round(startTime - start_time)
// console.log("T1: " + startTime);
// console.log("T2: " + Math.round(start_time));
// console.log("T1 - T2: " + time_in_trial);
//cursor time is an array with the time measurement for every [x,y] position relative to the start of the trial
cursor_time.push(time_in_trial);
}
function drawLine(ctx, x1, y1, x2, y2) {
ctx.beginPath();
ctx.strokeStyle = trial.drawline_color; // drawLine color
ctx.lineWidth = trial.lineWidth;
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2)
ctx.stroke();
ctx.closePath();
}
function mouseUpFunc(e){
let release_click_time;
release_click_time = performance.now();
if (isDrawing === true) {
drawLine(ctx, x, y, e.offsetX, e.offsetY);
x = 0;
y = 0;
isDrawing = false;
}
let info = {
key: -1,
rt: release_click_time - start_time,
clickX: e.offsetX,
clickY: e.offsetY,
pos_tracking: pos_tracking ,
cursor_time: cursor_time
}
after_response(info);
}
// store response
var response = {
rt: null,
X_click: null,
Y_click: null
};
// function to end trial when it is time
var end_trial = function() {
var trial_data = {
"rt": response.rt,
"X_click": response.clickX,
"Y_click": response.clickY,
"position": JSON.stringify(pos_tracking),
"cursor time": JSON.stringify(cursor_time)
};
// document.getElementById('jspsych-content').style.maxWidth = default_maxWidth; // restore
//window.removeEventListener("mousedown", mouseDownFunc);
canvas.removeEventListener("mouseup", mouseUpFunc);
// kill any remaining setTimeout handlers
jsPsych.pluginAPI.clearAllTimeouts();
// gather the data to store for the trial
// clear the display
display_element.innerHTML = '';
// move on to the next trial
jsPsych.finishTrial(trial_data);
};
function after_response(info) {
// only record the first response
if (response.key == null) {
response = info;
}
if (trial.response_ends_trial) {
end_trial();
}
};
// end trial if trial_duration is set
if (trial.trial_duration !== null) {
jsPsych.pluginAPI.setTimeout(function() {
end_trial();
}, trial.trial_duration);
}
};
return plugin;
})();