src/util/Util.js
/**
 * Convert the position to pixel units.
 *
 * @name module:util.to_px
 * @function
 * @public
 * @param {number[]} pos - the input position
 * @param {string} posUnit - the position units
 * @param {Window} win - the associated Window
 * @param {boolean} [integerCoordinates = false] - whether or not to round the position coordinates.
 * @returns {number[]} the position in pixel units
 */
Convert the position to pixel units.
Convert the position to pixel units .
function to_px(pos, posUnit, win, integerCoordinates = false)
{
	const response = {
		origin: "util.to_px",
		context: "when converting a position to pixel units",
	};
	let pos_px;
	if (posUnit === "pix")
	{
		pos_px = pos;
	}
	else if (posUnit === "norm")
	{
		pos_px = [pos[0] * win.size[0] / 2.0, pos[1] * win.size[1] / 2.0];
	}
	else if (posUnit === "height")
	{
		const minSize = Math.min(win.size[0], win.size[1]);
		pos_px = [pos[0] * minSize, pos[1] * minSize];
	}
	else
	{
		throw Object.assign(response, { error: `unknown position units: ${posUnit}` });
	}
	if (integerCoordinates)
	{
		return [Math.round(pos_px[0]), Math.round(pos_px[1])];
	}
	else
	{
		return pos_px;
	}
}
function to_px ( pos , posUnit , win , integerCoordinates = false ) { const response = { origin : "util.to_px" , context : "when converting a position to pixel units" , } ; let pos_px ; if ( posUnit === "pix" ) { pos_px = pos ; } else if ( posUnit === "norm" ) { pos_px = [ pos [ 0 ] * win . size [ 0 ] / 2.0 , pos [ 1 ] * win . size [ 1 ] / 2.0 ] ; } else if ( posUnit === "height" ) { const minSize = Math . min ( win . size [ 0 ] , win . size [ 1 ] ) ; pos_px = [ pos [ 0 ] * minSize , pos [ 1 ] * minSize ] ; } else { throw Object . assign ( response , { error : ` ${ posUnit } ` } ) ; } if ( integerCoordinates ) { return [ Math . round ( pos_px [ 0 ] ) , Math . round ( pos_px [ 1 ] ) ] ; } else { return pos_px ; } }
Convert the position to pixel units.
Convert the position to pixel units .