Skip to content

Instantly share code, notes, and snippets.

@hajimehoshi
Created April 28, 2017 18:52
Show Gist options
  • Save hajimehoshi/0aa1311d22b21f2db0dd5aad56f9f66a to your computer and use it in GitHub Desktop.
Save hajimehoshi/0aa1311d22b21f2db0dd5aad56f9f66a to your computer and use it in GitHub Desktop.
package main
import (
"image"
"log"
"math"
"time"
"github.com/hajimehoshi/ebiten"
"github.com/peterhellberg/plasma"
"github.com/peterhellberg/plasma/palette"
)
const (
w, h, size = 640, 480, 128
fw, fh, fsize = float64(w), float64(h), float64(size)
)
var (
start = time.Now()
texture = plasmaImage(size, size, 1)
distanceTable = [h * 2][w * 2]int{}
angleTable = [h * 2][w * 2]int{}
ratio = 32.0
)
func initialize() {
go func() {
s := 1
for range time.Tick(32 * time.Millisecond) {
s++
texture = plasmaImage(size, size, s)
}
}()
for y := 0; y < h*2; y++ {
for x := 0; x < w*2; x++ {
fx, fy := float64(x), float64(y)
distance := int(ratio*size/math.Sqrt((fx-fw)*(fx-fw)+(fy-fh)*(fy-fh))) % size
angle := int(0.5 * size * math.Atan2(fy-fh, fx-fw) / math.Pi)
distanceTable[y][x] = distance
angleTable[y][x] = angle
}
}
}
func update(screen *ebiten.Image) error {
animation := time.Since(start).Seconds()
shiftX := int(fsize * 0.2 * animation)
shiftY := int(fsize * 0.05 * animation)
shiftLookX := int(fw/2 + float64(int(fw/2*math.Sin(animation))))
shiftLookY := int(fh/2 + float64(int(fh/2*math.Sin(animation*1.6))))
buffer := image.NewRGBA(image.Rect(0, 0, w, h))
for y := 0; y < h; y++ {
for x := 0; x < w; x++ {
buffer.Set(x, y, texture.At(
int(uint(distanceTable[y+shiftLookY][x+shiftLookX]+shiftX)%size),
int(uint(angleTable[y+shiftLookY][x+shiftLookX]+shiftY)%size),
))
}
}
screen.ReplacePixels(buffer.Pix)
return nil
}
func plasmaImage(w, h, s int) image.Image {
return plasma.New(w, h, 4).Image(w, h, s, palette.DefaultGradient)
}
func main() {
initialize()
if err := ebiten.Run(update, w, h, 1, "Plasma (Ebiten)"); err != nil {
log.Fatal(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment