- Notifications
You must be signed in to change notification settings - Fork1
Processing sketchs with Go, using p5Js as core
License
NotificationsYou must be signed in to change notification settings
bregydoc/PGoJs
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
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.
Get with:
go get -u -v github.com/bregydoc/PGoJs
PGoJs need togopherJs builder for work, you make sure you have it.
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><htmllang="en"><head><metacharset="UTF-8"><title>PGoJs</title></head><body><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.7/p5.js"></script><scriptsrc="sketch.js"></script></body></html>
You need import the PGoJs library, I recommended use this with different namespace (like "p")
package mainimport (p"github.com/bregydoc/PGoJs/Processing")funcsetup() {p.CreateCanvas(600,600)p.Background(230)}funcdraw() {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)}funcmain() {p.Setup=setupp.Draw=drawp.LaunchApp()}
First, make sure you havegopherJs, 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.
Go idiomatic?
package mainimport (p"github.com/bregydoc/PGoJs/Processing")typeBallstruct {diameterfloat64position*p.PVectorvelocity*p.PVector}funcnewBall(x,yfloat64,diameterfloat64)*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)}varballs []*Ballfuncsetup() {p.CreateCanvas(600,600)p.Background(230)balls=append(balls,newBall(200,200,50))}funcdraw() {p.Background(230)for_,ball:=rangeballs {ball.updateLogic()ball.drawBall()}}//Create mousePressed function and linked with p.MousePressedfuncmousePressed() {balls=append(balls,newBall(200,200,50))}funcmain() {p.Setup=setupp.Draw=drawp.MousePressed=mousePressedp.LaunchApp()}