diff --git a/src/webzen/draw/pixel.go b/src/webzen/draw/pixel.go index aae75fc..c893631 100644 --- a/src/webzen/draw/pixel.go +++ b/src/webzen/draw/pixel.go @@ -10,11 +10,9 @@ import ( func SetPixel(x, y float64, color [4]int) { canvas := document.GetElementById("canvas") - // Get the canvas context context := canvas.GetContext("2d") - // Draw the rectangle rgba := colors.GetRGBA(color) context.Set("fillStyle", rgba) - context.FillRect(x, y, 1, 1) // Set the rectangle dimensions as needed + context.FillRect(x, y, 1, 1) } diff --git a/src/webzen/draw/rect.go b/src/webzen/draw/rect.go index 5727820..f499b0c 100644 --- a/src/webzen/draw/rect.go +++ b/src/webzen/draw/rect.go @@ -10,21 +10,17 @@ import ( func DrawRect(width, height, x, y float64, color [4]int) { canvas := document.GetElementById("canvas") - // Get the canvas context context := canvas.GetContext("2d") - // Draw the rectangle rgba := colors.GetRGBA(color) context.Set("fillStyle", rgba) - context.FillRect(x, y, width, height) // Set the rectangle dimensions as needed + context.FillRect(x, y, width, height) } func FillBackground(color [4]int) { canvas := document.GetElementById("canvas") - // Get the canvas context context := canvas.GetContext("2d") - // Fill the canvas with the specified color rgba := colors.GetRGBA(color) context.Set("fillStyle", rgba) context.FillRect(0, 0, canvas.Get("width").Float(), canvas.Get("height").Float()) diff --git a/src/webzen/image/image.go b/src/webzen/image/image.go index b1bfbbc..99c7162 100644 --- a/src/webzen/image/image.go +++ b/src/webzen/image/image.go @@ -11,26 +11,25 @@ import ( ) type Image struct { - imagePath string - width float64 - height float64 - x float64 - y float64 + imagePath string + width float64 + height float64 + x float64 + y float64 } func LoadImage(imagePath string, width, height, x, y float64) Image { - return Image{imagePath, width, height, x, y} + return Image{imagePath, width, height, x, y} } func (i *Image) Draw() { - DrawImage(i.imagePath, i.width, i.height, i.x, i.y) + DrawImage(i.imagePath, i.width, i.height, i.x, i.y) } func DrawImage(imagePath string, width, height, x, y float64) { canvas := document.GetElementById("canvas") context := canvas.GetContext("2d") - // Load and draw the image img := document.CreateElement("img") img.Set("src", imagePath) img.AddEventListener("load", js.FuncOf(func(this js.Value, p []js.Value) interface{} { @@ -38,7 +37,6 @@ func DrawImage(imagePath string, width, height, x, y float64) { return nil })) - // Handle image loading errors img.AddEventListener("error", js.FuncOf(func(this js.Value, p []js.Value) interface{} { global.Alert("Failed to load image: " + imagePath) return nil diff --git a/src/webzen/init.go b/src/webzen/init.go index a99b0cc..40adff0 100644 --- a/src/webzen/init.go +++ b/src/webzen/init.go @@ -18,15 +18,12 @@ func Init() { document.Body.AppendCanvasChild(canvas) } - // Set the canvas size to match the viewport size canvas.Set("width", document.DocumentElement.ClientWidth()) canvas.Set("height", document.DocumentElement.ClientHeight()) - // Prevent scrolling on the HTML and body elements document.DocumentElement.Style.Set("overflow", "hidden") document.Body.Style.Set("overflow", "hidden") - // Listen for window resize events to adjust the canvas size 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()) diff --git a/src/webzen/shape/rect.go b/src/webzen/shape/rect.go index 1835fce..a2e54de 100644 --- a/src/webzen/shape/rect.go +++ b/src/webzen/shape/rect.go @@ -8,27 +8,23 @@ import ( "github.com/dimkauzh/webzen/src/backend/document" ) - type Rect struct { - x float64 - y float64 - width float64 - height float64 - color [4]int - + x float64 + y float64 + width float64 + height float64 + color [4]int } func NewRect(x, y, width, height float64, color [4]int) Rect { - return Rect{x, y, width, height, color} + return Rect{x, y, width, height, color} } func (r *Rect) Draw() { - canvas := document.GetElementById("canvas") - // Get the canvas context + canvas := document.GetElementById("canvas") context := canvas.GetContext("2d") - // Draw the rectangle rgba := colors.GetRGBA(r.color) context.Set("fillStyle", rgba) - context.FillRect(r.x, r.y, r.width, r.height) + context.FillRect(r.x, r.y, r.width, r.height) }