Fork of https://github.com/tfriedel6/canvas to remove the cgo part, only leaving software rendering
Find a file
Thomas Friedel 62eb6793c7 updated doc
2018-05-08 20:58:34 +02:00
examples changed window title 2018-04-30 16:56:04 +02:00
goglimpl a go-gl implementation of the gl interface 2018-01-24 14:20:12 +01:00
sdlcanvas also close window on quit event 2018-04-14 11:21:37 +02:00
.gitignore split main loop for manual main loop support 2018-03-24 19:20:30 +01: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 implemented globalAlpha 2018-04-09 17:39:26 +02:00
images.go updated docs 2018-04-30 16:30:19 +02:00
LICENSE added BSD license 2018-03-28 11:33:15 +02:00
made_shaders.go implemented globalAlpha 2018-04-09 17:39:26 +02:00
make_shaders.go shaders are now in their own file; text fill now uses font rendering more directly and supports fill styles 2018-02-26 15:40:48 +01: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 clipping with the rect function now uses scissor test instead of stencil 2018-04-04 17:10:36 +02:00
paths.go fixed doc 2018-04-30 12:10:18 +02:00
README.md Update README.md 2018-05-08 16:46:31 +02:00
shaders.go implemented globalAlpha 2018-04-09 17:39:26 +02:00
text.go updated docs 2018-04-30 16:30:19 +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.

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