-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
133 lines (132 loc) · 3.96 KB
/
main.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
!(function() {
class DogAnimation {
constructor(canvas) {
this.canvas = canvas
canvas.width = window.innerWidth
window.onresize = () => (canvas.width = window.innerWidth)
canvas.height = 200
// 记录上一帧的时间
this.lastWalkingTime = Date.now()
// 当前画的图片索引
this.keyFrameIndex = -1
this.ctx = this.canvas.getContext('2d')
// 图片目录
this.RES_PATH = './images'
this.IMG_COUNT = 8
this.dog = {
// 一步10px
stepDistance: 9,
// 狗的速度
speed: 0.15,
// 鼠标的x坐标
mouseX: -1,
// 往前走停留的位置
frontStopX: -1,
// 往回走停留的位置,
backStopX: window.innerWidth
}
}
async start() {
await this.loadResources()
this.pictureWidth = this.dogPictures[0].naturalWidth / 2
// 小狗初始化的位置放在最右边
this.dog.mouseX = window.innerWidth - this.pictureWidth
this.recordMousePosition()
window.requestAnimationFrame(this.walk.bind(this))
}
// 记录鼠标位置
recordMousePosition() {
window.addEventListener('mousemove', event => {
this.dog.frontStopX = event.clientX - this.pictureWidth
this.dog.backStopX = event.clientX
})
window.addEventListener('touchstart', event => {
this.dog.frontStopX = event.touches[0].clientX - this.pictureWidth
this.dog.backStopX = event.touches[0].clientX
})
}
// 加载图片
loadResources() {
let imagesPath = []
for (let i = 0; i <= this.IMG_COUNT; i++) {
imagesPath.push(`${this.RES_PATH}/${i}.png`)
}
let works = []
imagesPath.forEach(imgPath => {
works.push(
new Promise(resolve => {
let img = new Image()
img.onload = () => resolve(img)
img.src = imgPath
})
)
})
return new Promise(resolve => {
Promise.all(works).then(dogPictures => {
this.dogPictures = dogPictures
resolve()
})
})
}
walk() {
let now = Date.now()
let diffDistance = (now - this.lastWalkingTime) * this.dog.speed
if (diffDistance < this.dog.stepDistance) {
window.requestAnimationFrame(this.walk.bind(this))
return
}
this.keyFrameIndex = ++this.keyFrameIndex % this.IMG_COUNT
let direct = -1,
stopWalking = false
// 如果鼠标在狗的前面则往前走
if (this.dog.frontStopX > this.dog.mouseX) {
direct = 1
} else if (this.dog.backStopX < this.dog.mouseX) {
// 如果鼠标在狗的后面则往回走
direct = -1
} else {
// 如果鼠标在狗在位置
stopWalking = true
// 初始化的时候小狗是反方向的,frontStopX为初始值-1
// 说明鼠标还没动过
direct =
this.dog.frontStopX === -1
? -1
: this.dog.backStopX - this.dog.mouseX > this.pictureWidth / 2
? 1
: -1
this.keyFrameIndex = -1
//this.dog.mouseX = this.dog.stopX;
}
let ctx = this.ctx
ctx.clearRect(0, 0, this.canvas.width, this.canvas.height)
ctx.save()
if (!stopWalking) {
this.dog.mouseX += this.dog.stepDistance * direct
}
if (direct === -1) {
ctx.scale(direct, 1)
}
let img = this.dogPictures[this.keyFrameIndex + 1]
let drawX = 0
drawX = this.dog.mouseX * direct - (direct === -1 ? this.pictureWidth : 0)
ctx.drawImage(
img,
0,
0,
img.naturalWidth,
img.naturalHeight,
drawX,
20,
186,
162
)
ctx.restore()
this.lastWalkingTime = now
window.requestAnimationFrame(this.walk.bind(this))
}
}
let canvas = document.querySelector('#dog-walking')
let dogAnimation = new DogAnimation(canvas)
dogAnimation.start()
})()