Created
May 9, 2022 08:10
-
-
Save tenox7/c5a814356fa8992ad4808c3451781b5b to your computer and use it in GitHub Desktop.
golang pixel lib clock
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 ( | |
"math" | |
"time" | |
"github.com/faiface/pixel" | |
"github.com/faiface/pixel/imdraw" | |
"github.com/faiface/pixel/pixelgl" | |
"golang.org/x/image/colornames" | |
) | |
const ( | |
m = (math.Pi / 180.0) * 6.0 | |
) | |
func drawClock(win *pixelgl.Window) { | |
t := time.Now().Local() | |
w := win.Bounds().W() | |
h := win.Bounds().H() | |
cX := win.Bounds().Center().X | |
cY := win.Bounds().Center().Y | |
l := (math.Min(w, h) / 2) - 10 | |
i := imdraw.New(nil) | |
// dial | |
i.Color = colornames.Darkgray | |
i.Push(pixel.V(cX, cY)) | |
i.Circle(l, 5) | |
// seconds | |
r := (float64(t.Second()) - 15.0) * m | |
x := math.Cos(r)*(8*l/9) + cX | |
y := h - (math.Sin(r)*(8*l/9) + cY) | |
i.Color = colornames.Red | |
i.Push(pixel.V(cX, cY), pixel.V(x, y)) | |
i.Line(2) | |
// minutes | |
r = (float64(t.Minute()) - 15.0) * m | |
x = math.Cos(r)*(4*l/5) + cX | |
y = h - (math.Sin(r)*(4*l/5) + cY) | |
i.Color = colornames.Navy | |
i.Push(pixel.V(cX, cY), pixel.V(x, y)) | |
i.Line(5) | |
// hours | |
r = (float64((t.Hour()*5)+t.Minute()/10) - 15.0) * m | |
x = math.Cos(r)*(2*l/3) + cX | |
y = h - (math.Sin(r)*(2*l/3) + cY) | |
i.Color = colornames.Darkgreen | |
i.Push(pixel.V(cX, cY), pixel.V(x, y)) | |
i.Line(10) | |
i.Draw(win) | |
} | |
func run() { | |
cfg := pixelgl.WindowConfig{ | |
Title: "Go Pixel Clock", | |
Bounds: pixel.R(0, 0, 1024, 768), | |
Resizable: true, | |
} | |
win, err := pixelgl.NewWindow(cfg) | |
if err != nil { | |
panic(err) | |
} | |
t := time.Tick(time.Second / 4) | |
for !win.Closed() { | |
win.Clear(colornames.White) | |
drawClock(win) | |
win.Update() | |
<-t | |
} | |
} | |
func main() { | |
pixelgl.Run(run) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment