首页 > 编程 > Golang > 正文

go语言制作一个gif动态图

2020-04-01 19:15:05
字体:大 中 小
来源:转载
供稿:网友

这篇文章主要介绍了go制作一个gif动态图的相关资料,需要的朋友可以参考下

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

ball.go

 

  1. package main 
  2. import ( 
  3. "github.com/hydra13142/cube" 
  4. "github.com/hydra13142/geom" 
  5. "github.com/hydra13142/paint" 
  6. "image" 
  7. "image/color/palette" 
  8. "image/gif" 
  9. "math" 
  10. "os" 
  11. ) 
  12. var ( 
  13. pln *cube.Plain 
  14. unx cube.Vector 
  15. uny cube.Vector 
  16. ) 
  17. const ( 
  18. H = 18 
  19. W = 36 
  20. ) 
  21. func init() { 
  22. pln, _ = cube.NewPlain(cube.Point{}, cube.Vector{4, 4, 3}) 
  23. uny = cube.FromTo(cube.Point{}, pln.VerticalPoint(cube.Point{0, 0, 10})).Unit() 
  24. unx = cube.OuterProduct(uny, cube.Vector{4, 4, 3}).Unit() 
  25. } 
  26. func main() { 
  27. var x [H + 1][W]cube.Point 
  28. var y [H + 1][W]geom.Point 
  29. dz := math.Pi / H 
  30. dxy := math.Pi * 2 / W 
  31. for i := 0; i <= H; i++ { 
  32. az := float64(i)*dz - math.Pi/2 
  33. r := 140 * math.Cos(az) 
  34. z := 140 * math.Sin(az) 
  35. for j := 0; j < W; j++ { 
  36. axy := float64(j) * dxy 
  37. x[i][j] = cube.Point{math.Cos(axy) * r, math.Sin(axy) * r, z} 
  38. } 
  39. } 
  40. pics := make([]*image.Paletted, 0, 20) 
  41. img := paint.Image{ 
  42. FR: paint.Green, 
  43. BG: paint.White, 
  44. } 
  45. stp := dxy / 20 
  46. delay := make([]int, 0, 20) 
  47. for t := 0; t < 20; t++ { 
  48. img.Image = image.NewPaletted(image.Rect(0, 0, 300, 300), palette.Plan9) 
  49. for i := 0; i <= H; i++ { 
  50. for j := 0; j < W; j++ { 
  51. ox := cube.FromTo(cube.Point{}, x[i][j]) 
  52. y[i][j] = geom.Point{cube.InnerProduct(ox, unx), cube.InnerProduct(ox, uny)} 
  53. a, b := x[i][j].X, x[i][j].Y 
  54. x[i][j].X = a*math.Cos(stp) - b*math.Sin(stp) 
  55. x[i][j].Y = b*math.Cos(stp) + a*math.Sin(stp) 
  56. } 
  57. } 
  58. for i := 0; i < H; i++ { 
  59. for j := 0; j < W; j++ { 
  60. img.Line( 
  61. 150+int(y[i][j].X), 
  62. 150-int(y[i][j].Y), 
  63. 150+int(y[i][(j+1)%W].X), 
  64. 150-int(y[i][(j+1)%W].Y), 
  65. ) 
  66. img.Line( 
  67. 150+int(y[i][j].X), 
  68. 150-int(y[i][j].Y), 
  69. 150+int(y[i+1][j].X), 
  70. 150-int(y[i+1][j].Y), 
  71. ) 
  72. } 
  73. } 
  74. pics = append(pics, img.Image.(*image.Paletted)) 
  75. delay = append(delay, 5) 
  76. } 
  77. file, _ := os.Create("ball.gif") 
  78. defer file.Close() 
  79. gif.EncodeAll(file, &gif.GIF{ 
  80. Image: pics, 
  81. Delay: delay, 
  82. LoopCount: 5 * len(delay), 
  83. }) 
  84. } 

woniu.go

 

 
  1. package main 
  2. import ( 
  3. "github.com/hydra13142/cube" 
  4. "github.com/hydra13142/geom" 
  5. "github.com/hydra13142/paint" 
  6. "image" 
  7. "image/color/palette" 
  8. "image/gif" 
  9. "math" 
  10. "os" 
  11. ) 
  12. var ( 
  13. pln *cube.Plain 
  14. unx, uny cube.Vector 
  15. ) 
  16. const ( 
  17. H = 18 
  18. W = 96 
  19. ) 
  20. func init() { 
  21. pln, _ = cube.NewPlain(cube.Point{}, cube.Vector{2, 2, 1}) 
  22. uny = cube.FromTo(cube.Point{}, pln.VerticalPoint(cube.Point{0, 0, 10})).Unit() 
  23. unx = cube.OuterProduct(uny, cube.Vector{2, 2, 1}).Unit() 
  24. } 
  25. func main() { 
  26. var x [H + 1][W]cube.Point 
  27. var y [H + 1][W]geom.Point 
  28. dz := math.Pi / H 
  29. dxy := math.Pi * 4 / W 
  30. for i := 0; i <= H; i++ { 
  31. az := float64(i)*dz - math.Pi/2 
  32. r := 300 * math.Cos(az) 
  33. z := 100 * math.Sin(az) 
  34. for j := 0; j < W; j++ { 
  35. axy := float64(j) * dxy 
  36. R := float64(j) * r / W 
  37. x[i][j] = cube.Point{math.Cos(axy) * R, math.Sin(axy) * R, z} 
  38. } 
  39. } 
  40. pics := make([]*image.Paletted, 0, 20) 
  41. img := paint.Image{ 
  42. FR: paint.Green, 
  43. BG: paint.White, 
  44. } 
  45. stp := math.Pi / W 
  46. delay := make([]int, 0, 2*W) 
  47. for t := 0; t < 2*W; t++ { 
  48. img.Image = image.NewPaletted(image.Rect(0, 0, 600, 300), palette.Plan9) 
  49. for i := 0; i <= H; i++ { 
  50. for j := 0; j < W; j++ { 
  51. ox := cube.FromTo(cube.Point{}, x[i][j]) 
  52. y[i][j] = geom.Point{cube.InnerProduct(ox, unx), cube.InnerProduct(ox, uny)} 
  53. a, b := x[i][j].X, x[i][j].Y 
  54. x[i][j].X = a*math.Cos(stp) - b*math.Sin(stp) 
  55. x[i][j].Y = b*math.Cos(stp) + a*math.Sin(stp) 
  56. } 
  57. } 
  58. img.Line( 
  59. 300+int(y[0][0].X), 
  60. 150-int(y[0][0].Y), 
  61. 300+int(y[H][0].X), 
  62. 150-int(y[H][0].Y), 
  63. ) 
  64. for i := 0; i < H; i++ { 
  65. for j := 1; j < W; j++ { 
  66. img.Line( 
  67. 300+int(y[i][j].X), 
  68. 150-int(y[i][j].Y), 
  69. 300+int(y[i][j-1].X), 
  70. 150-int(y[i][j-1].Y), 
  71. ) 
  72. img.Line( 
  73. 300+int(y[i][j].X), 
  74. 150-int(y[i][j].Y), 
  75. 300+int(y[i+1][j].X), 
  76. 150-int(y[i+1][j].Y), 
  77. ) 
  78. } 
  79. } 
  80. pics = append(pics, img.Image.(*image.Paletted)) 
  81. delay = append(delay, 5) 
  82. } 
  83. file, _ := os.Create("woniu.gif") 
  84. defer file.Close() 
  85. gif.EncodeAll(file, &gif.GIF{ 
  86. Image: pics, 
  87. Delay: delay, 
  88. LoopCount: 5 * len(delay), 
  89. }) 
  90. } 

rotate.go

 

 
  1. package main 
  2. import ( 
  3. "github.com/hydra13142/cube" 
  4. "github.com/hydra13142/geom" 
  5. "github.com/hydra13142/paint" 
  6. "image" 
  7. "image/color/palette" 
  8. "image/gif" 
  9. "math" 
  10. "os" 
  11. ) 
  12. var ( 
  13. pln *cube.Plain 
  14. unx, uny cube.Vector 
  15. ) 
  16. const ( 
  17. H = 9 
  18. W = 36 
  19. ) 
  20. func init() { 
  21. pln, _ = cube.NewPlain(cube.Point{}, cube.Vector{4, 4, 3}) 
  22. uny = cube.FromTo(cube.Point{}, pln.VerticalPoint(cube.Point{0, 0, 10})).Unit() 
  23. unx = cube.OuterProduct(uny, cube.Vector{4, 4, 3}).Unit() 
  24. } 
  25. func main() { 
  26. var x [H*W + 1]cube.Point 
  27. var y [H*W + 1]geom.Point 
  28. dxy := (math.Pi * 2) / W 
  29. dz := math.Pi / H 
  30. for i := 0; i <= H*W; i++ { 
  31. az := float64(i)*dz/W - math.Pi/2 
  32. r := 140 * math.Cos(az) 
  33. z := 140 * math.Sin(az)  
  34. axy := float64(i) * dxy 
  35. x[i] = cube.Point{math.Cos(axy) * r, math.Sin(axy) * r, z} 
  36. } 
  37. pics := make([]*image.Paletted, 0, 20) 
  38. img := paint.Image{ 
  39. FR: paint.Green, 
  40. BG: paint.White, 
  41. } 
  42. stp := math.Pi * 2 / (W * 3) 
  43. delay := make([]int, 0, 3*W) 
  44. for t := 0; t < 3*W; t++ { 
  45. img.Image = image.NewPaletted(image.Rect(0, 0, 300, 300), palette.Plan9) 
  46. for i := 0; i <= H*W; i++ { 
  47. ox := cube.FromTo(cube.Point{}, x[i]) 
  48. y[i] = geom.Point{cube.InnerProduct(ox, unx), cube.InnerProduct(ox, uny)} 
  49. a, b := x[i].X, x[i].Z 
  50. x[i].X = a*math.Cos(stp) - b*math.Sin(stp) 
  51. x[i].Z = b*math.Cos(stp) + a*math.Sin(stp) 
  52. } 
  53. img.Line( 
  54. 150+int(y[0].X), 
  55. 150-int(y[0].Y), 
  56. 150+int(y[H*W].X), 
  57. 150-int(y[H*W].Y), 
  58. ) 
  59. for i := 0; i < H*W; i++ { 
  60. img.Line( 
  61. 150+int(y[i].X), 
  62. 150-int(y[i].Y), 
  63. 150+int(y[i+1].X), 
  64. 150-int(y[i+1].Y), 
  65. ) 
  66. } 
  67. pics = append(pics, img.Image.(*image.Paletted)) 
  68. delay = append(delay, 8) 
  69. } 
  70. file, _ := os.Create("rotate.gif") 
  71. defer file.Close() 
  72. gif.EncodeAll(file, &gif.GIF{ 
  73. Image: pics, 
  74. Delay: delay, 
  75. LoopCount: 5 * len(delay), 
  76. }) 
  77. } 

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表