Skip to content

Instantly share code, notes, and snippets.

var getImageNaturalSize = function(img){
var w = img.width,
h = img.height,
tw, th; // Temp
if (typeof img.naturalWidth !== undefined) { // for Firefox, Safari, Chrome
w = img.naturalWidth;
h = img.naturalHeight;
} else if (typeof img.runtimeStyle !== undefined) { // for IE
var run = img.runtimeStyle;
@akm2
akm2 / gist:6190920
Last active December 20, 2015 20:29
function isImageLoaded(img) {
if (!img.complete) return false;
if (typeof img.naturalWidth !== 'undefined' && img.naturalWidth === 0) return false;
return true;
}
function onImageLoaded(img, callback) {
if (isImageLoaded(img)) callback.call(img);
else img.addEventListener('load', callback, false);
}
@akm2
akm2 / vector2d.js
Created November 17, 2012 21:52
Vector2d
/**
* Vector2d
*/
function Vector2d(x, y) {
this.x = x || 0;
this.y = y || 0;
}
Vector2d.add = function(a, b) {
return new Vector2d(a.x + b.x, a.y + b.y);
@akm2
akm2 / log.js
Created October 25, 2012 07:26
log
var log = (function(c) {
var limit = 1000, count = 0;
function log() {
if (limit) { if (limit === count) return; count++; }
c.log.apply(c, arguments);
return log;
}
log.limit = function(_limit) { limit = _limit < 0 ? 0 : _limit; return log; };
@akm2
akm2 / gist:3936493
Created October 23, 2012 03:41
printf
function printf(format) {
var pattern;
for (var i = 0, len = arguments.length - 1; i < len; i++) {
pattern = new RegExp('\\{' + i + '\\}', 'g');
format = format.replace(pattern, arguments[i + 1]);
}
return format;
}
// printf('Hello {0}', 'World'); // Hello World
@akm2
akm2 / random.js
Created October 21, 2012 15:52
Random
var Random = (function() {
/**
* Alea, Mash function
*
* @see http://baagoe.com/en/RandomMusings/javascript/
*/
function Random() {
var s0 = 0, s1 = 0, s2 = 0, c = 1;