Fork of https://github.com/tfriedel6/canvas to remove the cgo part, only leaving software rendering
Find a file
2018-09-18 14:15:42 +02:00
examples added golang.org/x/mobile/gl implementation and an example using shiny 2018-05-28 17:08:24 +02:00
glimpl implemented shadowBlur (wip) 2018-07-27 14:11:53 +02:00
sdlcanvas added MouseWheel event function 2018-09-08 11:07:55 +02:00
testdata implemented text stroking 2018-09-01 17:06:00 +02:00
.gitignore added an iOS GL implementation and example 2018-05-15 18:49:35 +02:00
canvas.go line dash is now only applied when calling stroke 2018-09-01 16:42:07 +02:00
canvas_test.go implemented text stroking 2018-09-01 17:06:00 +02:00
color.go switched from float32 to float64 for better compatibility with the default go math package; moved vector and matrix code into package 2018-03-21 12:45:32 +01:00
freetype.go added ascent and descent to text measurements 2018-05-19 15:51:00 +01:00
gradients.go performance optimization, avoid use of stencil buffer when drawing with opaque styles 2018-07-19 16:59:22 +02:00
imagedata.go changed openglinterface a bit to be less specific to the go-gl implementation 2018-05-10 13:08:30 +02:00
images.go implemented shadowBlur (wip) 2018-07-27 14:11:53 +02:00
LICENSE added BSD license 2018-03-28 11:33:15 +02:00
made_shaders.go implemented shadowBlur (wip) 2018-07-27 14:11:53 +02:00
make_shaders.go implemented shadowBlur (wip) 2018-07-27 14:11:53 +02:00
math.go switched from float32 to float64 for better compatibility with the default go math package; moved vector and matrix code into package 2018-03-21 12:45:32 +01:00
openglinterface.go implemented shadowBlur (wip) 2018-07-27 14:11:53 +02:00
paths.go fixed a bug when lines in a polygon are parallel 2018-09-18 14:15:42 +02:00
README.md implemented text stroking 2018-09-01 17:06:00 +02:00
shaders.go implemented shadowBlur (wip) 2018-07-27 14:11:53 +02:00
shadows.go vary the gaussian shader kernel size depending on the blur radius for better performance 2018-07-27 15:58:38 +02:00
text.go implemented text stroking 2018-09-01 17:06:00 +02:00
triangulation.go fixed a bug in the triangulation function 2018-07-27 16:46:18 +02: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 convenience package

The sdlcanvas subpackage provides a very simple way to get started with just a few lines of code. As the name implies it is based on the SDL library. It creates a window for you and gives you a canvas to draw with.

OS support

  • Linux
  • Windows
  • OSX
  • Android
  • iOS

Unfortunately using full Go apps using gomobile doesn't work since gomobile does not seem to create a GL view with a stencil buffer, and the canvas package makes heavy use of the stencil buffer. Therefore the gomobile bind command has to be used together with platform specific projects.

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
  • fillStyle
  • strokeText
  • strokeStyle
  • linear gradients
  • radial gradients
  • image patterns
  • lineWidth
  • lineEnd (square, butt, round)
  • lineJoin (bevel, miter, round)
  • miterLimit
  • lineDash
  • getLineDash
  • global alpha
  • drawImage
  • getImageData
  • putImageData
  • clearRect
  • shadowColor
  • shadowOffset(X/Y)
  • shadowBlur

Missing features

  • globalCompositeOperation
  • lineDashOffset
  • textBaseline
  • isPointInPath
  • isPointInStroke