Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisvoncsefalvay/9569257 to your computer and use it in GitHub Desktop.
Save chrisvoncsefalvay/9569257 to your computer and use it in GitHub Desktop.
Randomising font sizes and positions
(function( $ ) {
$.fn.randomise = function(options) {
var settings = $.extend({
// Setting defaults
position: "absolute",
sizemin: 10,
sizemax: 64,
xmin: 0,
ymin: 0,
xmax: 640,
ymax: 480
}, options);
// Loop function to randomise positions
// and font-size.
return this.each(function() {
$(this).css( 'position', settings.position );
var fs = Math.floor(Math.random() * (settings.sizemax - settings.sizemin)) + settings.sizemin;
var fx = Math.floor(Math.random() * (settings.xmax - settings.xmin)) + settings.xmin;
var fy = Math.floor(Math.random() * (settings.ymax - settings.ymin)) + settings.ymin;
$( this ).css('font-size', fs);
$( this ).css('top', fx);
$( this ).css('left', fy);
});
};
}( jQuery ));
// Use example, utilising all defaults
$('.objects').randomise();
@chrisvoncsefalvay
Copy link
Author

Partly created as a test harness for collision detection.

How to work it:

Initiate randomise(); with the parameters for font-size, xpos and ypos. Target the desired object through specifying the target - don't forget to specify . for classes/# for ids.

Update: Thanks to @henrahmagix, this is now a fully fledged jQuery plugin. Enjoy at your heart's delight.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment