examples/perspective: Refactoring

This commit is contained in:
Hajime Hoshi 2018-01-30 01:54:00 +09:00
parent c4faf5b12c
commit 531e661f0b

View File

@ -38,17 +38,25 @@ func update(screen *ebiten.Image) error {
if ebiten.IsRunningSlowly() { if ebiten.IsRunningSlowly() {
return nil return nil
} }
// Split the image into horizontal lines and draw them with different scales.
op := &ebiten.DrawImageOptions{} op := &ebiten.DrawImageOptions{}
w, h := gophersImage.Size() w, h := gophersImage.Size()
for i := 0; i < h; i++ { for i := 0; i < h; i++ {
op.GeoM.Reset() op.GeoM.Reset()
width := w + i*3/4
x := ((h - i) * 3 / 4) / 2 // Move the image's center to the upper-left corner.
op.GeoM.Scale(float64(width)/float64(w), 1) op.GeoM.Translate(-float64(w)/2, -float64(h)/2)
op.GeoM.Translate(float64(x), float64(i))
maxWidth := float64(w) + float64(h)*3/4 // Scale each lines and adjust the position.
op.GeoM.Translate(-maxWidth/2, -float64(h)/2) lineW := w + i*3/4
x := -float64(lineW) / float64(w) / 2
op.GeoM.Scale(float64(lineW)/float64(w), 1)
op.GeoM.Translate(x, float64(i))
// Move the image's center to the screen's center.
op.GeoM.Translate(screenWidth/2, screenHeight/2) op.GeoM.Translate(screenWidth/2, screenHeight/2)
r := image.Rect(0, i, w, i+1) r := image.Rect(0, i, w, i+1)
op.SourceRect = &r op.SourceRect = &r
screen.DrawImage(gophersImage, op) screen.DrawImage(gophersImage, op)