@@ -496,27 +496,63 @@ var Mouse = require('../core/Mouse');
496496 continue ;
497497
498498 var bodyA = constraint . bodyA ,
499- bodyB = constraint . bodyB ;
499+ bodyB = constraint . bodyB ,
500+ start ,
501+ end ;
500502
501503 if ( bodyA ) {
502- c . beginPath ( ) ;
503- c . moveTo ( bodyA . position . x + constraint . pointA . x , bodyA . position . y + constraint . pointA . y ) ;
504+ start = Vector . add ( bodyA . position , constraint . pointA ) ;
504505 } else {
505- c . beginPath ( ) ;
506- c . moveTo ( constraint . pointA . x , constraint . pointA . y ) ;
506+ start = constraint . pointA ;
507507 }
508508
509- if ( bodyB ) {
510- c . lineTo ( bodyB . position . x + constraint . pointB . x , bodyB . position . y + constraint . pointB . y ) ;
509+ if ( constraint . render . type === 'pin' ) {
510+ c . beginPath ( ) ;
511+ c . arc ( start . x , start . y , 4 , 0 , 2 * Math . PI ) ;
512+ c . closePath ( ) ;
511513 } else {
512- c . lineTo ( constraint . pointB . x , constraint . pointB . y ) ;
514+ if ( bodyB ) {
515+ end = Vector . add ( bodyB . position , constraint . pointB ) ;
516+ } else {
517+ end = constraint . pointB ;
518+ }
519+
520+ c . beginPath ( ) ;
521+ c . moveTo ( start . x , start . y ) ;
522+
523+ if ( constraint . render . type === 'spring' ) {
524+ var delta = Vector . sub ( end , start ) ,
525+ normal = Vector . perp ( Vector . normalise ( delta ) ) ,
526+ coils = Math . ceil ( Common . clamp ( constraint . length / 5 , 12 , 20 ) ) ,
527+ offset ;
528+
529+ for ( var j = 0 ; j < coils ; j += 1 ) {
530+ offset = j % 2 === 0 ? 1 : - 1 ;
531+
532+ c . lineTo (
533+ start . x + delta . x * ( j / coils ) + normal . x * offset * 4 ,
534+ start . y + delta . y * ( j / coils ) + normal . y * offset * 4
535+ ) ;
536+ }
537+ }
538+
539+ c . lineTo ( end . x , end . y ) ;
513540 }
514541
515542 if ( constraint . render . lineWidth ) {
516543 c . lineWidth = constraint . render . lineWidth ;
517544 c . strokeStyle = constraint . render . strokeStyle ;
518545 c . stroke ( ) ;
519546 }
547+
548+ if ( constraint . render . anchors ) {
549+ c . fillStyle = constraint . render . strokeStyle ;
550+ c . beginPath ( ) ;
551+ c . arc ( start . x , start . y , 3 , 0 , 2 * Math . PI ) ;
552+ c . arc ( end . x , end . y , 3 , 0 , 2 * Math . PI ) ;
553+ c . closePath ( ) ;
554+ c . fill ( ) ;
555+ }
520556 }
521557 } ;
522558
0 commit comments