This repository was archived by the owner on Apr 10, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
158 lines (137 loc) · 4.2 KB
/
Copy pathmain.js
File metadata and controls
158 lines (137 loc) · 4.2 KB
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
import { html, render } from 'https://cdn.jsdelivr.net/npm/htm@2.2.1/preact/standalone.module.js';
let canvas = document.getElementById('canvas')
let container = document.getElementById('container')
let graphics, actor, actorInstance
let lastAdvanceTime
let animations = []
async function init() {
graphics = new Flare.Graphics(canvas)
// 初始化 lib
await new Promise(resolve => graphics.initialize(() => {
resolve()
}, './lib'))
// 加载 flare
actor = await load('./Bob (Minion).flr')
// 初始化 actor
actorInstance = actor.makeInstance()
actorInstance.initialize(graphics)
// 初始化动画
if (actorInstance.animations.length) {
animations = actorInstance.animations.map(animation => ({
animation,
animationInstance: new Flare.AnimationInstance(animation._Actor, animation),
speed: 1.0,
mix: 1.0,
play: false
}))
}
lastAdvanceTime = performance.now()
advance();
}
// 运行动画
function advance() {
requestAnimationFrame(() => advance())
const now = performance.now()
const elapsed = (now - lastAdvanceTime) / 1000
lastAdvanceTime = now
// first
if (animations.length > 0) {
// animations[0].animationInstance.time += elapsed
// animations[0].animationInstance.apply(actorInstance, 1.0)
animations.forEach(({animationInstance, play, speed, mix}, idx) => {
if (play) {
animationInstance.time += elapsed * speed
animationInstance.apply(actorInstance, mix)
}
})
}
// next
if (actorInstance) {
// 运行动画
actorInstance.advance(elapsed)
}
draw()
_render()
}
// 将动画应用到 canvas
function draw() {
if (!actorInstance) return
actorInstance.draw(graphics)
graphics.flush()
}
function load(url) {
const loader = new Flare.ActorLoader()
return new Promise((resolve, reject) => {
loader.load(url, (actor) => {
if (!actor || actor.error) {
reject(!actor ? null : actor.error)
} else {
resolve(actor)
}
})
})
}
function _render() {
render(html`
<div class="columns">
${animations.map((obj) => (
html`
<div class="column col-3">
<div class="card">
<div class="card-header">
<div class="card-title">${obj.animation.name}</div>
</div>
<div class="card-body">
<form class="form-group">
<!-- 播放 -->
<label class="form-switch">
<input type="checkbox" checked="${obj.play}" onInput=${(e) => {
obj.play = e.target.checked
}} />
<i class="form-icon"></i> Play
</label>
<!-- 速度 -->
<label class="form-label" for="speed">Speed: ${obj.speed}</label>
<input
class="slider tooltip" type="range"
min="-10" max="10" step="0.01"
value="${obj.speed}" data-tooltip="${obj.speed}"
onInput=${(e) => {
if (e.target.value) {
obj.speed = e.target.value
}
}}
/>
<!-- Mix -->
<label class="form-label" for="mix">Mix: ${obj.mix}</label>
<input
class="slider tooltip" type="range"
min="-10" max="10" step="0.01"
value="${obj.mix}" data-tooltip="${obj.mix}"
onInput=${(e) => {
if (e.target.value) {
obj.mix = e.target.value
}
}}
/>
</form>
</div>
<div class="card-body">
<button class="btn" onClick=${() => {
obj.speed = 1
obj.mix = 1
obj.play = false
obj.animationInstance.time = 0
obj.animationInstance.apply(actorInstance, 1)
}}>Reset</button>
</div>
</div>
</div>
`
))}
</div>
`, container)
}
init().then(() => {
// _render()
})