Fork of https://github.com/tfriedel6/canvas to remove the cgo part, only leaving software rendering
Find a file
2018-05-10 13:08:30 +02:00
examples added a simple event handling example 2018-05-09 19:38:06 +02:00
goglimpl changed openglinterface a bit to be less specific to the go-gl implementation 2018-05-10 13:08:30 +02:00
sdlcanvas added a key char event handler 2018-05-09 19:45:21 +02:00
.gitignore reduced the goglimpl.go and added the complete versions for reference 2018-05-09 19:06:04 +02:00
canvas.go updated doc 2018-05-08 20:58:34 +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 updated docs 2018-04-30 16:30:19 +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 changed openglinterface a bit to be less specific to the go-gl implementation 2018-05-10 13:08:30 +02:00
make_shaders.go changed openglinterface a bit to be less specific to the go-gl implementation 2018-05-10 13:08:30 +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 changed openglinterface a bit to be less specific to the go-gl implementation 2018-05-10 13:08:30 +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-09 19:52:32 +02:00
shaders.go implemented globalAlpha 2018-04-09 17:39:26 +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 (untested)

When using just the canvas package without the sdlcanvas subpackage, this can probably also be made to work for Android and iOS, but this is as yet untested and would require the GL interface to be implemented for the platform.

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