sketchy

package module
v0.0.0-...-6797ab3 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2024 License: MIT Imports: 18 Imported by: 5

README

sketchy_logo_002

Sketchy is a framework for making generative art in Go. It is inspired by vsketch and openFrameworks. It uses canvas for drawing and the ebiten game engine for the GUI. It's designed to provide controls (sliders) via simple JSON that can be used within a familiar update() and draw() framework to enable quick iteration on designs.

The Getting Started guide is a good place to start, and even walk through creating a "Hello Circle" sketch from scratch.

Below are a couple of screenshots from the example sketches:

Fractal

fractal_example

Noise

Screenshot_20230318_123516

10PRINT

10print_example

Installation

Prerequisites

Sketchy requires Go version 1.17 or higher. It assumes that go is in the system path. If you are running Windows, install Windows Subsystem for Linux (WSL), so that you have bash, which is used by the install script.

Clone the repo

git clone https://github.com/aldernero/sketchy.git

Install sketchy environment

cd sketchy/scripts
./sketch_install.sh <target_directory>

This will create a directory target_directory, build the sketchy binary, and copy the binary and template files to the newly created directory.

Example:

❯ cd ~/sketchy/scripts
❯ ./sketchy_install.sh ~/sketchy_files
Sucessfully installed sketchy environment to /home/vernon/sketchy_files
❯ tree ~/sketchy_files
/home/vernon/sketchy_files
├── sketchy
└── template
    ├── main.go
    └── sketch.json

1 directory, 3 files

Sketchy is now installed and ready to run from target_directory.

Running the examples

For any of the examples in the examples directory, run using standard go commands:

❯ cd ~/sketchy/examples/lissajous
❯ go run main.go

Creating a new sketch

The syntax for creating a new sketch is sketchy init project_name. This will create a new directory with a configuration file and base sketch file:

❯ ./sketchy init mysketch
❯ tree mysketch
mysketch
├── go.mod
├── go.sum
├── main.go
└── sketch.json

Sketchy init's a go module and runs go mod tidy to get all of the go dependencies.

The next step are to configure sketch parameter and controls in sketch.json and add the drawing code to main.go. See the examples directory and documentation for more details.

Running a sketch

The syntax for running a sketch is sketchy run project_name. This is just a wrapper around running go run main.go from the project directory. Even the empty example above will run, althought you'll just see the 2 example controls and a blank drawing area.

Saving sketches and configurations

There are three builtin keyboard shortcuts for saving sketch images and configurations:

  • "s" key - saves the current frame as an SVG file. The filename has the format <prefix>_<timestamp>.svg, where <prefix> by default is the project name (what you used during sketchy init project_name)
  • "p" key - same as above but saves the current frame as a PNG image.
  • "c" key - saves the configuration (control values and sketch parameters) as JSON. The filename has the format <prefix>_config_<timestamp>.json, where <prefix> by default is the project name (what you used during sketchy init project_name)

"Expert" Mode

If you are already familiar with Go, you have probably realized you can just copy the template files into a new directory and run typical Go commands to build and run the sketch:

cp sketchy/template/* ~/my_sketch_dir/
cd ~/my_sketch_dir
go mod init sketch
go mod tidy

Make your file edits, then

go run main.go

Documentation

Index

Constants

View Source
const (
	SliderHeight              = 4.0
	SliderHPadding            = 2.0
	SliderVPadding            = 2.0
	SliderMouseWheelThreshold = 0.5
	SliderBackgroundColor     = "#1e1e1e"
	SliderOutlineColor        = "#ffdb00"
	SliderFillColor           = "#ffdb00"
	SliderTextColor           = "#ffffff"
	SliderGradientStart       = "cyan"
	SliderGradientEnd         = "magenta"
	ToggleHeight              = 5.5
	ToggleHPadding            = 3.0
	ToggleVPadding            = 2.0
	ToggleBackgroundColor     = "#1e1e1e"
	ToggleOutlineColor        = "#ffdb00"
	ToggleFillColor           = "#ffdb00"
	ToggleTextColor           = "#ffffff"
	ButtonHeight              = 5.5
	TextHeight                = 5.5
	FontSize                  = 10
)
View Source
const (
	DefaultTitle              = "Sketch"
	DefaultPrefix             = "sketch"
	DefaultBackgroundColor    = "#1e1e1e"
	DefaultOutlineColor       = "#ffdb00"
	DefaultSketchOutlineColor = ""
	ControlAreaMargin         = 1.0
	MmPerPx                   = 0.26458333
	DefaultDPI                = 96.0
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Sketch

type Sketch struct {
	Title                     string        `json:"Title"`
	Prefix                    string        `json:"Prefix"`
	SketchWidth               float64       `json:"SketchWidth"`
	SketchHeight              float64       `json:"SketchHeight"`
	ControlWidth              float64       `json:"ControlWidth"`
	ControlBackgroundColor    string        `json:"ControlBackgroundColor"`
	ControlOutlineColor       string        `json:"ControlOutlineColor"`
	SketchBackgroundColor     string        `json:"SketchBackgroundColor"`
	SketchOutlineColor        string        `json:"SketchOutlineColor"`
	DisableClearBetweenFrames bool          `json:"DisableClearBetweenFrames"`
	ShowFPS                   bool          `json:"ShowFPS"`
	RasterDPI                 float64       `json:"RasterDPI"`
	RandomSeed                int64         `json:"RandomSeed"`
	Sliders                   []Slider      `json:"Sliders"`
	Toggles                   []Toggle      `json:"Toggles"`
	Updater                   SketchUpdater `json:"-"`
	Drawer                    SketchDrawer  `json:"-"`
	DidControlsChange         bool          `json:"-"`
	Rand                      gaul.Rng      `json:"-"`

	ToggleControlMap map[string]int `json:"-"`

	Tick          int64              `json:"-"`
	ControlCanvas *canvas.Canvas     `json:"-"`
	SketchCanvas  *canvas.Canvas     `json:"-"`
	FontFamily    *canvas.FontFamily `json:"-"`
	FontFace      *canvas.FontFace   `json:"-"`
	// contains filtered or unexported fields
}

func NewSketchFromFile

func NewSketchFromFile(fname string) (*Sketch, error)

func (*Sketch) CanvasCoords

func (s *Sketch) CanvasCoords(x, y float64) gaul.Point

CanvasCoords converts window coordinates (pixels, upper left origin) to canvas coordinates (mm, lower left origin)

func (*Sketch) CanvasRect

func (s *Sketch) CanvasRect() gaul.Rect

func (*Sketch) Clear

func (s *Sketch) Clear()

func (*Sketch) Draw

func (s *Sketch) Draw(screen *ebiten.Image)

func (*Sketch) DrawControls

func (s *Sketch) DrawControls(ctx *canvas.Context)

func (*Sketch) DumpState

func (s *Sketch) DumpState()

func (*Sketch) FullWidth

func (s *Sketch) FullWidth() float64

func (*Sketch) Height

func (s *Sketch) Height() float64

func (*Sketch) Init

func (s *Sketch) Init()

func (*Sketch) Layout

func (s *Sketch) Layout(
	int,
	int,
) (int, int)

func (*Sketch) PlaceControls

func (s *Sketch) PlaceControls(_ float64, _ float64, ctx *canvas.Context)

func (*Sketch) PointInSketchArea

func (s *Sketch) PointInSketchArea(x, y float64) bool

PointInSketchArea calculates coordinates in pixels, useful when checkin if mouse clicks are in the sketch area

func (*Sketch) RandomHeight

func (s *Sketch) RandomHeight() float64

func (*Sketch) RandomWidth

func (s *Sketch) RandomWidth() float64

func (*Sketch) RandomizeSlider

func (s *Sketch) RandomizeSlider(name string)

func (*Sketch) RandomizeSliders

func (s *Sketch) RandomizeSliders()

func (*Sketch) SketchCoords

func (s *Sketch) SketchCoords(x, y float64) gaul.Point

SketchCoords converts canvas coordinates (mm, lower left origin) to sketch coordinates (pixels, upper left origin) this ignores the control area

func (*Sketch) Slider

func (s *Sketch) Slider(name string) float64

func (*Sketch) Toggle

func (s *Sketch) Toggle(name string) bool

func (*Sketch) Update

func (s *Sketch) Update() error

func (*Sketch) UpdateControls

func (s *Sketch) UpdateControls()

func (*Sketch) Width

func (s *Sketch) Width() float64

type SketchDrawer

type SketchDrawer func(s *Sketch, ctx *canvas.Context)

type SketchUpdater

type SketchUpdater func(s *Sketch)

type Slider

type Slider struct {
	Name               string     `json:"Name"`
	Pos                gaul.Point `json:"-"`
	Width              float64    `json:"Width"`
	Height             float64    `json:"Height"`
	MinVal             float64    `json:"MinVal"`
	MaxVal             float64    `json:"MaxVal"`
	Val                float64    `json:"Val"`
	Incr               float64    `json:"Incr"`
	OutlineColor       string     `json:"OutlineColor"`
	BackgroundColor    string     `json:"BackgroundColor"`
	FillColor          string     `json:"FillColor"`
	UseGradientFill    bool       `json:"UseGradientFill"`
	GradientStartColor string     `json:"GradientStartColor"`
	GradientEndColor   string     `json:"GradientEndColor"`
	TextColor          string     `json:"TextColor"`
	DrawRect           bool       `json:"DrawRect"`

	DidJustChange bool `json:"-"`
	// contains filtered or unexported fields
}

func (*Slider) CheckAndUpdate

func (s *Slider) CheckAndUpdate(ctx *canvas.Canvas) (bool, error)

func (*Slider) Draw

func (s *Slider) Draw(ctx *canvas.Context)

func (*Slider) GetPercentage

func (s *Slider) GetPercentage() float64

func (*Slider) GetRect

func (s *Slider) GetRect() gaul.Rect

func (*Slider) IsInside

func (s *Slider) IsInside(x float64, y float64) bool

func (*Slider) Randomize

func (s *Slider) Randomize()

func (*Slider) SetFont

func (s *Slider) SetFont(ff *canvas.FontFamily)

func (*Slider) StringVal

func (s *Slider) StringVal() string

func (*Slider) Update

func (s *Slider) Update(x float64)

type Toggle

type Toggle struct {
	Name            string     `json:"Name"`
	Pos             gaul.Point `json:"-"`
	Width           float64    `json:"Width"`
	Height          float64    `json:"Height"`
	Checked         bool       `json:"Checked"`
	IsButton        bool       `json:"IsButton"`
	OutlineColor    string     `json:"OutlineColor"`
	BackgroundColor string     `json:"BackgroundColor"`
	FillColor       string     `json:"FillColor"`
	TextColor       string     `json:"TextColor"`

	DidJustChange bool `json:"-"`
	// contains filtered or unexported fields
}

func (*Toggle) CheckAndUpdate

func (t *Toggle) CheckAndUpdate(ctx *canvas.Canvas) (bool, error)

func (*Toggle) Draw

func (t *Toggle) Draw(ctx *canvas.Context)

func (*Toggle) GetRect

func (t *Toggle) GetRect() gaul.Rect

func (*Toggle) IsInside

func (t *Toggle) IsInside(x float64, y float64) bool

func (*Toggle) SetFont

func (t *Toggle) SetFont(ff *canvas.FontFamily)

func (*Toggle) Update

func (t *Toggle) Update()

Jump to

Keyboard shortcuts

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