Skip to content

Instantly share code, notes, and snippets.

@peterhellberg
Created March 1, 2020 21:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterhellberg/76dfb78bb393a8e5661df08f9464ecc9 to your computer and use it in GitHub Desktop.
Save peterhellberg/76dfb78bb393a8e5661df08f9464ecc9 to your computer and use it in GitHub Desktop.
Twisting column using gui (and gfx) inspired by https://trasevol.dog/2018/01/24/di20/
package main
import (
"image"
"image/draw"
"time"
"github.com/peterhellberg/gfx"
"github.com/peterhellberg/gui"
)
var start = time.Now()
func main() {
gui.Run(loop)
}
func loop() {
win, err := gui.Open(
gui.Title("gui-twisting-column"),
gui.Size(512, 512),
gui.Decorated(false),
gui.Resizable(false),
)
if err != nil {
panic(err)
}
ticker := time.NewTicker(32 * time.Millisecond)
go func() {
for t := range ticker.C {
win.Send(gui.EventUpdate{t})
}
}()
for event := range win.Events() {
switch event := event.(type) {
case gui.EventClose:
ticker.Stop()
win.Close()
case gui.EventKeyboardDown:
if event.Key == "escape" {
ticker.Stop()
win.Close()
}
case gui.EventKeyboardChar:
if event.Char == 'q' {
ticker.Stop()
win.Close()
}
case gui.EventUpdate:
win.Draw(update)
}
}
}
func update(dst draw.Image) image.Rectangle {
m := gfx.NewImage(128, 128)
t := time.Since(start).Seconds() / 3
for y := 0.0; y < 127; y++ {
yy := y / 1024
a := cos(0.2*sin(t*0.1+yy*2)) + 0.5*cos(-0.2*t+yy/2)
for i := 0.0; i <= 0.75; i += 0.25 {
x1 := 64 + 32*cos(a+i+0.25)
x2 := 64 + 32*cos(a+i)
if x1 < x2 {
gfx.DrawLineBresenham(m, gfx.V(x1, y), gfx.V(x2, y), gfx.ColorWhite)
gfx.SetVec(m, gfx.V(x1, y), gfx.ColorBlack)
gfx.SetVec(m, gfx.V(x2, y), gfx.ColorBlack)
}
}
}
s := gfx.NewScaledImage(m, 4)
gfx.DrawSrc(dst, s.Bounds(), s, gfx.ZP)
return dst.Bounds()
}
const tau = gfx.Pi * 2
func cos(x float64) float64 {
return gfx.MathCos(x * tau)
}
func sin(x float64) float64 {
return gfx.MathSin(x * tau)
}
@peterhellberg
Copy link
Author

ezgif-5-b743586a3371

@peterhellberg
Copy link
Author

ezgif-5-8ccf2db33c2a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment