main.go 993 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package main
  2. import (
  3. "log"
  4. "net/http"
  5. "sync"
  6. "dev.hexasoftware.com/stdio/wsrpc"
  7. _ "dev.hexasoftware.com/stdio/prettylog"
  8. "dev.hexasoftware.com/stdio/wsrpc/sample/canvas/canvas"
  9. )
  10. var objlock sync.Mutex
  11. type Point struct {
  12. x, y int
  13. }
  14. func ClientHandler(ctx *wsrpc.ClientCtx) {
  15. log.Println("Client connected", ctx.WS.RemoteAddr())
  16. c := canvas.New(ctx)
  17. points := []Point{}
  18. c.OnMouseMove = func(x, y int) {
  19. objlock.Lock()
  20. defer objlock.Unlock()
  21. c.Clear()
  22. points = append(points, Point{x, y})
  23. lenPoints := len(points)
  24. // Test this
  25. if lenPoints > 2 {
  26. for i := 1; i < lenPoints; i++ {
  27. c.Line(points[i-1].x, points[i-1].y,
  28. points[i].x, points[i].y)
  29. }
  30. }
  31. if lenPoints > 15 { // try 100 drawCalls
  32. points = points[1:] // remove first
  33. }
  34. }
  35. }
  36. func main() {
  37. var mux = http.NewServeMux()
  38. wsrpc.RegisterTo(mux, ClientHandler)
  39. mux.Handle("/", http.FileServer(http.Dir("web")))
  40. log.Println("Listening :8080")
  41. http.ListenAndServe(":8080", mux)
  42. }