Skip to content

Instantly share code, notes, and snippets.

@agmcleod
Created October 29, 2014 12:56
Show Gist options
  • Save agmcleod/4429283376124c1f1bb6 to your computer and use it in GitHub Desktop.
Save agmcleod/4429283376124c1f1bb6 to your computer and use it in GitHub Desktop.
Testing cocoon js RAF
var me = {};
(function () {
/**
* Convert first character of a string to uppercase, if it's a letter.
* @ignore
* @function
* @name capitalize
* @param {String} str Input string.
* @return {String} String with first letter made uppercase.
*/
var capitalize = function (str) {
return str.substring(0, 1).toUpperCase() + str.substring(1, str.length);
};
/**
* A collection of utilities to ease porting between different user agents.
* @namespace me.agent
* @memberOf me
*/
me.agent = (function () {
var api = {};
/**
* Known agent vendors
* @ignore
*/
var vendors = [ "ms", "MS", "moz", "webkit", "o" ];
/**
* Get a vendor-prefixed property
* @public
* @name prefixed
* @function
* @param {String} name Property name
* @param {Object} [obj=window] Object or element reference to access
* @return {Mixed} Value of property
* @memberOf me.agent
*/
api.prefixed = function (name, obj) {
obj = obj || window;
if (name in obj) {
return obj[name];
}
var uc_name = capitalize(name);
var result;
vendors.some(function (vendor) {
var name = vendor + uc_name;
return (result = (name in obj) ? obj[name] : undefined);
});
return result;
};
/**
* Set a vendor-prefixed property
* @public
* @name setPrefixed
* @function
* @param {String} name Property name
* @param {Mixed} value Property value
* @param {Object} [obj=window] Object or element reference to access
* @memberOf me.agent
*/
api.setPrefixed = function (name, value, obj) {
obj = obj || window;
if (name in obj) {
obj[name] = value;
return;
}
var uc_name = capitalize(name);
vendors.some(function (vendor) {
var name = vendor + uc_name;
if (name in obj) {
obj[name] = value;
return true;
}
return false;
});
};
return api;
})();
})();
var parent = document.getElementById("screen");
var canvas = document.createElement("canvas");
canvas.width = 1136;
canvas.height = 640;
var ctx = canvas.getContext("2d");
parent.appendChild(canvas);
window.requestAnimationFrame = me.agent.prefixed('requestAnimationFrame');
function render () {
requestAnimationFrame(render);
ctx.clearRect(0, 0, 1136, 640);
ctx.fillStyle = '#ff0';
ctx.fillRect(50, 50, 250, 250);
}
render();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment