-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathblobs.ts
72 lines (66 loc) · 2.4 KB
/
blobs.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import {genFromOptions} from "../internal/gen";
import {renderPath} from "../internal/render/svg";
import {renderPath2D} from "../internal/render/canvas";
import {mapPoints} from "../internal/util";
import {checkBlobOptions, checkCanvasOptions, checkSvgOptions} from "../internal/check";
export interface BlobOptions {
// A given seed will always produce the same blob.
// Use `Math.random()` for pseudorandom behavior.
seed: string | number;
// Actual number of points will be `3 + extraPoints`.
extraPoints: number;
// Increases the amount of variation in point position.
randomness: number;
// Size of the bounding box.
size: number;
}
export interface CanvasOptions {
// Coordinates of top-left corner of the blob.
offsetX?: number;
offsetY?: number;
}
export interface SvgOptions {
fill?: string; // Default: "#ec576b".
stroke?: string; // Default: "none".
strokeWidth?: number; // Default: 0.
}
export const canvasPath = (blobOptions: BlobOptions, canvasOptions: CanvasOptions = {}): Path2D => {
try {
checkBlobOptions(blobOptions);
checkCanvasOptions(canvasOptions);
} catch (e) {
throw `(blobs2): ${e}`;
}
return renderPath2D(
mapPoints(genFromOptions(blobOptions), ({curr}) => {
curr.x += canvasOptions.offsetX || 0;
curr.y += canvasOptions.offsetY || 0;
return curr;
}),
);
};
export const svg = (blobOptions: BlobOptions, svgOptions: SvgOptions = {}): string => {
try {
checkBlobOptions(blobOptions);
checkSvgOptions(svgOptions);
} catch (e) {
throw `(blobs2): ${e}`;
}
const path = svgPath(blobOptions);
const size = Math.floor(blobOptions.size);
const fill = svgOptions.fill === undefined ? "#ec576b" : svgOptions.fill;
const stroke = svgOptions.stroke === undefined ? "none" : svgOptions.stroke;
const strokeWidth = svgOptions.strokeWidth === undefined ? 0 : svgOptions.strokeWidth;
return `
<svg width="${size}" height="${size}" viewBox="0 0 ${size} ${size}" xmlns="http://www.w3.org/2000/svg">
<path stroke="${stroke}" stroke-width="${strokeWidth}" fill="${fill}" d="${path}"/>
</svg>`.trim();
};
export const svgPath = (blobOptions: BlobOptions): string => {
try {
checkBlobOptions(blobOptions);
} catch (e) {
throw `(blobs2): ${e}`;
}
return renderPath(genFromOptions(blobOptions));
};