fragment → Documentation → API → Triggers
Mouse triggers are called when the event happens on the <canvas>
used for your sketch.
- Type:
(listener(event:MouseEvent): function) => void
Register a click
listener on the <canvas>
.
- Type:
(listener(event:MouseEvent): function) => void
Register a mousedown
listener on the <canvas>
.
- Type:
(listener(event:MouseEvent): function) => void
Register a mouseup
listener on the <canvas>
.
- Type:
(listener(event:MouseEvent): function) => void
Register a mousemove
listener on the <canvas>
.
Keyboard triggers are called when the event happens on window
.
A key
argument can be optionnaly passed to call the listener only when a specific key is typed. If a single function is provided, the listener will be called for every key typed. The key
argument can also be an array, in this case it will be called for every key typed in the array.
⚠ The
key
argument is case sensitive, soonKeyPress('a', () =>)
andonKeyPress('A', () =>)
is not the same thing
import { onKeyPress } from "@fragment/triggers";
export let init = () => {
onKeyPress(() => {
console.log("called for every keypress");
});
onKeyPress('a', () => {
console.log("called when 'a' is pressed");
});
onKeyPress('A', () => {
console.log("called when 'A' is pressed");
});
onKeyPress(['a', 'A'], () => {
console.log("called when 'a' or 'A' is pressed");
});
};
- Type:
(key?: (string|string[]), listener(event:KeyboardEvent): function) => void
Register a keypress
listener for key
on window
.
- Type:
(key?: (string|string[]), listener(event:KeyboardEvent): function) => void
Register a keydown
listener for key
on window
.
- Type:
(key?: (string|string[]), listener(event:KeyboardEvent): function) => void
Register a keyup
listener for key
on window
.
MIDI triggers are called when using a MIDI device after authorizing usage of the Web MIDI API.
- Type:
(note?:(string|string[]), listener(event:MIDIEvent): function) => void
Register a listener called when note
is played on. Notes: ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"]
.
- Type:
(note?:(string|string[]), listener(event:MIDIEvent): function) => void
Register a listener called when note
is played off. Notes: ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"]
.
- Type:
(noteNumber?:(number|number[], listener(event:MIDIEvent): function) => void
Register a listener called when noteNumber
is played on.
- Type:
(noteNumber?:(number|number[], listener(event:MIDIEvent): function) => void
Register a listener called when noteNumber
is played off.
- Type:
(control?:(number|number[]), listener(event:MIDIEvent): function) => void
Register a listener called when control
changes.