-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrote the raw api to the backend and inserted the new api into webzen
- Loading branch information
Showing
9 changed files
with
146 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package document | ||
|
||
import "syscall/js" | ||
|
||
type canvas struct { | ||
canvas js.Value | ||
} | ||
|
||
type context struct { | ||
context js.Value | ||
} | ||
|
||
func (c *canvas) GetContext(context_str string) context { | ||
return context{c.canvas.Call("getContext", context_str)} | ||
} | ||
|
||
func (c *canvas) Get(key string) js.Value { | ||
return c.canvas.Get(key) | ||
} | ||
|
||
func (c *canvas) Set(key string, value interface{}) { | ||
c.canvas.Set(key, value) | ||
} | ||
|
||
func (c *canvas) IsNull() bool { | ||
return c.canvas.IsNull() | ||
} | ||
|
||
func (c *context) Set(key string, value interface{}) { | ||
c.context.Set(key, value) | ||
} | ||
|
||
func (c *context) FillText(text string, x, y float64) { | ||
c.context.Call("fillText", text, x, y) | ||
} | ||
|
||
func (c *context) FillRect(x, y, width, height float64) { | ||
c.context.Call("fillRect", x, y, width, height) | ||
} | ||
|
||
func (c *context) DrawImage(img js.Value, x, y, width, height float64) { | ||
c.context.Call("drawImage", img, x, y, width, height) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package global | ||
|
||
import "syscall/js" | ||
|
||
func Alert(text string) { | ||
js.Global().Call("alert", text) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package window | ||
|
||
import "syscall/js" | ||
|
||
func AddEventListener(ev string, fn js.Func) { | ||
js.Global().Get("window").Call("addEventListener", ev, fn) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,26 @@ | ||
package image | ||
|
||
import "syscall/js" | ||
import ( | ||
"syscall/js" | ||
"webzen/src/backend/document" | ||
"webzen/src/backend/global" | ||
) | ||
|
||
func DrawImage(imagePath string, width, height, x, y float64) { | ||
canvas := js.Global().Get("document").Call("getElementById", "canvas") | ||
context := canvas.Call("getContext", "2d") | ||
canvas := document.GetElementById("canvas") | ||
context := canvas.GetContext("2d") | ||
|
||
// Load and draw the image | ||
img := js.Global().Get("document").Call("createElement", "img") | ||
img := document.CreateElement("img") | ||
img.Set("src", imagePath) | ||
img.Call("addEventListener", "load", js.FuncOf(func(this js.Value, p []js.Value) interface{} { | ||
context.Call("drawImage", img, x, y, width, height) | ||
img.AddEventListener("load", js.FuncOf(func(this js.Value, p []js.Value) interface{} { | ||
context.DrawImage(img, x, y, width, height) | ||
return nil | ||
})) | ||
|
||
// Handle image loading errors | ||
img.Call("addEventListener", "error", js.FuncOf(func(this js.Value, p []js.Value) interface{} { | ||
js.Global().Call("alert", "Failed to load image") | ||
img.AddEventListener("error", js.FuncOf(func(this js.Value, p []js.Value) interface{} { | ||
global.Alert("Failed to load image: " + imagePath) | ||
return nil | ||
})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,31 @@ | ||
package webzen | ||
|
||
import "syscall/js" | ||
import ( | ||
"syscall/js" | ||
"webzen/src/backend/document" | ||
"webzen/src/backend/window" | ||
) | ||
|
||
func Init() { | ||
var canvas js.Value | ||
// Create a canvas element if it doesn't exist | ||
canvas = js.Global().Get("document").Call("getElementById", "canvas") | ||
canvas := document.GetElementById("canvas") | ||
if canvas.IsNull() { | ||
canvas = js.Global().Get("document").Call("createElement", "canvas") | ||
canvas = document.CreateCanvasElement() | ||
canvas.Set("id", "canvas") | ||
js.Global().Get("document").Get("body").Call("appendChild", canvas) | ||
document.Body.AppendCanvasChild(canvas) | ||
} | ||
|
||
// Set the canvas size to match the viewport size | ||
canvas.Set("width", js.Global().Get("document").Get("documentElement").Get("clientWidth")) | ||
canvas.Set("height", js.Global().Get("document").Get("documentElement").Get("clientHeight")) | ||
canvas.Set("width", document.DocumentElement.ClientWidth()) | ||
canvas.Set("height", document.DocumentElement.ClientHeight()) | ||
|
||
// Prevent scrolling on the HTML and body elements | ||
js.Global().Get("document").Get("documentElement").Get("style").Set("overflow", "hidden") | ||
js.Global().Get("document").Get("body").Get("style").Set("overflow", "hidden") | ||
document.DocumentElement.Style.Set("overflow", "hidden") | ||
document.Body.Style.Set("overflow", "hidden") | ||
|
||
// Listen for window resize events to adjust the canvas size | ||
js.Global().Get("window").Call("addEventListener", "resize", js.FuncOf(func(this js.Value, p []js.Value) interface{} { | ||
canvas.Set("width", js.Global().Get("document").Get("documentElement").Get("clientWidth")) | ||
canvas.Set("height", js.Global().Get("document").Get("documentElement").Get("clientHeight")) | ||
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 | ||
})) | ||
} |