-
Notifications
You must be signed in to change notification settings - Fork 14
/
030.雪碧图 哆啦A梦.html
152 lines (131 loc) · 4.28 KB
/
030.雪碧图 哆啦A梦.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="./assets/global.css">
<style>
.doraemon {
width: 25px;
height: 38px;
position: relative;
background-image: url('./assets/doraemon.png');
}
.doraemon .name {
position: absolute;
top: -14px;
left: 50%;
transform: translateX(-50%);
font-size: 12px;
text-align: center;
white-space: nowrap;
}
</style>
</head>
<body>
<div class="doraemon">
<div class="name">哆啦A梦</div>
</div>
<script>
// 写一下 doraemon 移动
// 操作元素
let doraemon = document.querySelector('.doraemon')
// 当前位置
let pos = { x: 0, y: 0 }
// 移动速度
let speed = 2;
// 当前动画
let currSprite = 'idle'
// 默认
let idleSprite = [{ x: 2, y: 0 }]
// 向下
let downSprite = [{ x: 1, y: 0 }, { x: 2, y: 0 }, { x: 3, y: 0 }]
// 向上
let upSprite = [{ x: 1, y: 1 }, { x: 2, y: 1 }, { x: 3, y: 1 }]
// 向左
let leftSprite = [{ x: 1, y: 3 }, { x: 2, y: 3 }, { x: 3, y: 3 }]
// 向右
let rightSprite = [{ x: 1, y: 2 }, { x: 2, y: 2 }, { x: 3, y: 2 }]
// 动画集
let anims = {
idle: idleSprite,
down: downSprite,
up: upSprite,
left: leftSprite,
right: rightSprite,
}
// 当前操作的位置
let backgroundPosition = ''
// 当前的位移
let transform = ''
// 初始动画
let spriteInterval = sprite()
// 延迟默认
let idleTimeout = 0
document.addEventListener('keydown', (ev) => {
let x = 0, y = 0;
if (ev.code === 'ArrowUp') y = -1;
if (ev.code === 'ArrowDown') y = 1;
if (ev.code === 'ArrowLeft') x = -1;
if (ev.code === 'ArrowRight') x = 1;
if (x || y) move(x, y)
})
function sprite() {
/** @type {{x:number,y:number}[]} */
let anim = anims[currSprite]
let animIndex = 0
let render = () => {
let curr = anim[animIndex]
let currX = Math.round(curr.x * 25)
let currY = Math.round(curr.y * 38)
backgroundPosition = `background-position: ${currX}px ${currY}px;`
doraemon.setAttribute('style', `${transform}${backgroundPosition}`)
animIndex++
if (anim.length === animIndex) animIndex = 0
}
render();
return setInterval(render, 200)
}
/**
* 元素移动
* @param {number} x
* @param {number} y
*/
function move(x, y) {
clearTimeout(idleTimeout)
idleTimeout = setTimeout(() => {
clearInterval(spriteInterval)
currSprite = 'idle';
spriteInterval = sprite()
}, 500);
x *= speed;
y *= speed;
pos.x += x;
pos.y += y;
if (x > 0 && currSprite != 'right') {
clearInterval(spriteInterval)
currSprite = 'right';
spriteInterval = sprite()
}
if (y > 0 && currSprite != 'down') {
clearInterval(spriteInterval)
currSprite = 'down';
spriteInterval = sprite()
}
if (x < 0 && currSprite != 'left') {
clearInterval(spriteInterval)
currSprite = 'left';
spriteInterval = sprite()
}
if (y < 0 && currSprite != 'up') {
clearInterval(spriteInterval)
currSprite = 'up';
spriteInterval = sprite()
}
transform = `transform: translate(${pos.x}px,${pos.y}px);`;
doraemon.setAttribute('style', `${transform}${backgroundPosition}`)
}
</script>
</body>
</html>