1- import { genBlob } from "../internal/gen" ;
2- import { rand } from "../internal/rand" ;
3- import { Point } from "../internal/types" ;
4- import { mapPoints } from "../internal/util" ;
1+ import { genFromOptions } from "../internal/gen" ;
52import { renderPath } from "../internal/render/svg" ;
63import { renderPath2D } from "../internal/render/canvas" ;
4+ import { mapPoints } from "../internal/util" ;
75
86export interface BlobOptions {
97 seed : string | number ;
@@ -23,50 +21,9 @@ export interface SvgOptions {
2321 strokeWidth ?: number ;
2422}
2523
26- const typeCheck = ( name : string , val : any , expected : string [ ] ) => {
27- const actual = typeof val ;
28- if ( ! expected . includes ( actual ) ) {
29- throw `(blobs2) "${ name } " should have type "${ expected . join ( "|" ) } " but was "${ actual } ".` ;
30- }
31- } ;
32-
33- const raw = ( blobOptions : BlobOptions ) : Point [ ] => {
34- const rgen = rand ( String ( blobOptions . seed ) ) ;
35-
36- typeCheck ( "blobOptions" , blobOptions , [ "object" ] ) ;
37- typeCheck ( "seed" , blobOptions . seed , [ "string" , "number" ] ) ;
38- typeCheck ( "extraPoints" , blobOptions . extraPoints , [ "number" ] ) ;
39- typeCheck ( "randomness" , blobOptions . randomness , [ "number" ] ) ;
40- typeCheck ( "size" , blobOptions . size , [ "number" ] ) ;
41-
42- // Scale of random movement increases as randomness approaches infinity.
43- // randomness = 0 -> rangeStart = 1
44- // randomness = 2 -> rangeStart = 0.8333
45- // randomness = 5 -> rangeStart = 0.6667
46- // randomness = 10 -> rangeStart = 0.5
47- // randomness = 20 -> rangeStart = 0.3333
48- // randomness = 50 -> rangeStart = 0.1667
49- // randomness = 100 -> rangeStart = 0.0909
50- const rangeStart = 1 / ( 1 + Math . abs ( blobOptions . randomness ) / 10 ) ;
51-
52- const points = genBlob (
53- 3 + Math . abs ( blobOptions . extraPoints ) ,
54- ( ) => ( rangeStart + rgen ( ) * ( 1 - rangeStart ) ) / 2 ,
55- ) ;
56-
57- const size = Math . abs ( blobOptions . size ) ;
58- return mapPoints ( points , ( { curr} ) => {
59- curr . x *= size ;
60- curr . y *= size ;
61- curr . handleIn . length *= size ;
62- curr . handleOut . length *= size ;
63- return curr ;
64- } ) ;
65- } ;
66-
6724export const canvasPath = ( blobOptions : BlobOptions , canvasOptions : CanvasOptions = { } ) : Path2D => {
6825 return renderPath2D (
69- mapPoints ( raw ( blobOptions ) , ( { curr} ) => {
26+ mapPoints ( genFromOptions ( blobOptions ) , ( { curr} ) => {
7027 curr . x += canvasOptions . offsetX || 0 ;
7128 curr . y += canvasOptions . offsetY || 0 ;
7229 return curr ;
@@ -87,5 +44,5 @@ export const svg = (blobOptions: BlobOptions, svgOptions: SvgOptions = {}): stri
8744} ;
8845
8946export const svgPath = ( blobOptions : BlobOptions ) : string => {
90- return renderPath ( raw ( blobOptions ) ) ;
47+ return renderPath ( genFromOptions ( blobOptions ) ) ;
9148} ;
0 commit comments