added an example for software rendering

updated readme
This commit is contained in:
Thomas Friedel 2019-05-13 12:29:15 +02:00
parent 349e01e301
commit b5212c916a
2 changed files with 46 additions and 1 deletions

View file

@ -12,7 +12,9 @@ The OpenGL backend is intended to provide decent performance. Obviously it will
## Software backend
The software backend can also be used if no OpenGL context is available. It will render into a standard Go RGBA image.
The software backend can also be used if no OpenGL context is available. It will render into a standard Go RGBA image.
There is experimental MSAA anti-aliasing, but it doesn't fully work properly yet. The best option for anti-aliasing currently is to render to a larger image and then scale it down.
## SDL/GLFW convenience packages

View file

@ -0,0 +1,43 @@
package main
import (
"image/png"
"math"
"os"
"github.com/tfriedel6/canvas"
"github.com/tfriedel6/canvas/backend/softwarebackend"
)
func main() {
backend := softwarebackend.New(720, 720)
cv := canvas.New(backend)
w, h := float64(cv.Width()), float64(cv.Height())
cv.SetFillStyle("#000")
cv.FillRect(0, 0, w, h)
for r := 0.0; r < math.Pi*2; r += math.Pi * 0.1 {
cv.SetFillStyle(int(r*10), int(r*20), int(r*40))
cv.BeginPath()
cv.MoveTo(w*0.5, h*0.5)
cv.Arc(w*0.5, h*0.5, math.Min(w, h)*0.4, r, r+0.1*math.Pi, false)
cv.ClosePath()
cv.Fill()
}
cv.SetStrokeStyle("#FFF")
cv.SetLineWidth(10)
cv.BeginPath()
cv.Arc(w*0.5, h*0.5, math.Min(w, h)*0.4, 0, math.Pi*2, false)
cv.Stroke()
f, err := os.OpenFile("result.png", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0777)
if err != nil {
panic(err)
}
err = png.Encode(f, backend.Image)
if err != nil {
panic(err)
}
}