@jspsych/plugin-tobii-validation 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1003 @@
1
+ var jsPsychTobiiValidation = (function (jspsych) {
2
+ 'use strict';
3
+
4
+ var version = "0.1.1";
5
+
6
+ class ValidationDisplay {
7
+ constructor(displayElement, params) {
8
+ this.displayElement = displayElement;
9
+ this.params = params;
10
+ this.currentPoint = null;
11
+ this.progressElement = null;
12
+ this.currentX = 0.5;
13
+ this.currentY = 0.5;
14
+ this.container = this.createContainer();
15
+ this.displayElement.appendChild(this.container);
16
+ if (params.show_progress) {
17
+ this.progressElement = this.createProgressIndicator();
18
+ this.displayElement.appendChild(this.progressElement);
19
+ }
20
+ }
21
+ createContainer() {
22
+ const container = document.createElement("div");
23
+ container.className = "tobii-validation-container";
24
+ return container;
25
+ }
26
+ createProgressIndicator() {
27
+ const progress = document.createElement("div");
28
+ progress.className = "tobii-validation-progress";
29
+ progress.setAttribute("role", "status");
30
+ progress.setAttribute("aria-live", "polite");
31
+ return progress;
32
+ }
33
+ async showInstructions() {
34
+ const wrapper = document.createElement("div");
35
+ wrapper.className = "tobii-validation-instructions";
36
+ wrapper.setAttribute("role", "dialog");
37
+ wrapper.setAttribute("aria-label", "Eye tracker validation instructions");
38
+ const content = document.createElement("div");
39
+ content.className = "instructions-content";
40
+ const heading = document.createElement("h2");
41
+ heading.textContent = "Eye Tracker Validation";
42
+ content.appendChild(heading);
43
+ const paragraph = document.createElement("p");
44
+ paragraph.innerHTML = this.params.instructions || "Look at each point to validate calibration accuracy.";
45
+ content.appendChild(paragraph);
46
+ const button = document.createElement("button");
47
+ button.className = "validation-start-btn";
48
+ button.textContent = "Start Validation";
49
+ content.appendChild(button);
50
+ wrapper.appendChild(content);
51
+ this.container.appendChild(wrapper);
52
+ return new Promise((resolve) => {
53
+ button.addEventListener("click", () => {
54
+ wrapper.remove();
55
+ resolve();
56
+ });
57
+ });
58
+ }
59
+ /**
60
+ * Initialize the traveling point at screen center
61
+ */
62
+ async initializePoint() {
63
+ if (this.currentPoint)
64
+ return;
65
+ this.currentPoint = document.createElement("div");
66
+ this.currentPoint.className = "tobii-validation-point";
67
+ this.currentPoint.setAttribute("role", "img");
68
+ this.currentPoint.setAttribute("aria-label", "Validation target point");
69
+ const x = 0.5 * window.innerWidth;
70
+ const y = 0.5 * window.innerHeight;
71
+ this.currentX = 0.5;
72
+ this.currentY = 0.5;
73
+ Object.assign(this.currentPoint.style, {
74
+ left: `${x}px`,
75
+ top: `${y}px`,
76
+ width: `${this.params.point_size || 20}px`,
77
+ height: `${this.params.point_size || 20}px`,
78
+ backgroundColor: this.params.point_color || "#00ff00",
79
+ transition: "none"
80
+ });
81
+ this.container.appendChild(this.currentPoint);
82
+ await this.delay(this.params.zoom_duration || 300);
83
+ }
84
+ /**
85
+ * Travel to the next point location with smooth animation
86
+ */
87
+ async travelToPoint(point, index, total) {
88
+ if (!this.currentPoint) {
89
+ await this.initializePoint();
90
+ }
91
+ if (this.progressElement) {
92
+ this.progressElement.textContent = `Point ${index + 1} of ${total}`;
93
+ }
94
+ this.currentPoint.setAttribute("aria-label", `Validation target point ${index + 1} of ${total}`);
95
+ const dx = point.x - this.currentX;
96
+ const dy = point.y - this.currentY;
97
+ const distance = Math.sqrt(dx * dx + dy * dy);
98
+ const travelDuration = Math.max(150, Math.min(400, 150 + distance * 200));
99
+ const x = point.x * window.innerWidth;
100
+ const y = point.y * window.innerHeight;
101
+ this.currentPoint.style.transition = `left ${travelDuration}ms ease-in-out, top ${travelDuration}ms ease-in-out`;
102
+ this.currentPoint.classList.remove("animation-zoom-out", "animation-zoom-in");
103
+ this.currentPoint.style.left = `${x}px`;
104
+ this.currentPoint.style.top = `${y}px`;
105
+ this.currentX = point.x;
106
+ this.currentY = point.y;
107
+ await this.delay(travelDuration);
108
+ }
109
+ /**
110
+ * Play zoom out animation (point grows larger)
111
+ */
112
+ async playZoomOut() {
113
+ if (!this.currentPoint)
114
+ return;
115
+ this.currentPoint.style.transition = "none";
116
+ this.currentPoint.classList.remove("animation-zoom-in");
117
+ this.currentPoint.classList.add("animation-zoom-out");
118
+ await this.delay(this.params.zoom_duration || 300);
119
+ }
120
+ /**
121
+ * Play zoom in animation (point shrinks to fixation size)
122
+ */
123
+ async playZoomIn() {
124
+ if (!this.currentPoint)
125
+ return;
126
+ this.currentPoint.classList.remove("animation-zoom-out");
127
+ this.currentPoint.classList.add("animation-zoom-in");
128
+ await this.delay(this.params.zoom_duration || 300);
129
+ }
130
+ /**
131
+ * Reset point state after data collection (keeps element for continued travel)
132
+ */
133
+ async resetPointForTravel() {
134
+ if (!this.currentPoint)
135
+ return;
136
+ this.currentPoint.classList.remove("animation-zoom-out", "animation-zoom-in");
137
+ this.currentPoint.style.transform = "translate(-50%, -50%) scale(1)";
138
+ await this.delay(50);
139
+ }
140
+ async hidePoint() {
141
+ if (this.currentPoint) {
142
+ this.currentPoint.remove();
143
+ this.currentPoint = null;
144
+ }
145
+ await this.delay(200);
146
+ }
147
+ /**
148
+ * Show validation result
149
+ * @param success Whether validation passed
150
+ * @param averageAccuracyNorm Average accuracy in normalized units
151
+ * @param averagePrecisionNorm Average precision in normalized units
152
+ * @param pointData Per-point validation data
153
+ * @param tolerance Tolerance threshold
154
+ * @param canRetry Whether a retry button should be shown on failure
155
+ * @returns 'retry' if user chose to retry, 'continue' otherwise
156
+ */
157
+ async showResult(success, averageAccuracyNorm, _averagePrecisionNorm, pointData, tolerance, canRetry = false) {
158
+ const result = document.createElement("div");
159
+ result.className = "tobii-validation-result";
160
+ result.setAttribute("role", "alert");
161
+ result.setAttribute("aria-live", "assertive");
162
+ let feedbackHTML = "";
163
+ if (this.params.show_feedback && pointData) {
164
+ feedbackHTML = this.createVisualFeedback(pointData, tolerance);
165
+ }
166
+ const statusClass = success ? "success" : "error";
167
+ const statusText = success ? "Validation Passed" : "Validation Failed";
168
+ let buttonsHTML;
169
+ if (success) {
170
+ buttonsHTML = `<button class="validation-continue-btn">Continue</button>`;
171
+ } else if (canRetry) {
172
+ buttonsHTML = `<button class="validation-retry-btn">Retry</button>
173
+ <button class="validation-continue-btn" style="margin-left: 10px;">Continue</button>`;
174
+ } else {
175
+ buttonsHTML = `<button class="validation-continue-btn">Continue</button>`;
176
+ }
177
+ result.innerHTML = `
178
+ <div class="tobii-validation-result-content ${statusClass}">
179
+ <h2>${statusText}</h2>
180
+ <p>Average error: ${((averageAccuracyNorm || 0) * 100).toFixed(1)}% (tolerance: ${((tolerance || 0) * 100).toFixed(0)}%)</p>
181
+ ${feedbackHTML}
182
+ ${buttonsHTML}
183
+ </div>
184
+ `;
185
+ this.container.appendChild(result);
186
+ return new Promise((resolve) => {
187
+ const retryBtn = result.querySelector(".validation-retry-btn");
188
+ const continueBtn = result.querySelector(".validation-continue-btn");
189
+ retryBtn?.addEventListener("click", () => {
190
+ result.remove();
191
+ resolve("retry");
192
+ });
193
+ continueBtn?.addEventListener("click", () => {
194
+ result.remove();
195
+ resolve("continue");
196
+ });
197
+ });
198
+ }
199
+ /**
200
+ * Reset display state for a retry attempt
201
+ */
202
+ resetForRetry() {
203
+ this.container.innerHTML = "";
204
+ this.currentPoint = null;
205
+ this.currentX = 0.5;
206
+ this.currentY = 0.5;
207
+ }
208
+ createVisualFeedback(pointData, tolerance) {
209
+ const tol = tolerance || 0.05;
210
+ const targetMarkers = pointData.map((data, idx) => {
211
+ const x = data.point.x * 100;
212
+ const y = data.point.y * 100;
213
+ return `
214
+ <div class="feedback-target" style="
215
+ left: ${x}%;
216
+ top: ${y}%;
217
+ " title="Target ${idx + 1}">
218
+ <span class="target-label">${idx + 1}</span>
219
+ </div>
220
+ `;
221
+ }).join("");
222
+ const gazeMarkers = pointData.map((data, idx) => {
223
+ if (!data.meanGaze)
224
+ return "";
225
+ const x = data.meanGaze.x * 100;
226
+ const y = data.meanGaze.y * 100;
227
+ const withinTolerance = data.accuracyNorm <= tol;
228
+ const colorClass = withinTolerance ? "gaze-pass" : "gaze-fail";
229
+ const statusSymbol = withinTolerance ? "\u2713" : "\u2717";
230
+ const statusLabel = withinTolerance ? "pass" : "fail";
231
+ return `
232
+ <div class="feedback-gaze ${colorClass}" style="
233
+ left: ${x}%;
234
+ top: ${y}%;
235
+ " title="Point ${idx + 1}: ${statusLabel}, error ${(data.accuracyNorm * 100).toFixed(1)}%"
236
+ aria-label="Point ${idx + 1}: ${statusLabel}, error ${(data.accuracyNorm * 100).toFixed(1)}%">
237
+ <span class="gaze-label">${statusSymbol}</span>
238
+ </div>
239
+ `;
240
+ }).join("");
241
+ const connectionLines = pointData.map((data) => {
242
+ if (!data.meanGaze)
243
+ return "";
244
+ const x1 = data.point.x * 100;
245
+ const y1 = data.point.y * 100;
246
+ const x2 = data.meanGaze.x * 100;
247
+ const y2 = data.meanGaze.y * 100;
248
+ const withinTolerance = data.accuracyNorm <= tol;
249
+ const lineColor = withinTolerance ? "#4ade80" : "#f87171";
250
+ return `
251
+ <svg class="feedback-line" style="position: absolute; left: 0; top: 0; width: 100%; height: 100%; pointer-events: none;">
252
+ <line x1="${x1}%" y1="${y1}%" x2="${x2}%" y2="${y2}%"
253
+ stroke="${lineColor}" stroke-width="2" stroke-dasharray="5,3" opacity="0.7"/>
254
+ </svg>
255
+ `;
256
+ }).join("");
257
+ const gazeSampleDots = pointData.map((data) => {
258
+ if (!data.gazeSamples)
259
+ return "";
260
+ const withinTolerance = data.accuracyNorm <= tol;
261
+ const sampleClass = withinTolerance ? "sample-pass" : "sample-fail";
262
+ return data.gazeSamples.map((sample) => {
263
+ const x = sample.x * 100;
264
+ const y = sample.y * 100;
265
+ return `<div class="feedback-sample ${sampleClass}" style="left: ${x}%; top: ${y}%;"></div>`;
266
+ }).join("");
267
+ }).join("");
268
+ const aspectRatio = window.innerWidth / window.innerHeight;
269
+ const canvas = `
270
+ <div class="validation-feedback">
271
+ <div class="feedback-canvas-fullscreen" style="aspect-ratio: ${aspectRatio.toFixed(3)};">
272
+ ${connectionLines}
273
+ ${gazeSampleDots}
274
+ ${targetMarkers}
275
+ ${gazeMarkers}
276
+ </div>
277
+ <div class="feedback-legend">
278
+ <span><span class="legend-color target-legend"></span> Target</span>
279
+ <span><span class="legend-color gaze-pass-legend"></span> Pass (\u2713)</span>
280
+ <span><span class="legend-color gaze-fail-legend"></span> Fail (\u2717)</span>
281
+ </div>
282
+ </div>
283
+ `;
284
+ return canvas;
285
+ }
286
+ clear() {
287
+ this.container.innerHTML = "";
288
+ if (this.progressElement) {
289
+ this.progressElement.textContent = "";
290
+ }
291
+ }
292
+ delay(ms) {
293
+ return new Promise((resolve) => setTimeout(resolve, ms));
294
+ }
295
+ }
296
+
297
+ const info = {
298
+ name: "tobii-validation",
299
+ version,
300
+ parameters: {
301
+ /** Number of validation points (5 or 9) */
302
+ validation_points: {
303
+ type: jspsych.ParameterType.INT,
304
+ default: 9
305
+ },
306
+ /** Size of validation points in pixels */
307
+ point_size: {
308
+ type: jspsych.ParameterType.INT,
309
+ default: 20
310
+ },
311
+ /** Color of validation points */
312
+ point_color: {
313
+ type: jspsych.ParameterType.STRING,
314
+ default: "#00ff00"
315
+ },
316
+ /** Duration to collect data at each point (ms) */
317
+ collection_duration: {
318
+ type: jspsych.ParameterType.INT,
319
+ default: 1e3
320
+ },
321
+ /** Show progress indicator */
322
+ show_progress: {
323
+ type: jspsych.ParameterType.BOOL,
324
+ default: true
325
+ },
326
+ /** Custom validation points */
327
+ custom_points: {
328
+ type: jspsych.ParameterType.COMPLEX,
329
+ default: null
330
+ },
331
+ /** Show visual feedback */
332
+ show_feedback: {
333
+ type: jspsych.ParameterType.BOOL,
334
+ default: true
335
+ },
336
+ /** Instructions text */
337
+ instructions: {
338
+ type: jspsych.ParameterType.STRING,
339
+ default: "Look at each point as it appears on the screen to validate calibration accuracy."
340
+ },
341
+ /** Background color of the validation container */
342
+ background_color: {
343
+ type: jspsych.ParameterType.STRING,
344
+ default: "#808080"
345
+ },
346
+ /** Primary button color */
347
+ button_color: {
348
+ type: jspsych.ParameterType.STRING,
349
+ default: "#28a745"
350
+ },
351
+ /** Primary button hover color */
352
+ button_hover_color: {
353
+ type: jspsych.ParameterType.STRING,
354
+ default: "#218838"
355
+ },
356
+ /** Retry button color */
357
+ retry_button_color: {
358
+ type: jspsych.ParameterType.STRING,
359
+ default: "#dc3545"
360
+ },
361
+ /** Retry button hover color */
362
+ retry_button_hover_color: {
363
+ type: jspsych.ParameterType.STRING,
364
+ default: "#c82333"
365
+ },
366
+ /** Success message color */
367
+ success_color: {
368
+ type: jspsych.ParameterType.STRING,
369
+ default: "#28a745"
370
+ },
371
+ /** Error message color */
372
+ error_color: {
373
+ type: jspsych.ParameterType.STRING,
374
+ default: "#dc3545"
375
+ },
376
+ /** Normalized tolerance for acceptable accuracy (0-1 scale, validation passes if average error <= this) */
377
+ tolerance: {
378
+ type: jspsych.ParameterType.FLOAT,
379
+ default: 0.05
380
+ },
381
+ /** Maximum number of retry attempts allowed on validation failure */
382
+ max_retries: {
383
+ type: jspsych.ParameterType.INT,
384
+ default: 1
385
+ },
386
+ /** Duration of zoom in/out animations in ms */
387
+ zoom_duration: {
388
+ type: jspsych.ParameterType.INT,
389
+ default: 300
390
+ }
391
+ },
392
+ data: {
393
+ /** Validation success status */
394
+ validation_success: {
395
+ type: jspsych.ParameterType.BOOL
396
+ },
397
+ /** Average accuracy */
398
+ average_accuracy: {
399
+ type: jspsych.ParameterType.FLOAT
400
+ },
401
+ /** Average precision */
402
+ average_precision: {
403
+ type: jspsych.ParameterType.FLOAT
404
+ },
405
+ /** Number of validation points used */
406
+ num_points: {
407
+ type: jspsych.ParameterType.INT
408
+ },
409
+ /** Full validation result data */
410
+ validation_data: {
411
+ type: jspsych.ParameterType.COMPLEX
412
+ },
413
+ /** Normalized tolerance used for pass/fail determination */
414
+ tolerance: {
415
+ type: jspsych.ParameterType.FLOAT
416
+ },
417
+ /** Number of validation attempts made */
418
+ num_attempts: {
419
+ type: jspsych.ParameterType.INT
420
+ }
421
+ }
422
+ };
423
+ class TobiiValidationPlugin {
424
+ constructor(jsPsych) {
425
+ this.jsPsych = jsPsych;
426
+ }
427
+ static {
428
+ this.info = info;
429
+ }
430
+ static removeStyles() {
431
+ const el = document.getElementById("tobii-validation-styles");
432
+ if (el) {
433
+ el.remove();
434
+ }
435
+ }
436
+ injectStyles(trial) {
437
+ TobiiValidationPlugin.removeStyles();
438
+ const css = `
439
+ .tobii-validation-container {
440
+ position: fixed;
441
+ top: 0;
442
+ left: 0;
443
+ width: 100%;
444
+ height: 100%;
445
+ background-color: ${trial.background_color};
446
+ font-family: 'Open Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
447
+ z-index: 9999;
448
+ }
449
+
450
+ .tobii-validation-instructions {
451
+ position: absolute;
452
+ top: 50%;
453
+ left: 50%;
454
+ transform: translate(-50%, -50%);
455
+ background-color: white;
456
+ padding: 40px;
457
+ border-radius: 10px;
458
+ box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
459
+ text-align: center;
460
+ max-width: 600px;
461
+ }
462
+
463
+ .tobii-validation-instructions h2 {
464
+ margin-top: 0;
465
+ margin-bottom: 20px;
466
+ font-size: 24px;
467
+ color: #333;
468
+ }
469
+
470
+ .tobii-validation-instructions p {
471
+ margin-bottom: 20px;
472
+ font-size: 16px;
473
+ line-height: 1.5;
474
+ color: #666;
475
+ }
476
+
477
+ .validation-start-btn,
478
+ .validation-continue-btn,
479
+ .validation-retry-btn {
480
+ background-color: ${trial.button_color};
481
+ color: white;
482
+ border: none;
483
+ padding: 12px 30px;
484
+ font-size: 16px;
485
+ border-radius: 5px;
486
+ cursor: pointer;
487
+ transition: background-color 0.3s;
488
+ }
489
+
490
+ .validation-start-btn:hover,
491
+ .validation-continue-btn:hover {
492
+ background-color: ${trial.button_hover_color};
493
+ }
494
+
495
+ .validation-retry-btn {
496
+ background-color: ${trial.retry_button_color};
497
+ }
498
+
499
+ .validation-retry-btn:hover {
500
+ background-color: ${trial.retry_button_hover_color};
501
+ }
502
+
503
+ .tobii-validation-point {
504
+ position: absolute;
505
+ border-radius: 50%;
506
+ transform: translate(-50%, -50%);
507
+ box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
508
+ }
509
+
510
+ .tobii-validation-point.animation-zoom-out {
511
+ animation: tobii-validation-zoom-out ${trial.zoom_duration / 1e3}s ease-out forwards;
512
+ }
513
+
514
+ @keyframes tobii-validation-zoom-out {
515
+ 0% {
516
+ transform: translate(-50%, -50%) scale(1);
517
+ }
518
+ 100% {
519
+ transform: translate(-50%, -50%) scale(2.5);
520
+ }
521
+ }
522
+
523
+ .tobii-validation-point.animation-zoom-in {
524
+ animation: tobii-validation-zoom-in ${trial.zoom_duration / 1e3}s ease-out forwards;
525
+ }
526
+
527
+ @keyframes tobii-validation-zoom-in {
528
+ 0% {
529
+ transform: translate(-50%, -50%) scale(2.5);
530
+ }
531
+ 100% {
532
+ transform: translate(-50%, -50%) scale(1);
533
+ }
534
+ }
535
+
536
+ .tobii-validation-progress {
537
+ position: fixed;
538
+ top: 20px;
539
+ left: 50%;
540
+ transform: translateX(-50%);
541
+ background-color: rgba(0, 0, 0, 0.7);
542
+ color: white;
543
+ padding: 10px 20px;
544
+ border-radius: 5px;
545
+ font-size: 14px;
546
+ z-index: 10000;
547
+ }
548
+
549
+ .tobii-validation-result {
550
+ position: absolute;
551
+ top: 50%;
552
+ left: 50%;
553
+ transform: translate(-50%, -50%);
554
+ background-color: white;
555
+ padding: 30px 40px;
556
+ border-radius: 10px;
557
+ box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
558
+ text-align: center;
559
+ width: 60vw;
560
+ max-width: 800px;
561
+ max-height: 85vh;
562
+ overflow-y: auto;
563
+ }
564
+
565
+ .tobii-validation-result-content h2 {
566
+ margin-top: 0;
567
+ margin-bottom: 20px;
568
+ font-size: 24px;
569
+ }
570
+
571
+ .tobii-validation-result-content.success h2 {
572
+ color: ${trial.success_color};
573
+ }
574
+
575
+ .tobii-validation-result-content.error h2 {
576
+ color: ${trial.error_color};
577
+ }
578
+
579
+ .tobii-validation-result-content p {
580
+ margin-bottom: 15px;
581
+ font-size: 16px;
582
+ color: #666;
583
+ }
584
+
585
+ .validation-feedback {
586
+ margin: 30px 0;
587
+ }
588
+
589
+ .validation-feedback h3 {
590
+ margin-bottom: 15px;
591
+ font-size: 18px;
592
+ color: #333;
593
+ }
594
+
595
+ .feedback-canvas {
596
+ position: relative;
597
+ width: 100%;
598
+ height: 300px;
599
+ background-color: #f0f0f0;
600
+ border: 2px solid #ddd;
601
+ border-radius: 5px;
602
+ margin-bottom: 15px;
603
+ }
604
+
605
+ .feedback-point {
606
+ position: absolute;
607
+ width: 20px;
608
+ height: 20px;
609
+ border-radius: 50%;
610
+ transform: translate(-50%, -50%);
611
+ border: 2px solid #333;
612
+ cursor: help;
613
+ }
614
+
615
+ .feedback-legend {
616
+ display: flex;
617
+ justify-content: center;
618
+ gap: 20px;
619
+ font-size: 14px;
620
+ color: #666;
621
+ }
622
+
623
+ .feedback-legend span {
624
+ display: flex;
625
+ align-items: center;
626
+ gap: 5px;
627
+ }
628
+
629
+ .legend-color {
630
+ display: inline-block;
631
+ width: 15px;
632
+ height: 15px;
633
+ border-radius: 50%;
634
+ border: 1px solid #333;
635
+ }
636
+
637
+ .target-legend {
638
+ background-color: transparent;
639
+ border: 3px solid #333;
640
+ }
641
+
642
+ .feedback-canvas-fullscreen {
643
+ position: relative;
644
+ width: 100%;
645
+ background-color: #2a2a2a;
646
+ border: 2px solid #444;
647
+ border-radius: 5px;
648
+ margin-bottom: 15px;
649
+ overflow: hidden;
650
+ }
651
+
652
+ .feedback-target {
653
+ position: absolute;
654
+ width: 24px;
655
+ height: 24px;
656
+ border-radius: 50%;
657
+ transform: translate(-50%, -50%);
658
+ border: 3px solid #fff;
659
+ background-color: transparent;
660
+ z-index: 10;
661
+ display: flex;
662
+ align-items: center;
663
+ justify-content: center;
664
+ }
665
+
666
+ .target-label {
667
+ color: #fff;
668
+ font-size: 10px;
669
+ font-weight: bold;
670
+ }
671
+
672
+ .feedback-gaze {
673
+ position: absolute;
674
+ width: 20px;
675
+ height: 20px;
676
+ border-radius: 50%;
677
+ transform: translate(-50%, -50%);
678
+ border: 2px solid;
679
+ z-index: 11;
680
+ display: flex;
681
+ align-items: center;
682
+ justify-content: center;
683
+ cursor: help;
684
+ }
685
+
686
+ .gaze-label {
687
+ color: #000;
688
+ font-size: 9px;
689
+ font-weight: bold;
690
+ }
691
+
692
+ .feedback-sample {
693
+ position: absolute;
694
+ width: 4px;
695
+ height: 4px;
696
+ border-radius: 50%;
697
+ transform: translate(-50%, -50%);
698
+ background-color: rgba(100, 100, 255, 0.4);
699
+ z-index: 5;
700
+ }
701
+
702
+ .accuracy-table {
703
+ width: 100%;
704
+ border-collapse: collapse;
705
+ margin-top: 15px;
706
+ font-size: 13px;
707
+ text-align: left;
708
+ }
709
+
710
+ .accuracy-table th,
711
+ .accuracy-table td {
712
+ padding: 8px 12px;
713
+ border: 1px solid #ddd;
714
+ }
715
+
716
+ .accuracy-table th {
717
+ background-color: #f5f5f5;
718
+ font-weight: 600;
719
+ color: #333;
720
+ }
721
+
722
+ .accuracy-table tr:nth-child(even) {
723
+ background-color: #fafafa;
724
+ }
725
+
726
+ .accuracy-table td {
727
+ color: #555;
728
+ }
729
+
730
+ .saccade-note {
731
+ font-size: 12px;
732
+ color: #888;
733
+ font-style: italic;
734
+ margin-top: 10px;
735
+ }
736
+
737
+ .tolerance-info {
738
+ font-size: 14px;
739
+ color: #666;
740
+ }
741
+
742
+ .gaze-pass-legend {
743
+ background-color: #4ade80;
744
+ }
745
+
746
+ .gaze-fail-legend {
747
+ background-color: #f87171;
748
+ }
749
+
750
+ .feedback-gaze.gaze-pass {
751
+ background-color: #4ade80;
752
+ border-color: #22c55e;
753
+ }
754
+
755
+ .feedback-gaze.gaze-fail {
756
+ background-color: #f87171;
757
+ border-color: #ef4444;
758
+ }
759
+
760
+ .feedback-sample.sample-pass {
761
+ background-color: rgba(74, 222, 128, 0.5);
762
+ }
763
+
764
+ .feedback-sample.sample-fail {
765
+ background-color: rgba(248, 113, 113, 0.5);
766
+ }
767
+ `;
768
+ const styleElement = document.createElement("style");
769
+ styleElement.id = "tobii-validation-styles";
770
+ styleElement.textContent = css;
771
+ document.head.appendChild(styleElement);
772
+ }
773
+ async trial(display_element, trial) {
774
+ this.injectStyles(trial);
775
+ const tobiiExt = this.jsPsych.extensions.tobii;
776
+ if (!tobiiExt) {
777
+ throw new Error("Tobii extension not initialized");
778
+ }
779
+ if (!tobiiExt.isConnected()) {
780
+ throw new Error("Not connected to Tobii server");
781
+ }
782
+ const validationDisplay = new ValidationDisplay(
783
+ display_element,
784
+ trial
785
+ );
786
+ await validationDisplay.showInstructions();
787
+ let points;
788
+ if (trial.custom_points) {
789
+ points = this.validateCustomPoints(trial.custom_points);
790
+ } else {
791
+ points = this.getValidationPoints(trial.validation_points);
792
+ }
793
+ const maxAttempts = 1 + trial.max_retries;
794
+ let attempt = 0;
795
+ let validationPassed = false;
796
+ let avgAccuracyNorm = 0;
797
+ let avgPrecisionNorm = 0;
798
+ let validationResult = { success: false };
799
+ try {
800
+ while (attempt < maxAttempts) {
801
+ attempt++;
802
+ const retriesRemaining = maxAttempts - attempt;
803
+ await tobiiExt.startValidation();
804
+ await tobiiExt.startTracking();
805
+ await validationDisplay.initializePoint();
806
+ for (let i = 0; i < points.length; i++) {
807
+ const point = points[i];
808
+ await validationDisplay.travelToPoint(point, i, points.length);
809
+ await validationDisplay.playZoomOut();
810
+ await validationDisplay.playZoomIn();
811
+ const collectionStartTime = performance.now();
812
+ await this.delay(trial.collection_duration);
813
+ const collectionEndTime = performance.now();
814
+ const gazeSamples = await tobiiExt.getGazeData(collectionStartTime, collectionEndTime);
815
+ await tobiiExt.collectValidationPoint(point.x, point.y, gazeSamples);
816
+ if (i < points.length - 1) {
817
+ await validationDisplay.resetPointForTravel();
818
+ }
819
+ }
820
+ await validationDisplay.hidePoint();
821
+ await tobiiExt.stopTracking();
822
+ validationResult = await tobiiExt.computeValidation();
823
+ avgAccuracyNorm = validationResult.averageAccuracyNorm || 0;
824
+ avgPrecisionNorm = validationResult.averagePrecisionNorm || 0;
825
+ validationPassed = validationResult.success && avgAccuracyNorm <= trial.tolerance;
826
+ const userChoice = await validationDisplay.showResult(
827
+ validationPassed,
828
+ avgAccuracyNorm,
829
+ avgPrecisionNorm,
830
+ validationResult.pointData || [],
831
+ trial.tolerance,
832
+ retriesRemaining > 0
833
+ );
834
+ if (userChoice === "continue") {
835
+ break;
836
+ }
837
+ validationDisplay.resetForRetry();
838
+ }
839
+ } finally {
840
+ validationDisplay.clear();
841
+ display_element.innerHTML = "";
842
+ TobiiValidationPlugin.removeStyles();
843
+ }
844
+ const trial_data = {
845
+ validation_success: validationPassed,
846
+ average_accuracy: avgAccuracyNorm,
847
+ average_precision: avgPrecisionNorm,
848
+ tolerance: trial.tolerance,
849
+ num_points: points.length,
850
+ validation_data: validationResult,
851
+ num_attempts: attempt
852
+ };
853
+ this.jsPsych.finishTrial(trial_data);
854
+ }
855
+ /**
856
+ * Validate custom validation points
857
+ */
858
+ validateCustomPoints(points) {
859
+ if (!Array.isArray(points) || points.length === 0) {
860
+ throw new Error("custom_points must be a non-empty array");
861
+ }
862
+ const validated = [];
863
+ for (let i = 0; i < points.length; i++) {
864
+ const point = points[i];
865
+ if (typeof point !== "object" || point === null || typeof point.x !== "number" || typeof point.y !== "number") {
866
+ throw new Error(`Invalid validation point at index ${i}: must have numeric x and y`);
867
+ }
868
+ if (point.x < 0 || point.x > 1 || point.y < 0 || point.y > 1) {
869
+ throw new Error(
870
+ `Validation point at index ${i} out of range: x and y must be between 0 and 1`
871
+ );
872
+ }
873
+ validated.push({ x: point.x, y: point.y });
874
+ }
875
+ return validated;
876
+ }
877
+ /**
878
+ * Get standard validation points for the given grid size
879
+ */
880
+ getValidationPoints(count) {
881
+ switch (count) {
882
+ case 5:
883
+ return [
884
+ { x: 0.1, y: 0.1 },
885
+ { x: 0.9, y: 0.1 },
886
+ { x: 0.5, y: 0.5 },
887
+ { x: 0.1, y: 0.9 },
888
+ { x: 0.9, y: 0.9 }
889
+ ];
890
+ case 9:
891
+ return [
892
+ { x: 0.1, y: 0.1 },
893
+ { x: 0.5, y: 0.1 },
894
+ { x: 0.9, y: 0.1 },
895
+ { x: 0.1, y: 0.5 },
896
+ { x: 0.5, y: 0.5 },
897
+ { x: 0.9, y: 0.5 },
898
+ { x: 0.1, y: 0.9 },
899
+ { x: 0.5, y: 0.9 },
900
+ { x: 0.9, y: 0.9 }
901
+ ];
902
+ case 13:
903
+ return [
904
+ { x: 0.1, y: 0.1 },
905
+ { x: 0.5, y: 0.1 },
906
+ { x: 0.9, y: 0.1 },
907
+ { x: 0.3, y: 0.3 },
908
+ { x: 0.7, y: 0.3 },
909
+ { x: 0.1, y: 0.5 },
910
+ { x: 0.5, y: 0.5 },
911
+ { x: 0.9, y: 0.5 },
912
+ { x: 0.3, y: 0.7 },
913
+ { x: 0.7, y: 0.7 },
914
+ { x: 0.1, y: 0.9 },
915
+ { x: 0.5, y: 0.9 },
916
+ { x: 0.9, y: 0.9 }
917
+ ];
918
+ case 15:
919
+ return [
920
+ { x: 0.1, y: 0.1 },
921
+ { x: 0.5, y: 0.1 },
922
+ { x: 0.9, y: 0.1 },
923
+ { x: 0.1, y: 0.3 },
924
+ { x: 0.5, y: 0.3 },
925
+ { x: 0.9, y: 0.3 },
926
+ { x: 0.1, y: 0.5 },
927
+ { x: 0.5, y: 0.5 },
928
+ { x: 0.9, y: 0.5 },
929
+ { x: 0.1, y: 0.7 },
930
+ { x: 0.5, y: 0.7 },
931
+ { x: 0.9, y: 0.7 },
932
+ { x: 0.1, y: 0.9 },
933
+ { x: 0.5, y: 0.9 },
934
+ { x: 0.9, y: 0.9 }
935
+ ];
936
+ case 19:
937
+ return [
938
+ { x: 0.1, y: 0.1 },
939
+ { x: 0.5, y: 0.1 },
940
+ { x: 0.9, y: 0.1 },
941
+ { x: 0.1, y: 0.3 },
942
+ { x: 0.3, y: 0.3 },
943
+ { x: 0.5, y: 0.3 },
944
+ { x: 0.7, y: 0.3 },
945
+ { x: 0.9, y: 0.3 },
946
+ { x: 0.1, y: 0.5 },
947
+ { x: 0.5, y: 0.5 },
948
+ { x: 0.9, y: 0.5 },
949
+ { x: 0.1, y: 0.7 },
950
+ { x: 0.3, y: 0.7 },
951
+ { x: 0.5, y: 0.7 },
952
+ { x: 0.7, y: 0.7 },
953
+ { x: 0.9, y: 0.7 },
954
+ { x: 0.1, y: 0.9 },
955
+ { x: 0.5, y: 0.9 },
956
+ { x: 0.9, y: 0.9 }
957
+ ];
958
+ case 25:
959
+ return [
960
+ { x: 0.1, y: 0.1 },
961
+ { x: 0.3, y: 0.1 },
962
+ { x: 0.5, y: 0.1 },
963
+ { x: 0.7, y: 0.1 },
964
+ { x: 0.9, y: 0.1 },
965
+ { x: 0.1, y: 0.3 },
966
+ { x: 0.3, y: 0.3 },
967
+ { x: 0.5, y: 0.3 },
968
+ { x: 0.7, y: 0.3 },
969
+ { x: 0.9, y: 0.3 },
970
+ { x: 0.1, y: 0.5 },
971
+ { x: 0.3, y: 0.5 },
972
+ { x: 0.5, y: 0.5 },
973
+ { x: 0.7, y: 0.5 },
974
+ { x: 0.9, y: 0.5 },
975
+ { x: 0.1, y: 0.7 },
976
+ { x: 0.3, y: 0.7 },
977
+ { x: 0.5, y: 0.7 },
978
+ { x: 0.7, y: 0.7 },
979
+ { x: 0.9, y: 0.7 },
980
+ { x: 0.1, y: 0.9 },
981
+ { x: 0.3, y: 0.9 },
982
+ { x: 0.5, y: 0.9 },
983
+ { x: 0.7, y: 0.9 },
984
+ { x: 0.9, y: 0.9 }
985
+ ];
986
+ default:
987
+ throw new Error(
988
+ `Unsupported validation_points value: ${count}. Use 5, 9, 13, 15, 19, or 25, or provide custom_points.`
989
+ );
990
+ }
991
+ }
992
+ /**
993
+ * Delay helper
994
+ */
995
+ delay(ms) {
996
+ return new Promise((resolve) => setTimeout(resolve, ms));
997
+ }
998
+ }
999
+
1000
+ return TobiiValidationPlugin;
1001
+
1002
+ })(jsPsychModule);
1003
+ //# sourceMappingURL=https://unpkg.com/@jspsych/plugin-tobii-validation@0.1.1/dist/index.browser.js.map