Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "http"
- "io"
- "log"
- "strconv"
- "fmt"
- )
- const (
- width = 320
- height = 200
- )
- // returns a javascript command, must not use ' without escaping
- func drawSomething(x string, y string) string {
- return "ctx.lineTo(" + x + ", " + y + ");"
- }
- // hello world, the web server
- func GoDrawServer(w http.ResponseWriter, req *http.Request) {
- keypress := req.FormValue("key")
- x := req.FormValue("x")
- y := req.FormValue("y")
- if x == "" || y == "" {
- // Starting point for the drawing
- x = strconv.Itoa(width / 3)
- y = strconv.Itoa(height / 3)
- }
- const stepsize = 10
- var newcommand string = "" // can not use ' without escaping
- switch keypress {
- case "up":
- ynum, _ := strconv.Atoi(y)
- y = strconv.Itoa(ynum - stepsize)
- newcommand = drawSomething(x, y);
- case "down":
- ynum, _ := strconv.Atoi(y)
- y = strconv.Itoa(ynum + stepsize)
- newcommand = drawSomething(x, y);
- case "left":
- xnum, _ := strconv.Atoi(x)
- x = strconv.Itoa(xnum - stepsize)
- newcommand = drawSomething(x, y);
- case "right":
- xnum, _ := strconv.Atoi(x)
- x = strconv.Itoa(xnum + stepsize)
- newcommand = drawSomething(x, y);
- }
- fmt.Println("x:", x, "y:", y);
- drawstart := `
- var elem = document.getElementById('myCanvas');
- if (elem && elem.getContext) {
- var ctx = elem.getContext('2d');
- if (ctx) {
- ctx.fillStyle = '#808080';
- ctx.fillRect(0, 0, ` + strconv.Itoa(width) + ", " + strconv.Itoa(height) + `);
- ctx.fillStyle = '#0000ff';
- ctx.strokeStyle = '#ff0000';
- ctx.lineWidth = 4;
- ctx.beginPath();
- `
- drawcommands := req.FormValue("drawcommands") + newcommand
- drawend := `
- ctx.fill();
- ctx.stroke();
- ctx.closePath();
- }}
- `
- js := `
- function postwith (to,p) {
- var myForm = document.createElement("form");
- myForm.method="post" ;
- myForm.action = to ;
- for (var k in p) {
- var myInput = document.createElement("input") ;
- myInput.setAttribute("name", k) ;
- myInput.setAttribute("value", p[k]);
- myForm.appendChild(myInput) ;
- }
- document.body.appendChild(myForm) ;
- myForm.submit() ;
- document.body.removeChild(myForm) ;
- }
- function checkKey(e) {
- switch (e.keyCode) {
- case 40:
- postwith('', {key:'down', x:'` + x + `', y:'` + y + `', drawcommands:'` + drawcommands + `'});
- top.focus();
- break;
- case 38:
- postwith('', {key:'up', x:'` + x + `', y:'` + y + `', drawcommands:'` + drawcommands + `'});
- top.focus();
- break;
- case 37:
- postwith('', {key:'left', x:'` + x + `', y:'` + y + `', drawcommands:'` + drawcommands + `'});
- top.focus();
- break;
- case 39:
- postwith('', {key:'right', x:'` + x + `', y:'` + y + `', drawcommands:'` + drawcommands + `'});
- top.focus();
- break;
- }
- }
- function main() {
- document.onkeydown = checkKey;
- top.focus();
- }
- `
- title := "GoDraw 1"
- // keypress is the fallback text for those that does not have canvas-support
- 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>"
- 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>"
- io.WriteString(w, document)
- }
- func main() {
- http.HandleFunc("/", GoDrawServer)
- if err := http.ListenAndServe(":7777", nil); err != nil {
- log.Fatal("ListenAndServe: ", err.String())
- }
- }
Add Comment
Please, Sign In to add comment