Skip to content

Instantly share code, notes, and snippets.

💭
🌊

Christian Fleschhut cfleschhut

💭
🌊
View GitHub Profile
@cfleschhut
cfleschhut / getDataURL.js
Created Jun 24, 2011
Quick base64 image encoding
View getDataURL.js
// 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 Jun 5, 2011
JavaScript Snippets: Augmented Native Objects
View sandbox.js
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");
}
View css3_snippets.css
.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
“Yellow Fade Technique” (Bulletproof Ajax)
View yft.js
/* 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() {
View jsanthology_timeandmotion.js
// 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
JavaScript Patterns [O’Reilly]
View jspatterns.js
/*
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
Plain JavaScript radioClass Fn
View radioclass.js
/* 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 Mar 28, 2011
ejohn.org “Advanced JavaScript” Code Examples
View learn.js
/* 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) {
You can’t perform that action at this time.