-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.go
82 lines (63 loc) · 1.9 KB
/
init.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
//go:build js && wasm
// +build js,wasm
package webzen
import (
"syscall/js"
"time"
"github.com/dimkauzh/webzen/src/backend/document"
"github.com/dimkauzh/webzen/src/backend/window"
"github.com/dimkauzh/webzen/src/keys"
)
var fpsInterval = time.Millisecond * 16
func SetFps(fps int) {
fpsInterval = time.Second / time.Duration(fps)
}
func GetFps() float64 {
return float64(time.Second) / float64(fpsInterval)
}
func clearCanvas() {
canvas := document.GetElementById("webzen")
context := canvas.GetContext("2d")
context.ClearRect(0, 0, canvas.Get("width").Float(), canvas.Get("height").Float())
}
func Init() {
canvas := document.GetElementById("webzen")
if canvas.IsNull() {
canvas = document.CreateCanvasElement()
canvas.Set("id", "canvas")
document.Body.AppendCanvasChild(canvas)
}
canvas.Set("width", document.DocumentElement.ClientWidth())
canvas.Set("height", document.DocumentElement.ClientHeight())
document.DocumentElement.Style.Set("overflow", "hidden")
document.Body.Style.Set("overflow", "hidden")
window.AddEventListener("resize", js.FuncOf(func(this js.Value, p []js.Value) interface{} {
canvas.Set("width", document.DocumentElement.ClientWidth())
canvas.Set("height", document.DocumentElement.ClientHeight())
return nil
}))
keys.SetupEventListeners()
}
func CustomInit(width, height int) {
canvas := document.GetElementById("webzen")
if canvas.IsNull() {
canvas = document.CreateCanvasElement()
canvas.Set("id", "canvas")
document.Body.AppendCanvasChild(canvas)
}
canvas.Set("width", width)
canvas.Set("height", height)
document.DocumentElement.Style.Set("overflow", "hidden")
document.Body.Style.Set("overflow", "hidden")
keys.SetupEventListeners()
}
func Update() {
time.Sleep(time.Millisecond * 16)
clearCanvas()
}
func GetWidth() int {
return document.DocumentElement.ClientIntWidth()
}
func GetHeight() int {
return document.DocumentElement.ClientIntHeight()
}