cdf
Version:
A library for creating oldschool demo-like animations with JavaScript
69 lines (61 loc) • 1.94 kB
JavaScript
var utils = require('utils');
var Point = require('class/Point');
var shorthands = function(cdf){
if(typeof window !== "undefined") {
window.L = Math.log;
window.C = Math.cos;
window.F = Math.floor;
window.A = Math.abs;
window.R = function (mn) {
return Math.random() * (mn || 1);
}
window.T = Math.tan;
window.S = Math.sin;
window.ZERO_VECTOR = new Point([0,0]);
window.PI_BY_TWO = utils.PI_BY_TWO;
window.TWO_PI = utils.TWO_PI;
window.DEG_TO_RAD = utils.DEG_TO_RAD;
window.RAD_TO_DEG = utils.RAD_TO_DEG;
}
/**
* Makes canvas size the same as passed elements'
* @param {Node} element
* @return {*|Canvas}
*/
cdf.Canvas.prototype.fit = function(element){
element = element || this.parent || window;
return this.size(element.innerWidth, element.innerHeight);
};
/**
* A shorthand that tells cdf to call fit function whenever window is resized
* @return {Canvas}
*/
cdf.Canvas.prototype.fitOnResize = function () {
var instance = this;
cdf.on('resize', function () { instance.fit(); })
return this;
};
/**
* A shorthand that sets fit options for canvas
* @param {boolean} addCSS
* @param {boolean} fitNow
* @return {Canvas}
*/
cdf.Canvas.prototype.setFit = function(addCSS, fitNow,fitOnResize) {
if(addCSS || addCSS === undefined) this.addFitCSS();
if(fitNow || fitNow === undefined) this.fit();
if(fitOnResize || fitOnResize === undefined) this.fitOnResize();
return this;
};
/**
* Adds CSS styles to canvas to center inside parent positioned element. Plays nicely with fit()
* @return {Canvas}
*/
cdf.Canvas.prototype.addFitCSS = function(s){
if(this.__fitCSSAdded)return;this.__fitCSSAdded=true;
s = this.element.style;
s.position='absolute'; s.margin='auto'; s.top=0;s.bottom=0;s.left=0;s.right=0;
return this;
};
}
module.exports = shorthands;