@@ -12,20 +12,32 @@ const settings = {
12
12
13
13
const sketch = ( { width, height } ) => {
14
14
const margin = 2 ;
15
- const gridXCount = 2 ;
15
+ const gridMargin = margin ;
16
+ const gridXCount = 10 ;
16
17
const gridYCount = gridXCount ;
18
+ const skipEdge = true ;
19
+ const count = 40 ;
20
+ const randomColors = false ;
21
+ const splicing = true ;
17
22
18
- const palette = random . pick ( palettes ) ;
23
+ // Use a palette inspired by Sol LeWitt
24
+ const palette = [ '#ea3f3f' , '#76b9ed' , '#f2d843' ] ;
25
+
26
+ // Or, use a random palette
27
+ // const palette = random.shuffle(random.pick(palettes)).slice(0, 3);
19
28
20
29
// Make a grid of points
21
30
const gridPoints = [ ] ;
22
31
for ( let x = 0 ; x < gridXCount ; x ++ ) {
23
32
for ( let y = 0 ; y < gridYCount ; y ++ ) {
33
+ if ( skipEdge ) {
34
+ if ( x === 0 || x === gridXCount - 1 || y === 0 || y === gridYCount - 1 ) continue ;
35
+ }
24
36
const u = gridXCount <= 1 ? 0.5 : x / ( gridXCount - 1 ) ;
25
37
const v = gridYCount <= 1 ? 0.5 : y / ( gridYCount - 1 ) ;
26
38
27
- const px = lerp ( margin , width - margin , u ) ;
28
- const py = lerp ( margin , height - margin , v ) ;
39
+ const px = lerp ( gridMargin , width - gridMargin , u ) ;
40
+ const py = lerp ( gridMargin , height - gridMargin , v ) ;
29
41
gridPoints . push ( [ px , py ] ) ;
30
42
}
31
43
}
@@ -41,35 +53,64 @@ const sketch = ({ width, height }) => {
41
53
return [ px , py ] ;
42
54
} ) ;
43
55
44
- // Connect a random point along one of the four edges of the grid
45
- // to a random point in the grid
46
- const connect = ( ) => {
47
- const cornerIndex = random . rangeFloor ( 0 , corners . length ) ;
48
- const nextCornerIndex = ( cornerIndex + 1 ) % corners . length ;
49
- const edgeStart = corners [ cornerIndex ] ;
50
- const edgeEnd = corners [ nextCornerIndex ] ;
51
- const t = random . value ( ) ;
52
- const start = lerpArray ( edgeStart , edgeEnd , t ) ;
53
-
56
+ const connect = ( start , color ) => {
57
+ if ( gridPoints . length === 0 ) return null ;
54
58
const gridIndex = random . rangeFloor ( 0 , gridPoints . length ) ;
55
59
const end = gridPoints [ gridIndex ] ;
56
- gridPoints . splice ( gridIndex , 1 ) ;
60
+ if ( splicing ) gridPoints . splice ( gridIndex , 1 ) ;
57
61
58
62
return {
59
- color : palette [ cornerIndex % palette . length ] ,
63
+ color : randomColors ? random . pick ( palette ) : color ,
60
64
path : [ start , end ]
61
65
} ;
62
66
} ;
63
67
64
- const maxLines = 30 ;
65
- const items = [ ] ;
66
- for ( let i = 0 ; i < maxLines ; i ++ ) {
67
- if ( gridPoints . length > 0 ) {
68
- const item = connect ( ) ;
69
- items . push ( item ) ;
68
+ const fromMidpoint = ( ) => {
69
+ // Midpoint of line
70
+ const t = 0.5 ;
71
+
72
+ // Can instead be random along edge
73
+ // const t = random.value();
74
+
75
+ const cornerIndex = random . rangeFloor ( 0 , corners . length ) ;
76
+ const nextCornerIndex = ( cornerIndex + 1 ) % corners . length ;
77
+ const edgeStart = corners [ cornerIndex ] ;
78
+ const edgeEnd = corners [ nextCornerIndex ] ;
79
+ return lerpArray ( edgeStart , edgeEnd , t ) ;
80
+ } ;
81
+
82
+ const fromCorner = ( ) => {
83
+ const cornerIndex = random . rangeFloor ( 0 , corners . length ) ;
84
+ return corners [ cornerIndex % corners . length ] ;
85
+ } ;
86
+
87
+ const fromCenter = ( ) => {
88
+ return [ width / 2 , height / 2 ] ;
89
+ } ;
90
+
91
+ let items = [ ] ;
92
+
93
+ // Gather all types..
94
+ const types = [
95
+ { count : count / 2 , emitter : fromMidpoint , color : palette [ 0 ] } ,
96
+ { count : count / 2 , emitter : fromCorner , color : palette [ 1 ] } ,
97
+ { count : count , emitter : fromCenter , color : palette [ 2 ] }
98
+ ] ;
99
+
100
+ // Emit each line type
101
+ types . forEach ( type => {
102
+ for ( let i = 0 ; i < type . count ; i ++ ) {
103
+ if ( gridPoints . length === 0 ) return ;
104
+ const start = type . emitter ( ) ;
105
+ items . push ( connect ( start , type . color ) ) ;
70
106
}
71
- }
107
+ } )
108
+
109
+ // Cleanup lines
110
+ items = items . filter ( Boolean ) ;
111
+ items = random . shuffle ( items ) ;
72
112
113
+ // Draw lines
73
114
return ( { context, width, height } ) => {
74
115
context . fillStyle = 'white' ;
75
116
context . fillRect ( 0 , 0 , width , height ) ;
@@ -80,9 +121,8 @@ const sketch = ({ width, height }) => {
80
121
context . lineTo ( point [ 0 ] , point [ 1 ] ) ;
81
122
} ) ;
82
123
context . fillStyle = context . strokeStyle = item . color ;
83
- context . lineWidth = 0.15 ;
124
+ context . lineWidth = 0.075 ;
84
125
context . globalAlpha = 1 ;
85
- context . globalCompositeOperation = 'multiply' ;
86
126
context . lineJoin = 'round' ;
87
127
context . lineCap = 'round' ;
88
128
context . stroke ( ) ;
0 commit comments