-
Notifications
You must be signed in to change notification settings - Fork 2
/
context.go
110 lines (89 loc) · 2.31 KB
/
context.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
package httpcanvas
import (
"fmt"
)
type Context struct {
Width float64
Height float64
command chan string
mouse chan mouseMovement
mouseX float64
mouseY float64
mouseClickedX float64
mouseClickedY float64
mouseClicked bool
}
func newContext(w, h float64, c chan string, m chan mouseMovement) *Context {
return &Context{w, h, c, m, 0, 0, 0, 0, false}
}
func (c *Context) updateMouse() {
reading := true
for reading {
select {
case m := <-c.mouse:
switch m.command {
case "MOUSECLICK":
c.mouseClicked = true
c.mouseClickedX = m.x
c.mouseClickedY = m.y
case "MOUSEMOVE":
c.mouseX = m.x
c.mouseY = m.y
}
default:
reading = false
}
}
}
func (c *Context) MouseLocation() (float64, float64) {
c.updateMouse()
return c.mouseX, c.mouseY
}
func (c *Context) MouseClicked() (float64, float64, bool) {
c.updateMouse()
clicked := c.mouseClicked
c.mouseClicked = false
return c.mouseClickedX, c.mouseClickedY, clicked
}
func (c *Context) BeginPath() {
c.command <- "beginPath"
}
func (c *Context) MoveTo(x, y float64) {
c.command <- fmt.Sprintf("moveTo|%f|%f", x, y)
}
func (c *Context) LineTo(x, y float64) {
c.command <- fmt.Sprintf("lineTo|%f|%f", x, y)
}
func (c *Context) Stroke() {
c.command <- "stroke"
}
func (c *Context) Arc(x, y float64, radius float64, startAngle, endAngle float64, anticlockwise bool) {
c.command <- fmt.Sprintf("arc|%f|%f|%f|%f|%f|%v", x, y, radius, startAngle, endAngle, anticlockwise)
}
func (c *Context) FillStyle(s string) {
c.command <- fmt.Sprintf("fillStyle|%s", s)
}
func (c *Context) Fill() {
c.command <- "fill"
}
func (c *Context) LineWidth(f float64) {
c.command <- fmt.Sprintf("lineWidth|%f", f)
}
func (c *Context) StrokeStyle(s string) {
c.command <- fmt.Sprintf("strokeStyle|%s", s)
}
func (c *Context) FillRect(x, y, w, h float64) {
c.command <- fmt.Sprintf("fillRect|%f|%f|%f|%f", x, y, w, h)
}
func (c *Context) StrokeRect(x, y, w, h float64) {
c.command <- fmt.Sprintf("strokeRect|%f|%f|%f|%f", x, y, w, h)
}
func (c *Context) ClearRect(x, y, w, h float64) {
c.command <- fmt.Sprintf("clearRect|%f|%f|%f|%f", x, y, w, h)
}
func (c *Context) ClearFrame() {
c.command <- "CLEARFRAME" // erase buffered frame
}
func (c *Context) ShowFrame() {
c.command <- "SHOWFRAME" // swap buffer
}