A rendering engine library for Go supporting both SDL2 and WebAssembly (HTML Canvas) targets.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
719 B

  1. package canvas
  2. import (
  3. "syscall/js"
  4. )
  5. // Canvas represents an HTML5 Canvas object.
  6. type Canvas struct {
  7. Value js.Value
  8. ctx2d js.Value
  9. }
  10. // GetCanvas gets an HTML5 Canvas object from the DOM.
  11. func GetCanvas(id string) Canvas {
  12. canvasEl := js.Global().Get("document").Call("getElementById", id)
  13. canvas2d := canvasEl.Call("getContext", "2d")
  14. c := Canvas{
  15. Value: canvasEl,
  16. ctx2d: canvas2d,
  17. }
  18. canvasEl.Set("width", c.ClientW())
  19. canvasEl.Set("height", c.ClientH())
  20. return c
  21. }
  22. // ClientW returns the client width.
  23. func (c Canvas) ClientW() int {
  24. return c.Value.Get("clientWidth").Int()
  25. }
  26. // ClientH returns the client height.
  27. func (c Canvas) ClientH() int {
  28. return c.Value.Get("clientHeight").Int()
  29. }