PageSourceSearch

https://www.qianxin.com/static/olympics/js/snow-plugin.js

js qianxin.com collected 2026-10-01 10:40:35 UTC 12,944 bytes, 303 lines download raw bytes

1// JavaScript Document
2(function ($) {
3    //for html5 canvas;
4    var $window = window,
5        $timeout = setTimeout;
6
7    var supportCanvas = function () {
8        var eCan = document.createElement("canvas");
9        return (typeof eCan.getContext) == "function";
10    };
11    window.Snow = function (element, settings) {
12        (function () {
13            var lastTime = 0;
14            var vendors = ['webkit', 'moz'];
15            for (var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
16                window.requestAnimationFrame = window[vendors[x] + 'RequestAnimationFrame'];
17                window.cancelAnimationFrame = window[vendors[x] + 'CancelAnimationFrame'] ||    // name has changed in Webkit
18                    window[vendors[x] + 'CancelRequestAnimationFrame'];
19            }
20
21            if (!window.requestAnimationFrame) {
22                window.requestAnimationFrame = function (callback, element) {
23                    /*
24                    var currTime = new Date().getTime();
25                    var timeToCall = Math.max(0, 16.7 - (currTime - lastTime));
26                    var id = window.setTimeout(function () {
27                        callback(currTime + timeToCall);
28                    }, timeToCall);
29                    lastTime = currTime + timeToCall;
30                     */
31                    var timeToCall = 14; //freezes in safari for windows ,and mac to , so i change time to call with 14;
32                    var id = window.setTimeout(function () {
33                        callback(timeToCall);
34                    }, timeToCall);
35
36                    return id;
37                };
38            }
39            if (!window.cancelAnimationFrame) {
40                window.cancelAnimationFrame = function (id) {
41                    clearTimeout(id);
42                };
43            }
44        }());
45        this.settings = settings,
46            this.flakes = [],
47            this.flakeCount = settings.count,
48            this.mx = -100,
49            this.my = -100,
50            this.init(element)
51    };
52    Snow.prototype.init = function (element) {
53        this.canvas = element.get(0), this.ctx = this.canvas.getContext("2d"), this.canvas.width = $window.innerWidth, this.canvas.height = $window.innerHeight, this.flakes = [];
54        for (var i = 0; i < this.flakeCount; i++) {
55            var x = Math.floor(Math.random() * this.canvas.width),
56                y = Math.floor(Math.random() * this.canvas.height),
57                size = Math.floor(100 * Math.random()) % this.settings.size + 2,
58                speed = Math.floor(100 * Math.random()) % this.settings.speed + Math.random() * size / 10 + .5,
59                opacity = .5 * Math.random() + this.settings.opacity;
60            this.flakes.push({
61                speed: speed,
62                velY: speed,
63                velX: 0,
64                x: x,
65                y: y,
66                size: size,
67                stepSize: Math.random() / 30,
68                step: 0,
69                angle: 180,
70                opacity: opacity
71            })
72        }
73        1 == this.settings.interaction && this.canvas.addEventListener("mousemove", function (e) {
74            this.mx = e.clientX, this.my = e.client
75        });
76        var thiz = this;
77        $($window).resize(function () {
78            thiz.ctx.clearRect(0, 0, thiz.canvas.width, thiz.canvas.height), thiz.canvas.width = $window.innerWidth, thiz.canvas.height = $window.innerHeight
79        });
80        if (typeof this.settings.image === "string") {
81            this.image = $("<img src='" + this.settings.image + "' style='display: none'>");
82        }
83        ;
84
85        this.snow();
86    }, Snow.prototype.snow = function () {
87        var thiz = this,
88            render = function () {
89                thiz.ctx.clearRect(0, 0, thiz.canvas.width, thiz.canvas.height);
90                for (var i = 0; i < thiz.flakeCount; i++) {
91                    var flake = thiz.flakes[i],
92                        x = thiz.mx,
93                        y = thiz.my,
94                        minDist = 100,
95                        x2 = flake.x,
96                        y2 = flake.y,
97                        dist = Math.sqrt((x2 - x) * (x2 - x) + (y2 - y) * (y2 - y));
98                    if (minDist > dist) {
99                        var force = minDist / (dist * dist),
100                            xcomp = (x - x2) / dist,
101                            ycomp = (y - y2) / dist,
102                            deltaV = force / 2;
103                        flake.velX -= deltaV * xcomp, flake.velY -= deltaV * ycomp
104                    } else
105                        switch (flake.velX *= .98, flake.velY <= flake.speed && (flake.velY = flake.speed), thiz.settings.windPower) {
106                            case !1:
107                                flake.velX += Math.cos(flake.step += .05) * flake.stepSize;
108                                break;
109                            case 0:
110                                flake.velX += Math.cos(flake.step += .05) * flake.stepSize;
111                                break;
112                            default:
113                                flake.velX += .01 + thiz.settings.windPower / 100
114                        }
115                    if (flake.y += flake.velY, flake.x += flake.velX, (flake.y >= thiz.canvas.height || flake.y <= 0) && thiz.resetFlake(flake), (flake.x >= thiz.canvas.width || flake.x <= 0) && thiz.resetFlake(flake), 0 == thiz.settings.image) {
116                        var grd = thiz.ctx.createRadialGradient(flake.x, flake.y, 0, flake.x, flake.y, flake.size - 1);
117                        grd.addColorStop(0, thiz.settings.startColor), grd.addColorStop(1, thiz.settings.endColor), thiz.ctx.fillStyle = grd, thiz.ctx.beginPath(), thiz.ctx.arc(flake.x, flake.y, flake.size, 0, 2 * Math.PI), thiz.ctx.fill()
118                    } else
119                        thiz.ctx.drawImage(thiz.image.get(0), flake.x, flake.y, 2 * flake.size, 2 * flake.size)
120                }
121                $window.cancelAnimationFrame(render), $window.requestAnimationFrame(render)
122            };
123        render()
124    }, Snow.prototype.resetFlake = function (flake) {
125        if (0 == this.settings.windPower || 0 == this.settings.windPower)
126            flake.x = Math.floor(Math.random() * this.canvas.width), flake.y = 0;
127        else if (this.settings.windPower > 0) {
128            var xarray = Array(Math.floor(Math.random() * this.canvas.width), 0),
129                yarray = Array(0, Math.floor(Math.random() * this.canvas.height)),
130                allarray = Array(xarray, yarray),
131                selected_array = allarray[Math.floor(Math.random() * allarray.length)];
132            flake.x = selected_array[0], flake.y = selected_array[1]
133        } else {
134            var xarray = Array(Math.floor(Math.random() * this.canvas.width), 0),
135                yarray = Array(this.canvas.width, Math.floor(Math.random() * this.canvas.height)),
136                allarray = Array(xarray, yarray),
137                selected_array = allarray[Math.floor(Math.random() * allarray.length)];
138            flake.x = selected_array[0], flake.y = selected_array[1]
139        }
140        flake.size = Math.floor(100 * Math.random()) % this.settings.size + 2, flake.speed = Math.floor(100 * Math.random()) % this.settings.speed + Math.random() * flake.size / 10 + .5, flake.velY = flake.speed, flake.velX = 0, flake.opacity = .5 * Math.random() + this.settings.opacity
141    };
142
143    $.fn.snow = function () {
144        var userCanvas = supportCanvas();
145        userCanvas && $(this).each(function (i, e) {
146            var scope = {};
147            $.each(e.attributes, function (index, key) {
148                scope[ $.camelCase(key.name) ] = Number(Number(key.value)) ? Number(key.value) : key.value
149            });
150            if (typeof scope.image === "string" && scope.image === "fal
150se") {
151                scope.image = false
152            }
153            ;
154
155            new Snow($(e), {
156                speed: 1 || 0,
157                interaction: scope.interaction || !0,
158                size: scope.size || 2,
159                count: scope.count || 200,
160                opacity: scope.opacity || 1,
161                startColor: scope.startColor || "rgba(255,255,255,1)",
162                endColor: scope.endColor || "rgba(255,255,255,0)",
163                windPower: scope.windPower || 0,
164                image: scope.image || !1
165            });
166        });
167        if (!userCanvas) {
168            var setting = {};
169            $(this).each(function (i, e) {
170                setting["image"] = $(e).attr("image") || "./imgs/snow.png";
171                $(this).remove();
172                createSnow("", 40);
173            });
174        }
175        ;
176    };
177
178    //for ie678
179    /**
180     * method createSnow("", 40);
181     * method removeSnow();
182     */
183    function k(a, b, c) {
184        if (a.addEventListener) a.addEventListener(b, c, false);
185        else a.attachEvent && a.attachEvent("on" + b, c)
186    }
187
188    function g(a) {
189        if (typeof window.onload != "function") window.onload = a;
190        else {
191            var b = window.onload;
192            window.onload = function () {
193                b();
194                a()
195            }
196        }
197    }
198
199    function h() {
200        var a = {};
201        for (type in {
202            Top: "",
203            Left: ""
204        }) {
205            var b = type == "Top" ? "Y" : "X";
206            if (typeof window["page" + b + "Offset"] != "undefined")
207                a[type.toLowerCase()] = window["page" + b + "Offset"];
208            else {
209                b = document.documentElement.clientHeight ? document.documentElement : document.body;
210                a[type.toLowerCase()] = b["scroll" + type]
211            }
212        }
213        return a
214    }
215
216    function l() {
217        var a = document.body,
218            b;
219        if (window.innerHeight) b = window.innerHeight;
220        else if (a.parentElement.clientHeight) b = a.parentElement.clientHeight;
221        else if (a && a.clientHeight) b = a.clientHeight;
222        return b
223    };
224    var j = true;
225    var f = true;
226    var m = null;
227    var c = [];
228    var createSnow = function (a, b) {
229        clearInterval(m);
230        c = [];
231        m = setInterval(function () {
232            f && b > c.length && Math.random() < b * 0.0025 && c.push(new i(a));
233            !f && !c.length && clearInterval(m);
234            for (var e = h().top, n = l(), d = c.length - 1; d >= 0; d--)
235                if (c[d])
236                    if (c[d].top < e || c[d].top + c[d].size + 1 > e + n) {
237                        c[d].remove();
238                        c[d] = null;
239                        c.splice(d, 1)
240                    } else {
241                        c[d].move();
242                        c[d].draw()
243                    }
244        }, 40);
245        k(window, "scroll",
246            function () {
247                for (var e = c.length - 1; e >= 0; e--) c[e].draw()
248            })
249    };
250    var removeSnow = function () {
251        clearInterval(m);
252        do {
253            c.pop().remove();
254        } while (c.length);
255    };
256
257    //雪花的构造函数;
258    function i(a) {
259        this.parent = document.body;
260        this.createEl(this.parent, a);
261        this.size = Math.random() * 5 + 5;
262        this.el.style.width = Math.round(this.size) + "px";
263        this.el.style.height = Math.round(this.size) + "px";
264        this.maxLeft = document.body.offsetWidth - this.size;
265        this.maxTop = document.body.offsetHeight - this.size;
266        this.left = Math.random() * this.maxLeft;
267        this.top = h().top + 1;
268        this.angle = 1.4 + 0.2 * Math.random();
269        this.minAngle = 1.4;
270        this.maxAngle = 1.6;
271        this.angleDelta = 0.01 * Math.random();
272        this.speed = 2 + Math.random()
273    }
274
275    i.prototype = {
276        createEl: function (a, b) {
277            this.el = document.createElement("img");
278            this.el.classname = "nicesnowclass";
279            this.el.setAttribute("src", b || "./imgs/snow.png");
280            this.el.style.position = "absolute";
281            this.el.style.display = "block";
282            this.el.style.zIndex = "99999";
283            this.parent.appendChild(this.el)
284        },
285        move: function () {
286            if (this.angle < this.minAngle || this.angle > this.maxAngle)
287                this.angleDelta = -this.angleDelta;
288            this.angle += this.angleDelta;
289            this.left += this.speed * Math.cos(this.angle * Math.PI);
290            this.top -= this.speed * Math.sin(this.angle * Math.PI);
291            if (this.left < 0) this.left = this.maxLeft;
292            else if (this.left > this.maxLeft) this.left = 0
293        },
294        draw: function () {
295            this.el.style.top = Math.round(this.top) + "px";
296            this.el.style.left = Math.round(this.left) + "px"
297        },
298        remove: function () {
299            this.parent.removeChild(this.el);
300            this.parent = this.el = null
301        }
302    };
303})(jQuery);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.