1 Built-In Functions
lgc edited this page 2024-08-15 01:51:49 +00:00

Data Functions

These are functions to access the data that empty.js provides.

Mouse Data

Mouse Position

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.


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)


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).


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

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

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.

c.fillRect(0, 5, 10, 15);

fillBackground();

Fills the entire canvas with the current color defined in c.fillStyle.


fillCircle(x, y, r);

Draws a filled circle centered at x, y with a radius of r pixels.


strokeCircle(x, y, r);

Draws a stroked circle centered at x, y with a radius of r pixels.


line(x1, y1, x2, y2);

Draws a line from x1, y2 to x2, y2.


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.


distance(x1, y1, x2, y2)
// returns number

Returns the distance between two points x1, y1 and x2, y2 calculated using Pythagorean theorem.


randomRange(min, max)
// returns number

Returns a random integer between and including min and max defined using Math.random(). Do not use for cryptography.


range(i)
// returns array of size i

Simmilar to Python's range() function. Returns an array with i elements. Each element contains its index.


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

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.

rgb(0, 255, 100)
// returns "rgb(0,255,100)"

This simply makes transforming numbers from JavaScript into CSS Color values a little more convenient.