Add Built-In Functions

Luca Conte 2024-08-15 01:51:49 +00:00
parent ea1189a21b
commit f62daa5f58
1 changed files with 168 additions and 0 deletions

168
Built-In Functions.-.md Normal file

@ -0,0 +1,168 @@
# Data Functions
These are functions to access the data that `empty.js` provides.
## Mouse Data
### Mouse Position
```js
getMousePosGlobal()
// returns {x : 0, y : 0}
```
Returns the "global" mouse pointer position. This position is not related to the canvas size or position. It only shows the position of the mouse inside the browser window.
---
```js
getMousePos()
// returns {x : 0, y : 0}
```
Returns the mouse pointer position relative to the position of the canvas (`{x : 0, y : 0}` would be top left)
---
```js
getLastMousePos()
// returns {x : 0, y : 0}
```
Returns the mouse pointer position relative to the position of the canvas during the last frame (if called from the `tick()` function, it will return the value `getMousePos()` would have returned in the last cycle).
---
```js
getRelativeMouseMovement()
// returns {x : 0, y : 0}
```
Returns the pixels the mouse moved since the last frame. Useful for calculating mouse movement speed for example.
### Mouse Buttons
```js
getMouseKey(key);
// returns boolean
```
Returns the state of the selected mouse key or button, `true` meaning button is currently pressed.
```
key = 0 : Left Mouse Button
key = 1 : Middle Mouse Button
key = 2 : Right Mouse Button
```
## Keyboard Data
```js
getKey(code)
// returns boolean
```
Returns the state of a key from it's code (i.e. `keyA`, `Space` or `ShiftLeft`), `true` meaning the key is currently pressed.
# Drawing Functions
These are functions that are supposed to make drawing on the canvas a bit easier.
All default JavaScript Canvas API functions are available by referencing the Canvas Context using `c`, i.e.
```js
c.fillRect(0, 5, 10, 15);
```
---
```js
fillBackground();
```
Fills the entire canvas with the current color defined in `c.fillStyle`.
---
```js
fillCircle(x, y, r);
```
Draws a filled circle centered at `x, y` with a radius of `r` pixels.
---
```js
strokeCircle(x, y, r);
```
Draws a stroked circle centered at `x, y` with a radius of `r` pixels.
---
```js
line(x1, y1, x2, y2);
```
Draws a line from `x1, y2` to `x2, y2`.
---
```js
drawRotatedImage(image, x, y, angle, width, height);
```
Draws a given image centered at `x, y` with a defined width and height, rotated by `angle` radiants around it's center.
# Other
Functions that don't necessarily have anything to do with `empty.js` but are simply useful shortcuts for common use cases.
---
```js
distance(x1, y1, x2, y2)
// returns number
```
Returns the distance between two points `x1, y1` and `x2, y2` calculated using Pythagorean theorem.
---
```js
randomRange(min, max)
// returns number
```
Returns a random integer between and including `min` and `max` defined using `Math.random()`. Do not use for cryptography.
---
```js
range(i)
// returns array of size i
```
Simmilar to Python's `range()` function. Returns an array with `i` elements. Each element contains its index.
---
```js
minmax(num, min, max)
// returns number
```
If `max` if `num` is greater than `max` and `min` if `num` is less than `min`, otherwise returns `num`.
---
## CSS Color Code Shortcuts
```js
rgb(r, g, b)
rgba(r, g, b, a)
hsl(h, s, l)
hsla(h, s, l, a)
// each return strings
```
These functions return the given values inside a String according to CSS Standard, i.e.
```js
rgb(0, 255, 100)
// returns "rgb(0,255,100)"
```
This simply makes transforming numbers from JavaScript into CSS Color values a little more convenient.