Skip to content

Commit 68a645e

Browse files
author
Xavier Godart
committed
update runal events doc
1 parent 7095aa8 commit 68a645e

2 files changed

Lines changed: 64 additions & 8 deletions

File tree

content/runal.md

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ function setup(c) {}
102102
function draw(c) {}
103103
```
104104

105+
You can add extra methods `onKey` and `onMouse` to catch keyboard and mouse events:
106+
```js
107+
function onKey(c, e) {}
108+
function onMouse(c, e) {}
109+
````
110+
105111
And you can then execute the file with:
106112
```sh
107113
./runal -f sketch.js
@@ -133,8 +139,17 @@ func main() {
133139
func setup(c *runal.Canvas) {}
134140
135141
func draw(c *runal.Canvas) {}
142+
```
143+
144+
You can add extra methods `onKey` and `onMouse` to catch keyboard and mouse events:
145+
```go
146+
func onKey(c *runal.Canvas, e runal.KeyEvent) {}
147+
func onMouse(c *runal.Canvas, e runal.MouseEvent) {}
148+
````
136149
137-
func onKey(c *runal.Canvas, key string) {}
150+
And you can then execute the file with:
151+
```sh
152+
go run sketch.go
138153
```
139154

140155
## Examples
@@ -222,6 +237,12 @@ Number of frames rendered since the beginning.
222237
#### c.isLooping
223238
Boolean indicating whether the render loop is active.
224239

240+
#### c.mouseX <sub>since v0.5.0</sub>
241+
Returns the horizontal mouse position relative to the terminal window.
242+
243+
#### c.mouseY <sub>since v0.5.0</sub>
244+
Returns the vertical mouse position relative to the terminal window.
245+
225246
<hr class="separator"/>
226247

227248
### Canvas
@@ -474,13 +495,9 @@ The **console.log** file is deleted upon exit.
474495

475496
### Events
476497

477-
#### onKey(c, event)
498+
#### onKey(c, event) <sub>since v0.5.0</sub>
478499
Listen to keyboard events.
479500

480-
Event is an object with 2 attributes:
481-
- **key**: the string representation of the key pressed (`a`, `b`, `c`, `1`, `2`, `3` etc...)
482-
- **code**: the numerical value of the key pressed (97, 98, 99, 49, 50, 51 etc...)
483-
484501
It's a root function, which means it should be placed at the same level as **setup()** and **draw()**.
485502
```js
486503
// mySketch.js
@@ -494,7 +511,44 @@ onKey(c, event) {
494511
if (event.key == "space") {
495512
saveCanvasToPNG("canvas.png");
496513
}
514+
515+
// same using the key code.
516+
if (event.code == 32) {
517+
saveCanvasToPNG("canvas.png");
518+
}
519+
}
520+
```
521+
522+
Event is an object with the following attributes:
523+
- **key**: the string representation of the key pressed (`a`, `b`, `c`, `1`, `2`, `3` etc...)
524+
- **code**: the numerical value of the key pressed (97, 98, 99, 49, 50, 51 etc...)
525+
526+
Key string representations for keys other than alphanumerical works as well. You can use the following strings to handle all your keyboard keys:
527+
`enter`, `tab`, `backspace`, `esc`, `space`, `up`, `down`, `left`, `right`, `begin`, `find`, `insert`, `delete`, `select`, `pgup`, `pgdown`, `home`, `end`, `kpenter`, `kpequal`, `kpmul`, `kpplus`, `kpcomma`, `kpminus`, `kpperiod`, `kpdiv`, `kp0`, `kp1`, `kp2`, `kp3`, `kp4`, `kp5`, `kp6`, `kp7`, `kp8`, `kp9`.
528+
529+
You can also use `ctrl+a`, `alt+b` etc...
530+
531+
#### onMouse(c, event) <sub>since v0.5.0</sub>
532+
Listen to mouse click events.
533+
534+
It's a root function, which means it should be placed at the same level as **setup()** and **draw()**.
535+
```js
536+
// mySketch.js
537+
setup(c) { ... }
538+
539+
draw(c) { ... }
540+
541+
onMouse(c, event) {
542+
// draw a circle at mouse position when
543+
// the mouse left button is clicked.
544+
if (event.button == "left") {
545+
c.circle(event.x, event.y, 5);
546+
}
497547
}
498548
```
549+
Event is an object with the following attributes:
550+
- **x**: the horizontal mouse position relative to the terminal window
551+
- **y**: the vertical mouse position relative to the terminal window
552+
- **button**: the mouse button that has been clicked. Possible values: **left**, **middle**, **right**
499553

500554
</section>

sass/main.scss

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ h4 {
8686
article code {
8787
border: 1px solid #000;
8888
border-radius: 4px;
89-
padding: 1px 4px;
89+
padding: 0px 4px;
9090
}
9191

9292
table td,
@@ -341,8 +341,10 @@ article {
341341
h4 sub {
342342
color: #333333;
343343
margin-left: 12px;
344-
vertical-align: baseline;
344+
vertical-align: text-bottom;
345+
font-size: 10px;
345346
font-style: italic;
347+
font-weight: lighter;
346348
}
347349

348350
h4 + p,

0 commit comments

Comments
 (0)