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.

67 lines
1.5 KiB

  1. package canvas
  2. import (
  3. "syscall/js"
  4. "git.kirsle.net/apps/doodle/lib/render"
  5. )
  6. // Methods here implement the drawing functions of the render.Engine
  7. // Clear the canvas to a certain color.
  8. func (e *Engine) Clear(color render.Color) {
  9. e.canvas.ctx2d.Set("fillStyle", color.ToHex())
  10. e.canvas.ctx2d.Call("fillRect", 0, 0, e.width, e.height)
  11. }
  12. // SetTitle sets the window title.
  13. func (e *Engine) SetTitle(title string) {
  14. js.Global().Get("document").Set("title", title)
  15. }
  16. // DrawPoint draws a pixel.
  17. func (e *Engine) DrawPoint(color render.Color, point render.Point) {
  18. e.canvas.ctx2d.Set("fillStyle", color.ToHex())
  19. e.canvas.ctx2d.Call("fillRect",
  20. int(point.X),
  21. int(point.Y),
  22. 1,
  23. 1,
  24. )
  25. }
  26. // DrawLine draws a line between two points.
  27. func (e *Engine) DrawLine(color render.Color, a, b render.Point) {
  28. e.canvas.ctx2d.Set("fillStyle", color.ToHex())
  29. for pt := range render.IterLine2(a, b) {
  30. e.canvas.ctx2d.Call("fillRect",
  31. int(pt.X),
  32. int(pt.Y),
  33. 1,
  34. 1,
  35. )
  36. }
  37. }
  38. // DrawRect draws a rectangle.
  39. func (e *Engine) DrawRect(color render.Color, rect render.Rect) {
  40. e.canvas.ctx2d.Set("strokeStyle", color.ToHex())
  41. e.canvas.ctx2d.Call("strokeRect",
  42. int(rect.X),
  43. int(rect.Y),
  44. int(rect.W),
  45. int(rect.H),
  46. )
  47. }
  48. // DrawBox draws a filled rectangle.
  49. func (e *Engine) DrawBox(color render.Color, rect render.Rect) {
  50. e.canvas.ctx2d.Set("fillStyle", color.ToHex())
  51. e.canvas.ctx2d.Call("fillRect",
  52. int(rect.X),
  53. int(rect.Y),
  54. int(rect.W),
  55. int(rect.H),
  56. )
  57. }