Skip to content

Commit

Permalink
custom shakes
Browse files Browse the repository at this point in the history
  • Loading branch information
sgreben committed Oct 17, 2018
1 parent 7fffe92 commit 237c80b
Showing 1 changed file with 28 additions and 12 deletions.
40 changes: 28 additions & 12 deletions cmd/gif/shake.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"github.com/sgreben/yeetgif/pkg/piecewiselinear"
"image"
"math"
"math/rand"
Expand All @@ -13,37 +14,52 @@ import (
func CommandShake(cmd *cli.Cmd) {
cmd.Spec = "[OPTIONS]"
var (
random = gifcmd.Float{Value: 0.5}
f = gifcmd.Float{Value: 1.0}
a = gifcmd.Float{Value: 8.0}
random = gifcmd.Float{Value: 0.75}
f = gifcmd.FloatsCSV{Values: []float64{1}}
a = gifcmd.FloatsCSV{Values: []float64{7}}
)
cmd.VarOpt("f frequency", &f, "")
cmd.VarOpt("a amplitude", &a, "")
cmd.VarOpt("r random", &random, "🌀")
cmd.Action = func() {
Shake(images, random.Value, f.Value, a.Value)
amplitudeF := piecewiselinear.Function{}
k := float64(len(a.Values) - 1)
for i := 0; i < len(a.Values); i++ {
amplitudeF.X = append(amplitudeF.X, float64(i)/k)
amplitudeF.Y = append(amplitudeF.Y, float64((a.Values)[i]))
}
frequencyF := piecewiselinear.Function{}
k = float64(len(f.Values) - 1)
for i := 0; i < len(f.Values); i++ {
frequencyF.X = append(frequencyF.X, float64(i)/k)
frequencyF.Y = append(frequencyF.Y, float64((f.Values)[i]))
}
Shake(images, random.Value, frequencyF.At, amplitudeF.At)
}
}

// Shake `images`
func Shake(images []image.Image, random, frequency, amplitude float64) {
n := len(images)
func Shake(images []image.Image, random float64, frequency , amplitude func(float64)float64) {
n := float64(len(images))
phaseY := math.Pi / 2
move := func(i int) {
rX, rY := rand.Float64(), rand.Float64()
t := float64(i)/n
a := amplitude(t)
f := frequency(t)
offset := image.Point{
X: int(amplitude * math.Sin(2*math.Pi*frequency*float64(i)/float64(n)+(rX*2*math.Pi))),
Y: int(amplitude * math.Sin(2*math.Pi*frequency*float64(i)/float64(n)+phaseY+(rY*2*math.Pi))),
X: int(a * math.Sin(2*math.Pi*f*t+(rX*2*math.Pi))),
Y: int(a * math.Sin(2*math.Pi*f*t+phaseY+(rY*2*math.Pi))),
}
if !config.Pad {
images[i] = imaging.Paste(image.NewNRGBA(images[i].Bounds()), images[i], offset)
return
}
bounds := images[i].Bounds()
bounds.Min.X -= int(amplitude)
bounds.Max.X += int(amplitude)
bounds.Min.Y -= int(amplitude)
bounds.Max.Y += int(amplitude)
bounds.Min.X -= int(a)
bounds.Max.X += int(a)
bounds.Min.Y -= int(a)
bounds.Max.Y += int(a)
images[i] = imaging.Paste(image.NewNRGBA(bounds), images[i], offset)
}
parallel(len(images), move)
Expand Down

0 comments on commit 237c80b

Please sign in to comment.