-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwheel.go
More file actions
325 lines (267 loc) · 8.05 KB
/
wheel.go
File metadata and controls
325 lines (267 loc) · 8.05 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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package main
import (
"image"
"image/color"
"image/draw"
"image/gif"
"io"
"log"
"math"
"runtime"
"sync"
"github.com/fogleman/gg"
"github.com/golang/freetype/truetype"
"golang.org/x/image/font"
"golang.org/x/image/font/gofont/goregular"
)
type Theme struct {
Transparent color.Color // #00000000
Black color.Color // #000000
White color.Color // #FFFFFF
BgPrimary color.Color // #36393F
BgSecondary color.Color // #2F3136
Outline color.Color // #202225
Blurple color.Color // #5865F2
Red color.Color // #ED4245
ArrowInactive color.Color // #B9BBBE
LightsInactive color.Color // #4F545C
Font *truetype.Font
Palette []color.Color
}
type WheelRenderer struct {
OuterRadius float64
InnerRadius float64
Options []string
Target int
FPS int
Duration int
}
func NewDefaultTheme() Theme {
theme := Theme{
Transparent: color.Transparent, // #00000000
Black: color.Black, // #000000
White: color.White, // #FFFFFF
BgPrimary: color.RGBA{54, 57, 63, 255}, // #36393F
BgSecondary: color.RGBA{47, 49, 54, 255}, // #2F3136
Outline: color.RGBA{32, 34, 37, 255}, // #202225
Blurple: color.RGBA{88, 101, 242, 255}, // #5865F2
Red: color.RGBA{237, 66, 69, 255}, // #ED4245
ArrowInactive: color.RGBA{185, 187, 190, 255}, // #B9BBBE
LightsInactive: color.RGBA{79, 84, 92, 255}, // #4F545C
}
var err error
theme.Font, err = truetype.Parse(goregular.TTF)
if err != nil {
log.Fatal(err)
}
theme.Palette = []color.Color{
theme.Transparent,
theme.Black,
theme.White,
theme.BgPrimary,
theme.BgSecondary,
theme.Outline,
theme.Blurple,
theme.Red,
theme.ArrowInactive,
theme.LightsInactive,
}
return theme
}
const (
BLINKING_START = 0.9
BLINKING_END = 1.0
)
func getTextColor(theme Theme, animation float64) color.Color {
if animation < BLINKING_START {
return theme.White
}
if animation <= BLINKING_END {
phase := (animation - BLINKING_START) / (BLINKING_END - BLINKING_START)
return interpolate(theme.White, theme.Red, phase)
}
return theme.Red
}
func getLightColor(theme Theme, angle, animation, rotation float64) color.Color {
if animation < BLINKING_START {
relative := math.Mod(angle-rotation, 2*math.Pi)
progress := relative / (2 * math.Pi)
brightness := (math.Sin(progress*2*math.Pi*3) + 1) / 2
return interpolate(theme.LightsInactive, theme.Blurple, brightness)
}
if animation >= BLINKING_START && animation <= BLINKING_END {
phase := (animation - BLINKING_START) / (BLINKING_END - BLINKING_START)
return interpolate(theme.LightsInactive, theme.Blurple, phase)
}
return theme.Red
}
func getArrowColor(theme Theme, animation float64) color.Color {
if animation < BLINKING_START {
return theme.ArrowInactive
}
if animation >= BLINKING_START && animation <= BLINKING_END {
phase := (animation - BLINKING_START) / (BLINKING_END - BLINKING_START)
return interpolate(theme.ArrowInactive, theme.Red, phase)
}
return theme.Red
}
func (wr *WheelRenderer) renderWheelBorder(theme Theme, width, height int, cx, cy float64) image.Image {
dc := gg.NewContext(width, height)
dc.NewSubPath()
dc.DrawCircle(cx, cy, wr.OuterRadius)
dc.NewSubPath()
dc.DrawCircle(cx, cy, wr.InnerRadius)
dc.SetFillRule(gg.FillRuleEvenOdd)
dc.SetColor(theme.Outline)
dc.Fill()
return dc.Image()
}
func (wr *WheelRenderer) drawWheel(theme Theme, dc *gg.Context, cx, cy, animation, rotation float64) {
// Draw wheel
angle := 2 * math.Pi / float64(len(wr.Options))
for i, label := range wr.Options {
start := rotation + angle*float64(i)
end := start + angle
dc.MoveTo(cx, cy)
dc.DrawArc(cx, cy, wr.InnerRadius, start, end)
dc.ClosePath()
if i%2 == 0 {
dc.SetColor(theme.BgPrimary)
} else {
dc.SetColor(theme.BgSecondary)
}
dc.Fill()
center := (start + end) / 2
labelX := cx + math.Cos(center)*wr.InnerRadius*0.6
labelY := cy + math.Sin(center)*wr.InnerRadius*0.6
dc.Push()
dc.Translate(labelX, labelY)
// Draw the label
dc.SetColor(theme.Black)
dc.DrawStringAnchored(label, 1, 1, 0.5, 0.5)
var fg color.Color = theme.White
if i == wr.Target {
fg = getTextColor(theme, animation)
}
dc.SetColor(fg)
dc.DrawStringAnchored(label, 0, 0, 0.5, 0.5)
dc.Pop()
}
// Draw division lines
dc.SetLineWidth(2)
dc.SetColor(theme.Outline)
for i := 0; i < len(wr.Options); i++ {
angle := rotation + angle*float64(i)
x := cx + math.Cos(angle)*wr.InnerRadius
y := cy + math.Sin(angle)*wr.InnerRadius
dc.MoveTo(cx, cy)
dc.LineTo(x, y)
dc.Stroke()
}
}
func (wr *WheelRenderer) drawCenter(theme Theme, dc *gg.Context, cx, cy float64) {
radius := wr.OuterRadius * 0.2
dc.SetColor(theme.Blurple)
dc.DrawCircle(cx, cy, radius)
dc.Fill()
dc.SetLineWidth(2)
dc.SetColor(theme.Outline)
dc.DrawCircle(cx, cy, radius)
dc.Stroke()
}
// Draws lights in a circle on the border of the wheel
func (wr *WheelRenderer) drawLights(theme Theme, dc *gg.Context, cx, cy, animation, rotation float64) {
count := len(wr.Options) * 2
radius := wr.OuterRadius * 0.015
offset := (wr.OuterRadius + wr.InnerRadius) / 2
step := 2 * math.Pi / float64(count)
for i := 0; i < count; i++ {
angle := step * float64(i)
x := cx + math.Cos(angle)*offset
y := cy + math.Sin(angle)*offset
animated := getLightColor(theme, angle, animation, rotation)
dc.SetColor(animated)
dc.DrawCircle(x, y, radius)
dc.Fill()
}
}
func (wr *WheelRenderer) drawArrow(theme Theme, dc *gg.Context, cx, cy, animation float64) {
size := wr.OuterRadius * 0.15
// Places the arrow slightly inside the border
offset := wr.InnerRadius + size*0.1
tipX := cx
tipY := cy - offset
dc.NewSubPath()
dc.MoveTo(tipX, tipY+size)
dc.LineTo(tipX-size/2, tipY)
dc.LineTo(tipX+size/2, tipY)
dc.ClosePath()
animated := getArrowColor(theme, animation)
dc.SetColor(animated)
dc.FillPreserve()
scaledLineWidth := wr.OuterRadius * 0.01
dc.SetLineWidth(scaledLineWidth)
dc.SetColor(theme.Outline)
dc.Stroke()
}
// Calculates the rotation distance needed to bring a target position to the top of a circle
func distanceToTarget(count, index int) float64 {
angle := 2 * math.Pi / float64(count)
start := angle * float64(index)
end := start + angle
center := (start + end) / 2
const top = (3 * math.Pi / 2)
return math.Mod(top-center+2*math.Pi, 2*math.Pi)
}
func (wr *WheelRenderer) RenderGIF(w io.Writer) error {
width, height := int(wr.OuterRadius*2), int(wr.OuterRadius*2)
cx, cy := float64(width)/2, float64(height)/2
spins := float64(wr.Duration) * 1.0
circles := spins * 2 * math.Pi
required := distanceToTarget(len(wr.Options), wr.Target)
destination := circles + required
delay := 100 / wr.FPS
frames := wr.FPS * wr.Duration
images := make([]*image.Paletted, frames)
delays := make([]int, frames)
var wg sync.WaitGroup
wg.Add(frames)
sem := make(chan struct{}, runtime.NumCPU())
theme := NewDefaultTheme()
border := wr.renderWheelBorder(theme, width, height, cx, cy)
// Processing frames in parallel :>
for frame := 0; frame < frames; frame++ {
sem <- struct{}{}
go func(frame int) {
defer func() { <-sem }()
defer wg.Done()
animation := float64(frame) / float64(frames-1)
eased := 1 - math.Pow(1-animation, 3)
rotation := math.Min(destination, destination*eased)
dc := gg.NewContext(width, height)
dc.SetColor(theme.Transparent)
dc.Clear()
dc.SetFontFace(truetype.NewFace(theme.Font, &truetype.Options{
Size: wr.OuterRadius * 0.06,
Hinting: font.HintingFull,
}))
wr.drawWheel(theme, dc, cx, cy, animation, rotation)
wr.drawCenter(theme, dc, cx, cy)
wr.drawArrow(theme, dc, cx, cy, animation)
dc.DrawImage(border, 0, 0)
wr.drawLights(theme, dc, cx, cy, animation, rotation)
render := dc.Image()
bounds := render.Bounds()
paletted := image.NewPaletted(bounds, theme.Palette)
draw.Draw(paletted, bounds, render, bounds.Min, draw.Src)
images[frame] = paletted
delays[frame] = delay
}(frame)
}
wg.Wait()
return gif.EncodeAll(w, &gif.GIF{
Image: images,
Delay: delays,
LoopCount: -1,
})
}