Skip to content

Commit 55ed55a

Browse files
committed
render options per object/shape
1 parent ba4db99 commit 55ed55a

File tree

1 file changed

+66
-26
lines changed

1 file changed

+66
-26
lines changed

ext/stage-p2.js

+66-26
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@
132132

133133
if (obj instanceof p2.Body && obj.shapes.length) {
134134
if (obj.concavePath && !this.options.debugPolygons) {
135-
var texture = this.drawConvex(obj.concavePath);
135+
var texture = this.drawConvex(obj.concavePath, obj.render);
136136
Stage.image(texture).appendTo(obj.ui).pin({
137137
handle : 0.5,
138138
offsetX : obj.shapeOffsets[i] ? obj.shapeOffsets[i][0] : 0,
@@ -143,32 +143,30 @@
143143
} else {
144144
for (var i = 0; i < obj.shapes.length; i++) {
145145
var shape = obj.shapes[i];
146+
var options = shape.render || obj.render;
146147

147148
var texture = null;
148149
if (shape instanceof p2.Circle) {
149-
texture = this.drawCircle(shape.radius);
150+
texture = this.drawCircle(shape.radius, options);
150151

151152
} else if (shape instanceof p2.Particle) {
152-
texture = this.drawCircle(2 * this.options.get('lineWidth'), {
153-
lineColor : "",
154-
fillColor : this.options.lineColor
155-
});
153+
texture = this.drawParticle(options);
156154

157155
} else if (shape instanceof p2.Plane) {
158-
texture = this.drawPlane(-10, 10, 10);
156+
texture = this.drawPlane(-10, 10, 10, options);
159157

160158
} else if (shape instanceof p2.Line) {
161-
texture = this.drawLine(shape.length);
159+
texture = this.drawLine(shape.length, options);
162160

163161
} else if (shape instanceof p2.Rectangle) {
164-
texture = this.drawRectangle(shape.width, shape.height);
162+
texture = this.drawRectangle(shape.width, shape.height, options);
165163

166164
} else if (shape instanceof p2.Capsule) {
167-
texture = this.drawCapsule(shape.length, shape.radius);
165+
texture = this.drawCapsule(shape.length, shape.radius, options);
168166

169167
} else if (shape instanceof p2.Convex) {
170168
if (shape.vertices.length) {
171-
texture = this.drawConvex(shape.vertices);
169+
texture = this.drawConvex(shape.vertices, options);
172170
}
173171
}
174172
Stage.image(texture).appendTo(obj.ui).pin({
@@ -181,7 +179,7 @@
181179
}
182180

183181
} else if (obj instanceof p2.Spring) {
184-
var texture = this.drawSpring(obj.restLength);
182+
var texture = this.drawSpring(obj.restLength, obj.render);
185183
Stage.image(texture).appendTo(obj.ui).pin({
186184
handle : 0.5
187185
});
@@ -195,8 +193,9 @@
195193

196194
Viewer.prototype.drawLine = function(length, options) {
197195
options = this.options.extend(options);
198-
var lineWidth = options.get('lineWidth'), lineColor = options
199-
.get('lineColor'), fillColor = options.get('fillColor');
196+
var lineWidth = options.get('lineWidth');
197+
var lineColor = options.get('lineColor');
198+
var fillColor = options.get('fillColor');
200199

201200
lineWidth *= 2;
202201
var ratio = options.ratio;
@@ -218,8 +217,9 @@
218217

219218
Viewer.prototype.drawRectangle = function(w, h, options) {
220219
options = this.options.extend(options);
221-
var lineWidth = options.get('lineWidth'), lineColor = options
222-
.get('lineColor'), fillColor = options.get('fillColor');
220+
var lineWidth = options.get('lineWidth');
221+
var lineColor = options.get('lineColor');
222+
var fillColor = options.get('fillColor');
223223

224224
var width = w + 2 * lineWidth;
225225
var height = h + 2 * lineWidth;
@@ -243,8 +243,44 @@
243243

244244
Viewer.prototype.drawCircle = function(radius, options) {
245245
options = this.options.extend(options);
246-
var lineWidth = options.get('lineWidth'), lineColor = options
247-
.get('lineColor'), fillColor = options.get('fillColor');
246+
var lineWidth = options.get('lineWidth');
247+
var lineColor = options.get('lineColor');
248+
var fillColor = options.get('fillColor');
249+
250+
var width = radius * 2 + lineWidth * 2;
251+
var height = radius * 2 + lineWidth * 2;
252+
var ratio = options.ratio;
253+
254+
return Stage.canvas(function(ctx) {
255+
this.size(width, height, ratio);
256+
257+
ctx.scale(ratio, ratio);
258+
ctx.beginPath();
259+
ctx.arc(width / 2, height / 2, radius, 0, 2 * Math.PI);
260+
if (fillColor) {
261+
ctx.fillStyle = fillColor;
262+
ctx.fill();
263+
}
264+
265+
if (lineColor) {
266+
ctx.moveTo(radius + lineWidth, radius + lineWidth);
267+
ctx.lineTo(lineWidth, radius + lineWidth);
268+
269+
ctx.lineWidth = lineWidth;
270+
ctx.strokeStyle = lineColor;
271+
ctx.stroke();
272+
}
273+
});
274+
};
275+
276+
Viewer.prototype.drawParticle = function(options) {
277+
options = this.options.extend(options);
278+
279+
var lineWidth = options.get('lineWidth');
280+
var lineColor = '';
281+
var fillColor = options.get('fillColor') || options.get('lineColor');
282+
283+
var radius = 2 * options.get('lineWidth');
248284

249285
var width = radius * 2 + lineWidth * 2;
250286
var height = radius * 2 + lineWidth * 2;
@@ -274,8 +310,9 @@
274310

275311
Viewer.prototype.drawCapsule = function(len, radius, options) {
276312
options = this.options.extend(options);
277-
var lineWidth = options.get('lineWidth'), lineColor = options
278-
.get('lineColor'), fillColor = options.get('fillColor');
313+
var lineWidth = options.get('lineWidth');
314+
var lineColor = options.get('lineColor');
315+
var fillColor = options.get('fillColor');
279316

280317
var width = len + 2 * radius + 2 * lineWidth;
281318
var height = 2 * radius + 2 * lineWidth;
@@ -306,8 +343,9 @@
306343

307344
Viewer.prototype.drawSpring = function(length, options) {
308345
options = this.options.extend(options);
309-
var lineWidth = options.get('lineWidth'), lineColor = options
310-
.get('lineColor'), fillColor = options.get('fillColor');
346+
var lineWidth = options.get('lineWidth');
347+
var lineColor = options.get('lineColor');
348+
var fillColor = options.get('fillColor');
311349

312350
length = Math.max(length, lineWidth * 10);
313351

@@ -346,8 +384,9 @@
346384

347385
Viewer.prototype.drawPlane = function(x0, x1, max, options) {
348386
options = this.options.extend(options);
349-
var lineWidth = options.get('lineWidth'), lineColor = options
350-
.get('lineColor'), fillColor = options.get('fillColor');
387+
var lineWidth = options.get('lineWidth');
388+
var lineColor = options.get('lineColor');
389+
var fillColor = options.get('fillColor');
351390

352391
var ratio = options.ratio;
353392

@@ -381,8 +420,9 @@
381420

382421
Viewer.prototype.drawConvex = function(verts, options) {
383422
options = this.options.extend(options);
384-
var lineWidth = options.get('lineWidth'), lineColor = options
385-
.get('lineColor'), fillColor = options.get('fillColor');
423+
var lineWidth = options.get('lineWidth');
424+
var lineColor = options.get('lineColor');
425+
var fillColor = options.get('fillColor');
386426

387427
if (!verts.length) {
388428
return;

0 commit comments

Comments
 (0)