11// https://www.blobmaker.app/
22// https://math.stackexchange.com/questions/873224/calculate-control-points-of-cubic-bezier-curve-approximating-a-part-of-a-circle
33
4- import { rad , smooth } from "./util" ;
4+ import { rad , smooth , rand } from "./util" ;
55import { render } from "./render" ;
66import { Point , BlobOptions } from "./types" ;
77
88export { BlobOptions } from "./types" ;
99
1010// Generates a random rounded shape.
1111export const blob = ( opt : BlobOptions ) : string => {
12- opt = Object . assign ( { } , opt ) ;
12+ // Random number generator.
13+ const rgen = rand ( opt . seed || String ( Date . now ( ) ) ) ;
1314
1415 if ( ! opt . stroke && ! opt . color ) {
15- throw new Error ( "no color or stroke specified" )
16+ throw new Error ( "no color or stroke specified" ) ;
1617 }
1718
1819 if ( opt . complexity <= 0 || opt . complexity > 1 ) {
@@ -29,7 +30,7 @@ export const blob = (opt: BlobOptions): string => {
2930
3031 const points : Point [ ] = [ ] ;
3132 for ( let i = 0 ; i < count ; i ++ ) {
32- const rand = 1 - 0.8 * opt . contrast * Math . random ( ) ;
33+ const rand = 1 - 0.8 * opt . contrast * rgen ( ) ;
3334
3435 points . push ( {
3536 x : Math . sin ( rad ( i * angle ) ) * radius * rand + opt . size / 2 ,
@@ -39,31 +40,32 @@ export const blob = (opt: BlobOptions): string => {
3940
4041 const smoothed = smooth ( points , {
4142 closed : true ,
42- strength : ( 4 / 3 ) * Math . tan ( rad ( angle / 4 ) ) / Math . sin ( rad ( angle / 2 ) ) ,
43+ strength : ( ( 4 / 3 ) * Math . tan ( rad ( angle / 4 ) ) ) / Math . sin ( rad ( angle / 2 ) ) ,
4344 } ) ;
4445
4546 return render ( smoothed , {
4647 closed : true ,
4748 width : opt . size ,
4849 height : opt . size ,
4950 fill : opt . color ,
50- transform : `rotate(${ Math . random ( ) * angle } ,${ opt . size / 2 } ,${ opt . size / 2 } )` ,
51- stroke : ( opt . stroke && opt . stroke . color ) ,
52- strokeWidth : ( opt . stroke && opt . stroke . width ) ,
51+ transform : `rotate(${ rgen ( ) * angle } ,${ opt . size / 2 } ,${ opt . size / 2 } )` ,
52+ stroke : opt . stroke && opt . stroke . color ,
53+ strokeWidth : opt . stroke && opt . stroke . width ,
5354 guides : opt . guides ,
5455 } ) ;
5556} ;
5657
5758console . log (
5859 blob ( {
5960 color : "pink" ,
60- complexity : 0.2 ,
61- contrast : 1 ,
62- size : 600 ,
61+ complexity : 0.4 ,
62+ seed : "16" ,
63+ contrast : 0.4 ,
64+ size : 400 ,
6365 guides : true ,
6466 stroke : {
6567 color : "red" ,
66- width : 1.8 ,
68+ width : 1 ,
6769 } ,
6870 } ) ,
6971) ;
0 commit comments