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
|
'use strict'
const canvas = document.getElementById('canvas')
class ATBBar {
constructor() {
// State
this.progress = 0
this.segmentCount = 6
this.queuedActions = []
// Drawing
this.canvas = document.createElement('canvas')
this.canvas.width = 200
this.canvas.height = 13
}
draw() {
const ctx = this.canvas.getContext('2d')
ctx.clearRect(0, 0, this.canvas.width, this.canvas.height)
ctx.save()
ctx.beginPath()
ctx.rect(0, 0, Math.round(this.canvas.width * this.progress), this.canvas.height)
ctx.clip()
ctx.fillStyle = '#77F'
ctx.fillRect(0, 0, this.canvas.width, this.canvas.height)
ctx.restore()
ctx.strokeStyle = '#000'
ctx.strokeRect(0.5, 0.5, this.canvas.width - 1, this.canvas.height - 1)
const segmentWidth = this.canvas.width / this.segmentCount
for (let i = 1; i < this.segmentCount; i++) {
const x = Math.round(segmentWidth * i) + 0.5
ctx.beginPath()
ctx.moveTo(x, 0)
ctx.lineTo(x, this.canvas.height)
ctx.stroke()
}
{
let i = 0
for (const action of this.queuedActions) {
const rectX = Math.floor(segmentWidth * i) + 2
const rectW = Math.floor(segmentWidth * (i + action.size)) - rectX - 2
const textX = Math.round(rectX + rectW / 2)
const rectH = this.canvas.height - 5
const text = action.label
const actionFilled = this.progress * this.segmentCount >= i + action.size
if (actionFilled) {
ctx.fillStyle = 'gold'
} else {
ctx.fillStyle = 'white'
}
ctx.fillRect(rectX, 2, rectW, rectH)
ctx.strokeStyle = 'black'
ctx.strokeRect(rectX + 0.5, 2.5, rectW, rectH)
ctx.textAlign = 'center'
ctx.fillStyle = 'black'
ctx.font = '5px pixel-font'
ctx.fillText(text, textX, this.canvas.height / 2 + 2.5)
i += action.size
}
}
}
update(dt) {
this.progress = Math.min(1, this.progress + dt / 6)
}
}
class ActionMenu {
constructor() {
this.canvas = document.createElement('canvas')
// State
this.options = [
['Fire', 'Fira', 'Firaga'],
['Blizz', 'Blizza', 'Blizzaga'],
['Aero', 'Aerora', 'Aeroga'],
['Zap', 'Zappa', 'Zappaga'],
['Bio', 'Biora'],
['Stun', 'Stunra', 'Stunga']
]
this.currentOptionIndex = 3
this.uiLevel = 1 // 1-3 -- which of "fire", "fira", "firaga" is selected.
// Drawing
// Button height is 9, with a margin of 1 between each plus 1 at the top and bottom.
const visibleButtonCount = 3
this.canvas.height = 9 * visibleButtonCount + 1 * (visibleButtonCount - 1) + 2
// Should be the width of a 3-segment ATB bar.
this.canvas.width = 100
}
draw() {
const ctx = this.canvas.getContext('2d')
ctx.clearRect(0, 0, this.canvas.width, this.canvas.height)
const startIndex = Math.max(0, this.currentOptionIndex - 1)
const endIndex = Math.min(this.options.length, startIndex + 3)
let y = 1
for (let i = startIndex; i < endIndex; i++) {
const maxLevel = this.options[i].length
const effectiveLevel = Math.min(maxLevel, this.uiLevel)
const text = this.options[i][effectiveLevel - 1]
const rectW = this.getRectW(effectiveLevel)
if (effectiveLevel < maxLevel) {
const ghostRectW = this.getRectW(maxLevel)
ctx.fillStyle = 'rgba(255, 255, 255, 0.3)'
ctx.fillRect(1, y, ghostRectW, 9)
ctx.strokeStyle = 'rgba(0, 0, 0, 0.3)'
ctx.strokeRect(1.5, y + 0.5, ghostRectW, 8)
}
if (i === this.currentOptionIndex) {
const mul = Math.sin(Date.now() / 200) * 0.125 + 0.875
ctx.fillStyle = `rgb(${mul * 255}, ${mul * 127}, 0)`
} else {
ctx.fillStyle = 'white'
}
ctx.fillRect(1, y, rectW, 9)
ctx.strokeStyle = 'black'
ctx.strokeRect(1.5, y + 0.5, rectW - 1, 8)
ctx.textAlign = 'center'
ctx.fillStyle = 'black'
ctx.font = '5px pixel-font'
ctx.fillText(text, 1 + Math.round(rectW / 2), y + 2 + 9 / 2)
y += 10
}
}
getRectW(level) {
return Math.floor((this.canvas.width - 2) / 3 * level)
}
downOption() {
this.currentOptionIndex++
if (this.currentOptionIndex >= this.options.length) {
this.currentOptionIndex = 0
}
}
upOption() {
this.currentOptionIndex--
if (this.currentOptionIndex <= -1) {
this.currentOptionIndex = this.options.length - 1
}
}
}
const atbBar = new ATBBar()
atbBar.queuedActions = [{label: 'Fire', size: 1}, {label: 'Fira', size: 2}, {label: 'Firaga', size: 3}]
const actionMenu = new ActionMenu()
let lastTime = Date.now()
function drawLoop() {
const dt = (Date.now() - lastTime) / 1000
lastTime = Date.now()
atbBar.update(dt)
atbBar.draw()
actionMenu.draw()
const ctx = canvas.getContext('2d')
ctx.clearRect(0, 0, canvas.width, canvas.height)
ctx.fillStyle = '#EEE'
ctx.fillRect(0, 0, canvas.width, canvas.height)
ctx.drawImage(actionMenu.canvas, 20, canvas.height - 20 - actionMenu.canvas.height)
ctx.drawImage(atbBar.canvas, 20, canvas.height - 20 - actionMenu.canvas.height - 2 - atbBar.canvas.height)
requestAnimationFrame(drawLoop)
}
canvas.addEventListener('keypress', evt => {
if (evt.keyCode === 38) {
actionMenu.upOption()
} else if (evt.keyCode === 40) {
actionMenu.downOption()
}
})
requestAnimationFrame(drawLoop)
|