先看效果
spider
源码原地址:
CodePen:Parametric Spider
稍微修改了一下,去掉了设置和署名,分享一下代码


HTML里要引入https://unpkg.co/gsap@3/dist/gsap.min.jshttps://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.7.9/dat.gui.min.js
然后再放上

1
<canvas id="spidercanvas" />

即可
如果想要一个按钮可以打开或关闭它的话,可以加上

1
<button onclick="spider()" id="spiderbutton">蜘蛛,启动!</button>

JS文件:

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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*--------------------
Vars
--------------------*/
const canvas = document.querySelector("#spidercanvas");
const ctx = canvas.getContext("2d");
const button = document.getElementById("spiderbutton");
const win = {
w: window.innerWidth,
h: window.innerHeight
};
const mouse = {
x: win.w * 0.5,
y: win.h * 0.5,
lerpX: win.w * 0.5,
lerpY: win.h * 0.5,
stepX: 0,
stepY: 0,
oldStepX: 0,
oldStepY: 0,
angle: 0
};
const opts = {
cellSize: 40,
pawRadius: 94,
pawHeight: 200,
pawRandomStep: 0.2,
bodyHeight: 20,
spiderOffsetX: 30,
spiderOffsetY: 40,
shadowY: 0.3,
speed: 0.12,
stop: false,
jump: 0
};
let cols = 0;
let rows = 0;
canvas.style.display = "block";
/*--------------------
button
--------------------*/
function spider() {
if (canvas.style.display == "none") {
canvas.style.display = "block";
} else {
canvas.style.display = "none";
}
}
/*--------------------
Lerp
--------------------*/
const lerp = (a, b, c) => {
return (1 - c) * a + c * b;
};

/*--------------------
Random
--------------------*/
const random = (start, range) => {
return start - range + Math.random() * range * 2;
};

/*--------------------
Resize
--------------------*/
const handleResize = () => {
win.w = window.innerWidth;
win.h = window.innerHeight;
canvas.width = win.w;
canvas.height = win.h;
cols = Math.round(win.w / opts.cellSize) + 1;
rows = Math.round(win.h / opts.cellSize) + 1;
};
handleResize();
window.addEventListener("resize", handleResize);

/*--------------------
Paws
--------------------*/
const Paws = [
{ x: win.w * 0.5, y: win.h * 0.5 },
{ x: win.w * 0.5, y: win.h * 0.5 },
{ x: win.w * 0.5, y: win.h * 0.5 },
{ x: win.w * 0.5, y: win.h * 0.5 },
{ x: win.w * 0.5, y: win.h * 0.5 },
{ x: win.w * 0.5, y: win.h * 0.5 },
{ x: win.w * 0.5, y: win.h * 0.5 },
{ x: win.w * 0.5, y: win.h * 0.5 }
];

/*--------------------
Clear
--------------------*/
const clear = () => {
ctx.clearRect(0, 0, win.w, win.h);
};

/*--------------------
Circle
--------------------*/
const circle = (x, y, r, c) => {
ctx.fillStyle = c;
ctx.beginPath();
ctx.ellipse(x, y, r, r, 0, 0, Math.PI * 2);
ctx.fill();
ctx.closePath();
};

/*--------------------
Rect
--------------------*/
const rect = (x, y, r, c) => {
ctx.fillStyle = c;
ctx.fillRect(x, y, r, r);
};

/*--------------------
Walk
--------------------*/
const walk = () => {
let ind = 0;
Array(10)
.fill()
.forEach((a, i) => {
const theta = i / 10;
if (i % 5 === 0) return;
const x = random(
mouse.x + cols * 0.5 + opts.pawRadius * Math.sin(theta * Math.PI * 2),
opts.pawRadius * opts.pawRandomStep
);
const y = random(
mouse.y + rows * 0.5 + opts.pawRadius * Math.cos(theta * Math.PI * 2),
opts.pawRadius * opts.pawRandomStep
);

gsap.to(Paws[ind++], {
x,
y,
duration: opts.speed,
ease: "power3.out()",
delay: ((i + 2) % 4) * 0.1
});
});
};

/*--------------------
Check Step
--------------------*/
const checkStep = () => {
mouse.stepX = Math.round((mouse.x / win.w) * (cols - 1)) * opts.cellSize;
mouse.stepY = Math.round((mouse.y / win.h) * (rows - 1)) * opts.cellSize;

if (mouse.oldStepX !== mouse.stepX || mouse.oldStepY !== mouse.stepY) {
walk();
}

mouse.oldStepX = mouse.stepX;
mouse.oldStepY = mouse.stepY;
// rect(mouse.stepX, mouse.stepY, opts.cellSize, 'rgba(255, 0, 0, .1)')
};

/*--------------------
Draw Spider
--------------------*/
const drawSpider = () => {
const x = mouse.lerpX - opts.spiderOffsetX;
const y = mouse.lerpY - opts.spiderOffsetY;

// Shadows
Paws.forEach((p, i) => {
ctx.strokeStyle = "#eee";
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(x, y + opts.bodyHeight);
const x2 = lerp(x, p.x, 0.5);
const y2 = lerp(y + opts.jump, p.y, opts.shadowY);
ctx.quadraticCurveTo(x2, y2, p.x, p.y);
ctx.stroke();
ctx.closePath();
});

// Paws
Paws.forEach((p, i) => {
ctx.strokeStyle = "black";
ctx.lineWidth = 1;

ctx.beginPath();
ctx.moveTo(x, y + opts.jump);
const x1 = lerp(x, p.x, 0.5);
const y1 = lerp(y + opts.jump, p.y - opts.pawHeight, 0.5);
ctx.quadraticCurveTo(x1, y1, p.x, p.y);
ctx.stroke();
ctx.closePath();
});

// Head
};

/*--------------------
Draw Mouse
--------------------*/
const drawMouse = () => {
mouse.lerpX = lerp(mouse.lerpX, mouse.x, 0.1);
mouse.lerpY = lerp(mouse.lerpY, mouse.y, 0.1);

if (opts.stop) {
mouse.lerpX = win.w * 0.5;
mouse.lerpY = win.h * 0.5;
mouse.x = win.w * 0.5;
mouse.y = win.h * 0.5;
}
checkStep();
};

/*--------------------
Draw
--------------------*/
const draw = (t) => {
clear();
drawMouse();
drawSpider();
requestAnimationFrame(draw);
};
draw();

/*--------------------
Mouse
--------------------*/
const handleMouseMove = (e) => {
if (opts.stop) {
mouse.lerpX = win.w * 0.5;
mouse.lerpY = win.h * 0.5;
mouse.x = win.w * 0.5;
mouse.y = win.h * 0.5;
} else {
mouse.x = e.clientX;
mouse.y = e.clientY;
}
};
const handleTouchMove = (e) => {
mouse.x = e.touches[0].clientX;
mouse.y = e.touches[0].clientY;
};
const handleMouseDown = () => {
if (opts.stop) return;
gsap.killTweensOf(opts);
gsap.to(opts, {
jump: opts.bodyHeight,
duration: 0.5,
ease: "power3.out"
});
};
const handleMouseUp = () => {
if (opts.stop) return;
gsap.killTweensOf(opts);
gsap.to(opts, {
jump: 0,
duration: 2,
ease: "elastic(1.4, 0.1)"
});
};
window.addEventListener("mousemove", handleMouseMove);
window.addEventListener("touchstart", handleTouchMove);
window.addEventListener("touchmove", handleTouchMove);
window.addEventListener("mousedown", handleMouseDown);
window.addEventListener("mouseup", handleMouseUp);

CSS文件:

1
2
3
4
5
6
7
8
#spidercanvas {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: -1; #把它放在背景前,其他元素后,放在其他元素前也行
}