https://github.com/h8gi/canvas
golang animation library
https://github.com/h8gi/canvas
animation go golang graphics processing
Last synced: 4 months ago
JSON representation
golang animation library
- Host: GitHub
- URL: https://github.com/h8gi/canvas
- Owner: h8gi
- Created: 2017-09-07T12:27:27.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2020-07-10T00:46:54.000Z (almost 6 years ago)
- Last Synced: 2024-06-19T04:11:14.640Z (almost 2 years ago)
- Topics: animation, go, golang, graphics, processing
- Language: Go
- Size: 38.1 KB
- Stars: 30
- Watchers: 3
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Canvas
`canvas` is 2d animation library.
## Installatioin
```sh
go get -u github.com/h8gi/canvas
```
## Usage
Create canvas object.
```go
c := canvas.NewCanvas(&canvas.CanvasConfig{
Width: 300,
Height: 300,
FrameRate: 60,
})
```
Set drawing function and start loop.
```go
c.Draw(func(ctx *canvas.Context) {
if ctx.IsMouseDragged {
ctx.DrawCircle(ctx.Mouse.X, ctx.Mouse.Y, 5)
ctx.Fill()
}
})
```
Struct `gg.Context` is embedded in `canvas.Context`.
See [https://github.com/fogleman/gg](https://github.com/fogleman/gg) about details.
## Example
See [example](example) directory.
```go
package main
import (
"github.com/faiface/pixel/pixelgl"
"github.com/h8gi/canvas"
"golang.org/x/image/colornames"
)
func main() {
c := canvas.NewCanvas(&canvas.CanvasConfig{
Width: 640,
Height: 400,
FrameRate: 30,
Title: "Hello Canvas!",
})
c.Setup(func(ctx *canvas.Context) {
ctx.SetColor(colornames.White)
ctx.Clear()
ctx.SetColor(colornames.Green)
ctx.SetLineWidth(5)
})
c.Draw(func(ctx *canvas.Context) {
ctx.Push()
if ctx.IsMouseDragged {
ctx.SetColor(colornames.Red)
}
ctx.DrawLine(ctx.Mouse.X, ctx.Mouse.Y,
ctx.PMouse.X, ctx.PMouse.Y)
ctx.Stroke()
ctx.Pop()
if ctx.IsKeyPressed(pixelgl.KeyUp) {
ctx.Push()
ctx.SetColor(colornames.White)
ctx.Clear()
ctx.Pop()
}
})
}
```
## Built With
- [gg](https://github.com/fogleman/gg) - 2D graphics library.
- [pixel](https://github.com/faiface/pixel) - 2D game library.