visual/VisualStim.js

  1. /** @module visual **/
  2. /**
  3. * Base class for all visual stimuli.
  4. *
  5. * @author Alain Pitiot
  6. * @version 2022.2.0
  7. * @copyright (c) 2017-2020 Ilixa Ltd. (http://ilixa.com) (c) 2020-2022 Open Science Tools Ltd. (https://opensciencetools.org)
  8. * @license Distributed under the terms of the MIT License
  9. */
  10. import * as PIXI from "pixi.js-legacy";
  11. import { MinimalStim } from "../core/MinimalStim.js";
  12. import { WindowMixin } from "../core/WindowMixin.js";
  13. import * as util from "../util/Util.js";
  14. /**
  15. * Base class for all visual stimuli.
  16. *
  17. * @extends MinimalStim
  18. * @mixes WindowMixin
  19. */
  20. export class VisualStim extends util.mix(MinimalStim).with(WindowMixin)
  21. {
  22. /**
  23. * @param {Object} options
  24. * @param {String} options.name - the name used when logging messages from this stimulus
  25. * @param {module:core.Window} options.win - the associated Window
  26. * @param {string} [options.units= "height"] - the units of the stimulus (e.g. for size, position, vertices)
  27. * @param {number} [options.ori= 0.0] - the orientation (in degrees)
  28. * @param {number} [options.opacity= 1.0] - the opacity
  29. * @param {number} [options.depth= 0] - the depth (i.e. the z order)
  30. * @param {Array.<number>} [options.pos= [0, 0]] - the position of the center of the stimulus
  31. * @param {number} [options.size= 1.0] - the size
  32. * @param {PIXI.Graphics} [options.clipMask= null] - the clip mask
  33. * @param {boolean} [options.autoDraw= false] - whether or not the stimulus should be automatically drawn on every frame flip
  34. * @param {boolean} [options.autoLog= false] - whether or not to log
  35. */
  36. constructor({ name, win, units, ori, opacity, depth, pos, size, clipMask, autoDraw, autoLog } = {})
  37. {
  38. super({ win, name, autoDraw, autoLog });
  39. this._addAttribute(
  40. "units",
  41. units,
  42. (typeof win !== "undefined" && win !== null) ? win.units : "height",
  43. this._onChange(true, true),
  44. );
  45. this._addAttribute(
  46. "pos",
  47. pos,
  48. [0, 0],
  49. );
  50. this._addAttribute(
  51. "size",
  52. size,
  53. undefined,
  54. );
  55. this._addAttribute(
  56. "ori",
  57. ori,
  58. 0.0,
  59. );
  60. this._addAttribute(
  61. "opacity",
  62. opacity,
  63. 1.0,
  64. this._onChange(true, false),
  65. );
  66. this._addAttribute(
  67. "depth",
  68. depth,
  69. 0,
  70. this._onChange(false, false),
  71. );
  72. this._addAttribute(
  73. "clipMask",
  74. clipMask,
  75. null,
  76. this._onChange(false, false),
  77. );
  78. // bounding box of the stimulus, in stimulus units
  79. // note: boundingBox does not take the orientation into account
  80. this._addAttribute("boundingBox", PIXI.Rectangle.EMPTY);
  81. // the stimulus need to be updated:
  82. this._needUpdate = true;
  83. // the PIXI representation also needs to be updated:
  84. this._needPixiUpdate = true;
  85. }
  86. /**
  87. * Force a refresh of the stimulus.
  88. *
  89. * refresh() is called, in particular, when the Window is resized.
  90. */
  91. refresh()
  92. {
  93. this._onChange(true, true)();
  94. }
  95. /**
  96. * Setter for the size attribute.
  97. *
  98. * @param {undefined | null | number | number[]} size - the stimulus size
  99. * @param {boolean} [log= false] - whether of not to log
  100. */
  101. setSize(size, log = false)
  102. {
  103. // size is either undefined, null, or a tuple of numbers:
  104. if (typeof size !== "undefined" && size !== null)
  105. {
  106. size = util.toNumerical(size);
  107. if (!Array.isArray(size))
  108. {
  109. size = [size, size];
  110. }
  111. }
  112. const hasChanged = this._setAttribute("size", size, log);
  113. if (hasChanged)
  114. {
  115. this._onChange(true, true)();
  116. }
  117. }
  118. /**
  119. * Setter for the orientation attribute.
  120. *
  121. * @param {number} ori - the orientation in degree with 0 as the vertical position, positive values rotate clockwise.
  122. * @param {boolean} [log= false] - whether of not to log
  123. */
  124. setOri(ori, log = false)
  125. {
  126. const hasChanged = this._setAttribute("ori", ori, log);
  127. if (hasChanged)
  128. {
  129. let radians = -ori * 0.017453292519943295;
  130. this._rotationMatrix = [
  131. [Math.cos(radians), -Math.sin(radians)],
  132. [Math.sin(radians), Math.cos(radians)]
  133. ];
  134. if (this._pixi instanceof PIXI.DisplayObject) {
  135. this._pixi.rotation = -ori * Math.PI / 180;
  136. } else {
  137. this._onChange(true, true)();
  138. }
  139. }
  140. }
  141. /**
  142. * Setter for the position attribute.
  143. *
  144. * @param {Array.<number>} pos - position of the center of the stimulus, in stimulus units
  145. * @param {boolean} [log= false] - whether of not to log
  146. */
  147. setPos(pos, log = false)
  148. {
  149. const prevPos = this._pos;
  150. const hasChanged = this._setAttribute("pos", util.toNumerical(pos), log);
  151. if (hasChanged)
  152. {
  153. this._needUpdate = true;
  154. // update the bounding box, without calling _estimateBoundingBox:
  155. this._boundingBox.x += this._pos[0] - prevPos[0];
  156. this._boundingBox.y += this._pos[1] - prevPos[1];
  157. }
  158. }
  159. /**
  160. * Setter for the depth attribute.
  161. *
  162. * @param {Array.<number>} depth - order in which stimuli is rendered, kind of css's z-index with a negative sign.
  163. * @param {boolean} [log= false] - whether of not to log
  164. */
  165. setDepth (depth = 0, log = false) {
  166. this._setAttribute("depth", depth, log);
  167. if (this._pixi) {
  168. this._pixi.zIndex = -this._depth;
  169. }
  170. }
  171. /**
  172. * Determine whether an object is inside the bounding box of the stimulus.
  173. *
  174. * @param {Object} object - the object
  175. * @param {string} units - the units
  176. * @return {boolean} whether or not the object is inside the bounding box of the stimulus
  177. */
  178. contains(object, units)
  179. {
  180. // get the position of the object, in pixel coordinates:
  181. const objectPos_px = util.getPositionFromObject(object, units);
  182. if (typeof objectPos_px === "undefined")
  183. {
  184. throw {
  185. origin: "VisualStim.contains",
  186. context: "when determining whether VisualStim: " + this._name + " contains object: " + util.toString(object),
  187. error: "unable to determine the position of the object",
  188. };
  189. }
  190. // test for inclusion:
  191. return this._getBoundingBox_px().contains(objectPos_px[0], objectPos_px[1]);
  192. }
  193. /**
  194. * Estimate the bounding box.
  195. *
  196. * @protected
  197. */
  198. _estimateBoundingBox()
  199. {
  200. throw {
  201. origin: "VisualStim._estimateBoundingBox",
  202. context: `when estimating the bounding box of visual stimulus: ${this._name}`,
  203. error: "this method is abstract and should not be called.",
  204. };
  205. }
  206. /**
  207. * Get the bounding box in pixel coordinates
  208. *
  209. * @protected
  210. * @returns {PIXI.Rectangle} the bounding box, in pixel coordinates
  211. */
  212. _getBoundingBox_px()
  213. {
  214. if (this._units === "pix")
  215. {
  216. return this._boundingBox.clone();
  217. }
  218. else if (this._units === "norm")
  219. {
  220. return new PIXI.Rectangle(
  221. this._boundingBox.x * this._win.size[0] / 2,
  222. this._boundingBox.y * this._win.size[1] / 2,
  223. this._boundingBox.width * this._win.size[0] / 2,
  224. this._boundingBox.height * this._win.size[1] / 2,
  225. );
  226. }
  227. else if (this._units === "height")
  228. {
  229. const minSize = Math.min(this._win.size[0], this._win.size[1]);
  230. return new PIXI.Rectangle(
  231. this._boundingBox.x * minSize,
  232. this._boundingBox.y * minSize,
  233. this._boundingBox.width * minSize,
  234. this._boundingBox.height * minSize,
  235. );
  236. }
  237. else
  238. {
  239. throw Object.assign(response, { error: `unknown units: ${this._units}` });
  240. }
  241. }
  242. /**
  243. * Generate a callback that prepares updates to the stimulus.
  244. * This is typically called in the constructor of a stimulus, when attributes are added
  245. * with _addAttribute.
  246. *
  247. * @protected
  248. * @param {boolean} [withPixi = false] - whether or not the PIXI representation must
  249. * also be updated
  250. * @param {boolean} [withBoundingBox = false] - whether or not to immediately estimate
  251. * the bounding box
  252. * @return {Function}
  253. */
  254. _onChange(withPixi = false, withBoundingBox = false)
  255. {
  256. return () =>
  257. {
  258. this._needUpdate = true;
  259. if (withPixi)
  260. {
  261. this._needPixiUpdate = true;
  262. }
  263. if (withBoundingBox)
  264. {
  265. this._estimateBoundingBox();
  266. }
  267. };
  268. }
  269. }