1
+ <!DOCTYPE html>
2
+ < html lang ="en ">
3
+ < head >
4
+ < title > Verlet Spider web - Sub Protocol</ title >
5
+ < meta name ="author " content ="Sub Protocol " />
6
+ < meta charset ="UTF-8 " />
7
+ < link rel ="stylesheet " href ="../css/style.css " type ="text/css " media ="screen, projection " />
8
+ < script type ="text/javascript " src ="../js/common/vec2.js "> </ script >
9
+ < script type ="text/javascript " src ="../js/common/constraint.js "> </ script >
10
+ < script type ="text/javascript " src ="../js/common/simulation.js "> </ script >
11
+ </ head >
12
+ < body >
13
+ < canvas id ="scratch " style ="width: 800px; height: 500px; background: #000; "> </ canvas >
14
+ < script type ="text/javascript ">
15
+
16
+
17
+ window . onload = function ( ) {
18
+ var canvas = document . getElementById ( "scratch" ) ;
19
+
20
+ // canvas dimensions
21
+ var width = parseInt ( canvas . style . width ) ;
22
+ var height = parseInt ( canvas . style . height ) ;
23
+
24
+ // retina
25
+ var dpr = window . devicePixelRatio || 1 ;
26
+ canvas . width = width * dpr ;
27
+ canvas . height = height * dpr ;
28
+ canvas . getContext ( "2d" ) . scale ( dpr , dpr ) ;
29
+
30
+ // simulation
31
+ var sim = new VerletSimulation ( width , height , canvas ) ;
32
+
33
+ // entities
34
+ var min = Math . min ( width , height ) * 0.5 ;
35
+ var segments = 20 ;
36
+ var cloth = sim . cloth ( new Vec2 ( width / 2 , height / 3 ) , min , min , segments , 6 , 0.9 ) ;
37
+
38
+ cloth . drawConstraints = function ( ctx , composite ) {
39
+ }
40
+
41
+ cloth . drawParticles = function ( ctx , composite ) {
42
+
43
+ var stride = min / segments ;
44
+ var x , y ;
45
+ for ( y = 1 ; y < segments ; ++ y ) {
46
+ for ( x = 1 ; x < segments ; ++ x ) {
47
+ ctx . beginPath ( ) ;
48
+
49
+ var i1 = ( y - 1 ) * segments + x - 1 ;
50
+ var i2 = ( y ) * segments + x ;
51
+
52
+ ctx . moveTo ( cloth . particles [ i1 ] . pos . x , cloth . particles [ i1 ] . pos . y ) ;
53
+ ctx . lineTo ( cloth . particles [ i1 + 1 ] . pos . x , cloth . particles [ i1 + 1 ] . pos . y ) ;
54
+
55
+ ctx . lineTo ( cloth . particles [ i2 ] . pos . x , cloth . particles [ i2 ] . pos . y ) ;
56
+ ctx . lineTo ( cloth . particles [ i2 - 1 ] . pos . x , cloth . particles [ i2 - 1 ] . pos . y ) ;
57
+
58
+ var off = cloth . particles [ i2 ] . pos . x - cloth . particles [ i1 ] . pos . x ;
59
+ off += cloth . particles [ i2 ] . pos . y - cloth . particles [ i1 ] . pos . y ;
60
+ off *= 0.25 ;
61
+
62
+ var coef = Math . round ( ( Math . abs ( off ) / stride ) * 255 ) ;
63
+ if ( coef > 255 )
64
+ coef = 255 ;
65
+
66
+ ctx . fillStyle = "rgba(" + coef + ",0," + ( 255 - coef ) + ",1)" ;
67
+
68
+
69
+
70
+ ctx . fill ( ) ;
71
+ }
72
+ }
73
+
74
+ /*var i;
75
+ for (i in composite.particles) {
76
+ var point = composite.particles[i];
77
+ ctx.beginPath();
78
+ ctx.arc(point.pos.x, point.pos.y, 1.2, 0, 2*Math.PI);
79
+ ctx.fillStyle = "#ff6000";
80
+ ctx.fill();
81
+ }*/
82
+ }
83
+
84
+ // animation loop
85
+ var legIndex = 0 ;
86
+ var loop = function ( ) {
87
+ sim . frame ( 16 ) ;
88
+ sim . draw ( ) ;
89
+ requestAnimFrame ( loop ) ;
90
+ } ;
91
+
92
+ loop ( ) ;
93
+ } ;
94
+
95
+
96
+ </ script >
97
+ </ body >
98
+ </ html >
0 commit comments