1+ let paused = false ;
2+ let cover ;
3+
4+ class Separations {
5+ constructor ( wavelength , amplitude , c1 , c2 ) {
6+ this . wavelength = wavelength ;
7+ this . amplitude = amplitude ;
8+ this . c1 = c1 ;
9+ this . c2 = c2 ;
10+ }
11+
12+ getSin ( x ) {
13+ return this . amplitude * sin ( 360 * x / this . wavelength ) ;
14+ }
15+
16+ getSeparation ( x ) {
17+ return int ( this . amplitude - Math . abs ( this . getSin ( x ) ) ) + 4 ;
18+ }
19+
20+ getColor ( x ) {
21+ return ( this . getSin ( x ) < 0 ) ? this . c2 : this . c1 ;
22+ }
23+ }
24+
25+ function restart ( ) {
26+ cover = new SeatCover ( ) ;
27+ }
28+
29+ function windowResized ( ) {
30+ resizeCanvas ( windowWidth , windowHeight ) ;
31+ restart ( ) ;
32+ }
33+
34+ function touchStarted ( ) {
35+ paused = ! paused ;
36+ }
37+
38+ function keyTyped ( ) {
39+ switch ( key ) {
40+ case 'p' :
41+ paused = ! paused ;
42+ break ;
43+ case 'c' :
44+ cover . change ( ) ;
45+ cover . recolor ( ) ;
46+ break ;
47+ case 'r' :
48+ restart ( ) ;
49+ break ;
50+ }
51+ }
52+
53+ function setup ( ) {
54+ let canvas = createCanvas ( windowWidth , windowHeight ) ;
55+ canvas . parent ( 'sketch-holder' ) ;
56+ frameRate ( 20 ) ;
57+ restart ( ) ;
58+ }
59+
60+ class SeatCover {
61+ constructor ( ) {
62+ this . hwavelength = height / 2 ;
63+ this . hamplitude = 8 ;
64+ this . vwavelength = width / 3 ;
65+ this . vamplitude = 6 ;
66+ // this.hcolor1 = color(200, 0, 0);
67+ // this.hcolor2 = color(222, 122, 0);
68+ // this.vcolor1 = color(50, 50, 220);
69+ // this.vcolor2 = color(120, 120, 120);
70+ this . hcolor1 = color ( 200 , 0 , 0 , 200 ) ;
71+ this . hcolor2 = color ( 222 , 50 , 50 , 182 ) ;
72+ this . vcolor1 = color ( 50 , 50 , 220 , 208 ) ;
73+ this . vcolor2 = color ( 120 , 120 , 120 , 228 ) ;
74+ }
75+
76+ change ( ) {
77+ this . hwavelength = random ( [ height / 2 , height / 3 , height / 4 , height / 5 ] ) ;
78+ this . hamplitude = int ( random ( 12 ) ) + 3 ;
79+ this . vwavelength = random ( [ width / 2 , width / 3 , width / 4 , width / 5 ] ) ;
80+ this . vamplitude = int ( random ( 12 ) ) + 3 ;
81+ }
82+
83+ recolor ( ) {
84+ this . hcolor1 = color ( random ( 255 ) , random ( 255 ) , random ( 255 ) , 200 ) ;
85+ this . hcolor2 = color ( random ( 255 ) , random ( 255 ) , random ( 255 ) , 200 ) ;
86+ this . vcolor1 = color ( random ( 255 ) , random ( 255 ) , random ( 255 ) , 200 ) ;
87+ this . vcolor2 = color ( random ( 255 ) , random ( 255 ) , random ( 255 ) , 200 ) ;
88+ }
89+
90+ draw ( ) {
91+ let seph = new Separations (
92+ this . hwavelength ,
93+ this . hamplitude ,
94+ this . hcolor1 ,
95+ this . hcolor2
96+ ) ;
97+ let sepv = new Separations (
98+ this . vwavelength ,
99+ this . vamplitude ,
100+ this . vcolor1 ,
101+ this . vcolor2
102+ ) ;
103+
104+ let x = sepv . getSeparation ( 0 ) ;
105+ let y = seph . getSeparation ( 0 ) ;
106+ strokeWeight ( 2 ) ;
107+ while ( x < width ) {
108+ stroke ( sepv . getColor ( x ) ) ;
109+ line ( x , 0 , x , height ) ;
110+ x += sepv . getSeparation ( x ) ;
111+ }
112+ while ( y < height ) {
113+ stroke ( seph . getColor ( y ) ) ;
114+ line ( 0 , y , width , y ) ;
115+ y += seph . getSeparation ( y ) ;
116+ }
117+ }
118+ }
119+
120+ function draw ( ) {
121+ clear ( ) ;
122+ background ( 200 ) ;
123+
124+ cover . draw ( ) ;
125+ if ( ! paused && frameCount % 50 == 0 ) {
126+ cover . change ( ) ;
127+ cover . recolor ( ) ;
128+ }
129+ }
0 commit comments