-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
189 lines (159 loc) · 5.01 KB
/
context.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Package gog is a Go Drawing Library
package gog
import (
"image"
"image/color"
"image/draw"
"github.com/setanarut/gog/v2/path"
"github.com/setanarut/gog/v2/shapes"
"github.com/setanarut/gog/v2/utils"
"github.com/setanarut/vec"
"github.com/srwiley/rasterx"
"github.com/srwiley/scanFT"
"golang.org/x/image/math/fixed"
)
var yellow = color.RGBA{255, 255, 0, 255}
var orangered = color.RGBA{255, 69, 0, 255}
var debugPathStrokeStyle = &StrokeStyle{
Color: color.White,
LineWidth: 1,
}
var debugCentroidStrokeStyle = &StrokeStyle{
Color: color.RGBA{0, 255, 255, 255}, // cyan
LineWidth: 2,
}
var debugBBoxCenterStrokeStyle = &StrokeStyle{
Color: color.RGBA{255, 128, 0, 255}, // orange
LineWidth: 2,
}
type Context struct {
AnimationFrames []image.Image
// Center point of Canvas
Center vec.Vec2
surface *image.RGBA
painter *scanFT.RGBAPainter
scannerFreeType *scanFT.ScannerFT
filler *rasterx.Filler
stroker *rasterx.Stroker
}
// NewContext returns a new drawing context.
func NewContext(width, height int) *Context {
ctx := new(Context)
ctx.surface = image.NewRGBA(image.Rect(0, 0, width, height))
ctx.painter = scanFT.NewRGBAPainter(ctx.surface)
ctx.scannerFreeType = scanFT.NewScannerFT(width, height, ctx.painter)
ctx.stroker = rasterx.NewStroker(width, height, ctx.scannerFreeType)
ctx.filler = &ctx.stroker.Filler
ctx.Center = vec.Vec2{float64(width) / 2, float64(height) / 2}
ctx.Clear(color.Black)
return ctx
}
// Fill draws path with fillColor
func (ctx *Context) Fill(p *path.Path, fillColor color.Color) {
ctx.filler.Start(utils.ToFixed(p.Start()))
for _, pt := range p.Points() {
ctx.filler.Line(utils.ToFixed(pt))
}
ctx.filler.SetColor(fillColor)
ctx.filler.Stop(p.IsClosed())
ctx.filler.Draw()
ctx.filler.Clear()
}
// Stroke draw paths with StrokeStyle
func (ctx *Context) Stroke(p *path.Path, strokeStyle *StrokeStyle) {
var capFunction rasterx.CapFunc
var joinStyle rasterx.JoinMode
switch strokeStyle.Cap {
case ButtCap:
capFunction = rasterx.ButtCap
case SquareCap:
capFunction = rasterx.SquareCap
case RoundCap:
capFunction = rasterx.RoundCap
case CubicCap:
capFunction = rasterx.CubicCap
case QuadraticCap:
capFunction = rasterx.QuadraticCap
}
switch strokeStyle.Join {
case MiterJoin:
joinStyle = rasterx.Miter
case RoundJoin:
joinStyle = rasterx.Round
case BevelJoin:
joinStyle = rasterx.Bevel
}
ctx.stroker.SetStroke(
fixed.Int26_6(strokeStyle.LineWidth*64), // line width
fixed.Int26_6(3*64), // miter limit
capFunction, // cap L
capFunction, // cap T
rasterx.RoundGap, // gap
joinStyle) // join mode
ctx.stroker.Start(utils.ToFixed(p.Start()))
for i := 1; i < len(p.Points()); i++ {
ctx.stroker.Line(utils.ToFixed(p.Points()[i]))
}
ctx.stroker.SetColor(strokeStyle.Color)
ctx.stroker.Stop(p.IsClosed())
ctx.stroker.Draw()
ctx.stroker.Clear()
}
// Clear clears canvas
func (ctx *Context) Clear(c color.Color) *Context {
// m := image.NewRGBA(image.Rect(0, 0, 640, 480))
draw.Draw(ctx.surface, ctx.surface.Bounds(),
&image.Uniform{c}, image.Point{}, draw.Src)
return ctx
}
// AppendAnimationFrame appends current canvas to animation frames.
func (ctx *Context) AppendAnimationFrame() {
ctx.AnimationFrames = append(ctx.AnimationFrames, utils.CloneRGBAImage(ctx.surface))
}
// ClearAnimationFrames clears context.AnimationFrames
func (ctx *Context) ClearAnimationFrames() {
ctx.AnimationFrames = nil
}
// SavePNG saves current canvas as static image
func (ctx *Context) SavePNG(filePath string) {
utils.WritePNG(filePath, ctx.surface)
}
// Surface returns canvas surface image
func (ctx *Context) Surface() *image.RGBA {
return ctx.surface
}
// SaveAPNG Saves APNG animation addes with AppendAnimationFrame().
//
// The successive delay times, one per frame, in 100ths of a second. (2 for 50 FPS, 4 for 25 FPS)
func (ctx *Context) SaveAPNG(filePath string, delay int) {
if len(ctx.AnimationFrames) == 0 {
panic("There is no frame in the image sequence, add at least one frame with AppendAnimationFrame().")
}
utils.WriteAnimatedPNG(filePath, ctx.AnimationFrames, uint16(delay))
}
// DebugDraw draws Path attributes for debug
func (ctx *Context) DebugDraw(pth *path.Path) {
// Draw Bounding box
ctx.Stroke(shapes.BBox(pth.Bounds()), debugStyle)
// Draw start point
circle := shapes.Circle(pth.Start(), 2)
ctx.Fill(circle, yellow)
// Draw end point
ctx.Fill(circle.SetPos(pth.End()), yellow)
// Draw second point
ctx.Fill(circle.SetPos(pth.Points()[1]), orangered)
// Draw all points
for i := 2; i < pth.Len()-1; i++ {
circle.SetPos(pth.Points()[i])
ctx.Fill(circle, color.White)
}
// Draw path stroke
ctx.Stroke(pth, debugPathStrokeStyle)
// Draw Centroid
circle.SetPos(pth.Anchor).Scale(vec.Vec2{1.3, 1.3})
ctx.Stroke(circle, debugCentroidStrokeStyle)
// Draw BBox center
a, b := pth.Bounds()
circle.SetPos(a.Lerp(b, 0.5))
ctx.Stroke(circle, debugBBoxCenterStrokeStyle)
}