Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 1 addition & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Features:
- Strong minification/compression
- Prints minified bytes and approximate ETH deploy cost on each reload
- A robust pseudo-random number generator and hashing function (for `tokenData` seed)
- See [PRNG](#prng) for an alternative
- The included PRNG is suggested by [@piterpasma](https://twitter.com/piterpasma), and uses an "xorshift128" generator.
- Some utilities for color, math, random, and vector to get you started
- Additional function analysis, listing the byte size of the largest functions after minification
- Reports syntax errors in the browser
Expand Down Expand Up @@ -69,35 +69,6 @@ You can also `npm install [some-module]` and then `import { foo } from 'some-mod

See [./docs/tips.md](./docs/tips.md) for a series of suggestions on how to reduce bundle size.

## PRNG

The hash function and psuedo-random number generator included in `src/` is fairly strong, and may be overkill for most ArtBlocks scripts. It uses MurmurHash to turn the `tokenData` bytes into an initial state for a permuted congruential generator (PCG).

The following suggestion by [@piterpasma](https://twitter.com/piterpasma), which uses "xorshift128" generator, may be more than sufficient for most applications and can be compressed to a far smaller output:

```js
// hash from ArtBlocks scripts
const hash = /* string from tokenData */;

// state of the PRNG
const xs_state = Uint32Array.from([0,0,0,0].map((_,i)=>parseInt(hash.substr(i*8+2,8),16)))

const prng = () => {
/* Algorithm "xor128" from p. 5 of Marsaglia, "Xorshift RNGs" */
let s, t = xs_state[3];
xs_state[3] = xs_state[2];
xs_state[2] = xs_state[1];
xs_state[1] = s = xs_state[0];
t ^= t << 11;
t ^= t >>> 8;
xs_state[0] = t ^ s ^ (s >>> 19);
return xs_state[0] / 0x100000000;
};

// prints value in range 0..1
console.log(prng());
```

## License

MIT, see [LICENSE.md](http://github.com/mattdesl/tiny-artblocks/blob/master/LICENSE.md) for details.
21 changes: 8 additions & 13 deletions src/sketch.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
import * as random from "./util/random";
import CreateGenerator from "./util/random";
import { Lch } from "./util/color";

export default (hash) => {
const { range, pick, gaussian } = CreateGenerator(hash);
// You may want to remove this line for production
console.log(hash);

// set the shared PRNG to new seed
random.set_seed(hash);

const colors = [
Lch(50, 50, random.range(180, 360)),
Lch(100, 70, random.range(0, 180)),
];

const colors = [Lch(50, 50, range(180, 360)), Lch(100, 70, range(0, 180))];

const background = Lch(95, 0, 0);

const margin = 0.15;
const shapes = Array(450)
.fill()
.map(() => [
random.range(margin, 1 - margin),
random.range(margin, 1 - margin),
Math.abs(random.gaussian(0, 0.01)),
random.pick(colors),
range(margin, 1 - margin),
range(margin, 1 - margin),
Math.abs(gaussian(0, 0.01)),
pick(colors),
]);

const setContext = (context, color) => {
Expand Down
Loading