Skip to content

Instantly share code, notes, and snippets.

@tinne26
Created November 18, 2022 23:09
Show Gist options
  • Save tinne26/057933b58728ef9fc57a938156314ca7 to your computer and use it in GitHub Desktop.
Save tinne26/057933b58728ef9fc57a938156314ca7 to your computer and use it in GitHub Desktop.
[ebiten/discussions/2461] fade normal
package main
import ( "time" ; "image" ; "image/color" )
import "github.com/hajimehoshi/ebiten/v2"
// Approach 2: use a small image and then scale its size too.
func init() { ebiten.SetWindowResizingMode(ebiten.WindowResizingModeEnabled) }
func main() { ebiten.RunGame(&Game{}) }
type Game struct { ticks int ; fade *ebiten.Image }
func (g *Game) Layout(w, h int) (int, int) { return w, h }
func (g *Game) Update() error { g.ticks++ ; return nil }
func (g *Game) Draw(screen *ebiten.Image) {
// base content or pattern
screen.Fill(color.White)
// init fade image if uninitialized (could also be done on init)
if g.fade == nil {
img := ebiten.NewImage(3, 3)
img.Fill(color.Black)
g.fade = img.SubImage(image.Rect(1, 1, 2, 2)).(*ebiten.Image)
}
// apply fading
fadingDuration := 5000*time.Millisecond // you may change this to test
opts := &ebiten.DrawImageOptions{}
opacity := float64(time.Second)*(float64(g.ticks)/float64(ebiten.TPS()))/float64(fadingDuration)
if opacity > 1.0 { opacity = 1.0 }
bounds := screen.Bounds()
opts.GeoM.Scale(float64(bounds.Dx()), float64(bounds.Dy()))
opts.ColorM.Translate(0, 0, 0, opacity - 1.0)
screen.DrawImage(g.fade, opts)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment