forked from shyras/osweb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sketchpad.js
126 lines (109 loc) · 3.59 KB
/
sketchpad.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
import GenericResponse from './generic_response.js'
import Canvas from '../backends/canvas.js'
/**
* Class representing a Sketchpad item.
* @extends GeneralResponse
*/
export default class Sketchpad extends GenericResponse {
constructor (experiment, name, script) {
super(experiment, name, script)
// Create and set public properties.
this.canvas = new Canvas(experiment, false)
this.elements = []
// Process the script.
this.from_string(script)
}
/**
* Sort function used for determining the draw index (z-index) of alle elemente.
* @param {Object} a - The first object to compare.
* @param {Object} b - The second object to compare.
* @return {Number} - The result of the comparison.
*/
_compare (a, b) {
// Sort function used for determining the draw index (z-index) of alle elemente.
if (a.z_index() < b.z_index()) { return 1 } else if (a.z_index() > b.z_index()) { return -1 } else { return 0 }
}
/** Implements the complete phase of the Sketchpad item. */
_complete () {
// Inherited.
super._complete()
}
/** Resets all item variables to their default value. */
reset () {
// Resets all item variables to their default value.
this.elements = []
this.vars.duration = 'keypress'
}
/** Process a time out response. */
process_response_timeout () {
// Nothing happens
}
/**
* Parse a definition string and retrieve all properties of the item.
* @param {String} script - The script containing the properties of the item.
*/
from_string (script) {
// Define and reset variables to their defaults.
this.variables = {}
this.comments = []
this.reset()
// Split the string into an array of lines.
if (script !== null) {
const lines = script.split('\n')
for (let i = 0; i < lines.length; i++) {
if ((lines[i] !== '') && (this.parse_variable(lines[i]) === false)) {
const tokens = this.syntax.split(lines[i])
if ((tokens.length > 0) && (tokens[0] === 'draw')) {
if (this.experiment.items._isClass(tokens[1]) === true) {
var element = this.experiment.items._newElementClass(tokens[1], this, lines[i])
this.elements.push(element)
} else {
this.experiment._runner._debugger.addError('Failed to parse definition: ' + tokens[1])
}
}
}
}
// Sort the elements usin the z-index.
this.elements.sort(this._compare)
}
}
/**
* Set the background color of the canvas if it is defined in the var store
*
* @memberof Sketchpad
*/
_set_bg_color () {
const backgroundColor = this.vars.get('background')
if (backgroundColor) {
this.canvas._styles.background_color = backgroundColor
}
}
/** Implements the prepare phase of an item. */
prepare () {
// Clear the canvas.
this.canvas.clear()
// Draw the elements.
for (let i = 0; i < this.elements.length; i++) {
if (this.elements[i].is_shown() === true) {
this.elements[i].draw()
}
}
// Inherited.
super.prepare()
}
/** Implements the run phase of the Sketschpad. */
run () {
// Inherited.
super.run()
this._set_bg_color()
// Set the onset and start the stimulus response process.
this.set_item_onset(this.canvas.show())
this.set_sri(false)
this.process_response()
}
* coroutine () {
yield
this._set_bg_color()
this.set_item_onset(this.canvas.show())
}
}