Created
May 29, 2012 08:53
-
-
Save ogorzalka/2823366 to your computer and use it in GitHub Desktop.
Make Canvas Responsive !
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($) { | |
$.fn.responsiveCanvas = function(method) { | |
var methods = { | |
// init functions | |
init : function(options) { | |
return this.each(function(i) { | |
var c = this, // THE canvas | |
ct = c.getContext('2d'), // check the context | |
$container = $(c).parent(); // parent container object | |
// Run function when browser loads & resizes | |
$(window).bind("load resize", function() { | |
methods.respondCanvas.call(c, $container); | |
}); | |
}); | |
}, | |
// the responsive function ! | |
respondCanvas: function($container) { | |
$(this).css({ | |
'width' : $container.innerWidth(), // max width | |
'height' : $container.innerHeight() // max height | |
}); | |
} | |
} | |
if (methods[method]) { | |
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1)); | |
} else if (typeof method === 'object' || !method) { | |
return methods.init.apply(this, arguments); | |
} else { | |
$.error( 'Method "' + method + '" does not exist in pluginName plugin!'); | |
} | |
} | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment