Skip to content

Instantly share code, notes, and snippets.

View gfcarvalho's full-sized avatar

Gustavo Carvalho gfcarvalho

View GitHub Profile
@gfcarvalho
gfcarvalho / rAF.js
Created March 12, 2014 03:49 — forked from paulirish/rAF.js
// http://paulirish.com/2011/requestanimationframe-for-smart-animating/
// http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating
// requestAnimationFrame polyfill by Erik Möller. fixes from Paul Irish and Tino Zijdel
// MIT license
(function() {
var lastTime = 0;
var vendors = ['ms', 'moz', 'webkit', 'o'];
(function(w){
var perfNow;
var perfNowNames = ['now', 'webkitNow', 'msNow', 'mozNow'];
if(!!w['performance']) for(var i = 0; i < perfNowNames.length; ++i)
{
var n = perfNowNames[i];
if(!!w['performance'][n])
{
perfNow = function(){return w['performance'][n]()};
break;
// relies on Date.now() which has been supported everywhere modern for years.
// as Safari 6 doesn't have support for NavigationTiming, we use a Date.now() timestamp for relative values
// if you want values similar to what you'd get with real perf.now, place this towards the head of the page
// but in reality, you're just getting the delta between now() calls, so it's not terribly important where it's placed
(function(){
// prepare base perf object
@gfcarvalho
gfcarvalho / imageToBase64.js
Created March 12, 2014 07:54
Converting a local image to base64 using JavaScript (canvas + regexp)
function imageToBase64(img)
{
var canvas, ctx, dataURL, base64;
canvas = document.createElement("canvas");
ctx = canvas.getContext("2d");
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(img, 0, 0);
dataURL = canvas.toDataURL("image/png");
@gfcarvalho
gfcarvalho / xhrImageDemo.html
Created March 12, 2014 08:30
Loads an image using XHR2 and creates an image object from it.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<canvas id="cnv" width="100" height="100"></canvas>
<script>
var ctx = document.getElementById('cnv').getContext('2d');
/**
* Converts an image to
* a base64 string.
*
* If you want to use the
* outputFormat or quality param
* I strongly recommend you read the docs
* @ mozilla for `canvas.toDataURL()`
*
* @param {String} url
@gfcarvalho
gfcarvalho / transparent-canvas.html
Created March 13, 2014 00:47
Two ways to detect if an image generated via canvas is blank (transparent PNG). (canvas.getImageData vs canvas.toDataURL)
<!doctype html>
<html>
<head>
<title>Check transparent canvas</title>
</head>
<body>
<canvas id='editor' style='border:solid'></canvas>
<canvas id='blank' style='display:none'></canvas>
<script>
canvas = document.getElementById('editor');
@gfcarvalho
gfcarvalho / touchToMouse.js
Created March 15, 2014 03:08
Simulate Mouse Events in Touch Environments
function touchHandler(event)
{
var touches = event.changedTouches,
first = touches[0],
type = "";
switch(event.type)
{
case "touchstart": type = "mousedown"; break;
case "touchmove": type="mousemove"; break;
case "touchend": type="mouseup"; break;
@gfcarvalho
gfcarvalho / countProperties.js
Created March 25, 2014 20:57
Count the number of properties of an Object, in JavaScript.
function countProperties(obj) {
var count = 0,
k;
for (k in obj) {
if (obj.hasOwnProperty(k)) {
count += 1;
}
}
return count;
@gfcarvalho
gfcarvalho / shuffle.js
Created April 1, 2014 03:26
Shuffles a given array using an implemented version of Fisher–Yates algorithm
function shuffle(array) {
var counter = array.length, temp, index;
// While there are elements in the array
while (counter--) {
// Pick a random index
index = (Math.random() * (counter+1)) | 0;
// And swap the last element with it
temp = array[counter];