Skip to content

Commit 0952516

Browse files
committed
added constraint.render.type and constraint.render.anchor
1 parent 08aa3f2 commit 0952516

2 files changed

Lines changed: 80 additions & 17 deletions

File tree

src/constraint/Constraint.js

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,6 @@ var Common = require('../core/Common');
5151

5252
constraint.length = typeof constraint.length !== 'undefined' ? constraint.length : length;
5353

54-
// render
55-
var render = {
56-
visible: true,
57-
lineWidth: 2,
58-
strokeStyle: '#ffffff'
59-
};
60-
61-
constraint.render = Common.extend(render, constraint.render);
62-
6354
// option defaults
6455
constraint.id = constraint.id || Common.nextId();
6556
constraint.label = constraint.label || 'Constraint';
@@ -71,6 +62,24 @@ var Common = require('../core/Common');
7162
constraint.angleB = constraint.bodyB ? constraint.bodyB.angle : constraint.angleB;
7263
constraint.plugin = {};
7364

65+
// render
66+
var render = {
67+
visible: true,
68+
lineWidth: 2,
69+
strokeStyle: '#ffffff',
70+
type: 'line',
71+
anchors: true
72+
};
73+
74+
if (constraint.length === 0) {
75+
render.type = 'pin';
76+
render.anchors = false;
77+
} else if (constraint.stiffness < 0.9) {
78+
render.type = 'spring';
79+
}
80+
81+
constraint.render = Common.extend(render, constraint.render);
82+
7483
return constraint;
7584
};
7685

@@ -353,6 +362,24 @@ var Common = require('../core/Common');
353362
* @default a random colour
354363
*/
355364

365+
/**
366+
* A `String` that defines the constraint rendering type.
367+
* The possible values are 'line', 'pin', 'spring'.
368+
* An appropriate render type will be automatically chosen unless one is given in options.
369+
*
370+
* @property render.type
371+
* @type string
372+
* @default 'line'
373+
*/
374+
375+
/**
376+
* A `Boolean` that defines if the constraint's anchor points should be rendered.
377+
*
378+
* @property render.anchors
379+
* @type boolean
380+
* @default true
381+
*/
382+
356383
/**
357384
* The first possible `Body` that this constraint is attached to.
358385
*

src/render/Render.js

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)