1.0.1 • Published 2 years ago
@nexty-org/core v1.0.1
Nexty core
These are pieces of code that I will collect here (and create a lib) in the course of my training/development, which I will use in other projects in the future (but this is not accurate).
NPM install
npm install @nexty-org/core
The import ways
// Way 1
import { Camera, EventManager } from "@nexty-org/core";
// Way 2
import * as NEXTY from "@nexty-org/core";
const rectangle = new NEXTY.Rectangle();
const line = new NEXTY.Line();
The some modules from this lib
Event Manager
// instead of
btn.addEventListener("click", (e) => {/*...some code*/});
window.addEventListener("mousemove", (e) => {/*...some code*/});
// do this
const eventManager = new EventManager(["click", "mousemove"]);
eventManager.addCallerToElem(btn, "click", (e) => {/*...some code*/});
eventManager.addCallerToElem(window, "mousemove", (e) => {/*...some code*/});
// What`s difference?
// If you want to keep all the events of your page to the store and somehow manage them, i think, this is are good solution.
// P.S. Only those events that are passed to the arguments during inizialization of the event manager, will be processed.
Mouse Drag
// This is a custom event that implements the mouse dragging event for any elem, but actually, i created it specially for the canvas.
const mouseDragEvent = new MouseDrag();
mouseDragEvent.addToElem(canvas);
canvas.addEventListener("mousedrag", (e) => {
console.log(
e.detail.startX, // drag start x
e.detail.startY, // drag start y
e.detail.offsetX, // drag offset from last drag x
e.detail.offsetY, // drag offset from last drag y
e.detail.offsetFromStartX, // drag offset from start x
e.detail.offsetFromStartY // drag offset from start y
);
});
Sat
// This class implements a Separating Axis Theorem for rectangles.
const result = SAT.rectsIsCollide(rect1, rect2);