Skip to content

Commit c68b70e

Browse files
committed
start defining v2 api
1 parent d68bb95 commit c68b70e

File tree

8 files changed

+88
-47
lines changed

8 files changed

+88
-47
lines changed

internal/animate/testing/script.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {divide, prepare} from "../prepare";
33
import {Coord, Point} from "../../types";
44
import {length, insertAt, insertCount, rad, mod, mapPoints, forPoints} from "../../util";
55
import {clear, drawInfo, drawClosed} from "../../render/canvas";
6-
import {genBlob} from "../../blobs";
6+
import {genBlob} from "../../gen";
77
import {rand} from "../../rand";
88

99
let animationSpeed = 2;

internal/blobs.ts renamed to internal/gen.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
import {Point} from "./types";
2-
import {smooth} from "../internal/util";
2+
import {smooth} from "./util";
33

44
export const genBlob = (pointCount: number, offset: () => number): Point[] => {
55
const angle = (Math.PI * 2) / pointCount;
6-
const boundingSize = 1;
7-
const boundingCenter = boundingSize / 2;
86

97
const points: Point[] = [];
108
for (let i = 0; i < pointCount; i++) {
119
const randPointOffset = offset();
1210
const pointX = Math.sin(i * angle);
1311
const pointY = Math.cos(i * angle);
1412
points.push({
15-
x: boundingCenter + pointX * randPointOffset,
16-
y: boundingCenter + pointY * randPointOffset,
13+
x: 0.5 + pointX * randPointOffset,
14+
y: 0.5 + pointY * randPointOffset,
1715
handleIn: {angle: 0, length: 0},
1816
handleOut: {angle: 0, length: 0},
1917
});
File renamed without changes.

public/blobs.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import {genBlob} from "../internal/gen";
2+
import {rand} from "../internal/rand";
3+
import {Point} from "../internal/types";
4+
import {mapPoints} from "../internal/util";
5+
import {renderPath, renderEditable} from "../internal/render/svg";
6+
7+
// TODO include editable types in this file
8+
// TODO make path editable in editable svg
9+
// TODO make editable svg more structured
10+
11+
export interface BlobOptions {
12+
seed: string | number;
13+
extraPoints: number;
14+
randomness: number;
15+
size: number;
16+
}
17+
18+
export interface CanvasOptions {}
19+
20+
export interface SvgOptions {
21+
style: {
22+
color: string;
23+
strokeColor: string;
24+
}
25+
}
26+
27+
export const raw = (blobOptions: BlobOptions): Point[] => {
28+
const rgen = rand(String(blobOptions.seed));
29+
30+
// Scale of random movement increases as randomness approaches infinity.
31+
// randomness = 0 -> rangeStart = 1
32+
// randomness = 2 -> rangeStart = 0.8333
33+
// randomness = 5 -> rangeStart = 0.6667
34+
// randomness = 10 -> rangeStart = 0.5
35+
// randomness = 20 -> rangeStart = 0.3333
36+
// randomness = 50 -> rangeStart = 0.1667
37+
// randomness = 100 -> rangeStart = 0.0909
38+
const rangeStart = 1 / (1 + Math.abs(blobOptions.randomness) / 10);
39+
40+
const points = genBlob(
41+
3 + Math.abs(blobOptions.extraPoints),
42+
() => rangeStart + rgen() * (1 - rangeStart),
43+
);
44+
45+
return mapPoints(points, ({curr}) => {
46+
curr.x *= blobOptions.size;
47+
curr.y *= blobOptions.size;
48+
curr.handleIn.length *= blobOptions.size;
49+
curr.handleOut.length *= blobOptions.size;
50+
return curr;
51+
});
52+
};
53+
54+
export const canvas = (blobOptions: BlobOptions, canvasOptions: CanvasOptions) => {
55+
// TODO
56+
};
57+
58+
export const svg = (blobOptions: BlobOptions, svgOptions: SvgOptions) => {};
59+
60+
export const svgPath = (blobOptions: BlobOptions): string => {
61+
return renderPath(raw(blobOptions));
62+
};
63+
64+
export const svgEditable = (blobOptions: BlobOptions, svgOptions: SvgOptions) => {
65+
// TODO fill from svgOptions
66+
// return renderEditable(raw(blobOptions), {
67+
// closed: true,
68+
// height: blobOptions.size,
69+
// width: blobOptions.size,
70+
// boundingBox: false,
71+
// fill: "",
72+
// guides: false,
73+
// stroke: "",
74+
// strokeWidth: 0,
75+
// transform: "",
76+
// });
77+
};

public/gen.ts

Lines changed: 0 additions & 34 deletions
This file was deleted.

legacy/blobs.test.ts renamed to public/legacy.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import blobs, {BlobOptions} from "./blobs";
1+
import blobs, {BlobOptions} from "./legacy";
22

33
const genMinimalOptions = (): BlobOptions => ({
44
size: 1000 * Math.random(),

legacy/blobs.ts renamed to public/legacy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import {rand} from "../internal/rand";
22
import {renderEditable} from "../internal/render/svg";
33
import {XmlElement} from "../editable";
4-
import {genBlob} from "../internal/blobs";
4+
import {genBlob} from "../internal/gen";
55
import {mapPoints} from "../internal/util";
66

77
const isBrowser = new Function("try {return this===window;}catch(e){ return false;}");
88
const isLocalhost = () => location.hostname === "localhost" || location.hostname === "127.0.0.1";
99
if (!isBrowser() || (isBrowser() && isLocalhost())) {
10-
console.warn("You are using the legacy blobs API!\nPlease use TODO instead.");
10+
console.warn("You are using the legacy blobs API!\nPlease use 'blobs/v2' instead.");
1111
}
1212

1313
export interface PathOptions {

rollup.config.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ import {uglify} from "rollup-plugin-uglify";
33

44
const bundles = [
55
{
6-
entry: "legacy/blobs.ts",
6+
entry: "public/legacy.ts",
77
output: "index.js",
88
},
99
{
10-
entry: "legacy/blobs.ts",
10+
entry: "public/legacy.ts",
1111
output: "v1/index.js",
1212
},
1313
{
14-
entry: "public/gen.ts",
15-
output: "v2/gen.js",
14+
entry: "public/blobs.ts",
15+
output: "v2/index.js",
1616
},
1717
];
1818

0 commit comments

Comments
 (0)