Guest User

Untitled

a guest
Jan 21st, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "http"
  5. "io"
  6. "log"
  7. "strconv"
  8. "fmt"
  9. )
  10.  
  11. const (
  12. width = 320
  13. height = 200
  14. )
  15.  
  16. // returns a javascript command, must not use ' without escaping
  17. func drawSomething(x string, y string) string {
  18. return "ctx.lineTo(" + x + ", " + y + ");"
  19. }
  20.  
  21. // hello world, the web server
  22. func GoDrawServer(w http.ResponseWriter, req *http.Request) {
  23. keypress := req.FormValue("key")
  24. x := req.FormValue("x")
  25. y := req.FormValue("y")
  26. if x == "" || y == "" {
  27. // Starting point for the drawing
  28. x = strconv.Itoa(width / 3)
  29. y = strconv.Itoa(height / 3)
  30. }
  31. const stepsize = 10
  32. var newcommand string = "" // can not use ' without escaping
  33. switch keypress {
  34. case "up":
  35. ynum, _ := strconv.Atoi(y)
  36. y = strconv.Itoa(ynum - stepsize)
  37. newcommand = drawSomething(x, y);
  38. case "down":
  39. ynum, _ := strconv.Atoi(y)
  40. y = strconv.Itoa(ynum + stepsize)
  41. newcommand = drawSomething(x, y);
  42. case "left":
  43. xnum, _ := strconv.Atoi(x)
  44. x = strconv.Itoa(xnum - stepsize)
  45. newcommand = drawSomething(x, y);
  46. case "right":
  47. xnum, _ := strconv.Atoi(x)
  48. x = strconv.Itoa(xnum + stepsize)
  49. newcommand = drawSomething(x, y);
  50. }
  51. fmt.Println("x:", x, "y:", y);
  52.  
  53. drawstart := `
  54. var elem = document.getElementById('myCanvas');
  55. if (elem && elem.getContext) {
  56. var ctx = elem.getContext('2d');
  57. if (ctx) {
  58.  
  59. ctx.fillStyle = '#808080';
  60. ctx.fillRect(0, 0, ` + strconv.Itoa(width) + ", " + strconv.Itoa(height) + `);
  61.  
  62. ctx.fillStyle = '#0000ff';
  63. ctx.strokeStyle = '#ff0000';
  64. ctx.lineWidth = 4;
  65.  
  66. ctx.beginPath();
  67. `
  68. drawcommands := req.FormValue("drawcommands") + newcommand
  69. drawend := `
  70. ctx.fill();
  71. ctx.stroke();
  72. ctx.closePath();
  73. }}
  74. `
  75.  
  76. js := `
  77.  
  78. function postwith (to,p) {
  79. var myForm = document.createElement("form");
  80. myForm.method="post" ;
  81. myForm.action = to ;
  82. for (var k in p) {
  83. var myInput = document.createElement("input") ;
  84. myInput.setAttribute("name", k) ;
  85. myInput.setAttribute("value", p[k]);
  86. myForm.appendChild(myInput) ;
  87. }
  88. document.body.appendChild(myForm) ;
  89. myForm.submit() ;
  90. document.body.removeChild(myForm) ;
  91. }
  92.  
  93. function checkKey(e) {
  94. switch (e.keyCode) {
  95. case 40:
  96. postwith('', {key:'down', x:'` + x + `', y:'` + y + `', drawcommands:'` + drawcommands + `'});
  97. top.focus();
  98. break;
  99. case 38:
  100. postwith('', {key:'up', x:'` + x + `', y:'` + y + `', drawcommands:'` + drawcommands + `'});
  101. top.focus();
  102. break;
  103. case 37:
  104. postwith('', {key:'left', x:'` + x + `', y:'` + y + `', drawcommands:'` + drawcommands + `'});
  105. top.focus();
  106. break;
  107. case 39:
  108. postwith('', {key:'right', x:'` + x + `', y:'` + y + `', drawcommands:'` + drawcommands + `'});
  109. top.focus();
  110. break;
  111. }
  112. }
  113.  
  114. function main() {
  115. document.onkeydown = checkKey;
  116. top.focus();
  117. }
  118.  
  119.  
  120. `
  121. title := "GoDraw 1"
  122. // keypress is the fallback text for those that does not have canvas-support
  123. body := "<h1>" + title + "</h1>Try the arrow keys<br><br><canvas id=\"myCanvas\" width=\"" + strconv.Itoa(width) + "\" height=\"" + strconv.Itoa(height) + "\">" + keypress + "</canvas><br><a href=\"/\">Clear</a>"
  124. document := "<!doctype html><html><head><title>" + title + "</title></head><body style=\"font-family: sans-serif; margin: 10em;\"onload=\"main()\">" + body + "<script type=\"text/javascript\">" + js + drawstart + drawcommands + drawend + "</script></body></html>"
  125. io.WriteString(w, document)
  126. }
  127.  
  128. func main() {
  129. http.HandleFunc("/", GoDrawServer)
  130. if err := http.ListenAndServe(":7777", nil); err != nil {
  131. log.Fatal("ListenAndServe: ", err.String())
  132. }
  133. }
Add Comment
Please, Sign In to add comment