Adding mouseX() and mouseY() to arrays

This commit is contained in:
2023-10-24 19:38:43 +02:00
parent 2ae11874c9
commit 9ffdca6c69
2 changed files with 120 additions and 84 deletions

View File

@ -8,6 +8,8 @@ declare global {
sub(amount: number): number[];
mult(amount: number): number[];
div(amount: number): number[];
mouseX(): T[],
mouseY(): T[],
palindrome(): T[];
random(index: number): T;
rand(index: number): T;
@ -34,6 +36,28 @@ declare global {
}
export const makeArrayExtensions = (api: UserAPI) => {
Array.prototype.mouseX = function <T>(this: T[]): T {
/**
* @returns Value from list based on mouse X position
*/
const mouse_position = api.mouseX();
const screenWidth = window.innerWidth;
const zoneWidth = screenWidth / this.length;
const zoneIndex = Math.floor(mouse_position / zoneWidth);
return this[zoneIndex];
};
Array.prototype.mouseY = function <T>(this: T[]): T {
const mouse_position = api.mouseY();
const screenHeight = window.innerHeight;
const zoneHeight = screenHeight / this.length;
const zoneIndex = Math.floor(mouse_position / zoneHeight);
return this[zoneIndex];
};
Array.prototype.square = function(): number[] {
/**
* @returns New array with squared values.

View File

@ -73,6 +73,18 @@ beat([1, 0.5].beat()) :: sound(['bass3'].bar())
`
)}
- <ic>mouseX()</ic> / <ic>mouseY()</ic>: divides the screen in <ic>n</ic> zones and returns the value corresponding to the mouse position on screen.</ic>
${makeExample(
"Controlling an arpeggio (octave and note) with mouse", `
beat(0.25)::sound('wt_piano')
.note([0,2,3,4,5,7,8,9,11,12].scale(
'minor', 30 + [0,12,24].mouseY()).mouseX())
.room(0.5).size(4).lpad(-2, .2).lpf(500, 0.3)
.ad(0, .2).out()
`, true
)}
- <ic>palindrome()</ic>: Concatenates a list with the same list in reverse.
${makeExample(