(function($){ $.okstars = function(el, options){ var base = this; base.$el = $(el); base.el = el; base.$el.data("okstars", base); base.init = function () { base.options = $.extend({}, $.okstars.options, options); if (! base.canvas) base.canvas = base.new_canvas(); base.context = base.canvas.getContext("2d"); base.build(); $(window).resize(base.build) }; base.new_canvas = function () { var canvas = document.createElement("canvas"); canvas.style.cssText = "position:fixed;top:0;left:0;z-index:-1;"; base.$el.append(canvas); return canvas; }; function randrange(a,b){ var x = Math.random() return a * (1-x) + b * x } base.build = function () { var w = base.canvas.width = window.innerWidth var h = base.canvas.height = window.innerHeight var context = base.context; var starcount = ~~randrange(base.options.starMin, base.options.starMax) var tt = +new Date() for (var i = 0; i < starcount; i++) { var radius = Math.random() * 2 context.fillStyle = base.mottle_gray(base.options.color, base.options.colorrange) context.fillRect( Math.random() * w, Math.random() * h, radius,radius) } // console.log("generated", starcount, "stars in", (+new Date() - tt), "ms") }; base.clamp = function (x, min, max) { return Math.max(min, Math.min(max, x)); }; base.mottle_gray = function (color, radius) { var rgb = color.split("") var rgbcolor = 'rgb(' var offset = Math.floor(Math.random()*radius*2 - radius) rgbcolor += parseInt(rgb[0], 16) * 16 + offset rgbcolor += "," rgbcolor += parseInt(rgb[1], 16) * 16 + offset rgbcolor += "," rgbcolor += parseInt(rgb[2], 16) * 16 + offset rgbcolor += ")" return rgbcolor }; base.init(); }; $.okstars.options = { transparent: false, starMin: 100, starMax: 1000, color: "888", colorrange: 128 }; $.fn.okstars = function(options){ return this.each(function(){ (new $.okstars(this, options)); }); }; })(jQuery);