1+ < style >
2+ canvas {
3+ position : absolute;
4+ left : 0 ;
5+ top : 0 ;
6+ width : 100% ;
7+ height : 100% ;
8+ background : black;
9+ }
10+ </ style >
11+
12+ < canvas id =canvas > </ canvas >
13+
14+ < script >
15+
16+ var ctx = canvas . getContext ( '2d' ) ;
17+
18+ var width = window . innerWidth ;
19+ var height = window . innerHeight ;
20+ canvas . width = width ;
21+ canvas . height = height ;
22+
23+ var atoms = [ ] ;
24+
25+ var yellow = create ( 300 , 'yellow' ) ; // 300
26+ var red = create ( 200 , 'red' ) ; // 200
27+ var green = create ( 200 , 'green' ) ; // 200
28+
29+
30+ var rules = [ ] ;
31+
32+ var rule1 = createRule ( green , green , - 0.32 , 80 ) ; // -0.32
33+ createRule ( green , red , - 0.17 , 80 ) ; // -0.17
34+ createRule ( green , yellow , 0.5 , 80 ) ; // 0.34
35+
36+ createRule ( red , red , - 0.1 , 80 ) ; // -0.1
37+ createRule ( red , green , - 0.34 , 80 ) ; // -0.34
38+
39+ createRule ( yellow , yellow , 0.25 , 80 ) ; // 0.15
40+ createRule ( yellow , green , - 0.2 , 80 ) ; // -0.2
41+
42+
43+ ( function update ( ) {
44+
45+ draw ( ) ;
46+ requestAnimationFrame ( update ) ;
47+
48+ } ) ( ) ;
49+
50+
51+ function display ( x , y , c , s ) {
52+
53+ ctx . fillStyle = c ;
54+ ctx . fillRect ( x , y , s , s ) ;
55+
56+ } //draw
57+
58+
59+ function atom ( x , y , color ) {
60+
61+ return { x, y, vx :0 , vy :0 , color} ;
62+
63+ } //atom
64+
65+ function random ( n ) {
66+
67+ return Math . random ( ) * n + 50 ;
68+
69+ } //random
70+
71+ function create ( number , color ) {
72+
73+ group = [ ] ;
74+
75+ for ( var i = 0 ; i < number ; i ++ ) {
76+
77+ group . push ( atom ( random ( width ) , random ( height ) , color ) ) ;
78+ atoms . push ( group [ i ] ) ;
79+
80+ } //for
81+
82+ return group ;
83+
84+ } //create
85+
86+ function rule ( atoms1 , atoms2 , g , maxd ) {
87+
88+ for ( let i = 0 ; i < atoms1 . length ; i ++ ) {
89+
90+ fx = 0 ;
91+ fy = 0 ;
92+
93+ for ( let j = 0 ; j < atoms2 . length ; j ++ ) {
94+
95+ a = atoms1 [ i ] ;
96+ b = atoms2 [ j ] ;
97+
98+ dx = a . x - b . x ;
99+ dy = a . y - b . y ;
100+
101+ d = Math . sqrt ( dx * dx + dy * dy ) ;
102+
103+ if ( d > 0 && d < maxd ) {
104+ F = ( g * 1 ) / d ;
105+ fx += F * dx ;
106+ fy += F * dy ;
107+ }
108+
109+ } //for
110+
111+ a . vx = ( a . vx + fx ) * 0.5 ;
112+ a . vy = ( a . vy + fy ) * 0.5 ;
113+
114+ a . x += a . vx ;
115+ a . y += a . vy ;
116+
117+ if ( a . x < 0 )
118+ if ( a . vx < 0 )
119+ a . vx *= - 1 ;
120+
121+ if ( a . x > width )
122+ if ( a . vx > 1 )
123+ a . vx *= - 1 ;
124+
125+ if ( a . y < 0 )
126+ if ( a . vy < 0 )
127+ a . vy *= - 1 ;
128+
129+ if ( a . y > height )
130+ if ( a . vy > 0 )
131+ a . vy *= - 1 ;
132+
133+ } //for
134+
135+ } //rule
136+
137+ function createRule ( group1 , group2 , force , d ) {
138+
139+ var o = { group1, group2, force, d} ;
140+ rules . push ( o ) ;
141+ return o ;
142+
143+ } //create
144+
145+ function applyRules ( ) {
146+
147+ for ( var o of rules ) {
148+ rule ( o . group1 , o . group2 , o . force , o . d ) ;
149+ }
150+
151+ } //applyRules
152+
153+
154+ function draw ( ) {
155+
156+ applyRules ( ) ;
157+
158+
159+ ctx . fillStyle = 'black' ;
160+ ctx . fillRect ( 0 , 0 , width , height ) ;
161+
162+
163+ for ( i = 0 ; i < atoms . length ; i ++ ) {
164+
165+ display ( atoms [ i ] . x , atoms [ i ] . y , atoms [ i ] . color , 5 ) ;
166+
167+ } //for
168+
169+
170+ } //draw
171+
172+ </ script >
0 commit comments