欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

go语言制作一个gif动态图

程序员文章站 2022-11-15 12:30:20
如题,关键不是图怎么样,而是说,go可以拿来实现动态图验证码,加上go支持cgi、fcgi,完全可以做个exe拿去增补现有的服务器么。 ball.go pack...

如题,关键不是图怎么样,而是说,go可以拿来实现动态图验证码,加上go支持cgi、fcgi,完全可以做个exe拿去增补现有的服务器么。

ball.go

package main
import (
  "github.com/hydra13142/cube"
  "github.com/hydra13142/geom"
  "github.com/hydra13142/paint"
  "image"
  "image/color/palette"
  "image/gif"
  "math"
  "os"
)
var (
  pln *cube.plain
  unx cube.vector
  uny cube.vector
)
const (
  h = 18
  w = 36
)
func init() {
  pln, _ = cube.newplain(cube.point{}, cube.vector{4, 4, 3})
  uny = cube.fromto(cube.point{}, pln.verticalpoint(cube.point{0, 0, 10})).unit()
  unx = cube.outerproduct(uny, cube.vector{4, 4, 3}).unit()
}
func main() {
  var x [h + 1][w]cube.point
  var y [h + 1][w]geom.point
  dz := math.pi / h
  dxy := math.pi * 2 / w
  for i := 0; i <= h; i++ {
    az := float64(i)*dz - math.pi/2
    r := 140 * math.cos(az)
    z := 140 * math.sin(az)
    for j := 0; j < w; j++ {
      axy := float64(j) * dxy
      x[i][j] = cube.point{math.cos(axy) * r, math.sin(axy) * r, z}
    }
  }
  pics := make([]*image.paletted, 0, 20)
  img := paint.image{
    fr: paint.green,
    bg: paint.white,
  }
  stp := dxy / 20
  delay := make([]int, 0, 20)
  for t := 0; t < 20; t++ {
    img.image = image.newpaletted(image.rect(0, 0, 300, 300), palette.plan9)
    for i := 0; i <= h; i++ {
      for j := 0; j < w; j++ {
        ox := cube.fromto(cube.point{}, x[i][j])
        y[i][j] = geom.point{cube.innerproduct(ox, unx), cube.innerproduct(ox, uny)}
        a, b := x[i][j].x, x[i][j].y
        x[i][j].x = a*math.cos(stp) - b*math.sin(stp)
        x[i][j].y = b*math.cos(stp) + a*math.sin(stp)
      }
    }
    for i := 0; i < h; i++ {
      for j := 0; j < w; j++ {
        img.line(
          150+int(y[i][j].x),
          150-int(y[i][j].y),
          150+int(y[i][(j+1)%w].x),
          150-int(y[i][(j+1)%w].y),
        )
        img.line(
          150+int(y[i][j].x),
          150-int(y[i][j].y),
          150+int(y[i+1][j].x),
          150-int(y[i+1][j].y),
        )
      }
    }
    pics = append(pics, img.image.(*image.paletted))
    delay = append(delay, 5)
  }
  file, _ := os.create("ball.gif")
  defer file.close()
  gif.encodeall(file, &gif.gif{
    image:   pics,
    delay:   delay,
    loopcount: 5 * len(delay),
  })
}

woniu.go

package main
import (
  "github.com/hydra13142/cube"
  "github.com/hydra13142/geom"
  "github.com/hydra13142/paint"
  "image"
  "image/color/palette"
  "image/gif"
  "math"
  "os"
)
var (
  pln   *cube.plain
  unx, uny cube.vector
)
const (
  h = 18
  w = 96
)
func init() {
  pln, _ = cube.newplain(cube.point{}, cube.vector{2, 2, 1})
  uny = cube.fromto(cube.point{}, pln.verticalpoint(cube.point{0, 0, 10})).unit()
  unx = cube.outerproduct(uny, cube.vector{2, 2, 1}).unit()
}
func main() {
  var x [h + 1][w]cube.point
  var y [h + 1][w]geom.point
  dz := math.pi / h
  dxy := math.pi * 4 / w
  for i := 0; i <= h; i++ {
    az := float64(i)*dz - math.pi/2
    r := 300 * math.cos(az)
    z := 100 * math.sin(az)
    for j := 0; j < w; j++ {
      axy := float64(j) * dxy
      r := float64(j) * r / w
      x[i][j] = cube.point{math.cos(axy) * r, math.sin(axy) * r, z}
    }
  }
  pics := make([]*image.paletted, 0, 20)
  img := paint.image{
    fr: paint.green,
    bg: paint.white,
  }
  stp := math.pi / w
  delay := make([]int, 0, 2*w)
  for t := 0; t < 2*w; t++ {
    img.image = image.newpaletted(image.rect(0, 0, 600, 300), palette.plan9)
    for i := 0; i <= h; i++ {
      for j := 0; j < w; j++ {
        ox := cube.fromto(cube.point{}, x[i][j])
        y[i][j] = geom.point{cube.innerproduct(ox, unx), cube.innerproduct(ox, uny)}
        a, b := x[i][j].x, x[i][j].y
        x[i][j].x = a*math.cos(stp) - b*math.sin(stp)
        x[i][j].y = b*math.cos(stp) + a*math.sin(stp)
      }
    }
    img.line(
      300+int(y[0][0].x),
      150-int(y[0][0].y),
      300+int(y[h][0].x),
      150-int(y[h][0].y),
    )
    for i := 0; i < h; i++ {
      for j := 1; j < w; j++ {
        img.line(
          300+int(y[i][j].x),
          150-int(y[i][j].y),
          300+int(y[i][j-1].x),
          150-int(y[i][j-1].y),
        )
        img.line(
          300+int(y[i][j].x),
          150-int(y[i][j].y),
          300+int(y[i+1][j].x),
          150-int(y[i+1][j].y),
        )
      }
    }
    pics = append(pics, img.image.(*image.paletted))
    delay = append(delay, 5)
  }
  file, _ := os.create("woniu.gif")
  defer file.close()
  gif.encodeall(file, &gif.gif{
    image:   pics,
    delay:   delay,
    loopcount: 5 * len(delay),
  })
}

rotate.go

package main
import (
  "github.com/hydra13142/cube"
  "github.com/hydra13142/geom"
  "github.com/hydra13142/paint"
  "image"
  "image/color/palette"
  "image/gif"
  "math"
  "os"
)
var (
  pln   *cube.plain
  unx, uny cube.vector
)
const (
  h = 9
  w = 36
)
func init() {
  pln, _ = cube.newplain(cube.point{}, cube.vector{4, 4, 3})
  uny = cube.fromto(cube.point{}, pln.verticalpoint(cube.point{0, 0, 10})).unit()
  unx = cube.outerproduct(uny, cube.vector{4, 4, 3}).unit()
}
func main() {
  var x [h*w + 1]cube.point
  var y [h*w + 1]geom.point
  dxy := (math.pi * 2) / w
  dz := math.pi / h
  for i := 0; i <= h*w; i++ {
    az := float64(i)*dz/w - math.pi/2
    r := 140 * math.cos(az)
    z := 140 * math.sin(az) 
    axy := float64(i) * dxy
    x[i] = cube.point{math.cos(axy) * r, math.sin(axy) * r, z}
  }
  pics := make([]*image.paletted, 0, 20)
  img := paint.image{
    fr: paint.green,
    bg: paint.white,
  }
  stp := math.pi * 2 / (w * 3)
  delay := make([]int, 0, 3*w)
  for t := 0; t < 3*w; t++ {
    img.image = image.newpaletted(image.rect(0, 0, 300, 300), palette.plan9)
    for i := 0; i <= h*w; i++ {
      ox := cube.fromto(cube.point{}, x[i])
      y[i] = geom.point{cube.innerproduct(ox, unx), cube.innerproduct(ox, uny)}
      a, b := x[i].x, x[i].z
      x[i].x = a*math.cos(stp) - b*math.sin(stp)
      x[i].z = b*math.cos(stp) + a*math.sin(stp)
    }
    img.line(
      150+int(y[0].x),
      150-int(y[0].y),
      150+int(y[h*w].x),
      150-int(y[h*w].y),
    )
    for i := 0; i < h*w; i++ {
      img.line(
        150+int(y[i].x),
        150-int(y[i].y),
        150+int(y[i+1].x),
        150-int(y[i+1].y),
      )
    }
    pics = append(pics, img.image.(*image.paletted))
    delay = append(delay, 8)
  }
  file, _ := os.create("rotate.gif")
  defer file.close()
  gif.encodeall(file, &gif.gif{
    image:   pics,
    delay:   delay,
    loopcount: 5 * len(delay),
  })
}

以上就是本文的全部内容了,希望大家能够喜欢。