-
Notifications
You must be signed in to change notification settings - Fork 0
/
canvas.go
60 lines (54 loc) · 1.04 KB
/
canvas.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
package pengo
import (
"image"
"image/color"
"image/jpeg"
"image/png"
"os"
)
// the canvas itseld
type Canvas struct {
width, height, quality int
img *image.RGBA
}
// create a new canvas with 80% quality
func NewCanvas(width, height int, clr color.Color) Canvas {
img := image.NewRGBA(image.Rectangle{
image.Point{0, 0},
image.Point{width, height},
})
for x := 0; x < width; x++ {
for y := 0; y < height; y++ {
img.Set(x, y, clr)
}
}
canvas := Canvas{
width: width,
height: height,
quality: 80,
img: img,
}
return canvas
}
// change default quality from 1 to 100
func (c *Canvas) SetQuality(quality int) {
c.quality = quality
}
// save canvas into a file
func (c *Canvas) Save(filename string, filetype string) error {
f, err := os.Create(filename)
if err != nil {
return err
}
if filetype == "PNG" {
err = png.Encode(f, c.img)
} else if filetype == "JPG" {
err = jpeg.Encode(f, c.img, &jpeg.Options{
Quality: c.quality,
})
}
return err
}
type Shape interface {
Draw(c *Canvas)
}