/PGoJs

Processing sketchs with Go, using p5Js as core

Primary LanguageGoMIT LicenseMIT

PGoJs

Processing with Go, using p5Js as core

PGoJs is a binding/port from p5Js using gopherJs, the idea is create sketchs in web using Golang but easy and fast like processing framework.

Install

Get with:

 go get -u -v github.com/bregydoc/PGoJs

How to use

PGoJs need to gopherJs builder for work, you make sure you have it.

Create a index.html file

You need a HTML file where you will embedded the js generated by PGo Js using gopherjs, your html index file has look like this:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>PGoJs</title>

</head>
<body>
    <script  src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.7/p5.js"></script>
    <script src="sketch.js"></script>
</body>
</html>

Write your sketch in Go

You need import the PGoJs library, I recommended use this with different namespace (like "p")

package main

import (
	p "github.com/bregydoc/PGoJs/Processing"
)

func setup() {
	p.CreateCanvas(600, 600)
	p.Background(230)
}

func draw() {
	p.StrokeWeight(4)
	if !p.MouseIsPressed {
		p.NoStroke()
	} else {
		p.Stroke("rgba(139,195,74 ,1)")
	}
	p.Line(p.PmouseX, p.PmouseY, p.MouseX, p.MouseY)
}

func main() {
	p.Setup = setup
	p.Draw = draw

	p.LaunchApp()
}

Build your .go file to .js file using gopherJs

First, make sure you have gopherJs, if you don't have, install with:

go get -u github.com/gopherjs/gopherjs

For build the sketch.js file only execute this line:

gopherjs build nameOfSketch.go -o sketch.js

Or:

export GOPATH=$HOME/goWork #The path of your GOPATH
$GOPATH/bin/gopherjs build nameOfSketch.go -o sketch.js

I usually create an .sh file with these parameters, and build in only one step.

The last step is open your HTML File with your web navigator

Sketch writing in golang

Extra example

Go idiomatic?

package main

import (
	p "github.com/bregydoc/PGoJs/Processing"
)

type Ball struct {
	diameter float64
	position *p.PVector
	velocity *p.PVector
}

func newBall(x, y float64, diameter float64) *Ball{
	return &Ball{diameter: diameter, position:p.CreateVector(x,y), velocity:p.Random2D().Mult(10)}
}

func (ball *Ball) updateLogic() {
	if ((ball.position.X + ball.diameter/2) > float64(p.Width)) || ((ball.position.X - ball.diameter/2) < 0){
		ball.velocity = p.CreateVector(ball.velocity.X * -1, ball.velocity.Y)
	}
	if ((ball.position.Y + ball.diameter/2) > float64(p.Height)) || ((ball.position.Y - ball.diameter/2) < 0){
		ball.velocity = p.CreateVector(ball.velocity.X, ball.velocity.Y* -1)
	}

	ball.position.Add(ball.velocity)

}

func (ball *Ball) drawBall() {
	p.NoStroke()
	p.Fill("rgba(139,195,74 ,1)")
	p.Ellipse(ball.position.X, ball.position.Y, ball.diameter, ball.diameter)
}


var balls []*Ball

func setup() {
	p.CreateCanvas(600, 600)
	p.Background(230)
	balls = append(balls, newBall(200, 200, 50))

}

func draw() {
	p.Background(230)
	for _, ball := range balls {
		ball.updateLogic()
		ball.drawBall()
	}
}

//Create mousePressed function and linked with p.MousePressed
func mousePressed() {
	balls = append(balls, newBall(200, 200, 50))
}



func main() {
	p.Setup = setup
	p.Draw = draw
	p.MousePressed = mousePressed

	p.LaunchApp()
}

Balls in gif