Files
CanvasLoader/docs/files/src_hcl-1.0.0.js.html
T

701 lines
21 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>src/hcl-1.0.0.js - heartcode-canvasloader</title>
<link rel="stylesheet" href="http://yui.yahooapis.com/3.9.1/build/cssgrids/cssgrids-min.css">
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
<link rel="stylesheet" href="../assets/css/main.css" id="site_styles">
<link rel="shortcut icon" type="image/png" href="../assets/favicon.png">
<script src="http://yui.yahooapis.com/combo?3.9.1/build/yui/yui-min.js"></script>
</head>
<body class="yui3-skin-sam">
<div id="doc">
<div id="hd" class="yui3-g header">
<div class="yui3-u-3-4">
<h1><img src="../assets/css/logo.png" title="heartcode-canvasloader"></h1>
</div>
<div class="yui3-u-1-4 version">
<em>API Docs for: 1.0.0</em>
</div>
</div>
<div id="bd" class="yui3-g">
<div class="yui3-u-1-4">
<div id="docs-sidebar" class="sidebar apidocs">
<div id="api-list">
<h2 class="off-left">APIs</h2>
<div id="api-tabview" class="tabview">
<ul class="tabs">
<li><a href="#api-classes">Classes</a></li>
<li><a href="#api-modules">Modules</a></li>
</ul>
<div id="api-tabview-filter">
<input type="search" id="api-filter" placeholder="Type to filter APIs">
</div>
<div id="api-tabview-panel">
<ul id="api-classes" class="apis classes">
<li><a href="../classes/CanvasLoader.html">CanvasLoader</a></li>
</ul>
<ul id="api-modules" class="apis modules">
<li><a href="../modules/CanvasLoader.html">CanvasLoader</a></li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div class="yui3-u-3-4">
<div id="api-options">
Show:
<label for="api-show-inherited">
<input type="checkbox" id="api-show-inherited" checked>
Inherited
</label>
<label for="api-show-protected">
<input type="checkbox" id="api-show-protected">
Protected
</label>
<label for="api-show-private">
<input type="checkbox" id="api-show-private">
Private
</label>
<label for="api-show-deprecated">
<input type="checkbox" id="api-show-deprecated">
Deprecated
</label>
</div>
<div class="apidocs">
<div id="docs-main">
<div class="content">
<h1 class="file-heading">File: src/hcl-1.0.0.js</h1>
<div class="file">
<pre class="code prettyprint linenums">
/*
* Copyright (c) 2013 Róbert Pataki
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the &quot;Software&quot;), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* ----------------------------------------------------------------------------------------
*
* Check out my GitHub: http://github.com/heartcode/
* Send me an email: heartcode@robertpataki.com
* Follow me on Twitter: http://twitter.com/#iHeartcode
* Blog: http://heartcode.robertpataki.com
*/
/**
* CanvasLoader uses the HTML5 canvas element in modern browsers and VML in IE6/7/8 to create and animate the most popular preloader shapes (oval, spiral, rectangle, square and rounded rectangle).&lt;br/&gt;&lt;br/&gt;
* It is important to note that CanvasLoader doesn&#x27;t show up and starts rendering automatically on instantiation. To start rendering and display the loader use the &lt;code&gt;show()&lt;/code&gt; method.
* @module CanvasLoader
**/
(function (window) {
&quot;use strict&quot;;
/**
* CanvasLoader is a JavaScript UI library that draws and animates circular preloaders using the Canvas HTML object.&lt;br/&gt;&lt;br/&gt;
* A CanvasLoader instance creates two canvas elements which are placed into a placeholder div (the id of the div has to be passed in the constructor). The second canvas is invisible and used for caching purposes only.&lt;br/&gt;&lt;br/&gt;
* If no id is passed in the constructor, the canvas objects are paced in the document directly.
* @class CanvasLoader
* @constructor
* @param {Object} target The target DOM element to place the spinner into
* @param {Object} [settings] Settings to customise the spinner instance
* @param {Number} [settings.diameter] The expected diameter
* @param {Number} [settings.density] The number of the shapes
* @param {Number} [settings.color] The color of the shapes
* @param {Number} [settings.range] The weight of the trail
* @param {Number} [settings.speed] The speed
* @param {Number} [settings.fps] The FPS
* @param {Number} [settings.shape] The shape type (oval, rect, square, roundRect)
* @param {String} [settings.id] The id of the CanvasLoader instance
* @param {Boolean} [settings.safeVML] Whether or not density should be capped in IE
*
*
**/
var CanvasLoader = function (target, settings) {
settings = settings || {};
this._init(target, settings);
}, p = CanvasLoader.prototype, engine, engines = [&quot;canvas&quot;, &quot;vml&quot;], shapes = [&quot;oval&quot;, &quot;spiral&quot;, &quot;square&quot;, &quot;rect&quot;, &quot;roundRect&quot;], cRX = /^\#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/, ie8 = navigator.appVersion.indexOf(&quot;MSIE&quot;) !== -1 &amp;&amp; parseFloat(navigator.appVersion.split(&quot;MSIE&quot;)[1]) === 8 ? true : false, canSup = !!document.createElement(&quot;canvas&quot;).getContext, safeDensity = 40, safeVML = true,
/**
* Creates a new element with the tag and applies the passed properties on it
* @private
* @method _addEl
* @param tag {String} The tag to be created
* @param par {String} The DOM element the new element will be appended to
* @param opt {Object} Additional properties passed to the new DOM element
* @return {Object} The DOM element
*/
_addEl = function (tag, par, opt) {
var el = document.createElement(tag), n;
for (n in opt) {
el[n] = opt[n];
}
if(typeof par !== &quot;undefined&quot;) {
par.appendChild(el);
}
return el;
},
/**
* Sets the css properties on the element
* @private
* @method _setCSS
* @param el {Object} The DOM element to be styled
* @param opt {Object} The style properties
* @return {Object} The DOM element
*/
_setCSS = function (el, opt) {
for (var n in opt) { el.style[n] = opt[n]; }
return el;
},
/**
* Sets the attributes on the element
* @private
* @method _setAttr
* @param el {Object} The DOM element to add the attributes to
* @param opt {Object} The attributes
* @return {Object} The DOM element
*/
_setAttr = function (el, opt) {
for (var n in opt) { el.setAttribute(n, opt[n]); }
return el;
},
/**
* Transforms the cache canvas before drawing
* @private
* @method _transCon
* @param x {Object} The canvas context to be transformed
* @param x {Number} x translation
* @param y {Number} y translation
* @param r {Number} Rotation radians
*/
_transCon = function(c, x, y, r) {
c.save();
c.translate(x, y);
c.rotate(r);
c.translate(-x, -y);
c.beginPath();
};
//////////////
//// Private fields
/////////////
/**
* Holds settings (this is updated with user settings)
* @private
* @type {Object}
*/
p._settings = {
shape: &quot;oval&quot;,
color: &quot;#000000&quot;,
diameter: 40,
range: 0.2,
density: 40,
speed: 2,
fps: 60,
autoShow: true
};
/**
* Holds default settings
* @private
* @type {Object}
*/
p._defaults = p._settings;
/**
* The div we place the canvas object into
* @property _cont
* @private
* @type Object
**/
p._cont = null;
/**
* The div we draw the shapes into
* @property _can
* @private
* @type Object
**/
p._can = null;
/**
* The canvas context
* @property _con
* @private
* @type Object
**/
p._con = null;
/**
* The canvas we use for caching
* @property _cCan
* @private
* @type Object
**/
p._cCan = null;
/**
* The context of the cache canvas
* @property _cCon
* @private
* @type Object
**/
p._cCon = null;
/**
* Adds a timer for the rendering
* @property _timer
* @private
* @type Boolean
**/
p._timer = 0;
/**
* The active shape id for rendering
* @property _currentId
* @private
* @type Number
**/
p._currentId = 0;
/**
* The diameter of the loader
* @property _diameter
* @private
* @type Number
* @default 40
**/
p._diameter = p._settings.diameter;
/**
* The color of the loader shapes in RGB
* @property _cRGB
* @private
* @type Object
**/
p._cRGB = {};
/**
* The color of the loader shapes in HEX
* @property _color
* @private
* @type String
* @default &quot;#000000&quot;
**/
p._color = p._settings.color;
/**
* The type of the loader shapes
* @property _shape
* @private
* @type String
* @default &quot;oval&quot;
**/
p._shape = p._settings.shape;
/**
* The number of shapes drawn on the loader canvas
* @property _density
* @private
* @type Number
* @default 40
**/
p._density = p._settings.density;
/**
* The amount of the modified shapes in percent.
* @property _range
* @private
* @type Number
**/
p._range = p._settings.range;
/**
* The speed of the loader animation
* @property _speed
* @private
* @type Number
**/
p._speed = p._settings.speed;
/**
* The FPS value of the loader animation rendering
* @property _fps
* @private
* @type Number
**/
p._fps = p._settings.fps;
//////////////
//// Private methods
/////////////
/**
* Initialization method
* @method _init
* @private
* @param target {Object} The target DOM element to place the spinner into
* @param settings {Object} Settings to customise the spinner instance
**/
p._init = function (target, settings) {
var arg = this._settings;
this.mum = target;
// Creates the parent div of the loader instance
this._cont = _addEl(&quot;div&quot;, this.mum, {className: &quot;canvasloader&quot;});
if (canSup) {
// For browsers with Canvas support...
engine = engines[0];
// Createse the canvas element
this._can = _addEl(&quot;canvas&quot;, this._cont);
this._con = this._can.getContext(&quot;2d&quot;);
// Create the cache canvas element
this._cCan = _setCSS(_addEl(&quot;canvas&quot;, this._cont), { display: &quot;none&quot; });
this._cCon = this._cCan.getContext(&quot;2d&quot;);
} else {
// For browsers without Canvas support...
engine = engines[1];
// Adds the VML stylesheet
if (typeof CanvasLoader.vmlSheet === &quot;undefined&quot;) {
document.getElementsByTagName(&quot;head&quot;)[0].appendChild(_addEl(&quot;style&quot;));
CanvasLoader.vmlSheet = document.styleSheets[document.styleSheets.length - 1];
var a = [&quot;group&quot;, &quot;oval&quot;, &quot;roundrect&quot;, &quot;fill&quot;];
for ( var n = 0; n &lt; a.length; ++n ) { CanvasLoader.vmlSheet.addRule(a[n], &quot;behavior:url(#default#VML); position:absolute;&quot;); }
}
this.vml = _addEl(&quot;group&quot;, this._cont);
}
// Shape setup
var shape = settings.shape || arg.shape;
for (var i = 0; i &lt; shapes.length; i++) {
if (shape === shapes[i]) {
this._shape = shape;
break;
}
}
// safeVML for safe IE rendering
safeVML = settings.safeVML || true;
// Diameter setup
var diameter = settings.diameter || arg.diameter;
this._diameter = Math.round(Math.abs(diameter));
_setCSS(this.mum, {&quot;margin-left&quot;: Math.round(diameter * -0.5) + &#x27;px&#x27;});
// Density setup
var density = settings.density || arg.density;
if (safeVML &amp;&amp; engine === engines[1]) {
this._density = Math.round(Math.abs(density)) &lt;= safeDensity ? Math.round(Math.abs(density)) : safeDensity;
} else {
this._density = Math.round(Math.abs(density));
}
if (this._density &gt; 360) { this._density = 360; }
this._currentId = 0;
// Colour setup
var color = settings.color;
this._color = cRX.test(color) ? color : arg.color;
this._cRGB = this._getRGB(this._color);
// Range setup
this._range = Math.abs(settings.range || arg.range);
// Speed setup
this._speed = Math.round(Math.abs(settings.speed || arg.speed));
// FPS setup
this._fps = Math.round(Math.abs(settings.fps || arg.fps));
// Initial rendering
this._draw();
//Hides the preloader
_setCSS(this._cont, {visibility: &quot;hidden&quot;, display: &quot;none&quot;});
var autoShow = settings.autoShow || arg.autoShow;
if(autoShow) {
this.show();
}
};
/**
* Return the RGB values of the passed color
* @private
* @method _getRGB
* @param color {String} The HEX color value to be converted to RGB
*/
p._getRGB = function (c) {
c = c.charAt(0) === &quot;#&quot; ? c.substring(1, 7) : c;
return {r: parseInt(c.substring(0, 2), 16), g: parseInt(c.substring(2, 4), 16), b: parseInt(c.substring(4, 6), 16) };
};
/**
* Return the default settings
* @private
* @method _getDefaults
*/
p._getDefaults = function () {
return this._defaults;
};
/**
* Draw the shapes on the canvas
* @private
* @method _draw
*/
p._draw = function () {
var i = 0, size, w, h, x, y, ang, rads, rad, de = this._density, animBits = Math.round(de * this._range), bitMod, minBitMod = 0, s, g, sh, f, d = 1000, arc = 0, c = this._cCon, di = this._diameter, e = 0.47;
if (engine === engines[0]) {
c.clearRect(0, 0, d, d);
_setAttr(this._can, {width: di, height: di});
_setAttr(this._cCan, {width: di, height: di});
while (i &lt; de) {
bitMod = i &lt;= animBits ? 1 - ((1 - minBitMod) / animBits * i) : bitMod = minBitMod;
ang = 270 - 360 / de * i;
rads = ang / 180 * Math.PI;
c.fillStyle = &quot;rgba(&quot; + this._cRGB.r + &quot;,&quot; + this._cRGB.g + &quot;,&quot; + this._cRGB.b + &quot;,&quot; + bitMod.toString() + &quot;)&quot;;
switch (this._shape) {
case shapes[0]:
case shapes[1]:
size = di * 0.07;
x = di * e + Math.cos(rads) * (di * e - size) - di * e;
y = di * e + Math.sin(rads) * (di * e - size) - di * e;
c.beginPath();
if (this._shape === shapes[1]) { c.arc(di * 0.5 + x, di * 0.5 + y, size * bitMod, 0, Math.PI * 2, false); } else { c.arc(di * 0.5 + x, di * 0.5 + y, size, 0, Math.PI * 2, false); }
break;
case shapes[2]:
size = di * 0.12;
x = Math.cos(rads) * (di * e - size) + di * 0.5;
y = Math.sin(rads) * (di * e - size) + di * 0.5;
_transCon(c, x, y, rads);
c.fillRect(x, y - size * 0.5, size, size);
break;
case shapes[3]:
case shapes[4]:
w = di * 0.3;
h = w * 0.27;
x = Math.cos(rads) * (h + (di - h) * 0.13) + di * 0.5;
y = Math.sin(rads) * (h + (di - h) * 0.13) + di * 0.5;
_transCon(c, x, y, rads);
if(this._shape === shapes[3]) {
c.fillRect(x, y - h * 0.5, w, h);
} else {
rad = h * 0.55;
c.moveTo(x + rad, y - h * 0.5);
c.lineTo(x + w - rad, y - h * 0.5);
c.quadraticCurveTo(x + w, y - h * 0.5, x + w, y - h * 0.5 + rad);
c.lineTo(x + w, y - h * 0.5 + h - rad);
c.quadraticCurveTo(x + w, y - h * 0.5 + h, x + w - rad, y - h * 0.5 + h);
c.lineTo(x + rad, y - h * 0.5 + h);
c.quadraticCurveTo(x, y - h * 0.5 + h, x, y - h * 0.5 + h - rad);
c.lineTo(x, y - h * 0.5 + rad);
c.quadraticCurveTo(x, y - h * 0.5, x + rad, y - h * 0.5);
}
break;
}
c.closePath();
c.fill();
c.restore();
++i;
}
} else {
_setCSS(this._cont, {width: di, height: di});
_setCSS(this.vml, {width: di, height: di});
switch (this._shape) {
case shapes[0]:
case shapes[1]:
sh = &quot;oval&quot;;
size = d * 0.14;
break;
case shapes[2]:
sh = &quot;roundrect&quot;;
size = d * 0.12;
break;
case shapes[3]:
case shapes[4]:
sh = &quot;roundrect&quot;;
size = d * 0.3;
break;
}
w = h = size;
x = d * 0.5 - h;
y = -h * 0.5;
while (i &lt; de) {
bitMod = i &lt;= animBits ? 1 - ((1 - minBitMod) / animBits * i) : bitMod = minBitMod;
ang = 270 - 360 / de * i;
switch (this._shape) {
case shapes[1]:
w = h = size * bitMod;
x = d * 0.5 - size * 0.5 - size * bitMod * 0.5;
y = (size - size * bitMod) * 0.5;
break;
case shapes[0]:
case shapes[2]:
if (ie8) {
y = 0;
if(this._shape === shapes[2]) {
x = d * 0.5 -h * 0.5;
}
}
break;
case shapes[3]:
case shapes[4]:
w = size * 0.95;
h = w * 0.28;
if (ie8) {
x = 0;
y = d * 0.5 - h * 0.5;
} else {
x = d * 0.5 - w;
y = -h * 0.5;
}
arc = this._shape === shapes[4] ? 0.6 : 0;
break;
}
g = _setAttr(_setCSS(_addEl(&quot;group&quot;, this.vml), {width: d, height: d, rotation: ang}), {coordsize: d + &quot;,&quot; + d, coordorigin: -d * 0.5 + &quot;,&quot; + (-d * 0.5)});
s = _setCSS(_addEl(sh, g, {stroked: false, arcSize: arc}), { width: w, height: h, top: y, left: x});
f = _addEl(&quot;fill&quot;, s, {color: this._color, opacity: bitMod});
++i;
}
}
this._tick(true);
};
/**
* Renders the loader animation
* @private
* @method _tick
*/
p._tick = function (init) {
var c = this._con, di = this._diameter;
if (!init) { this._currentId += 360 / this._density * this._speed; }
if (engine === engines[0]) {
c.clearRect(0, 0, di, di);
_transCon(c, di * 0.5, di * 0.5, this._currentId / 180 * Math.PI);
c.drawImage(this._cCan, 0, 0, di, di);
c.restore();
} else {
if (this._currentId &gt;= 360) { this._currentId -= 360; }
_setCSS(this.vml, {rotation:this._currentId});
}
};
//////////////
//// Public methods
/////////////
/**
* Shows the rendering of the loader animation
* @method show
* @chainable
* @return {CanvasLoader} The CanvasLoader instance
*/
p.show = function () {
var self = this;
if(!self._timer) {
self._timer = setInterval(function () { self._tick(); }, Math.round(1000 / self._fps));
_setCSS(self._cont, {visibility: &quot;visible&quot;, display: &quot;block&quot;});
}
return self;
};
/**
* Stops the rendering of the loader animation and hides the loader
* @method hide
* @chainable
* @return {CanvasLoader} The CanvasLoader instance
*/
p.hide = function () {
if(this._timer) {
clearInterval(this._timer);
this._timer = null;
_setCSS(this._cont, {visibility: &quot;hidden&quot;, display: &quot;none&quot;});
}
return this;
};
/**
* Clears the DOM and resets all params
* @method destruct
*/
p.destruct = function () {
this.hide();
this.mum.removeChild(this._cont);
for (var n in this) {
delete this[n];
this[n] = null;
}
};
/**
* Return argument value by key if defined
* @param {String} key The key to look up
* @return The value of the key
* @method get
*/
p.get = function(key) {
if(key === &quot;defaults&quot;) {
return this._getDefaults();
}
if(this._settings.hasOwnProperty(key) &amp;&amp; this.hasOwnProperty(&quot;_&quot; + key)) {
return this[&quot;_&quot; + key];
}
};
window.CanvasLoader = CanvasLoader;
}(window));
</pre>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="../assets/vendor/prettify/prettify-min.js"></script>
<script>prettyPrint();</script>
<script src="../assets/js/yui-prettify.js"></script>
<script src="../assets/../api.js"></script>
<script src="../assets/js/api-filter.js"></script>
<script src="../assets/js/api-list.js"></script>
<script src="../assets/js/api-search.js"></script>
<script src="../assets/js/apidocs.js"></script>
</body>
</html>