@@ -27,9 +27,11 @@ let renderer;
27
27
//lineto renderer objects
28
28
let canvas , context ;
29
29
let physics_element ;
30
+ let render_physics ;
30
31
31
- function init ( phy_el , render_element ) {
32
+ function init ( phy_el , rend_phy , render_element ) {
32
33
physics_element = phy_el ;
34
+ render_physics = rend_phy ;
33
35
const start = Date . now ( ) ;
34
36
engine = Matter . Engine . create ( { enableSleeping :true } ) ;
35
37
engine . world . gravity . y = config . physics . gravity ;
@@ -44,36 +46,38 @@ function init(phy_el,render_element){
44
46
window . addEventListener ( 'graph_edge' , onMatterEdge , false ) ;
45
47
window . addEventListener ( 'engine' , onEngine , false ) ;
46
48
47
- if ( config . physics . renderer . type_lineto ) {
48
- canvas = document . createElement ( 'canvas' ) ;
49
- context = canvas . getContext ( '2d' ) ;
50
- canvas . width = physics_element . offsetWidth ;
51
- canvas . height = physics_element . offsetHeight ;
52
- render_element . appendChild ( canvas ) ;
53
- }
54
- if ( config . physics . renderer . type_native ) {
55
- renderer = Matter . Render . create ( {
56
- element : render_element ,
57
- engine : engine ,
58
- options : {
59
- width : render_element . offsetWidth ,
60
- height : render_element . offsetHeight ,
61
- showAngleIndicator : false ,
62
- showVelocity : true ,
63
- showBounds : true ,
64
- showBroadphase : true ,
65
- showAxes : true ,
66
- showIds : true ,
67
- showCollisions : true ,
68
- showSleeping :true ,
69
- showDebug :false ,
70
- wireframes : true ,
71
- constraintIterations :config . physics . simulation . constraintIterations
72
- //constraintIterations default = 2
73
- //positionIterations default = 6
74
- //velocityIterations default = 4
75
- }
76
- } ) ;
49
+ if ( render_physics ) {
50
+ if ( config . physics . renderer . type_lineto ) {
51
+ canvas = document . createElement ( 'canvas' ) ;
52
+ context = canvas . getContext ( '2d' ) ;
53
+ canvas . width = physics_element . offsetWidth ;
54
+ canvas . height = physics_element . offsetHeight ;
55
+ render_element . appendChild ( canvas ) ;
56
+ }
57
+ if ( config . physics . renderer . type_native ) {
58
+ renderer = Matter . Render . create ( {
59
+ element : render_element ,
60
+ engine : engine ,
61
+ options : {
62
+ width : render_element . offsetWidth ,
63
+ height : render_element . offsetHeight ,
64
+ showAngleIndicator : false ,
65
+ showVelocity : true ,
66
+ showBounds : true ,
67
+ showBroadphase : true ,
68
+ showAxes : true ,
69
+ showIds : true ,
70
+ showCollisions : true ,
71
+ showSleeping :true ,
72
+ showDebug :false ,
73
+ wireframes : true ,
74
+ constraintIterations :config . physics . simulation . constraintIterations
75
+ //constraintIterations default = 2
76
+ //positionIterations default = 6
77
+ //velocityIterations default = 4
78
+ }
79
+ } ) ;
80
+ }
77
81
}
78
82
79
83
if ( config . physics . move_objects_with_mouse ) {
@@ -180,11 +184,13 @@ function run(){
180
184
if ( any_vertex_to_move ) {
181
185
utils . send ( 'graph_edge' , { type :'refresh_all' } ) ;
182
186
}
183
- if ( config . physics . renderer . type_lineto ) {
184
- render_lineto ( engine , context ) ;
185
- }
186
- if ( config . physics . renderer . type_native ) {
187
- Matter . Render . world ( renderer ) ;
187
+ if ( render_physics ) {
188
+ if ( config . physics . renderer . type_lineto ) {
189
+ render_lineto ( engine , context ) ;
190
+ }
191
+ if ( config . physics . renderer . type_native ) {
192
+ Matter . Render . world ( renderer ) ;
193
+ }
188
194
}
189
195
}
190
196
@@ -212,14 +218,21 @@ function edge_add(params){
212
218
let b_2 = engine . world . bodies . find ( body => ( body . id == params . dest ) ) ;
213
219
console . log ( `phy> added edge from '${ b_1 . name } ' to '${ b_2 . name } ' with weight (${ params . weight . toFixed ( 2 ) } )` ) ;
214
220
215
- var constraint = Matter . Constraint . create ( {
216
- bodyA : b_1 ,
217
- bodyB : b_2 ,
218
- length :100 / params . weight ,
219
- stiffness : 0.01 ,
220
- damping : 0.05
221
- } ) ;
222
- Matter . World . addConstraint ( engine . world , constraint ) ;
221
+ let length = 200 ;
222
+ if ( params . weight != 0 ) { //weight of 0 means no edge constraint
223
+ length = 100 / params . weight ;
224
+ if ( length > 500 ) {
225
+ length = 500 ;
226
+ }
227
+ var constraint = Matter . Constraint . create ( {
228
+ bodyA : b_1 ,
229
+ bodyB : b_2 ,
230
+ length :length ,
231
+ stiffness : 0.01 ,
232
+ damping : 0.05
233
+ } ) ;
234
+ Matter . World . addConstraint ( engine . world , constraint ) ;
235
+ }
223
236
}
224
237
225
238
function onMatterEdge ( e ) {
@@ -229,7 +242,9 @@ function onMatterEdge(e){
229
242
}
230
243
231
244
function onResize ( e ) {
232
- if ( config . physics . renderer . type_native ) {
245
+ if ( render_physics ) {
246
+ if ( config . physics . renderer . type_native ) {
247
+ }
233
248
}
234
249
}
235
250
@@ -244,6 +259,11 @@ function onEngine(e){
244
259
engine . world . bodies . forEach ( body => { body . frictionAir = e . detail . frictionAir ; } ) ;
245
260
localStorage . setItem ( "frictionAir" , e . detail . frictionAir ) ;
246
261
}
262
+ if ( typeof ( e . detail . render_physics ) != "undefined" ) {
263
+ console . log ( `phy> render physics = ${ e . detail . render_physics } ` ) ;
264
+ localStorage . setItem ( "render_physics" , e . detail . render_physics ) ;
265
+
266
+ }
247
267
}
248
268
249
269
export { init , run } ;
0 commit comments