Skip to content

Commit 19e46aa

Browse files
committed
add new mouse events
1 parent e5c47a9 commit 19e46aa

1 file changed

Lines changed: 83 additions & 7 deletions

File tree

content/runal.md

Lines changed: 83 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -599,11 +599,11 @@ Listen to keyboard events.
599599
It's a root function, which means it should be placed at the same level as **setup()** and **draw()**.
600600
```js
601601
// mySketch.js
602-
setup(c) { ... }
602+
function setup(c) { ... }
603603

604-
draw(c) { ... }
604+
function draw(c) { ... }
605605

606-
onKey(c, event) {
606+
function onKey(c, event) {
607607
// save the current canvas in a png file
608608
// when the "c" key is pressed.
609609
if (event.key == "space") {
@@ -626,17 +626,17 @@ Key string representations for keys other than alphanumerical works as well. You
626626
627627
You can also use `ctrl+a`, `alt+b` etc...
628628
629-
#### onMouse(c, event) <sub>since v0.5.0</sub>
629+
#### onMouseClick(c, event) <sub>since v0.8.0</sub>
630630
Listen to mouse click events.
631631
632632
It's a root function, which means it should be placed at the same level as **setup()** and **draw()**.
633633
```js
634634
// mySketch.js
635-
setup(c) { ... }
635+
function setup(c) { ... }
636636

637-
draw(c) { ... }
637+
function draw(c) { ... }
638638

639-
onMouse(c, event) {
639+
function onMouseClick(c, event) {
640640
// draw a circle at mouse position when
641641
// the mouse left button is clicked.
642642
if (event.button == "left") {
@@ -649,4 +649,80 @@ Event is an object with the following attributes:
649649
- **y**: the vertical mouse position relative to the terminal window
650650
- **button**: the mouse button that has been clicked. Possible values: **left**, **middle**, **right**
651651
652+
#### onMouseRelease(c, event) <sub>since v0.8.0</sub>
653+
Listen to mouse release events.
654+
655+
It's a root function, which means it should be placed at the same level as **setup()** and **draw()**.
656+
```js
657+
// mySketch.js
658+
function setup(c) { ... }
659+
660+
function draw(c) { ... }
661+
662+
function onMouseRelease(c, event) {
663+
// draw a circle at mouse position when
664+
// the mouse left button is released.
665+
if (event.button == "left") {
666+
c.circle(event.x, event.y, 5);
667+
}
668+
}
669+
```
670+
Event is an object with the following attributes:
671+
- **x**: the horizontal mouse position relative to the terminal window
672+
- **y**: the vertical mouse position relative to the terminal window
673+
- **button**: the mouse button that has been clicked. Possible values: **left**, **middle**, **right**
674+
675+
#### onMouseWheel(c, event) <sub>since v0.8.0</sub>
676+
Listen to mouse wheel events.
677+
678+
It's a root function, which means it should be placed at the same level as **setup()** and **draw()**.
679+
```js
680+
// mySketch.js
681+
let x, y;
682+
function setup(c) {
683+
x = c.width/2;
684+
y = c.height/2;
685+
}
686+
687+
function draw(c) {
688+
c.circle(x, y, 5);
689+
}
690+
691+
function onMouseWheel(c, event) {
692+
// draw a circle at mouse position when
693+
// the mouse left button is released.
694+
if (event.button == "wheelup") {
695+
y = Math.max(0, y - 1);
696+
} else if (event.button == "wheeldown") {
697+
y = Math.min(c.height, y + 1);
698+
}
699+
}
700+
```
701+
Event is an object with the following attributes:
702+
- **x**: the horizontal mouse position relative to the terminal window
703+
- **y**: the vertical mouse position relative to the terminal window
704+
- **button**: the wheel motion. Possible values: **wheelup**, **wheeldown**
705+
706+
#### onMouseMove(c, event) <sub>since v0.8.0</sub>
707+
Listen to mouse moved events.
708+
709+
It's a root function, which means it should be placed at the same level as **setup()** and **draw()**.
710+
```js
711+
// mySketch.js
712+
let color = 0;
713+
function setup(c) {}
714+
function draw(c) {}
715+
716+
function onMouseMove(c, event) {
717+
color = color + 1;
718+
if (color > 255) {
719+
color = 0;
720+
}
721+
c.backgroundBg(color);
722+
}
723+
```
724+
Event is an object with the following attributes:
725+
- **x**: the horizontal mouse position relative to the terminal window
726+
- **y**: the vertical mouse position relative to the terminal window
727+
652728
</section>

0 commit comments

Comments
 (0)