web-client.scm 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. (define-record-type <canvas>
  2. (make-canvas params)
  3. canvas?
  4. (params get-params))
  5. (define current-canvas (make-canvas '((uri-root . "localhost:43334"))))
  6. (define (set-canvas! new-canvas)
  7. (set! current-canvas new-canvas))
  8. (define (send-to-canvas! uri-path obj)
  9. (when (or (not (canvas? current-canvas))
  10. (not (pair? (get-params current-canvas))))
  11. (error "current-canvas does not have valid parameters"))
  12. (let ((param-list (get-params current-canvas)))
  13. (let ((uri-root-pair (assq 'uri-root param-list)))
  14. (unless uri-root-pair
  15. (error "current-canvas does not have required key uri-root"))
  16. (make-post-request (cdr uri-root-pair) "/api/PushBuffer" obj))))
  17. (define color-mapping
  18. '((black . "#000")
  19. (gray . "#808080")
  20. (red . "#f00")
  21. (darkred . "#8b0000")
  22. (lime . "#0f0")
  23. (green . "#008000")
  24. (darkgreen . "#006400")
  25. (blue . "#00f")
  26. (darkblue . "#00008b")
  27. (cyan . "#0ff")
  28. (darkcyan . "#008b8b")
  29. (magenta . "#f0f")
  30. (darkmagenta . "#8b008b")
  31. (yellow . "#ff0")
  32. (goldenrod "#daa520")
  33. (white . "#fff")))
  34. (define (push-buffer! instruction)
  35. (let ((p (open-output-string)))
  36. (write instruction p)
  37. (let ((result (get-output-string p)))
  38. (close-output-port p)
  39. (send-to-canvas! "/api/PushBuffer" result)))
  40. ;; to prevent the buffer from growing too quickly
  41. (wait-a-moment!))
  42. (define (make-line x1 y1 x2 y2)
  43. `((type . "line")
  44. (x1 . ,x1)
  45. (y1 . ,y1)
  46. (x2 . ,x2)
  47. (y2 . ,y2)))
  48. (define (make-turtle-line x1 y1 x2 y2 pen-down shown)
  49. `((type . "turtleLine")
  50. (penDown . ,pen-down)
  51. (shown . ,shown)
  52. (x1 . ,x1)
  53. (y1 . ,y1)
  54. (x2 . ,x2)
  55. (y2 . ,y2)))
  56. (define (draw-line x1 y1 x2 y2)
  57. ;; TODO: ensure arguments are numbers
  58. (push-buffer! (make-line x1 y1 x2 y2)))
  59. (define (draw-turtle-line x1 y1 x2 y2 pen-down shown)
  60. (push-buffer! (make-turtle-line x1 y1 x2 y2 pen-down shown)))
  61. (define clear-screen
  62. (case-lambda
  63. (()
  64. (push-buffer! '((type . "clear"))))
  65. ((sym)
  66. (push-buffer! '((type . "clear")))
  67. (when (eq? sym 'complete)
  68. (make-post-request "localhost:43334" "/api/ClearBuffer" "")))))
  69. (define (canvas-image-rotate x y theta1 delta-theta)
  70. (push-buffer! `((type . "rotate")
  71. (x . ,x)
  72. (y . ,y)
  73. (facing . ,(+ theta1 delta-theta)))))
  74. (define (canvas-bg-color color)
  75. (let ((color-pair (assq color color-mapping)))
  76. (unless color-pair
  77. (error "bg-color" "Not a valid color" color))
  78. (push-buffer! `((type . "bgColor")
  79. (color . ,(cdr color-pair))))))
  80. (define (canvas-line-color color)
  81. (let ((color-pair (assq color color-mapping)))
  82. (unless color-pair
  83. (error "line-color" "Not a valid color" color))
  84. (push-buffer! `((type . "lineColor")
  85. (color . ,(cdr color-pair))))))
  86. (define (canvas-line-width width)
  87. (push-buffer! `((type . "lineWidth")
  88. (width . ,width))))
  89. (define (canvas-show-turtle x y theta)
  90. (push-buffer! `((type . "showTurtle")
  91. (x . ,x)
  92. (y . ,y)
  93. (facing . ,theta))))
  94. (define (canvas-hide-turtle)
  95. (push-buffer! `((type . "hideTurtle"))))