fogleman / gg

Go Graphics - 2D rendering in Go with a simple API.
https://godoc.org/github.com/fogleman/gg
MIT License
4.34k stars 352 forks source link

context.Image() always returns the same image in main scope. #152

Closed setanarut closed 2 years ago

setanarut commented 2 years ago
package main

import (
    "fmt"
    "image"
    "math/rand"

    "github.com/fogleman/gg"
)

const frames = 3
const w, h int = 256, 256

func main() {
    dc := gg.NewContext(w, h)
    var arr = make([]image.Image, frames, frames)

    for i := 0; i < frames; i++ {
        dc.DrawRectangle(0, 0, float64(w), float64(h))
        dc.SetRGB(rand.Float64(), rand.Float64(), rand.Float64())
        dc.Fill()
        arr[i] = dc.Image()
        fmt.Println("arr    :", arr[i].At(0, 0))
    }
    //all frames are the same image.
    fmt.Println("- arr in main scope -")
    fmt.Println(arr[0].At(0, 0))
    fmt.Println(arr[1].At(0, 0))
    fmt.Println(arr[2].At(0, 0))
}
> go run main.go
arr    : {154 239 169 255}
arr    : {111 108 175 255}
arr    : {16 39 24 255}
- arr in main scope -
{16 39 24 255}
{16 39 24 255}
{16 39 24 255}