1
0
mirror of https://github.com/psychopy/psychojs.git synced 2025-05-10 10:40:54 +00:00
psychojs/docs/visual_ShapeStim.js.html
2019-07-08 09:07:09 +02:00

287 lines
11 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: visual/ShapeStim.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: visual/ShapeStim.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/** @module visual */
/**
* Basic Shape Stimulus.
*
* @author Alain Pitiot
* @version 3.1.4
* @copyright (c) 2019 Ilixa Ltd. ({@link http://ilixa.com})
* @license Distributed under the terms of the MIT License
*/
import { VisualStim } from './VisualStim';
import { Color } from '../util/Color';
import { ColorMixin } from '../util/ColorMixin';
import * as util from '../util/Util';
/**
* &lt;p>This class provides the basic functionalities of shape stimuli.&lt;/p>
*
* @class
* @extends VisualStim
* @mixes ColorMixin
* @param {Object} options
* @param {String} options.name - the name used when logging messages from this stimulus
* @param {Window} options.win - the associated Window
* @param {number} options.lineWidth - the line width
* @param {Color} [options.lineColor= Color('white')] the line color
* @param {Color} options.fillColor - the fill color
* @param {number} [options.opacity= 1.0] - the opacity
* @param {Array.&lt;Array.&lt;number>>} [options.vertices= [[-0.5, 0], [0, 0.5], [0.5, 0]]] - the shape vertices
* @param {boolean} [options.closeShape= true] - whether or not the shape is closed
* @param {Array.&lt;number>} [options.pos= [0, 0]] - the position of the center of the shape
* @param {number} [options.size= 1.0] - the size
* @param {number} [options.ori= 0.0] - the orientation (in degrees)
* @param {string} options.units - the units of the stimulus vertices, size and position
* @param {number} [options.contrast= 1.0] - the contrast
* @param {number} [options.depth= 0] - the depth
* @param {boolean} [options.interpolate= true] - whether or not the shape is interpolated
* @param {boolean} [options.autoDraw= false] - whether or not the stimulus should be automatically drawn on every frame flip
* @param {boolean} [options.autoLog= false] - whether or not to log
*/
export class ShapeStim extends util.mix(VisualStim).with(ColorMixin)
{
constructor({
name,
win,
lineWidth = 1.5,
lineColor = new Color('white'),
fillColor,
opacity = 1.0,
vertices = [[-0.5, 0], [0, 0.5], [0.5, 0]],
closeShape = true,
pos = [0, 0],
size = 1.0,
ori = 0.0,
units,
contrast = 1.0,
depth = 0,
interpolate = true,
autoDraw,
autoLog
} = {}) {
super({ name, win, units, ori, opacity, pos, size, autoDraw, autoLog });
// the PIXI polygon corresponding to the vertices, in pixel units:
this._pixiPolygon_px = undefined;
this._addAttributes(ShapeStim, lineWidth, lineColor, fillColor, vertices, closeShape, contrast, depth, interpolate);
/*if (autoLog)
logging.exp("Created %s = %s" % (self.name, str(self)));*/
}
/**
* Setter for the line width attribute.
*
* @public
* @param {number} lineWidth - the line width
* @param {boolean} [log= false] - whether of not to log
*/
setLineWidth(lineWidth, log = false) {
this._setAttribute('lineWidth', lineWidth, log);
this._needUpdate = true;
}
/**
* Setter for the line color attribute.
*
* @public
* @param {Color} lineColor - the line color
* @param {boolean} [log= false] - whether of not to log
*/
setLineColor(lineColor, log = false) {
this._setAttribute('lineColor', lineColor, log);
this._needUpdate = true;
}
/**
* Setter for the fill color attribute.
*
* @public
* @param {Color} fillColor - the fill color
* @param {boolean} [log= false] - whether of not to log
*/
setFillColor(fillColor, log = false) {
this._setAttribute('fillColor', fillColor, log);
this._needUpdate = true;
}
/**
* Setter for the vertices attribute.
*
* @public
* @param {Array.&lt;Array.&lt;number>>} vertices - the vertices
* @param {boolean} [log= false] - whether of not to log
*/
setVertices(vertices, log = false) {
this._psychoJS.logger.debug('set the vertices of ShapeStim:', this.name);
this._setAttribute('vertices', vertices, log);
/*this._setAttribute({
name: 'vertices',
value: vertices,
assert: v => (v != null) &amp;&amp; (typeof v !== 'undefined') &amp;&amp; Array.isArray(v) )
log);
*/
this._needVertexUpdate = true;
this._needUpdate = true;
}
/**
* Determine whether this stimulus contains the given object.
*
* @public
* @param {Object} object - the object
* @param {string} units - the units
* @return {boolean} whether or not the stimulus contains the object
*/
contains(object, units) {
this._psychoJS.logger.debug('test whether BaseShameStim:', this.name, 'contains object: ', ('name' in object) ? object.name : object);
// get position of object:
const objectPos_px = util.getPositionFromObject(object, units);
if (typeof objectPos_px === 'undefined')
throw { origin : 'ShapeStim.contains', context : 'when determining whether BaseShameStim: ' + this._name + ' contains object: ' + util.toString(object), error : 'unable to determine the position of the object' };
// test for inclusion
// note: the vertices are centered around (0, 0) so we need to add to them the stimulus' position
const pos_px = util.to_px(this.pos, this.units, this.win);
const polygon_px = this._vertices_px.map(v => [v[0] + pos_px[0], v[1] + pos_px[1]]);
return util.IsPointInsidePolygon(objectPos_px, polygon_px);
}
/**
* Update the stimulus, if necessary.
*
* @private
*/
_updateIfNeeded() {
if (!this._needUpdate)
return;
this._needUpdate = false;
this._getPolygon(/*true*/); // this also updates _vertices_px
this._pixi = undefined;
// no polygon to draw: return immediately
if (typeof this._pixiPolygon_px === 'undefined')
return;
// prepare the polygon in the given color and opacity:
this._pixi = new PIXI.Graphics();
this._pixi.lineStyle(this._lineWidth, this._lineColor.int, this._opacity, 0.5);
if (typeof this._fillColor !== 'undefined')
this._pixi.beginFill(this._fillColor.int, this._opacity);
this._pixi.drawPolygon(this._pixiPolygon_px);
if (typeof this._fillColor !== 'undefined')
this._pixi.endFill();
// set polygon position and rotation:
this._pixi.position = util.to_pixiPoint(this.pos, this.units, this.win);
this._pixi.rotation = this.ori * Math.PI / 180.0;
}
/**
* Get the PIXI polygon (in pixel units) corresponding to the vertices.
*
* @private
* @return {Object} the PIXI polygon corresponding to this stimulus vertices.
*/
_getPolygon(/*force = false*/) {
if (!this._needVertexUpdate)
return;
this._needVertexUpdate = false;
//if (!force &amp;&amp; typeof this._pixiPolygon_px !== 'undefined')
// return this._pixiPolygon_px;
// make sure the vertices in pixel units are available, and flatten the array of arrays:
this._getVertices_px(/*force*/);
let coords_px = [];
for (const vertex_px of this._vertices_px)
coords_px.push.apply(coords_px, vertex_px);
// close the polygon if need be:
if (coords_px.length >= 6 &amp;&amp; this._closeShape) {
// note: we first check whether the vertices already define a closed polygon:
const n = coords_px.length;
if (coords_px[0] !== coords_px[n - 2] || coords_px[1] !== coords_px[n - 1]) {
coords_px.push(coords_px[0]);
coords_px.push(coords_px[1]);
}
}
// create the PIXI polygon:
this._pixiPolygon_px = new PIXI.Polygon(coords_px);
return this._pixiPolygon_px;
}
}
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-core.html">core</a></li><li><a href="module-data.html">data</a></li><li><a href="module-sound.html">sound</a></li><li><a href="module-util.html">util</a></li><li><a href="module-visual.html">visual</a></li></ul><h3>Classes</h3><ul><li><a href="module-core.BuilderKeyResponse.html">BuilderKeyResponse</a></li><li><a href="module-core.EventManager.html">EventManager</a></li><li><a href="module-core.GUI.html">GUI</a></li><li><a href="module-core.Keyboard.html">Keyboard</a></li><li><a href="module-core.KeyPress.html">KeyPress</a></li><li><a href="module-core.MinimalStim.html">MinimalStim</a></li><li><a href="module-core.Mouse.html">Mouse</a></li><li><a href="module-core.PsychoJS.html">PsychoJS</a></li><li><a href="module-core.ServerManager.html">ServerManager</a></li><li><a href="module-core.Window.html">Window</a></li><li><a href="module-data.ExperimentHandler.html">ExperimentHandler</a></li><li><a href="module-data.TrialHandler.html">TrialHandler</a></li><li><a href="module-sound.Sound.html">Sound</a></li><li><a href="module-sound.TonePlayer.html">TonePlayer</a></li><li><a href="module-sound.TrackPlayer.html">TrackPlayer</a></li><li><a href="module-util.Clock.html">Clock</a></li><li><a href="module-util.Color.html">Color</a></li><li><a href="module-util.CountdownTimer.html">CountdownTimer</a></li><li><a href="module-util.EventEmitter.html">EventEmitter</a></li><li><a href="module-util.Logger.html">Logger</a></li><li><a href="module-util.MixinBuilder.html">MixinBuilder</a></li><li><a href="module-util.MonotonicClock.html">MonotonicClock</a></li><li><a href="module-util.PsychObject.html">PsychObject</a></li><li><a href="module-util.Scheduler.html">Scheduler</a></li><li><a href="module-visual.ImageStim.html">ImageStim</a></li><li><a href="module-visual.MovieStim.html">MovieStim</a></li><li><a href="module-visual.Rect.html">Rect</a></li><li><a href="module-visual.ShapeStim.html">ShapeStim</a></li><li><a href="module-visual.Slider.html">Slider</a></li><li><a href="module-visual.TextStim.html">TextStim</a></li><li><a href="module-visual.VisualStim.html">VisualStim</a></li></ul><h3>Mixins</h3><ul><li><a href="module-core.WindowMixin.html">WindowMixin</a></li><li><a href="module-util.ColorMixin.html">ColorMixin</a></li></ul><h3>Interfaces</h3><ul><li><a href="module-sound.SoundPlayer.html">SoundPlayer</a></li></ul><h3>Global</h3><ul><li><a href="global.html#offerDataForDownload">offerDataForDownload</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Mon Jul 08 2019 09:06:18 GMT+0200 (CEST)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>