Skip to content

Commit

Permalink
feat: register features
Browse files Browse the repository at this point in the history
  • Loading branch information
vhf committed Mar 28, 2024
1 parent 3a2ac26 commit 9a91225
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
30 changes: 27 additions & 3 deletions generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,20 @@ function draw(width = window.innerWidth, height = window.innerHeight) {
ctx.fillRect(0, 0, width, height);
ctx.save();

const ellipseWidth = ($objkt.seed % 4) + 3;
const ellipseHeight = ($objkt.seed % 6) + 3;

$objkt.registerFeatures({
background: colorFeatures(colors.bgFill),
object: colorFeatures(colors.ellipseFill),
});

ctx.ellipse(
width / 2,
height / 2,
width / (($objkt.seed % 4) + 3),
width / (($objkt.seed % 6) + 3),
Math.PI / (($objkt.seed % 3) + 1),
width / ellipseWidth,
height / ellipseHeight,
Math.PI / (($objkt.seed % 2) + 1) / 2,
0,
2 * Math.PI
);
Expand All @@ -46,6 +54,22 @@ function draw(width = window.innerWidth, height = window.innerHeight) {
$objkt.capture();
}

function colorFeatures(color) {
const rgb = parseInt(color, 16);
const r = (rgb >> 16) & 0xff;
const g = (rgb >> 8) & 0xff;
const b = (rgb >> 0) & 0xff;
const lum = 0.2126 * r + 0.7152 * g + 0.0722 * b;

if (lum >= 80 && lum <= 100) {
return 'shade';
}
if (lum < 40) {
return 'dark';
}
return 'light';
}

function exportCanvas(mime) {
return async ({ resolution: { x: x, y: y } }) => {
draw(x, y);
Expand Down
18 changes: 14 additions & 4 deletions snippet.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,29 @@
const query = new URLSearchParams(window.location.search);

const $objkt = {
version: '0.0.1',
registerExport,
_exports: {},
_exported: null,
_v: '0.0.1',
capture,
isCapture: query.has('capture'),
registerExport,
registerFeatures,
seed: Math.floor(Math.random() * Number.MAX_SAFE_INTEGER),
capture,
};
window.$objkt = $objkt;
if (query.has('seed')) {
$objkt.seed = parseInt(query.get('seed'), 16) % Number.MAX_SAFE_INTEGER;
}

function registerFeatures(features) {
if (typeof features === 'undefined') {
return ($objkt.features = null);
}
if (typeof features !== 'object' || Array.isArray(features)) {
throw new Error('registerFeatures expects an object');
}
return ($objkt.features = features);
}

function registerExport(args, fn) {
const err = new Error(`Cannot register exporter for ${JSON.stringify(args)}`);
if (typeof fn !== 'function') throw err;
Expand Down

0 comments on commit 9a91225

Please sign in to comment.