processing

package module
v0.0.0-...-853b2b0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 2, 2017 License: MIT Imports: 2 Imported by: 0

README

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CENTER string
View Source
var CHORD string
View Source
var Draw func()
View Source
var HALF_PI float64

Constants in p5Js: (In go are only variables)

View Source
var Height int
View Source
var LEFT string
View Source
var MouseButton string
View Source
var MouseClicked func()
View Source
var MouseDragged func()
View Source
var MouseIsPressed bool
View Source
var MouseMoved func()
View Source
var MousePressed func()
View Source
var MouseReleased func()
View Source
var MouseWheel func(event Event)
View Source
var MouseX int

Cast of different variables

View Source
var MouseY int
View Source
var OPEN string //Is a String

Modes for any functions:

View Source
var PmouseX int
View Source
var PmouseY int
View Source
var PwinMouseX int
View Source
var PwinMouseY int
View Source
var QUARTER_PI float64
View Source
var RIGHT string
View Source
var Setup func()
View Source
var TWO_PI float64
View Source
var Width int
View Source
var WinMouseX int
View Source
var WinMouseY int

Functions

func Alpha

func Alpha(color *js.Object) int

func Arc

func Arc(a, b, c, d, start, stop interface{}, mode string)

2D PRIMITIVES:

func Background

func Background(values ...interface{})

func Blue

func Blue(color *js.Object) int

func Brightness

func Brightness(color *js.Object) int

func Clear

func Clear()

func Color

func Color(params ...interface{}) *js.Object

func ColorMode

func ColorMode(mode string, maxValues ...int)

func CreateCanvas

func CreateCanvas(width, height int)

func Ellipse

func Ellipse(x, y, w interface{}, h ...interface{})

func Fill

func Fill(firstValue interface{}, extraValues ...float64)

func Green

func Green(color *js.Object) int

func Hue

func Hue(color *js.Object) int

func LaunchApp

func LaunchApp()

func LerpColor

func LerpColor(from interface{}, to interface{}, amt float64) *js.Object

func Lightness

func Lightness(color *js.Object) int

func Line

func Line(x1, y1, x2, y2 interface{})

func NoFill

func NoFill()

func NoStroke

func NoStroke()

func Point

func Point(x, y interface{})

func Quad

func Quad(x1, y1, x2, y2, x3, y3, x4, y4 interface{})

func Rect

func Rect(x, y, w, h interface{}, extraParameters ...interface{})

func Red

func Red(color *js.Object) int

func Saturation

func Saturation(color *js.Object) int

func Stroke

func Stroke(firstValue interface{}, extraValues ...int)

func StrokeWeight

func StrokeWeight(weight int)

func Triangle

func Triangle(x1, y1, x2, y2, x3, y3 interface{})

Types

type Event

type Event struct {
	// contains filtered or unexported fields
}

func (*Event) Delta

func (event *Event) Delta() float64

type PVector

type PVector struct {
	X float64
	Y float64
	Z float64
	// contains filtered or unexported fields
}

func CreateVector

func CreateVector(cord ...float64) *PVector

func Random2D

func Random2D() *PVector

func Random3D

func Random3D() *PVector

func (*PVector) Add

func (v *PVector) Add(x interface{}, other ...interface{}) *PVector

func (*PVector) Copy

func (v *PVector) Copy() *PVector

func (*PVector) Cross

func (v *PVector) Cross(v2 *PVector) *PVector

func (*PVector) Dist

func (v *PVector) Dist(v2 *PVector) float64

func (*PVector) Div

func (v *PVector) Div(val float64) *PVector

func (*PVector) Dot

func (v *PVector) Dot(v2 *PVector) float64

func (*PVector) Mag

func (v *PVector) Mag() float64

func (*PVector) MagSq

func (v *PVector) MagSq() float64

func (*PVector) Mult

func (v *PVector) Mult(val float64) *PVector

func (*PVector) Normalize

func (v *PVector) Normalize() *PVector

func (*PVector) Set

func (v *PVector) Set(cord ...float64) *PVector

func (*PVector) Sub

func (v *PVector) Sub(x interface{}, other ...interface{}) *PVector

func (*PVector) ToString

func (v *PVector) ToString() string

Directories

Path Synopsis
constAndVarHeader.go
constAndVarHeader.go

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL