mirror of
https://github.com/psychopy/psychojs.git
synced 2025-05-10 10:40:54 +00:00
386 lines
12 KiB
HTML
386 lines
12 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<title>JSDoc: Source: core/Window.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: core/Window.js</h1>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<section>
|
|
<article>
|
|
<pre class="prettyprint source linenums"><code>/**
|
|
* Window responsible for displaying the experiment stimuli
|
|
*
|
|
* @author Alain Pitiot
|
|
* @version 3.0.0b13
|
|
* @copyright (c) 2018 Ilixa Ltd. ({@link http://ilixa.com})
|
|
* @license Distributed under the terms of the MIT License
|
|
*/
|
|
|
|
import { Color } from '../util/Color';
|
|
import { PsychObject } from '../util/PsychObject';
|
|
import { MonotonicClock } from '../util/Clock';
|
|
|
|
/**
|
|
* <p>Window displays the various stimuli of the experiment.</p>
|
|
* <p>It sets up a [PIXI]{@link http://www.pixijs.com/} renderer, which we use to render the experiment stimuli.</p>
|
|
*
|
|
* @name module:core.Window
|
|
* @class
|
|
* @extends PsychObject
|
|
* @param {Object} options
|
|
* @param {PsychoJS} options.psychoJS - the PsychoJS instance
|
|
* @param {string} [options.name] the name of the window
|
|
* @param {boolean} [options.fullscr= false] whether or not to go fullscreen
|
|
* @param {Color} [options.color= Color('black')] the background color of the window
|
|
* @param {string} [options.units= 'pix'] the units of the window
|
|
* @param {boolean} [options.autoLog= true] whether or not to log
|
|
*/
|
|
export class Window extends PsychObject {
|
|
|
|
/**
|
|
* Getter for monitorFramePeriod.
|
|
*
|
|
* @name module:core.Window#monitorFramePeriod
|
|
* @function
|
|
* @public
|
|
*/
|
|
get monitorFramePeriod() { return this._monitorFramePeriod; }
|
|
|
|
constructor({
|
|
psychoJS,
|
|
name,
|
|
fullscr = false,
|
|
color = new Color('black'),
|
|
units = 'pix',
|
|
autoLog = true
|
|
} = {}) {
|
|
super(psychoJS, name);
|
|
|
|
// messages to be logged at the next "flip":
|
|
this._msgToBeLogged = [];
|
|
|
|
// list of all elements, in the order they are currently drawn:
|
|
this._drawList = [];
|
|
|
|
this._addAttributes(Window, fullscr, color, units, autoLog);
|
|
this._addAttribute('size', []);
|
|
|
|
|
|
// setup PIXI:
|
|
this._setupPixi();
|
|
|
|
// monitor frame period:
|
|
this._monitorFramePeriod = 1.0 / this.getActualFrameRate();
|
|
|
|
this._frameCount = 0;
|
|
|
|
this._flipCallbacks = [];
|
|
|
|
/*if (autoLog)
|
|
logging.exp("Created %s = %s" % (self.name, str(self)));*/
|
|
}
|
|
|
|
|
|
/**
|
|
* Close the window.
|
|
*
|
|
* <p> Note: this actually only removes the canvas used to render the experiment stimuli.</p>
|
|
*
|
|
* @name module:core.Window#close
|
|
* @function
|
|
* @public
|
|
*/
|
|
close() {
|
|
if (document.body.contains(this._renderer.view))
|
|
document.body.removeChild(this._renderer.view);
|
|
|
|
window.removeEventListener('resize', this._resizeCallback);
|
|
window.removeEventListener('orientationchange', this._resizeCallback);
|
|
}
|
|
|
|
|
|
/**
|
|
* Estimate the frame rate.
|
|
*
|
|
* @name module:core.Window#getActualFrameRate
|
|
* @function
|
|
* @public
|
|
* @return {number} always returns 60.0 at the moment
|
|
*
|
|
* @todo estimate the actual frame rate.
|
|
*/
|
|
getActualFrameRate() {
|
|
// TODO
|
|
return 60.0;
|
|
}
|
|
|
|
|
|
/**
|
|
* Take the browser full screen if possible.
|
|
*
|
|
* @name module:core.Window#adjustScreenSize
|
|
* @function
|
|
* @public
|
|
*/
|
|
adjustScreenSize() {
|
|
if (this.fullscr) {
|
|
if (typeof document.documentElement.requestFullscreen === 'function')
|
|
document.documentElement.requestFullscreen();
|
|
else if (typeof document.documentElement.mozRequestFullScreen === 'function')
|
|
document.documentElement.mozRequestFullScreen();
|
|
else if (typeof document.documentElement.webkitRequestFullscreen === 'function')
|
|
document.documentElement.webkitRequestFullscreen();
|
|
else if (typeof document.documentElement.msRequestFullscreen === 'function')
|
|
document.documentElement.msRequestFullscreen();
|
|
else
|
|
this.psychoJS.logger.warn('Unable to go fullscreen.');
|
|
|
|
// the Window and all of the stimuli need updating:
|
|
this._needUpdate = true;
|
|
for (const stim of this._drawList)
|
|
stim._needUpdate = true;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* Log a message.
|
|
*
|
|
* <p> Note: the message will be time-stamped at the next call to requestAnimationFrame.</p>
|
|
*
|
|
* @name module:core.Window#logOnFlip
|
|
* @function
|
|
* @public
|
|
* @param {Object} options
|
|
* @param {String} options.msg the message to be logged
|
|
* @param {integer} level the log level
|
|
* @param {Object} [obj] the object associated with the message
|
|
*/
|
|
logOnFlip({
|
|
msg,
|
|
level,
|
|
obj = undefined } = {}) {
|
|
this._msgToBeLogged.push({ msg, level, obj });
|
|
}
|
|
|
|
|
|
/**
|
|
* Callback function for callOnFlip.
|
|
*
|
|
* @callback module:core.Window~OnFlipCallback
|
|
* @param {*} [args] optional arguments
|
|
*/
|
|
/**
|
|
* Add a callback function that will run after the next screen flip, i.e. immediately after the next rendering of the
|
|
* Window.
|
|
*
|
|
* <p>This is typically used to reset a timer or clock.</p>
|
|
*
|
|
* @name module:core.Window#callOnFlip
|
|
* @function
|
|
* @public
|
|
* @param {module:core.Window~OnFlipCallback} flipCallback - callback function.
|
|
* @param {...*} flipCallbackArgs - arguments for the callback function.
|
|
*/
|
|
callOnFlip(flipCallback, ...flipCallbackArgs) {
|
|
this._flipCallbacks.push({function: flipCallback, arguments: flipCallbackArgs});
|
|
}
|
|
|
|
|
|
/**
|
|
* Render the stimuli onto the canvas.
|
|
*
|
|
* @name module:core.Window#render
|
|
* @function
|
|
* @public
|
|
*/
|
|
render() {
|
|
this._frameCount++;
|
|
|
|
// render the PIXI container:
|
|
this._renderer.render(this._rootContainer);
|
|
|
|
// this is to make sure that the GPU is done rendering, it may not be necessary
|
|
// [http://www.html5gamedevs.com/topic/27849-detect-when-view-has-been-rendered/]
|
|
this._renderer.gl.readPixels(0, 0, 1, 1, this._renderer.gl.RGBA, this._renderer.gl.UNSIGNED_BYTE, new Uint8Array(4));
|
|
|
|
// log:
|
|
this._writeLogOnFlip();
|
|
|
|
// call the callOnFlip functions and remove them:
|
|
for (let callback of this._flipCallbacks)
|
|
callback['function'](...callback['arguments']);
|
|
this._flipCallbacks = [];
|
|
|
|
// prepare the scene for the next animation frame:
|
|
this._refresh();
|
|
}
|
|
|
|
|
|
/**
|
|
* Update this window, if need be.
|
|
*
|
|
* @name module:core.Window#_updateIfNeeded
|
|
* @function
|
|
* @private
|
|
*/
|
|
_updateIfNeeded() {
|
|
if (this._needUpdate) {
|
|
this._renderer.backgroundColor = this._color.int;
|
|
|
|
this._needUpdate = false;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* Recompute the window's _drawList and _container children for the next animation frame.
|
|
*
|
|
* @name module:core.Window#_refresh
|
|
* @function
|
|
* @private
|
|
*/
|
|
_refresh() {
|
|
this._updateIfNeeded();
|
|
|
|
// if a stimuli needs to be updated, we remove it from the window container, update it, then put it back
|
|
for (const stim of this._drawList)
|
|
if (stim._needUpdate) {
|
|
this._rootContainer.removeChild(stim._pixi);
|
|
stim._updateIfNeeded();
|
|
this._rootContainer.addChild(stim._pixi);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* Setup PIXI.
|
|
*
|
|
* <p>A new renderer is created and a container is added to it. The renderer's touch and mouse events are handled by the {@link EventManager}.</p>
|
|
*
|
|
* @name module:core.Window#_setupPixi
|
|
* @function
|
|
* @private
|
|
*/
|
|
_setupPixi() {
|
|
// the size of the PsychoJS Window is always that of the browser
|
|
this._size[0] = window.innerWidth;
|
|
this._size[1] = window.innerHeight;
|
|
|
|
// create a PIXI renderer and add it to the document:
|
|
this._renderer = PIXI.autoDetectRenderer(this._size[0], this._size[1], {
|
|
backgroundColor: this.color.int
|
|
});
|
|
this._renderer.view.style["transform"] = "translatez(0)";
|
|
this._renderer.view.style.position = "absolute";
|
|
document.body.appendChild(this._renderer.view);
|
|
|
|
// top-level container:
|
|
this._rootContainer = new PIXI.Container();
|
|
this._rootContainer.interactive = true;
|
|
|
|
// set size of renderer and position of root container:
|
|
this._onResize(this);
|
|
|
|
// touch/mouse events should be treated by PsychoJS' event manager:
|
|
this.psychoJS.eventManager.addMouseListeners(this._renderer);
|
|
|
|
// update the renderer size when the browser's size or orientation changes:
|
|
this._resizeCallback = e => this._onResize(this);
|
|
window.addEventListener('resize', this._resizeCallback);
|
|
window.addEventListener('orientationchange', this._resizeCallback);
|
|
}
|
|
|
|
|
|
/**
|
|
* Treat a window resize event.
|
|
*
|
|
* <p>We adjust the size of the renderer and the position of the root container.</p>
|
|
* <p>Note: since this method will be called by the DOM window (i.e. 'this' is
|
|
* the DOM window), we need to pass it a {@link Window}.</p>
|
|
*
|
|
* @name module:core.Window#_onResize
|
|
* @function
|
|
* @private
|
|
* @param {Window} win - The PsychoJS window
|
|
*/
|
|
_onResize(win) {
|
|
// update the size of the PsychoJS Window:
|
|
win._size[0] = window.innerWidth;
|
|
win._size[1] = window.innerHeight;
|
|
|
|
win._renderer.view.style.width = win._size[0] + 'px';
|
|
win._renderer.view.style.height = win._size[1] + 'px';
|
|
win._renderer.view.style.left = '0px';
|
|
win._renderer.view.style.top = '0px';
|
|
win._renderer.resize(win._size[0], win._size[1]);
|
|
|
|
// setup the container such that (0,0) is at the centre of the window
|
|
// with positive coordinates to the right and top:
|
|
win._rootContainer.position.x = win._size[0] / 2.0;
|
|
win._rootContainer.position.y = win._size[1] / 2.0;
|
|
win._rootContainer.scale.y = -1;
|
|
}
|
|
|
|
|
|
/**
|
|
* Send all logged messages to the {@link Logger}.
|
|
*
|
|
* @name module:core.Window#_writeLogOnFlip
|
|
* @function
|
|
* @private
|
|
*/
|
|
_writeLogOnFlip() {
|
|
var logTime = MonotonicClock.getReferenceTime();
|
|
for (var i = 0; i < this._msgToBeLogged.length; ++i) {
|
|
var entry = this._msgToBeLogged[i];
|
|
this._psychoJS.logger.log(entry.msg, entry.level, logTime, entry.obj);
|
|
}
|
|
|
|
this._msgToBeLogged = [];
|
|
}
|
|
|
|
}
|
|
</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.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.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>
|
|
</nav>
|
|
|
|
<br class="clear">
|
|
|
|
<footer>
|
|
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Fri Dec 28 2018 13:41:42 GMT+0100 (CET)
|
|
</footer>
|
|
|
|
<script> prettyPrint(); </script>
|
|
<script src="scripts/linenumber.js"> </script>
|
|
</body>
|
|
</html>
|