-
Notifications
You must be signed in to change notification settings - Fork 0
/
templates.go
114 lines (101 loc) · 2.21 KB
/
templates.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package gop5js
import (
"html/template"
"net/http"
)
const templateIndex = `<!DOCTYPE html>
<html>
<head>
<meta name="viewport" width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0>
<style> body {padding: 0; margin: 0;} </style>
<script src="/lib/p5.js"></script>
<script src="globals.js"></script>
<script src="sketch.js"></script>
</head>
<body>
</body>
</html>
`
const templateGlobals = `
/*
* All the global JS vars starts with a capital letter!
*/
{{ range .StringVars }}
var {{ .Name }} = "{{ .Value }}";{{ end }}
{{ range .IntVars }}
var {{ .Name }} = {{ .IntValue }};{{ end }}
{{ with .Images }}
var images = [];
function preload(){
{{ range $key, $value := .}}
images['{{ $key }}'] = loadImage('/files/{{ $value }}');
{{ end }}
}
{{ end }}
`
func globalsHandler(w http.ResponseWriter, r *http.Request) {
globalParams := struct {
StringVars []struct {
Name string
Value string
}
IntVars []struct {
Name string
IntValue int
}
Images map[string]string
}{
[]struct{ Name, Value string }{
{"ServerName", serverName},
{"ServerPort", ServerPort},
},
[]struct {
Name string
IntValue int
}{
{"CanvasWidth", CanvasWidth},
{"CanvasHeight", CanvasHeight},
},
images,
}
t := template.Must(template.New("globals").Parse(templateGlobals))
err := t.Execute(w, globalParams)
ErrorContainer.add(err)
}
const templateSketch = `
var socket = new WebSocket("ws://" + ServerName + ServerPort + "/ws");
var p5Data = {
mouseX: 0,
mouseY: 0
};
var images = {}
function setup() {
createCanvas(CanvasWidth, CanvasHeight);
noLoop();
}
var sketch_draw = "";
function draw() {
clear();
eval(sketch_draw);
socket.send(JSON.stringify(getParams()));
}
socket.onmessage = function (event) {
newData = JSON.parse(event.data);
sketch_draw = newData.sketch_draw;
draw();
}
function getParams() {
return {
mouse_x: mouseX,
mouse_y: mouseY,
p_mouse_x: pmouseX,
p_mouse_y: pmouseY,
win_mouse_x: winMouseX,
win_mouse_y: winMouseY,
p_win_mouse_x: winMouseX,
p_win_mouse_y: winMouseY,
mouse_button: (mouseButton === 0) ? "" : mouseButton,
mouse_is_pressed: mouseIsPressed
}
}
`