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 SetTransform / GetTransform / ApplyTransform #175

Open bit101 opened 2 years ago

bit101 commented 2 years ago

The Context struct has a matrix property and various methods to manipulate that matrix. But although you can create a new Matrix, there is no way to apply a matrix to the context in one shot. Other packages I've used in the past have had one method such as SetTransform which replaces the current transform matrix with a new matrix, and ApplyTransform which applies a full matrix as an additional transform to the current matrix. As well as a method to get the current transform.

I believe this would be as simple as:

func (dc *Context) SetTransform(matrix gg.Matrix) {
    dc.matrix = matrix
}

func (dc *Context) ApplyTransform(matrix gg.Matrix) {
    dc.matrix = dc.matrix.Multiply(matrix)
}

func (dc *Context) GetTransform() gg.Matrix {
        return dc.matrix
}

Though I might have gotten the order reversed in the second one.

The Get and Set methods would be the most useful and if you had those, you could roll your own Apply method.