Skip to content

Commit

Permalink
Make vivid wait for window load to be able to acquire image width and…
Browse files Browse the repository at this point in the history
… height.
  • Loading branch information
uxder committed Mar 23, 2012
1 parent b6ff1bb commit 2966623
Showing 1 changed file with 37 additions and 36 deletions.
73 changes: 37 additions & 36 deletions src/vivid.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Vivid - A jQuery Html5 Canvas Photo Effects Plugin Vivid - A jQuery Html5 Canvas Photo Effects Plugin
Author: Scott Murphy 2012 Author: Scott Murphy 2012
Github: uxder Github: uxder
Version - totally still working on this version Version - totally still working on this version and Experimental
Description: Description:
Vivid is a jQuery Plugin that transforms an image into a canvas so you can apply effects to it. Vivid is a jQuery Plugin that transforms an image into a canvas so you can apply effects to it.
Apply vivid to any image and it will replace it with a canvas that has all the same css styles and classes applied to the Apply vivid to any image and it will replace it with a canvas that has all the same css styles and classes applied to the
Expand Down Expand Up @@ -73,14 +73,13 @@ Vivid.core = (function( window, document, $){
options = { options = {
filter: 'blackWhite' filter: 'blackWhite'
}; };

/** /**
* Constructor Method for Nebula * Constructor Method for Vivid
* @param {Object Literal} [op] User defined setting to the plugin options * @param {Object Literal} [op] User defined setting to the plugin options
* @return this for jQuery chaining * @return this for jQuery chaining
*/ */
var n = function( el, op ) { var n = function( el, op ) {

// Mix in the passed-in options with the default options // Mix in the passed-in options with the default options
options = $.extend( {}, options, op ); options = $.extend( {}, options, op );


Expand All @@ -94,15 +93,13 @@ Vivid.core = (function( window, document, $){
applyFilter(options.filter); applyFilter(options.filter);


//remove the image //remove the image
$elem.remove(); $elem.hide();



// for chaining // for chaining
return this; return this;
}; };





/** /**
* apply the desired filter effect * apply the desired filter effect
* @param {String} [filterName] Name of effect to initialize * @param {String} [filterName] Name of effect to initialize
Expand All @@ -118,14 +115,15 @@ Vivid.core = (function( window, document, $){
"ctx": ctx, "ctx": ctx,
"options": options "options": options
}; };

//try { //try {
effect = new Vivid.filter[filterName](settings); effect = new Vivid.filter[filterName](settings);
// } catch(err) { // } catch(err) {
// throw new Error("Oops. Looks like the Vivid Filter you requested is not available."); // throw new Error("Oops. Looks like the Vivid Filter you requested is not available.");
// } // }
} }


/** /**
* Create a dynamic canvas element and hide the $elem image * Create a dynamic canvas element and hide the $elem image
*/ */
Expand All @@ -134,19 +132,19 @@ Vivid.core = (function( window, document, $){
var canvasHtml, var canvasHtml,
style, style,
classList; classList;

imgW = $elem.width(); imgW = $elem.width();
imgH = $elem.height(); imgH = $elem.height();

//create a canvas element //create a canvas element
canvasHtml = '<canvas width="'+ imgW +'" height="'+ imgH +'"></canvas>'; canvasHtml = '<canvas width="'+ imgW +'" height="'+ imgH +'"></canvas>';
$elem.after(canvasHtml); $elem.after(canvasHtml);

//save and create canvas context //save and create canvas context
$canvas = $elem.next('canvas'); $canvas = $elem.next('canvas');
ctx = $canvas[0].getContext('2d'); ctx = $canvas[0].getContext('2d');
} }

/** /**
* Also computed Styles of the image to the canvas * Also computed Styles of the image to the canvas
*/ */
Expand All @@ -155,26 +153,26 @@ Vivid.core = (function( window, document, $){
var style = {}, var style = {},
cStyle, cStyle,
camelize; camelize;

camelize = function(a,b){ camelize = function(a,b){
return b.toUpperCase(); return b.toUpperCase();
} }

//get the computer style for the image*/ //get the computer style for the image*/
cStyle = window.getComputedStyle($elem[0], null) cStyle = window.getComputedStyle($elem[0], null)

//loop through it and reformat as an object we can pass to jQuery .css method //loop through it and reformat as an object we can pass to jQuery .css method
for(var i=0;i<cStyle.length;i++){ for(var i=0;i<cStyle.length;i++){
var p = cStyle[i]; var p = cStyle[i];
var c = p.replace(/\-([a-z])/g, camelize); var c = p.replace(/\-([a-z])/g, camelize);
var v = cStyle.getPropertyValue(p); var v = cStyle.getPropertyValue(p);
style[c] = v; style[c] = v;
} }

//apply style //apply style
$canvas.css(style); $canvas.css(style);
} }

/** /**
* Transfer over classes and Ids that were on the image to the canvas * Transfer over classes and Ids that were on the image to the canvas
*/ */
Expand All @@ -188,17 +186,17 @@ Vivid.core = (function( window, document, $){
//transfer any ID elements //transfer any ID elements
if($elem.attr('id')) $canvas.attr('id', $elem.attr('id')); if($elem.attr('id')) $canvas.attr('id', $elem.attr('id'));
} }


/** /**
* Draw the image onto the canvas after the image has been loaded * Draw the image onto the canvas after the image has been loaded
*/ */
var draw = function() { var draw = function() {
ctx.drawImage($elem[0], 0, 0, imgW, imgH); ctx.drawImage($elem[0], 0, 0, imgW, imgH);
} }

return n; return n;

})( window, document, jQuery ); })( window, document, jQuery );


/* /*
Expand All @@ -216,20 +214,24 @@ if ( typeof Object.create !== 'function' ) {
* Finally Create the Vivid jQuery Plugin Object * Finally Create the Vivid jQuery Plugin Object
*/ */
$.fn.vivid = function(options) { $.fn.vivid = function(options) {

//test for canvas support //test for canvas support
//or is it best to have user test this with modernizr? //or is it best to have user test this with modernizr?
var canvasSupport = function() { var canvasSupport = function() {
var elem = document.createElement('canvas'); var elem = document.createElement('canvas');
return !!(elem.getContext && elem.getContext('2d')); return !!(elem.getContext && elem.getContext('2d'));
} }

//check for canvas support //check for canvas support
if(!canvasSupport()) return; if(!canvasSupport()) return;


//create Vivid object var self = this; //self correct scope
return this.each(function() { //need to wait for the window to load to ensure that image width is calculated
Object.create(Vivid.core(this,options)); $(window).load(function() {
//create Vivid object
return self.each(function() {
Object.create(Vivid.core(this,options));
});
}); });
} }


Expand All @@ -244,7 +246,7 @@ Vivid.filter.blackWhite = (function( window, document, jQuery) {
options = { options = {
lightness: 0.3 lightness: 0.3
}; };

/** /**
* Constructor * Constructor
* @param {Object Literal} [settings] Object that contains elem, $elem, imgW, imgH, $canvas, ctx, options * @param {Object Literal} [settings] Object that contains elem, $elem, imgW, imgH, $canvas, ctx, options
Expand All @@ -257,24 +259,23 @@ Vivid.filter.blackWhite = (function( window, document, jQuery) {
//initialize plugin //initialize plugin
this.init(); this.init();
} }

//Pubic Methods //Pubic Methods
filter.prototype = { filter.prototype = {
init: function() { init: function() {
var imgd = s.ctx.getImageData(0, 0, s.imgW, s.imgH); var imgd = s.ctx.getImageData(0, 0, s.imgW, s.imgH);
var p = imgd.data; var p = imgd.data;
for (var i = 0, n = p.length; i < n; i += 4) { for (var i = 0, n = p.length; i < n; i += 4) {
var grayscale = p[i] * options.lightness + p[i+1] * 0.6 + p[i+2] * 0.1; var grayscale = p[i] * options.lightness + p[i+1] * 0.6 + p[i+2] * 0.1;

p[i] = grayscale; //red p[i] = grayscale; //red
p[i+1] = grayscale; //blue p[i+1] = grayscale; //blue
p[i+2] = grayscale; //green p[i+2] = grayscale; //green
} }
s.ctx.putImageData(imgd, 0, 0); s.ctx.putImageData(imgd, 0, 0);
} }
} }

//return the plugin object //return the plugin object
return filter; return filter;
})( window, document, jQuery ); })( window, document, jQuery );

0 comments on commit 2966623

Please sign in to comment.