Fork of https://github.com/tfriedel6/canvas to remove the cgo part, only leaving software rendering
Find a file
2018-05-17 15:11:03 +02:00
examples added size change event handler to events example 2018-05-17 15:11:03 +02:00
glandroidimpl added an Android GL implementation and an example for an Android app 2018-05-10 17:33:03 +02:00
gliosimpl added an iOS GL implementation and example 2018-05-15 18:49:35 +02:00
goglimpl no longer using TEXTURE_1D since it is not available on GL ES 2018-05-10 17:26:31 +02:00
sdlcanvas added a size change event handler 2018-05-16 00:11:31 +01:00
.gitignore added an iOS GL implementation and example 2018-05-15 18:49:35 +02:00
canvas.go clear stencil buffer on activate 2018-05-17 15:08:22 +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 more docs 2018-04-30 16:34:50 +02:00
gradients.go no longer using TEXTURE_1D since it is not available on GL ES 2018-05-10 17:26:31 +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 changed openglinterface a bit to be less specific to the go-gl implementation 2018-05-10 13:08:30 +02:00
LICENSE added BSD license 2018-03-28 11:33:15 +02:00
made_shaders.go no longer using TEXTURE_1D since it is not available on GL ES 2018-05-10 17:26:31 +02:00
make_shaders.go no longer using TEXTURE_1D since it is not available on GL ES 2018-05-10 17:26:31 +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 no longer using TEXTURE_1D since it is not available on GL ES 2018-05-10 17:26:31 +02:00
openglinterface.go.all reduced the goglimpl.go and added the complete versions for reference 2018-05-09 19:06:04 +02:00
paths.go changed openglinterface a bit to be less specific to the go-gl implementation 2018-05-10 13:08:30 +02:00
README.md Update README.md 2018-05-15 18:51:42 +02:00
shaders.go no longer using TEXTURE_1D since it is not available on GL ES 2018-05-10 17:26:31 +02:00
text.go changed openglinterface a bit to be less specific to the go-gl implementation 2018-05-10 13:08:30 +02:00
triangulation.go paths with sub-paths fixed 2018-04-04 16:01:11 +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
  • strokeStyle
  • linear gradients
  • radial gradients
  • image patterns
  • lineWidth
  • lineEnd (square, butt, round)
  • lineJoin (bevel, miter, round)
  • lineDash
  • global alpha
  • drawImage
  • getImageData
  • putImageData

Missing features

  • globalCompositeOperation
  • lineDashOffset
  • miterLimit
  • shadows
  • textBaseline
  • clearRect
  • getLineDash
  • isPointInPath
  • isPointInStroke
  • strokeText