primitive 1.0.1 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/module.js DELETED
@@ -1,126 +0,0 @@
1
- // node es module entrypoint
2
-
3
- import ow from 'ow'
4
- import path from 'path'
5
- import rmfr from 'rmfr'
6
- import tempy from 'tempy'
7
- import Time from 'time-diff'
8
-
9
- import context from './lib/context'
10
- import primitive from './lib/primitive'
11
-
12
- const supportedOutputFormats = new Set([
13
- 'png',
14
- 'jpg',
15
- 'svg',
16
- 'gif'
17
- ])
18
-
19
- /**
20
- * Reproduces the given input image using geometric primitives.
21
- *
22
- * Returns a Promise for the generated model.
23
- *
24
- * Available output formats:
25
- * - png
26
- * - jpg
27
- * - svg
28
- * - gif
29
- *
30
- * @name primitive
31
- * @function
32
- *
33
- * @param {Object} opts - Configuration options
34
- * @param {string} opts.input - Input image to process (can be a local path, http url, or data url)
35
- * @param {string} [opts.output] - Path to generate output image
36
- * @param {number} [opts.numSteps=200] - Number of steps to process [1, 1000]
37
- * @param {number} [opts.minEnergy] - Minimum energy to stop processing early [0, 1]
38
- * @param {number} [opts.shapeAlpha=128] - Alpha opacity of shapes [0, 255]
39
- * @param {string} [opts.shapeType=traingle] - Type of shapes to use
40
- * @param {number} [opts.numCandidates=1] - Number of top-level candidates per step [1, 32]
41
- * @param {number} [opts.numCandidateShapes=50] - Number of random candidate shapes per step [10, 1000]
42
- * @param {number} [opts.numCandidateMutations=100] - Number of candidate mutations per step [10, 500]
43
- * @param {number} [opts.numCandidateExtras=0] - Number of extra candidate shapes per step [0, 16]
44
- * @param {function} [opts.onStep] - Optional async function taking in the model and step index
45
- * @param {function} [opts.log=noop] - Optional logging function (console.log to enable logging)
46
- *
47
- * @return {Promise}
48
- */
49
- export default async (opts) => {
50
- const {
51
- input,
52
- output,
53
- onStep,
54
- numSteps = 200,
55
- nthFrame = 0,
56
- ...rest
57
- } = opts
58
-
59
- ow(opts, ow.object.label('opts'))
60
- ow(input, ow.string.nonEmpty.label('input'))
61
- ow(nthFrame, ow.number.integer)
62
- ow(numSteps, ow.number.integer.positive.label('numSteps'))
63
- if (output) ow(output, ow.string.nonEmpty.label('output'))
64
-
65
- const ext = output && path.extname(output).slice(1).toLowerCase()
66
- const isGIF = (ext === 'gif')
67
-
68
- if (output && !supportedOutputFormats.has(ext)) {
69
- throw new Error(`unsupported output format "${ext}"`)
70
- }
71
-
72
- const target = await context.loadImage(input)
73
-
74
- const tempDir = isGIF && tempy.directory()
75
- const tempOutput = isGIF && path.join(tempDir, 'frame-%d.png')
76
- const frames = []
77
-
78
- const { model, step } = await primitive({
79
- ...rest,
80
- context,
81
- target,
82
- onStep: async (model, step) => {
83
- if (onStep) await onStep(model, step)
84
-
85
- if (isGIF) {
86
- if (nthFrame <= 0 || (step - 1) % nthFrame === 0) {
87
- const frame = tempOutput.replace('%d', frames.length)
88
- await context.saveImage(model.current, frame)
89
- frames.push(frame)
90
- }
91
- } else if (output) {
92
- if (nthFrame > 0 && (step - 1) % nthFrame === 0) {
93
- const frame = output.replace('.', `-${step - 1}.`)
94
- await context.saveImage(model.current, frame, opts)
95
- }
96
- }
97
- }
98
- })
99
-
100
- const time = new Time()
101
-
102
- for (let s = 1; s <= numSteps; ++s) {
103
- time.start(`step ${s}`)
104
-
105
- const candidates = await step(s)
106
-
107
- console.log(`${s})`, {
108
- time: time.end(`step ${s}`),
109
- candidates,
110
- score: model.score
111
- })
112
-
113
- if (!candidates) break
114
- }
115
-
116
- if (output) {
117
- if (isGIF) {
118
- await context.saveGIF(frames, output, opts)
119
- await rmfr(tempDir)
120
- } else {
121
- await context.saveImage(model.current, output, opts)
122
- }
123
- }
124
-
125
- return model
126
- }
package/notes.md DELETED
@@ -1,15 +0,0 @@
1
-
2
- - core
3
- - support resizing input
4
- - min-heap of possible {energy,shape} pairs to add
5
- - add a way of short-circuiting candidates in later steps in order to reuse previously rejected shapes
6
- - add a way of focusing attention on more detailed areas like faces
7
- - add svg viewbox (https://github.com/fogleman/primitive/pull/79/files)
8
- - add svg blur option (https://github.com/fogleman/primitive/pull/56/files)
9
- - output options
10
- - svg support
11
- - browser version
12
- - test browser / canvas support
13
- - web demo
14
- - threejs demo (https://bl.ocks.org/duhaime/6f2a71a76091162c57f66ea9e04db407)
15
-
package/readme.md DELETED
@@ -1,149 +0,0 @@
1
- # primitive ([demo](https://transitive-bullshit.github.io/primitive-web/))
2
-
3
- > Reproduce images from geometric primitives (Node.js + browser port of [primitive](https://github.com/fogleman/primitive)).
4
-
5
- [![NPM](https://img.shields.io/npm/v/primitive.svg)](https://www.npmjs.com/package/primitive) [![Build Status](https://travis-ci.com/transitive-bullshit/primitive.svg?branch=master)](https://travis-ci.com/transitive-bullshit/primitive) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
6
-
7
- This library is also available as a [CLI](https://github.com/transitive-bullshit/primitive-cli).
8
-
9
- [![Demo](https://storage.googleapis.com/transitive-bullshit-primitive/artem-bali-578205-unsplash-triangle-500.png)](https://transitive-bullshit.github.io/primitive-web/)
10
-
11
- #### Table of Contents
12
-
13
- - [Install](#install)
14
- - [Usage](#usage)
15
- - [How It Works](#how-it-works)
16
- - [Node API](#node-api)
17
- * [primitive](#primitive)
18
- - [Browser API](#browser-api)
19
- * [primitive](#primitive-1)
20
- - [Related](#related)
21
- - [Examples](#examples)
22
- - [License](#license)
23
-
24
- ## Install
25
-
26
- ```bash
27
- npm install --save primitive
28
- ```
29
-
30
- ## Usage
31
-
32
- Primitive is usable from both Node.js and browser environments. Most of the API options between the two are the same, with minor differences in input and output configurations.
33
-
34
- A good place to start is by checking out the [web demo](https://transitive-bullshit.github.io/primitive-web/).
35
-
36
- Available shape types:
37
-
38
- - triangle
39
- - ellipse
40
- - rotated-ellipse
41
- - rectangle
42
- - rotated-rectangle
43
- - random (will use all the shape types)
44
-
45
- ## How It Works
46
-
47
- A target image is provided as input. The algorithm tries to find the single most optimal shape that can be drawn to minimize the error between the target image and the drawn image. It repeats this process, adding one shape at a time. Around 50 to 200 shapes are needed to reach a result that is recognizable yet artistic and abstract.
48
-
49
- This GIF demonstrates the iterative nature of the algorithm, attempting to minimize the mean squared error by adding one shape at a time (use a ".gif" output file to generate one yourself).
50
-
51
- [![Demo](https://raw.githubusercontent.com/transitive-bullshit/primitive/master/media/caleb-woods-248879-unsplash-rotated-ellipse-500.gif)](https://transitive-bullshit.github.io/primitive-web/)
52
-
53
- ## Node API
54
-
55
- <!-- Generated by documentation.js. Update this documentation by updating the source code. -->
56
-
57
- ### [primitive](https://github.com/transitive-bullshit/primitive/blob/b242853b0f4634b85b788b8c4d81d4d480d7f617/index.js#L48-L98)
58
-
59
- Reproduces the given input image using geometric primitives.
60
-
61
- Returns a Promise for the generated model.
62
-
63
- Available output formats:
64
-
65
- - png
66
- - jpg
67
- - svg
68
- - gif
69
-
70
- Type: `function (opts): Promise`
71
-
72
- - `opts` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** Configuration options
73
- - `opts.input` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Input image to process (can be a local path, http url, or data url)
74
- - `opts.output` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** Path to generate output image
75
- - `opts.numSteps` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** Number of steps to process [1, 1000] \(optional, default `200`)
76
- - `opts.minEnergy` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?** Minimum energy to stop processing early [0, 1]
77
- - `opts.shapeAlpha` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** Alpha opacity of shapes [0, 255] \(optional, default `128`)
78
- - `opts.shapeType` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Type of shapes to use (optional, default `traingle`)
79
- - `opts.numCandidates` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** Number of top-level candidates per step [1, 32] \(optional, default `1`)
80
- - `opts.numCandidateShapes` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** Number of random candidate shapes per step [10, 1000] \(optional, default `50`)
81
- - `opts.numCandidateMutations` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** Number of candidate mutations per step [10, 500] \(optional, default `100`)
82
- - `opts.numCandidateExtras` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** Number of extra candidate shapes per step [0, 16] \(optional, default `0`)
83
- - `opts.onStep` **[function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)?** Optional async function taking in the model and step index
84
- - `opts.log` **[function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** Optional logging function (console.log to enable logging) (optional, default `noop`)
85
-
86
- ## Browser API
87
-
88
- <!-- Generated by documentation.js. Update this documentation by updating the source code. -->
89
-
90
- ### [primitive](https://github.com/transitive-bullshit/primitive/blob/b242853b0f4634b85b788b8c4d81d4d480d7f617/browser.js#L34-L83)
91
-
92
- Reproduces the given input image using geometric primitives.
93
-
94
- Optionally draws the results to an HTML canvas.
95
-
96
- Returns a Promise for the generated model.
97
-
98
- Type: `function (opts): Promise`
99
-
100
- - `opts` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** Configuration options
101
- - `opts.input` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [Image](https://developer.mozilla.org/docs/Web/API/HTMLImageElement/Image) | ImageData)** URL, Image, or ImageData of input image to process
102
- - `opts.output` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [HTMLCanvasElement](https://developer.mozilla.org/docs/Web/API/HTMLCanvasElement))?** Selector or DOM Element of HTMLCanvas to draw results
103
- - `opts.numSteps` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** Number of steps to process [1, 1000] (optional, default `200`)
104
- - `opts.minEnergy` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?** Minimum energy to stop processing early [0, 1]
105
- - `opts.shapeAlpha` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** Alpha opacity of shapes [0, 255] (optional, default `128`)
106
- - `opts.shapeType` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Type of shapes to use (optional, default `traingle`)
107
- - `opts.numCandidates` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** Number of top-level candidates per step [1, 32] (optional, default `1`)
108
- - `opts.numCandidateShapes` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** Number of random candidate shapes per step [10, 1000] (optional, default `50`)
109
- - `opts.numCandidateMutations` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** Number of candidate mutations per step [10, 500] (optional, default `100`)
110
- - `opts.numCandidateExtras` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** Number of extra candidate shapes per step [0, 16] (optional, default `0`)
111
- - `opts.onStep` **[function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)?** Optional async function taking in the model and step index
112
- - `opts.log` **[function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** Optional logging function (console.log to enable logging) (optional, default `noop`)
113
-
114
- ## Related
115
-
116
- There are several other ports of the primitive algorithm, including at least two JavaScript ports that I'm aware of. This module isn't necessarily better; I created it out of pure fascination.
117
-
118
- - [primitive-cli](https://github.com/transitive-bullshit/primitive-cli) - CLI for this module.
119
- - [primitive](https://github.com/fogleman/primitive) - Original Go version by [Michael Fogleman](https://www.michaelfogleman.com/).
120
- - [primitive.js](https://github.com/ondras/primitive.js) - JavaScript (browser) port by [Ondřej Žára](https://github.com/ondras).
121
- - [geometrize](http://www.geometrize.co.uk/) - Haxe and C++ ports by [Sam Twidale](https://samcodes.co.uk/).
122
- - [node-primitive](https://github.com/vincentdesmares/node-primitive) - Node.js port by [Vincent Desmares](https://github.com/vincentdesmares).
123
- - [sqip](https://github.com/technopagan/sqip) - An SVG-based LQIP library built on top of the [original primitive](https://github.com/fogleman/primitive).
124
-
125
- ## Examples
126
-
127
- ![alekzan-powell](https://storage.googleapis.com/transitive-bullshit-primitive/alekzan-powell-408990-unsplash-triangle-500.png "500 triangles")
128
-
129
- ![atikh-bana](https://storage.googleapis.com/transitive-bullshit-primitive/atikh-bana-125761-unsplash-rotated-ellipse-500.png "500 rotated ellipses")
130
-
131
- ![caleb-woods](https://storage.googleapis.com/transitive-bullshit-primitive/caleb-woods-248879-unsplash-triangle-500.png "500 triangles")
132
-
133
- ![glauco-zuccaccia](https://storage.googleapis.com/transitive-bullshit-primitive/glauco-zuccaccia-132831-unsplash-rotated-ellipse-500.png "500 rotated ellipses")
134
-
135
- ![jessica-weiller](https://storage.googleapis.com/transitive-bullshit-primitive/jessica-weiller-60884-unsplash-triangle-500.png "500 triangles")
136
-
137
- ![nicolas-picard](https://storage.googleapis.com/transitive-bullshit-primitive/nicolas-picard-450042-unsplash-random-500.png "500 random shapes")
138
-
139
- ![stefanus-martanto](https://storage.googleapis.com/transitive-bullshit-primitive/stefanus-martanto-setyo-husodo-14699-unsplash-random-500.png "500 random shapes")
140
-
141
- ![timothy-paul-smith](https://storage.googleapis.com/transitive-bullshit-primitive/timothy-paul-smith-405497-unsplash-rotated-rectangle-500.png "500 rectangles")
142
-
143
- ![umanoide](https://storage.googleapis.com/transitive-bullshit-primitive/umanoide-112489-unsplash-rotated-ellipse-500.png "500 ellipses")
144
-
145
- ## License
146
-
147
- MIT © [Travis Fischer](https://github.com/transitive-bullshit)
148
-
149
- All images are provided by [Unsplash](https://unsplash.com/) via a [CC0](https://creativecommons.org/publicdomain/zero/1.0/) license.
package/rollup.config.js DELETED
@@ -1,28 +0,0 @@
1
- import babel from 'rollup-plugin-babel'
2
- import commonjs from 'rollup-plugin-commonjs'
3
- import resolve from 'rollup-plugin-node-resolve'
4
-
5
- import pkg from './package.json'
6
-
7
- export default {
8
- input: 'browser.js',
9
- output: [
10
- {
11
- file: pkg.browser,
12
- format: 'cjs',
13
- sourceMap: true
14
- }
15
- ],
16
- external: [ 'ow' ],
17
- plugins: [
18
- babel({
19
- exclude: 'node_modules/**',
20
- externalHelpers: false,
21
- runtimeHelpers: true
22
- }),
23
- resolve({
24
- browser: true
25
- }),
26
- commonjs()
27
- ]
28
- }