/
canvas.go
113 lines (95 loc) · 2.53 KB
/
canvas.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// +build wasm
package canvas
import (
"errors"
"fmt"
"image/color"
"syscall/js"
)
func hexColor(c color.Color) string {
r, g, b, _ := c.RGBA()
r = r * 0xff / 0xffff
g = g * 0xff / 0xffff
b = b * 0xff / 0xffff
return fmt.Sprintf("#%02X%02X%02X", r, g, b)
}
// Canvas represents an HTML <canvas> element and its associated 2D
// rendering context.
type Canvas struct {
elem js.Value
ctx js.Value
}
// New returns a new Canvas that wraps the <canvas> elem. This will
// most likely be retrieved using
//
// js.Global.Get("document").Call("getElementById", id)
//
// If the browser doesn't support CanvasRenderingContext2D, an error
// is returned.
func New(elem js.Value) (*Canvas, error) {
if js.Global().Get("CanvasRenderingContext2D") == js.Undefined() {
// TODO: Is this necessary? I would think that anything that
// supports WebAssembly should probably also have support for
// canvases. Maybe Node.js?
return nil, errors.New("Browser doesn't support canvas 2D")
}
return &Canvas{
elem: elem,
ctx: elem.Call("getContext", "2d"),
}, nil
}
// Rect returns an Object that draws a rectangle with the given
// bounds.
func (c *Canvas) Rect(r Rectangle) Object {
r = r.Canon()
p := func(pb *PathBuilder) {
pb.Rect(r)
}
return &pathObj{
c: c,
path: p,
}
}
// Path returns an Object that draws a path build by the function p.
// For more information, see the documentation for PathBuilder.
func (c *Canvas) Path(p func(*PathBuilder)) Object {
return &pathObj{
c: c,
path: p,
}
}
// Text returns an Object that draws the string text. If mw is a
// positive number, than it specifies the maximumWidth parameter of
// the canvas context's fillText() and drawText() methods.
func (c *Canvas) Text(text string, mw float64) Object {
return &textObj{
c: c,
text: text,
mw: mw,
}
}
// Clear clears the pixels in the rectangle specified by r.
func (c *Canvas) Clear(r Rectangle) {
r = r.Canon()
c.ctx.Call("clearRect", r.Min.X, r.Min.Y, r.Dx(), r.Dy())
}
// MeasureText returns a TextMetrics containing information about the
// string text.
func (c *Canvas) MeasureText(text string) (tm TextMetrics) {
return TextMetrics{c.ctx.Call("measureText", text)}
}
// Width returns the width of the canvas.
func (c *Canvas) Width() float64 {
return c.elem.Get("width").Float()
}
// Height returns the height of the canvas.
func (c *Canvas) Height() float64 {
return c.elem.Get("height").Float()
}
type Font struct {
Size int
Name string
}
func (f Font) String() string {
return fmt.Sprintf("%v %v", f.Size, f.Name)
}