Skip to content

BrownNPC/Raylib-Go-Wasm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

76 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Raylib Go (golang) bindings for the web.

Important

Please open an issue if something is unsupported

Motivation

The Go bindings for raylib had not been able to compile to web for a decade, so I did it myself :D

Contributors

Special thanks to @kerudev for RayGUI binding help, and @genriot for rlgl and image bindings help

Table Of Contents

What doesnt work?

  • Unsure, open an issue please.

Getting started

1. Download this repository.

Either use git clone or click the green "Code" button and download it in your project folder.

2. Inside your go.mod file put this code:

replace (
	github.com/gen2brain/raylib-go/raygui => ./Raylib-Go-Wasm/raygui
	github.com/gen2brain/raylib-go/raylib => ./Raylib-Go-Wasm/raylib
	github.com/BrownNPC/Raylib-Go-Wasm/wasm-runtime => ./Raylib-Go-Wasm/wasm-runtime
)

and then run go mod tidy

3. Porting code to web

There is only one change you need to make!

In your file go to where you have defined

for !rl.WindowShouldClose(){

and replace that with

var update = func(){

after the function definition add the line

rl.SetMainLoop(update)
for !rl.WindowShouldClose(){
  update()
}

Look at this example if you dont understand.

Loading assets

See here

Making it compatible with desktop again

Comment the line

rl.SetMainLoop(update)

and in your go.mod comment the line

replace github.com/gen2brain/raylib-go/raylib => ./Raylib-Go-Wasm/raylib

Running

1. Compile your code to a .wasm file.

For MacOS and Linux:

GOOS=js GOARCH=wasm go build -o ./Raylib-Go-Wasm/index/main.wasm .

For Windows Powershell:

$env:GOOS="js"; $env:GOARCH="wasm"; go build -o ./Raylib-Go-Wasm/index/main.wasm .

2. Copy Golang wasm runtime (only needs to be copied once)

For All platforms:

  cp "$(go env GOROOT)/lib/wasm/wasm_exec.js" ./Raylib-Go-Wasm/index/wasm_exec.js

3. Compile the server (only needs to be compiled once)

go build ./Raylib-Go-Wasm/server/server.go

4. Run the code with:

./server

or if you're using Windows

./server.exe

this will serve your app on http://localhost:8080

You dont need to restart the server after you recompile.

Tips

I recommend you run the server in a seperate terminal, so you can recompile your code easier.

You can also setup air to automatically recompile your code when a file changes

firstly make sure air is installed and then make a file called .air.toml next to your main.go (dont forget the dot in .air.toml)

put this in the file and simply run the air command while in the same directory as the .air.toml file

[build]
  cmd = "GOOS=js GOARCH=wasm go build -o ./Raylib-Go-Wasm/index/main.wasm ."
  bin = ""                     
  include_ext = ["go"]
  exclude_dir = ["vendor"]
  delay = 100
[log]
  time = false

Publish to Github Pages

copy the contents of the folder Raylib-Go-Wasm/index into your github pages repository

About

raylib-go bindings for the web

Resources

Stars

59 stars

Watchers

2 watching

Forks

Packages

 
 
 

Contributors

Languages