/*jslint plusplus: true, white: true, indent: 2, maxlen: 90 */
/*global HTMLCanvasElement, document */
/**
* @author Wolfgang Kowarschick
* @copyright 2013, Wolfgang Kowarschick
* <br/>
* Redistribution and use in source and binary forms, with or without
* modification, are permitted under the terms of the
* Creative Commons License Attribution-NonCommercial-ShareAlike 3.0 Unported
* (CC BY-NC-SA 3.0: http://creativecommons.org/licenses/by-nc-sa/3.0/).
*/
(function($wk$)
{ "use strict";
////////////////////////////////////////////////////////////////////////////////
// Class "Math"
////////////////////////////////////////////////////////////////////////////////
/**
* @class
* @name $wk$.Math
*/
/**
* Computes a random integer number within an interval.
* @method
* @name $wk$.Math.randomInt
* @param {Number | Array<Number>} p_interval
* @returns An integer number within the interval
* [<code>p_interval[0]</code>, <code>p_interval[1]</code>].
*/
Math.randomInt =
function(p_interval)
{ if (p_interval instanceof Array)
{ var l_min = p_interval[0], l_max = p_interval[1];
return l_min+Math.floor(Math.random()*(l_max-l_min+1));
}
else
{ return Math.floor(p_interval); }
};
/**
* Computes a random number within an interval.
* @method
* @name $wk$.Math.randomNum
* @param {Number | Array<Number>} p_interval
* @returns A number within the interval
* [<code>p_interval[0]</code>, <code>p_interval[1]</code>].
*/
Math.randomNum =
function(p_interval)
{ if (p_interval instanceof Array)
{ var l_min = p_interval[0], l_max = p_interval[1];
return l_min+Math.random()*(l_max-l_min);
}
else
{ return p_interval; }
};
$wk$.Math = Math;
////////////////////////////////////////////////////////////////////////////////
//End of Class "Math"
////////////////////////////////////////////////////////////////////////////////
}(this.$wk$));
/**
* @class
* @name HTMLCanvasElement
*/
/**
* @method
* @name HTMLCanvasElement#mousePos
* @source http://stackoverflow.com/questions/55677/how-do-i-get-the-coordinates-of-a-mouse-click-on-a-canvas-element
* @source http://stackoverflow.com/questions/10449890/detect-mouse-click-location-within-canvas
*
* @param {Object} p_event An event object dispatched after an mouse
* interaction with a canvas element.
* @returns The mouse position relative to the canvas left and top borders.
*/
HTMLCanvasElement.prototype.mousePos
= function(p_event)
{ "use strict";
var l_parent_node = document.body.parentNode,
lm_style = document.defaultView.getComputedStyle,
l_total_offset_x =
l_parent_node.offsetLeft
+ (parseInt(lm_style(this).borderLeftWidth, 10) || 0),
l_total_offset_y =
l_parent_node.offsetTop
+ (parseInt(lm_style(this).borderTopWidth, 10) || 0),
l_canvas_x = 0,
l_canvas_y = 0,
l_element = this;
do
{ l_total_offset_x += l_element.offsetLeft - l_element.scrollLeft;
l_total_offset_y += l_element.offsetTop - l_element.scrollTop;
l_element = l_element.offsetParent;
}
while(l_element);
l_canvas_x = p_event.pageX - l_total_offset_x;
l_canvas_y = p_event.pageY - l_total_offset_y;
if ( 0 <= l_canvas_x && 0 <= l_canvas_y
&& l_canvas_x < this.width && l_canvas_y < this.height)
{ return {x: l_canvas_x, y: l_canvas_y}; }
else
{ return null; }
};