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" ;
5
2
import { renderPath } from "../internal/render/svg" ;
6
3
import { renderPath2D } from "../internal/render/canvas" ;
4
+ import { mapPoints } from "../internal/util" ;
7
5
8
6
export interface BlobOptions {
9
7
seed : string | number ;
@@ -23,50 +21,9 @@ export interface SvgOptions {
23
21
strokeWidth ?: number ;
24
22
}
25
23
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
-
67
24
export const canvasPath = ( blobOptions : BlobOptions , canvasOptions : CanvasOptions = { } ) : Path2D => {
68
25
return renderPath2D (
69
- mapPoints ( raw ( blobOptions ) , ( { curr} ) => {
26
+ mapPoints ( genFromOptions ( blobOptions ) , ( { curr} ) => {
70
27
curr . x += canvasOptions . offsetX || 0 ;
71
28
curr . y += canvasOptions . offsetY || 0 ;
72
29
return curr ;
@@ -87,5 +44,5 @@ export const svg = (blobOptions: BlobOptions, svgOptions: SvgOptions = {}): stri
87
44
} ;
88
45
89
46
export const svgPath = ( blobOptions : BlobOptions ) : string => {
90
- return renderPath ( raw ( blobOptions ) ) ;
47
+ return renderPath ( genFromOptions ( blobOptions ) ) ;
91
48
} ;
0 commit comments