Skip to content

Commit ad9bf8c

Browse files
committed
fogleman#79 Use viewbox so svg is not a fixed size (easier to animate)
1 parent 856c46c commit ad9bf8c

File tree

1 file changed

+29
-19
lines changed

1 file changed

+29
-19
lines changed

primitive/model.go

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ import (
99
)
1010

1111
type Model struct {
12-
Sw, Sh int
13-
Scale float64
14-
Background Color
15-
Target *image.RGBA
16-
Current *image.RGBA
17-
Context *gg.Context
18-
Score float64
19-
Shapes []Shape
20-
Colors []Color
21-
Scores []float64
22-
Workers []*Worker
12+
Sw, Sh, Vw, Vh int
13+
Scale float64
14+
Background Color
15+
Target *image.RGBA
16+
Current *image.RGBA
17+
Context *gg.Context
18+
Score float64
19+
Shapes []Shape
20+
Colors []Color
21+
Scores []float64
22+
Workers []*Worker
2323
}
2424

2525
func NewModel(target image.Image, background Color, size, numWorkers int) *Model {
@@ -41,6 +41,8 @@ func NewModel(target image.Image, background Color, size, numWorkers int) *Model
4141
model := &Model{}
4242
model.Sw = sw
4343
model.Sh = sh
44+
model.Vw = sw / int(scale)
45+
model.Vh = sh / int(scale)
4446
model.Scale = scale
4547
model.Background = background
4648
model.Target = imageToRGBA(target)
@@ -85,15 +87,23 @@ func (model *Model) Frames(scoreDelta float64) []image.Image {
8587

8688
func (model *Model) SVG() string {
8789
bg := model.Background
90+
fillA := model.Colors[0].A
8891
var lines []string
89-
lines = append(lines, fmt.Sprintf("<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"%d\" height=\"%d\">", model.Sw, model.Sh))
90-
lines = append(lines, fmt.Sprintf("<rect x=\"0\" y=\"0\" width=\"%d\" height=\"%d\" fill=\"#%02x%02x%02x\" />", model.Sw, model.Sh, bg.R, bg.G, bg.B))
91-
lines = append(lines, fmt.Sprintf("<g transform=\"scale(%f) translate(0.5 0.5)\">", model.Scale))
92+
lines = append(lines, fmt.Sprintf("<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" viewBox=\"0 0 %d %d\">", model.Vw, model.Vh))
93+
lines = append(lines, fmt.Sprintf("<rect width=\"100%%\" height=\"100%%\" fill=\"#%02x%02x%02x\" />", bg.R, bg.G, bg.B))
94+
group := "<g fill-opacity=\"%f\">"
95+
lines = append(lines, fmt.Sprintf(group, float64(fillA)/255))
9296
for i, shape := range model.Shapes {
97+
var attrs []string
9398
c := model.Colors[i]
94-
attrs := "fill=\"#%02x%02x%02x\" fill-opacity=\"%f\""
95-
attrs = fmt.Sprintf(attrs, c.R, c.G, c.B, float64(c.A)/255)
96-
lines = append(lines, shape.SVG(attrs))
99+
fill := "fill=\"#%02x%02x%02x\""
100+
fill = fmt.Sprintf(fill, c.R, c.G, c.B)
101+
attrs = append(attrs, fill)
102+
if c.A != fillA {
103+
opacity := "fill-opacity=\"%f\""
104+
attrs = append(attrs, fmt.Sprintf(opacity, float64(c.A)/255))
105+
}
106+
lines = append(lines, shape.SVG(strings.Join(attrs, " ")))
97107
}
98108
lines = append(lines, "</g>")
99109
lines = append(lines, "</svg>")
@@ -148,7 +158,7 @@ func (model *Model) Step(shapeType ShapeType, alpha, repeat int) int {
148158
return counter
149159
}
150160

151-
func (model *Model) runWorkers(t ShapeType, a, n, age, m int) *State {
161+
func (model *Model) runWorkers(t ShapeType, alphaVal, n, age, m int) *State {
152162
wn := len(model.Workers)
153163
ch := make(chan *State, wn)
154164
wm := m / wn
@@ -158,7 +168,7 @@ func (model *Model) runWorkers(t ShapeType, a, n, age, m int) *State {
158168
for i := 0; i < wn; i++ {
159169
worker := model.Workers[i]
160170
worker.Init(model.Current, model.Score)
161-
go model.runWorker(worker, t, a, n, age, wm, ch)
171+
go model.runWorker(worker, t, alphaVal, n, age, wm, ch)
162172
}
163173
var bestEnergy float64
164174
var bestState *State

0 commit comments

Comments
 (0)