Un module NodeJS incluant plusieurs fonctions réutilisables pour des projets divers, est également utilisable en navigateur
Cloner ou télécharger le répertoire
Pour NodeJS : Utilisez require('ToolboxModule')
Pour navigateur : Mettre <script src="toolbox-min.js"></script> en premier dans <head>
- calcSpeedFromDelta
- calcDistance
- calcAngle
- collisionX
- collisionY
- collision
- constrain
- range
- trackKeys
- random
- randomFloat
- randomNormal
- removeFromArray
- generateArray
- delay
- map
- loadImage
- isImage
- loadAudio
- isAudio
- getCurrentDateTime
- sha256
- MD5
- Fonctions console
- Fonctions Maths
- ask
- $
- cleanup
- parseUrl
- class Rectangle
- defineControlledProperty
- arrayEqual
- colourNameToHex
- sum
- mean
- stddev
- rangeArray
- round
- encodeHTML
- decodeHTML
class Player {
//...
move(ms){
//...
const tempVelX = calcSpeedFromDelta(ms, this.speedX);
this.x += tempVelX;
//...
}
//...
}
class Missile {
//...
move(ms){
//...
if(calcDistance(this, player) < 50){
//Change animation
}
//...
}
//...
}
class Missile {
//...
draw(ctx){
//...
const ang = calcAngle(this, player);
ctx.rotate(ang);
//...
}
//...
}
class Player {
//...
constructor(x, y, w, h){
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
//...
move(ms){
//...
// Do movement
if(collisionX(this, BORDER.LEFT)){
//Cancel movement
}
//...
}
//...
}
class Player {
//...
constructor(x, y, w, h){
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
//...
move(ms){
//...
// Do movement
if(collisionY(this, BORDER.BOTTOM)){
//Cancel movement
}
//...
}
//...
}
class Player {
//...
constructor(x, y, w, h){
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
//...
move(ms){
//...
// Do movement
if(collisionX(this, BORDERS)){
//Cancel movement
}
//...
}
//...
}
vY = constrain(40, -20, 30);
vY //? => 30
vY = constrain(-57, -20, 30);
vY //? => -20
const t = range(5);
t //? => [0, 1, 2, 3, 4]
// binding key events
const keyInput = trackKeys(document, [
//Moving sideways
"ArrowLeft", "KeyA",
"ArrowRight", "KeyD",
//Jumping
"ArrowUp", "KeyW"
]);
//...
//If key ArrowUp is pressed
if(keyInput.ArrowUp){
//Do jumping...
}
if(keyInput.ArrowLeft){
//Do move left...
}
if(keyInput.ArrowRight){
//Do move right...
}
Donne un nombre rond aléatoire entre min et max, si on donne un tableau renvoie un élément aléatoire du tableau
const n = random(50, 100);
const k = random(20);
n //? => un chiffre entre [50 et 100[
k //? => un chiffre entre [0 et 20[
const n = randomFloat(50.0, 100.0);
const k = randomFloat(20.0);
n //? => un chiffre dans [50.0 et 100.0[
k //? => un chiffre entre [0.0 et 20.0[
const n = randomNormal(50.0, 100.0);
n //? => nombre aléatoire normal
let tab = [ /* elements... */ ];
//...
tab[i] //? => 3;
//...
removeFromArray(tab, 3);
const tab = generateArray(10, 20);
tab.length //? => 10
tab[0].length //? => 20
//...
tab[7][15] = 5;
//...
delay(2000)
.then(_ => {
//Do stuffs...
})
.catch(err => {
error(`Error while in delay ! ${err}`);
});
//...
(async function(){
await delay(2000);
//Do stuffs...
})();
const valMap = map(25, 0, 100, 50, 60);
valMap //? => 52.5
loadImage(player, './image/player.png');
//...
//Si chargé
player.img //? => Image Object
//...
const sprite_brick = loadImage('./image/brick.png');
//...
if(isImage(link)){
//do image related stuff
}
const music = loadAudio('./music/background.mp3', { autoplay: true, loop: true, volume: 0.3 });
const fire_sound = loadAudio('./music/jumping.ogg', { volume: 0.2, loop: true });
const jump_sound = loadAudio('./music/jumping.ogg', { volume: 0.5 });
if(isAudio(link)){
//do audio related stuff
}
const oTime = getCurrentDateTime();
oTime //? => "2019-04-08 17:31:47"
const msg = 'password';
const sha = [sha256, sha384, sha512];
Promise.all(sha.map(o => o(msg))).then(log);
/*
[
"5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8",
"a8b64babd0aca91a59bdbb7761b421d4f2bb38280d3a75ba0f21f2bebc45583d446c598660c94ce680c47d19c30783a7",
"b109f3bbbc244eb82441917ed06d618b9008dd09b3befd1b5e07394c706a8bb980b1d7785e5976ec049b46df5f1326af5a2ea6d103fd07c95385ffab0cacbc86"
]
*/
const hashMd = MD5('password');
hashMd //? => 5f4dcc3b5aa765d61d8327deb882cf99
// Wrapper de console.log
log("Some amazing text !");
error("Something wrong...");
max([ 8, 3, 1, 3, 0 ]); //? => 8
min([ 8, 3, 1, 3, 0 ]); //? => 0
max(0, 5, 2); //? => 5
min(0, 5, 2); //? => 0
pow(2, 3); //? => 8
floor(2.635); //? => 2
ceil(2.443); //? => 3
abs(-23); //? => 23
sqrt(9); //? => 3
const opt = {
link: "localhost"
name: "vues"
value: "joker"
responseType: "json"
requestType: "get"
};
//opt => (XHR req) localhost/?vues=joker
try {
const res = await ask(opt);
res //? => JSON formatted String
//Do things...
} catch(err){
error("Can't ask !", err);
}
const h1 = $("h1#myH1")[0];
h1.css('color', 'red');
h1.html("<div>Hello there !</div>");
h1.attr("data", 'val1');
h1.click(_ => {
alert("H1 has been clicked !");
});
//...
h1.hide();
//...
h1.show();
const { log, map, cleanup } = require("../ToolboxModule");
//..
cleanup(_ => {
//Saving files...
log("Closing !");
});
const inf = parseUrl('localhost/test?vues=joker&help=batman');
inf //? => { link: 'localhost', dir: 'test', params: { vues: 'joker', help: 'batman', length: 2 }}
Objet pour intialiser un rectangle qui est dessinable dans un canvas et si un image est attaché, dessine l'image
const brick = new Rectangle(40, 30, 50, 50, 'green');
//...
//ctx = canvas.getContext('2d');
brick.draw(ctx);
//...
const player = new Rectangle(0, 0, 50, 100, 'yellow');
loadImage(player, "./images/player.png");
// OR
player.loadImage("./images/player.png");
//...
//Si l'image est chargé, dessine player.png
player.draw(ctx);
Wrapper de Object.defineProperty(objet, nom_propriete, { get: valeur_initial, set: check_function })
class Player(){
constructor(x, y){
defineControlledProperty(this, 'x', x, nx => {
if(isNaN(nx) || nx < 0) throw 'Incorrect X !';
});
defineControlledProperty(this, 'y', y, ny => {
if(isNaN(ny) || ny < 0) throw 'Incorrect Y !';
});
}
//...
}
const player = new Player(-20, 30);
//? => error ! : Incorrect X !
const t1 = [1,5,3];
const t2 = [1,2,3];
arrayEqual(t1, t2) //? => false
arrayEqual(t1, [1,5,3]) //? => true
arrayEqual(t2, [1,2,3]) //? => true
const hex = colourNameToHex('aquamarine');
hex //? => '#7fffd4'
//...
const hex = colourNameToHex('blda');
hex //? => false
const tab = [ 2, 1, 1, 3, 7, 4, 1, 8, 0, 0 ];
const total = sum(tab);
total //? => 27
const tab = [ 2, 1, 1, 3, 7, 4, 1, 8, 0, 0 ];
const mu = mean(tab);
mu //? => 2.7
const tab = [ 2, 1, 1, 3, 7, 4, 1, 8, 0, 0 ];
const sd = stddev(tab);
sd //? => 7.21
const tab = [ 2, 1, 1, 3, 7, 4, 1, 8, 0, 0 ];
const interval = rangeArray(tab);
interval //? => [ 0, 8 ]
round(Math.PI, 2) //? => 3.14
round(Math.PI, 4) //? => 3.1416
const str = "<a href='localhost:8080?query=dataset&page=3' id=\"useful_link\">Link</a>";
const res = encodeHTML(str);
str //? => "<a href='localhost:8080?query=dataset&page=3' id="useful_link">Link</a>"
const str = "<a href='localhost:8080?query=dataset&page=3' id="useful_link">Link</a>";
const res = decodeHTML(str);
str //? => "<a href='localhost:8080?query=dataset&page=3' id=\"useful_link\">Link</a>"
Copyright (c) 2019-present, SAUNDERS Pierre