|
| 1 | +This guide covers installation of sketchy and creating your first sketch. |
| 2 | + |
| 3 | +# Installation |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | +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. |
| 7 | + |
| 8 | +## Clone the repo |
| 9 | + |
| 10 | +```shell |
| 11 | +git clone https://github.com/aldernero/sketchy.git |
| 12 | +``` |
| 13 | +## Install sketchy environment |
| 14 | +```shell |
| 15 | +cd sketchy/scripts |
| 16 | +./sketch_install.sh <target_directory> |
| 17 | +``` |
| 18 | +This will create a directory `target_directory`, build the sketchy binary, and copy the binary and template files to the newly created directory. |
| 19 | + |
| 20 | +Example: |
| 21 | + |
| 22 | +```bash |
| 23 | +❯ cd ~/sketchy/scripts |
| 24 | +❯ ./sketchy_install.sh ~/sketchy_files |
| 25 | +Sucessfully installed sketchy environment to /home/vernon/sketchy_files |
| 26 | +❯ tree ~/sketchy_files |
| 27 | +/home/vernon/sketchy_files |
| 28 | +├── sketchy |
| 29 | +└── template |
| 30 | + ├── main.go |
| 31 | + └── sketch.json |
| 32 | + |
| 33 | +1 directory, 3 files |
| 34 | +``` |
| 35 | +Sketchy is now installed and ready to run from `target_directory`. |
| 36 | + |
| 37 | +## Running the examples |
| 38 | +For any of the examples in the `examples` directory, run using standard go commands: |
| 39 | +```shell |
| 40 | +❯ cd ~/sketchy/examples/lissajous |
| 41 | +❯ go run main.go |
| 42 | +``` |
| 43 | + |
| 44 | +# Creating a new sketch |
| 45 | + |
| 46 | +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: |
| 47 | +```shell |
| 48 | +❯ ./sketchy init mysketch |
| 49 | +❯ tree mysketch |
| 50 | +mysketch |
| 51 | +├── go.mod |
| 52 | +├── go.sum |
| 53 | +├── main.go |
| 54 | +└── sketch.json |
| 55 | +``` |
| 56 | +Sketchy init's a go module and runs `go mod tidy` to get all of the go dependencies. |
| 57 | + |
| 58 | +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. |
| 59 | + |
| 60 | +# Example: creating a "Hello Circle" sketch |
| 61 | +Rather than a typical "Hello World!" program, let's create something graphical that illustrates how to use controls and draw in the sketch area. |
| 62 | + |
| 63 | +Create a new sketch called `hello_circle` |
| 64 | +```shell |
| 65 | +❯ ./sketchy init hello_circle |
| 66 | +❯ cd hello_circle |
| 67 | +❯ ls |
| 68 | +go.mod go.sum main.go sketch.json |
| 69 | +``` |
| 70 | +So far this is identical to the previous section. Let's look at the contents of `sketch.json`: |
| 71 | +```json |
| 72 | +❯ cat sketch.json |
| 73 | +{ |
| 74 | + "SketchWidth": 800, |
| 75 | + "SketchHeight": 800, |
| 76 | + "ControlWidth": 240, |
| 77 | + "Controls": [ |
| 78 | + { |
| 79 | + "Name": "control1", |
| 80 | + "MinVal": 1, |
| 81 | + "MaxVal": 100, |
| 82 | + "Val": 10, |
| 83 | + "Incr": 1 |
| 84 | + }, |
| 85 | + { |
| 86 | + "Name": "control2", |
| 87 | + "MinVal": 0, |
| 88 | + "MaxVal": 2, |
| 89 | + "Val": 0.9, |
| 90 | + "Incr": 0.01 |
| 91 | + } |
| 92 | + ] |
| 93 | +} |
| 94 | +``` |
| 95 | +This is the default configuration with 2 example controls. The first 3 lines define the sketch area size (800 x 800 pixels), and the the control area width (240 pixels). The "Controls" section lists the controls that will appear as sliders in the sketch. Let's make them more meaningful. The first one will represent the radius of a circle we draw in the sketch area. The second one will represent the line width of the circle. Change the values to the following: |
| 96 | +```json |
| 97 | +❯ cat sketch.json |
| 98 | +{ |
| 99 | + "SketchWidth": 800, |
| 100 | + "SketchHeight": 800, |
| 101 | + "ControlWidth": 240, |
| 102 | + "Controls": [ |
| 103 | + { |
| 104 | + "Name": "radius", |
| 105 | + "MinVal": 1, |
| 106 | + "MaxVal": 350, |
| 107 | + "Val": 200, |
| 108 | + "Incr": 1 |
| 109 | + }, |
| 110 | + { |
| 111 | + "Name": "thickness", |
| 112 | + "MinVal": 0.5, |
| 113 | + "MaxVal": 10, |
| 114 | + "Val": 1, |
| 115 | + "Incr": 0.5 |
| 116 | + } |
| 117 | + ] |
| 118 | +} |
| 119 | +``` |
| 120 | +Notice that the radius can vary from 1 to 350 pixels and the line thickness can vary from 0.5 to 10 pixels. |
| 121 | + |
| 122 | +Run the sketch to see the controls in action: |
| 123 | +You can run `sketchy run hello_circle` from sketchy's base directory, or if you are inside the project directory, you can use go directly: |
| 124 | +```shell |
| 125 | +go run main.go |
| 126 | +``` |
| 127 | +You should see 2 sliders in the control area on the left. You can change the values by clicking or dragging within the slider bar area. You can also use the mouse wheel to increment and decrement the value. The sketch area is blank at the moment, let's change that! |
| 128 | + |
| 129 | +Close the sketch and open `main.go` in an editor. There are two functions `update` and `draw` where you implement the drawing. For a simple case like this we don't need `update`, we can do everything in the `draw` function. |
| 130 | +```go |
| 131 | +func update(s *sketchy.Sketch) { |
| 132 | + // Update logic goes here |
| 133 | +} |
| 134 | + |
| 135 | +func draw(s *sketchy.Sketch, c *gg.Context) { |
| 136 | + // Drawing code goes here |
| 137 | +} |
| 138 | +``` |
| 139 | +Notice that the `draw` function takes two arguments. The first argument stores the Sketch struct used to store our sketch information, including the two slider controls. Here is how you get the value from a slider: |
| 140 | + |
| 141 | +```go |
| 142 | +val := s.Var("slider name") |
| 143 | +``` |
| 144 | + |
| 145 | +The value will be a float64. For our case we could define two variables that are tied to the controls we defined earlier: |
| 146 | +```go |
| 147 | +radius := s.Var("radius") |
| 148 | +thickness := s.Var("thickness") |
| 149 | +``` |
| 150 | +Notice the argument to `Var` is the same name we used in `sketch.json`. |
| 151 | + |
| 152 | +The other argument to `draw` is a `gg` drawing context. See the [gg](https://github.com/fogleman/gg) documentation for full details. For this example we will simply 1) set a drawing color, 2) set the line thickness, 3) define the circle object, and 4) draw the circle. Here is the entire draw function: |
| 153 | +```go |
| 154 | +func draw(s *sketchy.Sketch, c *gg.Context) { |
| 155 | + // Drawing code goes here |
| 156 | + radius := s.Var("radius") |
| 157 | + thickness := s.Var("thickness") |
| 158 | + c.SetColor(color.White) |
| 159 | + c.SetLineWidth(thickness) |
| 160 | + c.DrawCircle(s.SketchWidth/2, s.SketchHeight/2, radius) |
| 161 | + c.Stroke() |
| 162 | +} |
| 163 | +``` |
| 164 | +The `DrawCircle` gg function takes 3 arguments: an x and y positions for the center of the circle and a radius. We can reference the `SketchWidth` and `SketchHeight` arguments directly on the sketch struct. Halving these values places the circle at the center of the drawing area. |
| 165 | + |
| 166 | +Run the sketch again, and you should see a white circle in the sketch area, and you should be able to vary the radius and thickness with the sliders. Congratulations, you made your first sketch! |
| 167 | + |
| 168 | +# Saving sketches and configurations |
| 169 | + |
| 170 | +There are two builtin keyboard shortcuts for saving sketch images and configurations: |
| 171 | +- "s" key - saves the current frame as a PNG. The filename has the format `<prefix>_<timestamp>.png`, where `<prefix>` by default is the project name (what you used during `sketchy init project_name`) |
| 172 | +- "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`) |
0 commit comments