Skip to content

Commit

Permalink
Reformatted and ready for v0.0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkauzh committed Oct 7, 2023
1 parent cbb0471 commit 2d2c287
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 32 deletions.
4 changes: 1 addition & 3 deletions src/webzen/draw/pixel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
6 changes: 1 addition & 5 deletions src/webzen/draw/rect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
16 changes: 7 additions & 9 deletions src/webzen/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,32 @@ 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{} {
context.DrawImage(img, x, y, width, height)
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
Expand Down
3 changes: 0 additions & 3 deletions src/webzen/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
20 changes: 8 additions & 12 deletions src/webzen/shape/rect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 2d2c287

Please sign in to comment.