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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
var Grid = (function(){
var Grid = function(opt){
var horizontal = this.horizontal = []
var vertical = this.vertical = []
var x, xmin, xmax
var y, ymin, ymax
var dx = opt.strokeWidth + opt.side
var dy = dx
var i
var s
opt = this.opt = defaults(opt, {
bg: "#ffffff",
stroke: "#000000",
strokeWidth: 2,
side: 10,
delay: 0,
shuffleDelay: false,
randomizeDelay: false,
duration: 500,
width: 300,
height: 300,
halign: "left",
valign: "top",
snap: null,
})
if (typeof opt.stroke == 'string') opt.stroke = [opt.stroke]
if (typeof opt.delay == 'string') opt.delay = [opt.delay]
if (opt.snap) {
s = this.snap = opt.snap
} else {
s = this.snap = Snap(opt.width, opt.height)
s.attr({ viewBox: "0 0 " + opt.width + " " + opt.height })
}
switch (opt.halign) {
case "right":
ymin = opt.height % dy
ymax = opt.height + 1
break
default:
case "left":
ymin = 0
ymax = opt.height
break
}
switch (opt.valign) {
case "bottom":
xmin = opt.width % dx
xmax = opt.width + 1
break
default:
case "top":
xmin = 0
xmax = opt.width
break
}
this.bg = s.rect(0, 0, "100%", "100%").attr({ fill: opt.bg })
this.pos = {
dx: dx, xmin: xmin, xmax: xmax,
dy: dy, ymin: ymin, ymax: ymax,
}
for (i = 0, x = xmin; x < xmax; x += dx, i++) {
vertical.push( s.path().attr({
strokeWidth: opt.strokeWidth,
stroke: opt.stroke[ i % opt.stroke.length ],
}) )
}
for (i = 0, y = ymin; y < ymax; y += dy, i++) {
horizontal.push( s.path().attr({
strokeWidth: opt.strokeWidth,
stroke: opt.stroke[ i % opt.stroke.length ],
}) )
}
}
Grid.prototype.animate = function(opt, cb){
opt = defaults(opt, {
h: "right",
v: "down",
})
var duration = this.opt.duration
var height = this.opt.height, width = this.opt.width
var x = this.pos.xmin, y = this.pos.ymin
var right = opt.h == "right"
var down = opt.v == "down"
this.vertical.forEach(function(p, i){
var start_path, end_path
if (down) {
start_path = vpath( x, 0, 0 )
end_path = vpath( x, 0, height )
}
else {
start_path = vpath( x, height, 0 )
end_path = vpath( x, height, -height )
}
var delay = this.opt.randomizeDelay
? choice(this.opt.delay)
: this.opt.delay[i % this.opt.delay.length]
p.attr({ d: start_path })
setTimeout(function(){
p.animate({ d: end_path }, duration)
}, delay)
x += this.pos.dx
}.bind(this))
this.horizontal.forEach(function(p, i){
var start_path, end_path
if (right) {
start_path = hpath( 0, y, 0 )
end_path = hpath( 0, y, width )
}
else {
start_path = hpath( width, y, 0 )
end_path = hpath( width, y, -width )
}
var delay
if (this.opt.shuffleDelay) {
delay = randrange.apply(null, this.opt.delay)
}
else if (this.opt.randomizeDelay) {
delay = choice(this.opt.delay)
}
else {
delay = this.opt.delay[i % this.opt.delay.length]
}
p.attr({ d: start_path })
setTimeout(function(){
p.animate({ d: end_path }, duration)
}, delay)
y += this.pos.dy
}.bind(this))
var max_delay = Math.max.apply(Math, this.opt.delay)
cb && setTimeout(cb, this.opt.duration + max_delay + 20)
}
function hpath (x, y, w) {
return "M" + x + " " + y + "h" + w
}
function vpath (x, y, w) {
return "M" + x + " " + y + "v" + w
}
return Grid
})()
|