Created
February 13, 2018 19:53
-
-
Save peterhellberg/14de62e40e8c29fed2e78cbef87b6aee to your computer and use it in GitHub Desktop.
Random squares effect rendered by Pixel. Inspiration: https://youtu.be/IGOD2eN21qI?t=3m59s
This file contains hidden or 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/color" | |
"math/rand" | |
"time" | |
"github.com/faiface/pixel" | |
"github.com/faiface/pixel/imdraw" | |
"github.com/faiface/pixel/pixelgl" | |
) | |
const ( | |
s, n, b = 32, 16, 6 | |
delay = 96 * time.Millisecond | |
) | |
func run() { | |
win, err := pixelgl.NewWindow(pixelgl.WindowConfig{ | |
Bounds: pixel.R(0, 0, float64(s*n), float64(s*n)), | |
VSync: true, | |
Undecorated: true, | |
}) | |
if err != nil { | |
panic(err) | |
} | |
ticker := time.NewTicker(delay) | |
for !win.Closed() { | |
win.SetClosed(win.JustPressed(pixelgl.KeyEscape) || win.JustPressed(pixelgl.KeyQ)) | |
select { | |
case <-ticker.C: | |
imd := imdraw.New(nil) | |
for i := 0; i < n; i++ { | |
for j := 0; j < n; j++ { | |
x, y := float64(i*s), float64(j*s) | |
imd.Color = gb.Random() | |
imd.Push(pixel.V(x, y), pixel.V(x+float64(s), y+float64(s))) | |
imd.Rectangle(0) | |
imd.Color = gb.Random() | |
imd.Push(pixel.V(x+b, y+b), pixel.V(x+float64(s)-b, y+float64(s)-b)) | |
imd.Rectangle(0) | |
} | |
} | |
imd.Draw(win) | |
default: | |
} | |
win.Update() | |
} | |
} | |
func main() { | |
pixelgl.Run(run) | |
} | |
type Palette []color.NRGBA | |
func (p Palette) Random() color.NRGBA { | |
return p[rand.Intn(len(p))] | |
} | |
// Flat UI Colors 2 - British Palette by Jan Losert | |
// https://flatuicolors.com/palette/gb | |
var gb = Palette{ | |
{0, 168, 255, 255}, | |
{0, 151, 230, 255}, | |
{156, 136, 255, 255}, | |
{140, 122, 230, 255}, | |
{251, 197, 49, 255}, | |
{225, 177, 44, 255}, | |
{76, 209, 55, 255}, | |
{68, 189, 50, 255}, | |
{72, 126, 176, 255}, | |
{64, 115, 158, 255}, | |
{232, 65, 24, 255}, | |
{194, 54, 22, 255}, | |
{245, 246, 250, 255}, | |
{220, 221, 225, 255}, | |
{127, 143, 166, 255}, | |
{113, 128, 147, 255}, | |
{39, 60, 117, 255}, | |
{25, 42, 86, 255}, | |
{53, 59, 72, 255}, | |
{47, 54, 64, 255}, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.