Skip to content

Commit

Permalink
refactor(snippet): rename seed2 and add ts
Browse files Browse the repository at this point in the history
  • Loading branch information
vhf committed May 30, 2024
1 parent 6924f23 commit d8028a3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 23 deletions.
53 changes: 34 additions & 19 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,37 @@ async function pngExport({ resolution: { x: x, y: y } }) {

### API

Every minted will be passed `seed=` and `seed2=`. `seed` will be different for each token while `seed2` is the same for all tokens of a project.

| | | |
| ----------------------------------- | -------------------------------------------- | --------------------------------------------------------------------------------------------------------- |
| `$o.isCapture` | `bool` | `true` in the capture environment or when a `?capture` query parameter exists |
| `$o.seed` | `integer` | if `?seed=af628e00` is present the decimal integer from this hex string, otherwise a random integer |
| `$o.seed2` | `integer` | similar to `seed` but project-wide |
| `$o.capture()` | `() => Promise(void)` | triggers the capture by calling the default exporter with the provided resolution |
| `$o.rnd()` | `() => float` | returns a random number `0 <= n < 1` based on `$o.seed`, one can reset the PRNG state with `$o.rnd(null)` |
| `$o.rnd2()` | `() => float` | same as `rnd()` but based on `$o.seed2`, a project-wide seed (identical for all tokens of a same project) |
| `$o.registerFeatures(obj)` | `(obj) => obj` | registers features (key-values), one can unregister all featurs with `$o.registerFeatures()` |
| `$o.registerExport(args, exportFn)` | `(args, exportFn) => true` | registers an exporter for a mime and resolution |
| ` args.mime` | `string` | mime type |
| ` args.resolution` | `{ x: int, y: int }` | default resolution |
| ` args.aspectRatio` (optional) | `float` | if provided this exporter will be only export respecting this aspect ratio |
| ` args.default` (optional) | `bool` | if `true` this exporter will be used for the capture |
| ` args.thumb` (optional) | `bool` | if `true` this exporter will be used for the thumbnail |
| ` exportFn` | `({resolution: {x, y}}) => Promise(dataURL)` | an async function returning a dataURL |

Every minted token will be passed `seed=` and `seedGlobal=`. `seed` will be different for each token while `seedGlobal` is the same for all tokens of a project.

| | | |
| ----------------------------------- | -------------------------------------------- | -------------------------------------------------------------------------------------------------------------- |
| `$o.isCapture` | `bool` | `true` in the capture environment or when a `?capture` query parameter exists |
| `$o.seed` | `integer` | if `?seed=af628e00` is present the decimal integer from this hex string, otherwise a random integer |
| `$o.seedGlobal` | `integer` | similar to `seed` but project-wide |
| `$o.capture()` | `() => Promise(void)` | triggers the capture by calling the default exporter with the provided resolution |
| `$o.rnd()` | `() => float` | returns a random number `0 <= n < 1` based on `$o.seed`, one can reset the PRNG state with `$o.rnd(null)` |
| `$o.rndGlobal()` | `() => float` | same as `rnd()` but based on `$o.seedGlobal`, a project-wide seed (identical for all tokens of a same project) |
| `$o.registerFeatures(obj)` | `(obj) => obj` | registers features (key-values), one can unregister all featurs with `$o.registerFeatures()` |
| `$o.registerExport(args, exportFn)` | `(args, exportFn) => true` | registers an exporter for a mime and resolution |
| ` args.mime` | `string` | mime type |
| ` args.resolution` | `{ x: int, y: int }` | default resolution |
| ` args.aspectRatio` (optional) | `float` | if provided this exporter will be only export respecting this aspect ratio |
| ` args.default` (optional) | `bool` | if `true` this exporter will be used for the capture |
| ` args.thumb` (optional) | `bool` | if `true` this exporter will be used for the thumbnail |
| ` exportFn` | `({resolution: {x, y}}) => Promise(dataURL)` | an async function returning a dataURL |

### Environment

In the capture environment and on objkt.com the generator will get the following query parameters:

- `seed=hex`
The seed specific to this token. For instance: `af628e00`
- `seedGlobal=hex`
The seed for this project, similar to `seed` except it will be the same for all tokens of a project.
- `iteration=int`
The iteration number, randomized. Only available to projects that have a max mints.
For a project with max N tokens we'll define `iterations = shuffle(1..N)` and token `#M` will get `iterations[M]`.
- `ts=int`
The Unix time representing the datetime of the mint transaction. For instance: `1530374852` for `2018-06-30T16:07:32Z`

In the capture environment, `capture=true` is also passed.
6 changes: 3 additions & 3 deletions src/snippet.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ window.$o = {
registerExport,
registerFeatures,
seed: Math.floor(Math.random() * Date.now()) % 4294967296,
seed2: Math.floor(Math.random() * Date.now()) % 4294967296,
seedGlobal: Math.floor(Math.random() * Date.now()) % 4294967296,
};
['seed', 'seed2'].forEach((p) => {
['seed', 'seedGlobal'].forEach((p) => {
if (query.has(p)) {
$o[p] =
parseInt(
Expand All @@ -38,7 +38,7 @@ function splitmix32(a, p) {
};
}
$o.rnd = splitmix32($o.seed, 'seed');
$o.rnd2 = splitmix32($o.seed2, 'seed2');
$o.rndGlobal = splitmix32($o.seedGlobal, 'seedGlobal');

function registerFeatures(features) {
if (typeof features === 'undefined') {
Expand Down
2 changes: 1 addition & 1 deletion test/test.snippet.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('$o', () => {
beforeEach('reset + seed $o', $oReset);
[
['seed', 'rnd'],
['seed2', 'rnd2'],
['seedGlobal', 'rndGlobal'],
].forEach(([seed, rnd]) => {
describe(`#${rnd}()`, () => {
it('should be deterministic', () => {
Expand Down

0 comments on commit d8028a3

Please sign in to comment.