11import React , { Component } from 'react' ;
22import PropTypes from 'prop-types' ;
33
4- import Matter , { Render , Engine , Events } from 'matter-js' ;
4+ import Matter , { Engine , Events } from 'matter-js' ;
55
66export default class World extends Component {
77 static propTypes = {
@@ -11,13 +11,6 @@ export default class World extends Component {
1111 y : PropTypes . number ,
1212 scale : PropTypes . number ,
1313 } ) ,
14- debug : PropTypes . shape ( {
15- offset : PropTypes . shape ( {
16- x : PropTypes . number ,
17- y : PropTypes . number ,
18- } ) ,
19- background : PropTypes . string ,
20- } ) ,
2114 onCollision : PropTypes . func ,
2215 onInit : PropTypes . func ,
2316 onUpdate : PropTypes . func ,
@@ -36,8 +29,6 @@ export default class World extends Component {
3629
3730 static contextTypes = {
3831 scale : PropTypes . number ,
39- renderWidth : PropTypes . number ,
40- renderHeight : PropTypes . number ,
4132 loop : PropTypes . object ,
4233 } ;
4334
@@ -55,92 +46,6 @@ export default class World extends Component {
5546 this . lastTime = currTime ;
5647 } ;
5748
58- onInit = ( ...args ) => {
59- if ( this . props . debug ) {
60- this . setupDebugRenderer ( ) ;
61- }
62-
63- this . props . onInit ( ...args ) ;
64- } ;
65-
66- stopDebugRendering = ( ) => {
67- if ( this . _render ) {
68- Render . stop ( this . _render ) ;
69- delete this . _render ;
70- }
71- } ;
72-
73- getDebugProps = ( ) => {
74- const debugProps = Object . assign (
75- {
76- offset : { } ,
77- // transparent background to see sprites, etc, in the world
78- background : 'rgba(0, 0, 0, 0)' ,
79- } ,
80- this . props . debug || { }
81- ) ;
82-
83- debugProps . offset = Object . assign (
84- {
85- x : 0 ,
86- y : 0 ,
87- } ,
88- debugProps . offset
89- ) ;
90-
91- return debugProps ;
92- } ;
93-
94- setupDebugRenderer = ( ) => {
95- if ( ! this . _debugRenderElement ) {
96- return ;
97- }
98-
99- const { renderWidth, renderHeight, scale } = this . context ;
100- const { offset, background } = this . getDebugProps ( ) ;
101-
102- const width = renderWidth / scale ;
103- const height = renderHeight / scale ;
104-
105- this . _render = Render . create ( {
106- canvas : this . _debugRenderElement ,
107- // Auto-zoom the canvas to the correct game area
108- bounds : {
109- min : {
110- x : offset . x ,
111- y : offset . y ,
112- } ,
113- max : {
114- x : offset . x + width ,
115- y : offset . y + height ,
116- } ,
117- } ,
118- options : {
119- wireframeBackground : background ,
120- width : renderWidth ,
121- height : renderHeight ,
122- } ,
123- } ) ;
124-
125- // Setting this as part of `.create` crashes Chrome during a deep clone. :/
126- // My guess: a circular reference
127- this . _render . engine = this . engine ;
128-
129- Render . run ( this . _render ) ;
130- } ;
131-
132- getCanvasRef = element => {
133- this . _debugRenderElement = element ;
134-
135- if ( element ) {
136- if ( ! this . _render ) {
137- this . setupDebugRenderer ( ) ;
138- }
139- } else {
140- this . stopDebugRendering ( ) ;
141- }
142- } ;
143-
14449 constructor ( props ) {
14550 super ( props ) ;
14651
@@ -160,41 +65,11 @@ export default class World extends Component {
16065 if ( gravity !== this . props . gravity ) {
16166 this . engine . world . gravity = gravity ;
16267 }
163-
164- if ( ! nextProps . debug ) {
165- this . stopDebugRendering ( ) ;
166- }
167- }
168-
169- componentDidUpdate ( ) {
170- if ( this . props . debug && this . _render ) {
171- const { renderWidth, renderHeight, scale } = this . context ;
172-
173- const { offset } = this . getDebugProps ( ) ;
174-
175- // When context changes (eg; `scale` due to a window resize),
176- // re-calculate the world stage
177- this . _render . options . width = renderWidth ;
178- this . _render . options . height = renderHeight ;
179-
180- this . _render . bounds = {
181- min : {
182- x : offset . x ,
183- y : offset . y ,
184- } ,
185- max : {
186- x : offset . x + renderWidth / scale ,
187- y : offset . y + renderHeight / scale ,
188- } ,
189- } ;
190-
191- Render . world ( this . _render ) ;
192- }
19368 }
19469
19570 componentDidMount ( ) {
19671 this . loopID = this . context . loop . subscribe ( this . loop ) ;
197- this . onInit ( this . engine ) ;
72+ this . props . onInit ( this . engine ) ;
19873 Events . on ( this . engine , 'afterUpdate' , this . props . onUpdate ) ;
19974 Events . on ( this . engine , 'collisionStart' , this . props . onCollision ) ;
20075 }
@@ -203,7 +78,6 @@ export default class World extends Component {
20378 this . context . loop . unsubscribe ( this . loopID ) ;
20479 Events . off ( this . engine , 'afterUpdate' , this . props . onUpdate ) ;
20580 Events . off ( this . engine , 'collisionStart' , this . props . onCollision ) ;
206- this . stopDebugRendering ( ) ;
20781 }
20882
20983 getChildContext ( ) {
@@ -221,27 +95,6 @@ export default class World extends Component {
22195 width : '100%' ,
22296 } ;
22397
224- const { renderWidth, renderHeight, scale } = this . context ;
225-
226- let debugRenderTarget = false ;
227-
228- if ( this . props . debug ) {
229- debugRenderTarget = (
230- < canvas
231- key = "debug-render-target"
232- style = { { position : 'relative' } }
233- width = { renderWidth }
234- height = { renderHeight }
235- ref = { this . getCanvasRef }
236- />
237- ) ;
238- }
239-
240- return (
241- < div style = { defaultStyles } >
242- { this . props . children }
243- { debugRenderTarget }
244- </ div >
245- ) ;
98+ return < div style = { defaultStyles } > { this . props . children } </ div > ;
24699 }
247100}
0 commit comments