a lightweight javascript utility library
==|| How to use ||==
attach https://ut1l.pages.dev/util.js to your html file using a script tag.
// [] = optional, || = can be a different value, will be explained in comment.
1. randomInt([min], max) // random integer between min and max. if min is not provided, it will generate a random number from 0 to max.
2. randomFloat([min], max, [decimalPlaces]) // random float between min and max, with optional decimalPlaces rounding. if min is not provided, it will generate a random number from 0 to max.
3. random() // same as Math.random()
4. randomCall(func, min, max) // call func() at random intervals between min and max
5. s(selector, [num]) // smart document.querySelector(), if num is passed, returns the nth element.
6. qsall(selector, [func]) // smart document.querySelectorAll(), if func is passed, runs func on each element. you can have the optional parameters on that function which are the element and index.
7. gebid(id) // get element by id
8. makeElement(type, [innerHTML], [parent], [attributes], [classList], [id]) // create an element with optional parameters.
type: type of element, for example div.
innerHTML: innerHTML of the element
parent: parent element of the element, if none, set to null or undefined
attributes: array of attributes, formatted as [{key: value}, {key: value}]
classList: array of classes, formatted as ["class1", "class2"]
id: id of the element
returns the created element
9. hide(element) // changes an element's display to none
10. show(element, display) // changes an element's display to the display param
11. |element|.css(config) // changes an element's css, formatted as {"property": "value", "property": "value"}. |element| is any element on the page. for example s(".example").css({});
12. |element|.listen(listener, callback) // adds an event listener to an element. |element| is any element on the page. for example s(".example").listen();
13. rgbToHex(r, g, b) // converts rgb colors to hex codes, returns the value
14. hexToRgb(hex) // converts hex colors to rgb colors, returns the value
15. randomColor(hex) // generates a random color, returns the value. if hex is set to true, returns the hex code instead of rgb.
16. detectCollision(element1, element2) // if the two elements provided are touching, then it will return true, otherwise it will return false.
17. mouse // mouse object
mouse.x // x coordinate
mouse.y // y coordinate
mouse.down // true if mouse is down
mouse.up // true if mouse is up
18. keys // keys object
keys.|code| // true if |code| is down. |code| is the KeyboardEvent.code of the key you want.
Any other functions that start with "util" are private and should NOT be used.