Skip to content

Commit

Permalink
Publish v0.22.0
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jan 4, 2025
1 parent 6301841 commit 104126a
Show file tree
Hide file tree
Showing 9 changed files with 9 additions and 224 deletions.
68 changes: 0 additions & 68 deletions dist/api/time.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,72 +47,4 @@ export interface TimeProviders {
* @param frameOffset
*/
offline(frameDelay?: number, referenceFPS?: number, frameOffset?: number): TimeProvider;
/**
* Similar to {@link TimeProviders.raf}, but also collects FPS samples and
* injects a canvas overlay to visualize recent frame rates and compute
* moving averages. Visualization can be configured via provided options.
*
* @param opts
*/
debug(opts?: Partial<DebugTimeProviderOpts>): TimeProvider;
}
export interface DebugTimeProviderOpts {
/**
* @defaultValue 60
*/
targetFPS: number;
/**
* Window size (number of frames) of recorded FPS samples and to compute the moving average frame rate
*
* @defaultValue 200
*/
period: number;
/**
* Canvas width in pixels
*
* @defaultValue same as {@link DebugTimeProviderOpts.period}
*/
width: number;
/**
* Canvas width in pixels
*
* @defaultValue 100
*/
height: number;
/**
* Custom CSS to attach to canvas element.
*
* @remarks
* By default the canvas is positioned in the top-right corner.
*/
style: string;
/**
* Background color
*
* @defaultValue `#222`
*/
bg: string;
/**
* Tuple of color values for the area plot gradient, in the following order:
*
* - target framerate
* - target framerate - 1
* - half target framerate
* - zero
*
* @defaultValue `["#0f0", "#ff0", "#f00", "#300"]`
*/
fps: [string, string, string, string];
/**
* Text color
*
* @defaultValue `#fff`
*/
text: string;
/**
* If true, visualization uses area plot, else line plot.
*
* @defaultValue true
*/
fill: boolean;
}
149 changes: 1 addition & 148 deletions dist/genart.js
Original file line number Diff line number Diff line change
Expand Up @@ -641,152 +641,6 @@
};
};

// src/time/debug.ts
var deque = (samples, pred, index = []) => ({
head: () => samples[index[0]],
push(x) {
while (index.length && pred(samples[index[index.length - 1]], x)) {
index.pop();
}
index.push(samples.length - 1);
},
shift() {
if (index[0] === 0) index.shift();
for (let i = index.length; i-- > 0; ) index[i]--;
}
});
var debugTimeProvider = ({
targetFPS = 60,
period = 200,
width = period,
height = 100,
style = "position:fixed;z-index:9999;top:0;right:0;",
bg = "#222",
text: text3 = "#fff",
fps = ["#0f0", "#ff0", "#f00", "#306"],
fill = true
} = {}) => {
let canvas;
let ctx;
const scaleX = width / period;
const showTickLabels = width >= 120;
let t0 = performance.now();
let frame = 0;
let now = 0;
let prev = 0;
let samples = [];
let min;
let max;
let peak = targetFPS;
let windowSum = 0;
let isStart = true;
const update = () => {
const res = [now, frame];
let delta = now - prev;
prev = now;
if (delta <= 0) return res;
const $fps = 1e3 / delta;
let num = samples.push($fps);
min.push($fps);
max.push($fps);
if (num > period) {
num--;
windowSum -= samples.shift();
min.shift();
max.shift();
}
windowSum += $fps;
const { clamp01: clamp012, round: round2 } = $genart.math;
peak += (max.head() * 1.1 - peak) * 0.1;
const grad = ctx.createLinearGradient(0, 0, 0, canvas.height);
grad.addColorStop(clamp012(1 - targetFPS / peak), fps[0]);
grad.addColorStop(clamp012(1 - (targetFPS - 1) / peak), fps[1]);
grad.addColorStop(clamp012(1 - targetFPS / 2 / peak), fps[2]);
grad.addColorStop(1, fps[3]);
ctx.fillStyle = bg;
ctx.fillRect(0, 0, width, height);
ctx[fill ? "fillStyle" : "strokeStyle"] = grad;
ctx.setLineDash([]);
ctx.beginPath();
ctx.moveTo(-1, height);
for (let i = 0; i < num; i++) {
ctx.lineTo(i * scaleX, (1 - samples[i] / peak) * height);
}
if (fill) {
ctx.lineTo((num - 1) * scaleX, height);
ctx.closePath();
ctx.fill();
} else {
ctx.stroke();
}
ctx.fillStyle = ctx.strokeStyle = text3;
ctx.setLineDash([1, 1]);
ctx.beginPath();
for (let step = peak > 90 ? 30 : peak > 30 ? 15 : 5, i = round2(Math.min(targetFPS, peak + step / 2), step); i > 0; i -= step) {
const y = (1 - i / peak) * height;
ctx.moveTo(width - 80, y);
if (showTickLabels) {
ctx.lineTo(width - 22, y);
ctx.fillText(String(i), width - 20, y + 1);
} else {
ctx.lineTo(width, y);
}
}
ctx.stroke();
if (num >= period) {
[
[`sma(${period}):`, windowSum / period],
["max:", max.head()],
["min:", min.head()]
].forEach(([label, value], i) => {
const y = height - 8 - i * 12;
ctx.fillText(label, 4, y);
ctx.fillText(value.toFixed(1) + " fps", 64, y);
});
}
return res;
};
return {
start() {
samples = [];
min = deque(samples, (a, b) => a >= b);
max = deque(samples, (a, b) => a <= b);
peak = targetFPS * 1.2;
windowSum = 0;
if (!canvas) {
canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = height;
canvas.id = "#FPS";
canvas.setAttribute("style", style);
document.body.appendChild(canvas);
ctx = canvas.getContext("2d");
ctx.font = "12px sans-serif";
ctx.textBaseline = "middle";
ctx.strokeStyle = text3;
ctx.setLineDash([1, 1]);
}
},
next(fn) {
requestAnimationFrame((t) => {
if (isStart) {
t0 = t;
frame = 0;
isStart = false;
} else {
frame++;
}
now = t - t0;
fn(now, frame);
update();
});
},
now() {
return [now, frame];
}
};
};

// src/time/offline.ts
var timeProviderOffline = (frameDelay = 250, referenceFPS = 60, frameOffset = 0) => {
let frame = frameOffset;
Expand Down Expand Up @@ -872,7 +726,6 @@
prng = prng_exports;
utils = utils_exports;
time = {
debug: debugTimeProvider,
offline: timeProviderOffline,
raf: timeProviderRAF
};
Expand Down Expand Up @@ -910,7 +763,7 @@
});
}
get version() {
return "0.21.0";
return "0.22.0";
}
get id() {
return this._opts.id;
Expand Down
2 changes: 1 addition & 1 deletion dist/genart.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/adapter-editart/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@genart-api/adapter-editart",
"version": "0.21.0",
"version": "0.22.0",
"description": "GenArtAPI platform adapter for editart.xyz",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-fxhash/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@genart-api/adapter-fxhash",
"version": "0.21.0",
"version": "0.22.0",
"description": "GenArtAPI platform adapter for fxhash.xyz",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-layer/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@genart-api/adapter-layer",
"version": "0.21.0",
"version": "0.22.0",
"description": "GenArtAPI platform adapter for layer.com",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-urlparams/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@genart-api/adapter-urlparams",
"version": "0.21.0",
"version": "0.22.0",
"description": "GenArtAPI reference platform adapter, using URL search params for artwork parameter configuration & serialization",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@genart-api/core",
"version": "0.21.0",
"version": "0.22.0",
"description": "Platform-independent extensible API for browser-based generative art",
"repository": {
"type": "git",
Expand Down
4 changes: 2 additions & 2 deletions project-template/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"vite-plugin-html": "^3.2.2"
},
"dependencies": {
"@genart-api/core": "^0.21.0",
"@genart-api/adpater-urlparams": "^0.21.0"
"@genart-api/core": "^0.22.0",
"@genart-api/adpater-urlparams": "^0.22.0"
}
}

0 comments on commit 104126a

Please sign in to comment.