2019-06-27 01:36:54 +00:00
|
|
|
package canvas
|
|
|
|
|
|
|
|
import (
|
|
|
|
"syscall/js"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Canvas represents an HTML5 Canvas object.
|
|
|
|
type Canvas struct {
|
|
|
|
Value js.Value
|
|
|
|
ctx2d js.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCanvas gets an HTML5 Canvas object from the DOM.
|
|
|
|
func GetCanvas(id string) Canvas {
|
|
|
|
canvasEl := js.Global().Get("document").Call("getElementById", id)
|
|
|
|
canvas2d := canvasEl.Call("getContext", "2d")
|
|
|
|
|
|
|
|
c := Canvas{
|
|
|
|
Value: canvasEl,
|
|
|
|
ctx2d: canvas2d,
|
|
|
|
}
|
|
|
|
|
|
|
|
canvasEl.Set("width", c.ClientW())
|
|
|
|
canvasEl.Set("height", c.ClientH())
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// ClientW returns the client width.
|
|
|
|
func (c Canvas) ClientW() int {
|
2019-06-27 18:57:26 +00:00
|
|
|
return js.Global().Get("window").Get("innerWidth").Int()
|
|
|
|
// return c.Value.Get("clientWidth").Int()
|
2019-06-27 01:36:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ClientH returns the client height.
|
|
|
|
func (c Canvas) ClientH() int {
|
2019-06-27 18:57:26 +00:00
|
|
|
return js.Global().Get("window").Get("innerHeight").Int()
|
|
|
|
// return c.Value.Get("clientHeight").Int()
|
2019-06-27 01:36:54 +00:00
|
|
|
}
|