Fork of https://github.com/tfriedel6/canvas to remove the cgo part, only leaving software rendering
Find a file
2019-02-22 16:39:30 +01:00
backend removed openglinterface 2019-02-22 16:39:30 +01:00
examples added an example for a gomobile (gomobile build) implementation 2018-10-31 12:43:15 +01:00
glfwcanvas added a Size function to get the window size 2018-11-13 12:19:05 +01:00
glimpl updated xmobile implementation for new version, also added to tests 2018-09-18 15:07:45 +02:00
sdlcanvas moved offscreen canvas to backend 2019-02-22 16:39:30 +01:00
testdata added some tests; improved gradient and image pattern transformations 2019-02-22 16:37:48 +01:00
.gitignore added an iOS GL implementation and example 2018-05-15 18:49:35 +02:00
canvas.go moved offscreen canvas to backend 2019-02-22 16:39:30 +01:00
canvas_test.go removed openglinterface 2019-02-22 16:39:30 +01:00
color.go made DrawStyle public for use by backends; changed color to be color.RGBA 2019-02-22 16:38:27 +01:00
freetype.go added ascent and descent to text measurements 2018-05-19 15:51:00 +01:00
go.mod separated out a Path2D type 2019-01-23 17:23:47 +01:00
go.sum separated out a Path2D type 2019-01-23 17:23:47 +01:00
gradients.go moved gradients to backend 2019-02-22 16:39:30 +01:00
images.go moved offscreen canvas to backend 2019-02-22 16:39:30 +01:00
LICENSE added BSD license 2018-03-28 11:33:15 +02:00
math.go arc transformation fix 2019-02-21 11:03:38 +01:00
path2d.go arc transformation fix 2019-02-21 11:03:38 +01:00
paths.go moved offscreen canvas to backend 2019-02-22 16:39:30 +01:00
README.md updated readme 2019-01-11 11:03:53 +01:00
shaders.go added some tests; improved gradient and image pattern transformations 2019-02-22 16:37:48 +01:00
shadows.go text shadow rendering moved to backend 2019-02-22 16:39:30 +01:00
text.go moved offscreen canvas to backend 2019-02-22 16:39:30 +01:00
triangulation.go moved clipping to backend 2019-02-22 16:39:30 +01:00

GoDoc is available here

Go canvas

Canvas is a Go library based on OpenGL that tries to provide the HTML5 canvas API as closely as possible.

Many of the basic functions are supported, but it is still a work in progress. The library aims to accept a lot of different parameters on each function in a similar way as the Javascript API does.

Whereas the Javascript API uses a context that all draw calls go to, here all draw calls are directly on the canvas type. The other difference is that here setters are used instead of properties for things like fonts and line width.

The library is intended to provide decent performance. Obviously it will not be able to rival hand coded OpenGL for a given purpose, but for many purposes it will be enough. It can also be combined with hand coded OpenGL.

SDL/GLFW convenience packages

The sdlcanvas and glfwcanvas subpackages provide a very simple way to get started with just a few lines of code. As the names imply they are based on the SDL library and the GLFW library respectively. They create a window for you and give you a canvas to draw with.

OS support

  • Linux
  • Windows
  • macOS
  • Android
  • iOS

Using gomobile to build a full Go app using gomobile build now works by using an offscreen texture to render to and then rendering that to the screen. See the example in examples/gomobile. The offscreen texture is necessary since gomobile automatically creates a GL context without a stencil buffer, which this library requires.

Example

Look at the example/drawing package for some drawing examples.

Here is a simple example for how to get started:

package main

import (
	"math"

	"github.com/tfriedel6/canvas/sdlcanvas"
)

func main() {
	wnd, cv, err := sdlcanvas.CreateWindow(1280, 720, "Hello")
	if err != nil {
		panic(err)
	}
	defer wnd.Destroy()

	wnd.MainLoop(func() {
		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()
	})
}

The result:

Implemented features

These features should work just like their HTML5 counterparts, but there are likely to be a lot of edge cases where they don't work exactly the same way.

  • beginPath
  • closePath
  • moveTo
  • lineTo
  • rect
  • arc
  • arcTo
  • quadraticCurveTo
  • bezierCurveTo
  • stroke
  • fill
  • clip
  • save
  • restore
  • scale
  • translate
  • rotate
  • transform
  • setTransform
  • fillText
  • measureText
  • textAlign
  • textBaseline
  • fillStyle
  • strokeText
  • strokeStyle
  • linear gradients
  • radial gradients
  • image patterns
  • lineWidth
  • lineEnd (square, butt, round)
  • lineJoin (bevel, miter, round)
  • miterLimit
  • lineDash
  • getLineDash
  • lineDashOffset
  • global alpha
  • drawImage
  • getImageData
  • putImageData
  • clearRect
  • shadowColor
  • shadowOffset(X/Y)
  • shadowBlur

Missing features

  • globalCompositeOperation
  • isPointInPath
  • isPointInStroke
  • textBaseline hanging and ideographic (currently work just like top and bottom)
  • full self intersecting polygon support