1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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);
|