Skip to content

Commit

Permalink
Minor refactor for the C5 lib
Browse files Browse the repository at this point in the history
  • Loading branch information
kzmath committed Dec 31, 2024
1 parent d9f25c3 commit f583af6
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions lib/C5.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ class C5 {
c5.loaded = true;
});

// Load the app when the page is loaded
window.addEventListener("load", () => {
c5.load();
c5.loaded = true;
Expand Down Expand Up @@ -222,6 +223,7 @@ class C5 {
c5._draw();
}

// Not used -- don't need these events for now
setEvents() {
let $ = this;
let eventNames = [
Expand All @@ -232,8 +234,6 @@ class C5 {
];
for (let k of eventNames) {
let intern = "_" + k + "Fn";
// $[intern] = function() { };
// $[intern].isPlaceHolder = true;
if ($[k]) {
$[intern] = $[k];
} else {
Expand All @@ -246,34 +246,34 @@ class C5 {

_draw() {
this.ctx.clearRect(0, 0, this.width, this.height);
const now = performance.now();
this.frameTime = now - this.timeStamp;
this.timeStamp = now;
if (this.frameCounter % 59 == 0) {
this.debugTable["FPS"] = Math.round(1000 / this.frameTime);
}
this.debugTable["Mouse Position"] = String(this.mouseX) + ", " + String(this.mouseY)

requestAnimationFrame(() => { this._draw() });

// Get UI dots interactions
for (let i = 0; i < this.axes.length; i++) {
this.axes[i].doControlDots();
}

// Call user update function and log drawing time
const t1 = performance.now();
this._drawFn();
const dt = performance.now() - t1;

// Draw UI dots
for (let i = 0; i < this.axes.length; i++) {
this.axes[i].drawControlDots();
}

// Get the running maximum draw time in the last 180 frames
if (dt > this.maxDrawTime) {
this.lastMaxTime = this.frameCounter;
this.maxDrawTime = dt
// Generate debug info
const now = performance.now();
this.frameTime = now - this.timeStamp;
this.timeStamp = now;
if (this.frameCounter % 59 == 0) {
this.debugTable["FPS"] = Math.round(1000 / this.frameTime);
}
this.debugTable["Mouse Position"] = String(this.mouseX) + ", " + String(this.mouseY)

// Get the running maximum draw time in the last 180 frames
this.drawTimeArray[this.frameCounter % 180] = dt;
this.maxDrawTime = 0;
for (let i = 0; i < this.drawTimeArray.length; i++) {
Expand All @@ -295,6 +295,8 @@ class C5 {
// Input
// ========================================
//

// TODO: unify mouse and touch events
setUpInputs() {
this.canvas.onmousemove = (event) => {
this.pmouseX = this.mouseX;
Expand Down

0 comments on commit f583af6

Please sign in to comment.