diff options
| author | Jules <jules@asdf.us> | 2014-07-16 00:43:23 -0400 |
|---|---|---|
| committer | Jules <jules@asdf.us> | 2014-07-16 00:43:23 -0400 |
| commit | 21cc87e31e18f3d0fefe2dae138493ad28df5725 (patch) | |
| tree | bec456c6cb4bb26df12068066bb23b8a073cc8d6 /columns/okstars.js | |
| parent | f62ef35f01e44f38552725425c1fe67ae1e682ec (diff) | |
| parent | 96da808972316b2ced88867e7960438e8159cd1e (diff) | |
Merge branch 'master' of ghghgh.us:trees
Diffstat (limited to 'columns/okstars.js')
| -rw-r--r-- | columns/okstars.js | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/columns/okstars.js b/columns/okstars.js new file mode 100644 index 0000000..4f1cde3 --- /dev/null +++ b/columns/okstars.js @@ -0,0 +1,74 @@ +(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); |
