diff --git a/go.mod b/go.mod index e482d5c..d28c007 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,10 @@ module example.com/game go 1.22.3 -require github.com/hajimehoshi/ebiten/v2 v2.7.4 +require ( + github.com/hajimehoshi/ebiten/v2 v2.7.4 + marwan.io/wasm-fetch v0.1.0 +) require ( github.com/ebitengine/gomobile v0.0.0-20240518074828-e86332849895 // indirect diff --git a/go.sum b/go.sum index faed1b1..44f35de 100644 --- a/go.sum +++ b/go.sum @@ -14,3 +14,5 @@ golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +marwan.io/wasm-fetch v0.1.0 h1:0kKeCsry7IQfPmxj78FUWtO6qwXIaQqMmZbzbZz2rkw= +marwan.io/wasm-fetch v0.1.0/go.mod h1:PlaZmevjPJGIy/BaqMtVjUKvrA8CgrDh+aomnuhmTO8= diff --git a/main.go b/main.go index 158fc45..25bc4fc 100644 --- a/main.go +++ b/main.go @@ -1,16 +1,18 @@ package main import ( + "bytes" "fmt" "image/color" "io" - "net/http" "os" "path/filepath" "runtime" "github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2/ebitenutil" + + fetch "marwan.io/wasm-fetch" ) type game struct { @@ -48,12 +50,11 @@ func main() { func open(name string) (io.ReadCloser, error) { name = filepath.Clean(name) if runtime.GOOS == "js" { - // TODO: use more lightweight method such as marwan-at-work/wasm-fetch - resp, err := http.Get(name) + resp, err := fetch.Fetch(name, &fetch.Opts{}) if err != nil { return nil, err } - return resp.Body, nil + return io.NopCloser(bytes.NewReader(resp.Body)), nil } return os.Open(name)