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