Created
March 1, 2020 21:03
-
-
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
Author
peterhellberg
commented
Mar 1, 2020
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(1024, 1024),
gui.Decorated(false),
gui.Resizable(false),
)
if err != nil {
panic(err)
}
ticker := time.NewTicker(48 * 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" {
win.Send(gui.EventClose{})
}
case gui.EventKeyboardChar:
if event.Char == 'q' {
win.Send(gui.EventClose{})
}
case gui.EventUpdate:
win.Draw(update)
}
}
}
func update(dst draw.Image) image.Rectangle {
m := gfx.NewImage(512, 512)
t := time.Since(start).Seconds() / 3
for y := 0.0; y < 512; y++ {
yy := y / 4096
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 := 256 + 128*cos(a+i+0.25)
x2 := 256 + 128*cos(a+i)
if x1 < x2 {
for x := x1; x < x2; x++ {
v := gfx.V(x, y)
n := v.To(gfx.V(x1, y)).Len() / 256
c := gfx.PaletteEDG8.At(n)
gfx.SetVec(m, v, c)
}
gfx.SetVec(m, gfx.V(x1, y), gfx.ColorBlack)
gfx.SetVec(m, gfx.V(x2, y), gfx.ColorBlack)
}
}
}
gfx.DrawSrc(dst, m.Bounds().Add(gfx.Pt(256, 256)), m, 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)
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment