Skip to content

Commit 4d2055d

Browse files
committed
expose editable rendered result in public api
1 parent 644a318 commit 4d2055d

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

index.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import {rand} from "./internal/math/rand";
44
import {Point} from "./internal/math/geometry";
55
import {rad} from "./internal/math/unit";
66
import {smooth} from "./internal/svg/smooth";
7-
import {render} from "./internal/svg/render";
7+
import {renderEditable} from "./internal/svg/render";
8+
import {XmlElement} from "./internal/xml";
89

910
export interface BlobOptions {
1011
// Bounding box dimensions.
@@ -34,8 +35,14 @@ export interface BlobOptions {
3435
guides?: boolean;
3536
}
3637

37-
// Generates a random rounded shape.
38+
// Generates an svg document string containing a randomized rounded shape.
3839
const blobs = (opt: BlobOptions): string => {
40+
return editable(opt).render();
41+
};
42+
43+
// Generates an editable data structure which can be rendered to an svg document
44+
// containing a randomized rounded shape.
45+
export const editable = (opt: BlobOptions): XmlElement => {
3946
// Random number generator.
4047
const rgen = rand(opt.seed || String(Date.now()));
4148

@@ -74,7 +81,7 @@ const blobs = (opt: BlobOptions): string => {
7481
strength: ((4 / 3) * Math.tan(rad(angle / 4))) / Math.sin(rad(angle / 2)),
7582
});
7683

77-
return render(smoothed, {
84+
return renderEditable(smoothed, {
7885
closed: true,
7986
width: opt.size,
8087
height: opt.size,
@@ -87,3 +94,4 @@ const blobs = (opt: BlobOptions): string => {
8794
};
8895

8996
export default blobs;
97+
export {IXml, XmlElement} from "./internal/xml";

internal/svg/render.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {loopAccess} from "./util";
22
import {Point, interpolate} from "./point";
3-
import {xml} from "../xml";
3+
import {xml, XmlElement} from "../xml";
44

55
export interface RenderOptions {
66
// Viewport size.
@@ -24,8 +24,9 @@ export interface RenderOptions {
2424
boundingBox?: boolean;
2525
}
2626

27-
// Renders a shape made up of the input points.
28-
export const render = (p: Point[], opt: RenderOptions): string => {
27+
// Renders a shape made up of the input points to an editable data structure
28+
// which can be rendered to svg.
29+
export const renderEditable = (p: Point[], opt: RenderOptions): XmlElement => {
2930
const points = p.map((point) => interpolate(point, opt.height));
3031

3132
// Compute guides from input point data.
@@ -49,8 +50,8 @@ export const render = (p: Point[], opt: RenderOptions): string => {
4950
});
5051
}
5152

52-
// Render path data attribute from points and handles. Must loop more times than the
53-
// number of points in order to correctly close the path.
53+
// Render path data attribute from points and handles. Must loop more times
54+
// than the number of points in order to correctly close the path.
5455
let path = "";
5556
for (let i = 0; i <= points.length; i++) {
5657
const point = loopAccess(points)(i);
@@ -155,5 +156,5 @@ export const render = (p: Point[], opt: RenderOptions): string => {
155156
}
156157
}
157158

158-
return xmlRoot.render();
159+
return xmlRoot;
159160
};

testing/blobs.test.ts

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

33
const genMinimalOptions = (): BlobOptions => ({
44
size: 1000 * Math.random(),
@@ -63,3 +63,16 @@ describe("blobs", () => {
6363
expect(() => blobs(options)).toThrow("color");
6464
});
6565
});
66+
67+
describe("editable", () => {
68+
it("should reflect changes when edited", () => {
69+
const options = genMinimalOptions();
70+
71+
const out = editable(options);
72+
const initial = out.render();
73+
out.attributes.id = "test";
74+
const modified = out.render();
75+
76+
expect(modified).not.toBe(initial);
77+
});
78+
});

0 commit comments

Comments
 (0)