Skip to content

Instantly share code, notes, and snippets.

View cfleschhut's full-sized avatar
🌍
#NeverStopLearning

Christian Fleschhut cfleschhut

🌍
#NeverStopLearning
View GitHub Profile
@cfleschhut
cfleschhut / getDataURL.js
Created June 24, 2011 00:19
Quick base64 image encoding
// adjust image selector in fn-call
// and run this from the (firebug, etc.) console
(function(imgObj) {
var imgsrc = imgObj.src;
var canvas = document.createElement("canvas"),
ctx = canvas.getContext("2d");
canvas.width = imgObj.width;
canvas.height = imgObj.height;
var newimg = new Image();
newimg.src = imgsrc;
@cfleschhut
cfleschhut / sandbox.js
Created June 5, 2011 18:39
JavaScript Snippets: Augmented Native Objects
String.augmentedProperties = [];
if (!String.prototype.camelize) {
String.prototype.camelize = function() {
return this.replace(/(\s)([a-zA-Z])/g, function(str, p1, p2) {
return p2.toUpperCase();
});
};
String.augmentedProperties.push("camelize");
}
@cfleschhut
cfleschhut / css3_snippets.css
Created May 23, 2011 20:51
CSS3 Snippets [TVM]
.default {
width: 100px; height: 100px; background: orange;
text-align: center; line-height: 100px;
position: absolute; top: 50px; left: 50px;
-webkit-transition: all 0.25s ease-in-out;
-moz-transition: all 0.25s ease-in-out;
}
/* Gradients (linear/radial) */
@cfleschhut
cfleschhut / yft.js
Created May 22, 2011 21:39
“Yellow Fade Technique” (Bulletproof Ajax)
/* Demo: http://jsfiddle.net/HdMMj/ */
function fadeUp(element, red, green, blue) {
if (element.fade) clearTimeout(element.fade);
element.style.backgroundColor = "rgb(" + red + ", " + green + ", " + blue + ")";
if (red == 255 && green == 255 && blue == 255) return;
var newred = red + Math.ceil((255 - red)/10);
var newgreen = green + Math.ceil((255 - green)/10);
var newblue = blue + Math.ceil((255 - blue)/10);
element.fade = setTimeout(function() {
@cfleschhut
cfleschhut / jsanthology_timeandmotion.js
Created May 17, 2011 23:03
SitePoint JS Anthology
// object movement
function init() {
var obj = document.getElementById("move");
obj.timerID = setInterval(function() {
moveObject(obj, 500, 0, 25);
}, 50);
}
function moveObject(target, destinationLeft, destinationTop, maxSpeed) {
var currentLeft = parseInt(target.style.left);
var currentTop = parseInt(target.style.top);
@cfleschhut
cfleschhut / jspatterns.js
Created May 15, 2011 22:39
JavaScript Patterns [O’Reilly]
/*
Chapter 3: Literals and Constructors
*/
// Object Constructor Catch
var o = new Object();
console.log(o.constructor.name);
var o = new Object(1);
console.log(o.constructor.name);
var o = new Object("str");
@cfleschhut
cfleschhut / radioclass.js
Created May 15, 2011 13:50
Plain JavaScript radioClass Fn
/* adapted from Ext Core radioClass */
function hasClass(el, cls) {
return new RegExp("(^|\\s)" + cls + "(\\s|$)").test(el.className);
}
function addClass(el, cls) {
if (!hasClass(el, cls)) {
el.className += el.className ? " " + cls : cls;
}
}
@cfleschhut
cfleschhut / learn.js
Created March 28, 2011 14:00
ejohn.org “Advanced JavaScript” Code Examples
/* Chapter 14: Enforcing Function Context */
// http://ejohn.org/apps/learn/#86
Function.prototype.bind = function() {
var fn = this, args = Array.prototype.slice.call(arguments), object = args.shift();
return function() {
return fn.apply(object, args.concat(Array.prototype.slice.call(arguments)));
};
};
// function bind(context, name) {