-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path11_background_disposal_methods.go
79 lines (66 loc) · 2.04 KB
/
11_background_disposal_methods.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Seriál "Programovací jazyk Go"
// https://www.root.cz/serialy/programovaci-jazyk-go/
//
// Patnáctá část
// Programovací jazyk Go a grafika: tvorba animovaných GIFů, grafická knihovna GG
// https://www.root.cz/clanky/programovaci-jazyk-go-a-grafika-tvorba-animovanych-gifu-graficka-knihovna-gg/
//
// Repositář:
// https://github.com/tisnik/go-root/
//
// Seznam demonstračních příkladů z patnácté části:
// https://github.com/tisnik/go-root/blob/master/article_15/README.md
//
// Demonstrační příklad číslo 11:
// Nastavení metody přepínání snímků: vykreslení pozadí
//
// Dokumentace ve stylu "literate programming":
// https://tisnik.github.io/go-root/article_15/11_background_disposal_methods.html
package main
import (
"image"
"image/color"
"image/draw"
"image/gif"
"os"
)
func CreateImage(width int, height int, imageIndex int) *image.Paletted {
var palette = []color.Color{
color.RGBA{150, 150, 150, 0},
color.RGBA{250, 0, 0, 255},
}
if imageIndex == 0 {
img := image.NewPaletted(image.Rect(0, 0, width, height), palette)
r := image.Rect(0, 0, width, height)
draw.Draw(img, r, &image.Uniform{palette[0]}, image.ZP, draw.Src)
r2 := image.Rect(0, 0, 50, height)
draw.Draw(img, r2, &image.Uniform{palette[1]}, image.ZP, draw.Src)
return img
}
img := image.NewPaletted(image.Rect(imageIndex*60, 0, imageIndex*60+50, height), palette)
r2 := image.Rect(imageIndex*60, 0, imageIndex*60+50, height)
draw.Draw(img, r2, &image.Uniform{palette[1]}, image.ZP, draw.Src)
return img
}
func main() {
var images []*image.Paletted
var delays []int
var disposals []byte
for i := 0; i < 6; i++ {
img := CreateImage(350, 50, i)
images = append(images, img)
delays = append(delays, 100)
disposals = append(disposals, gif.DisposalBackground)
}
outfile, err := os.Create("11.gif")
if err != nil {
panic(err)
}
defer outfile.Close()
gif.EncodeAll(outfile, &gif.GIF{
Image: images,
Delay: delays,
Disposal: disposals,
BackgroundIndex: 0,
})
}