Matter.Render
The Matter.Render module is a lightweight, optional utility which provides a simple canvas based renderer for visualising instances of Matter.Engine.
It is intended for development and debugging purposes, but may also be suitable for simple games.
It includes a number of drawing options including wireframe, vector with support for sprites and viewports.
Methods
Matter.Render._getPixelRatio
Gets the pixel ratio of the canvas.
Parameters
-
canvasHTMLElement
Returns
pixel ratio
Matter.Render._getTexture
Gets the requested texture (an Image) via its path
Returns
texture
Matter.Render._mean
Returns the mean value of the given numbers.
Parameters
-
valuesNumber[]
Returns
the mean of given values
Matter.Render.applyBackground
Applies the background to the canvas using CSS.
Matter.Render.bodies
Description
Parameters
-
renderRender -
bodiesBody[] -
contextRenderingContext
Matter.Render.bodyAxes
Draws body angle indicators and axes
Parameters
-
renderRender -
bodiesBody[] -
contextRenderingContext
Matter.Render.bodyBounds
Draws body bounds
Parameters
-
renderRender -
bodiesBody[] -
contextRenderingContext
Matter.Render.bodyConvexHulls
Optimised method for drawing body convex hull wireframes in one pass
Parameters
-
renderRender -
bodiesBody[] -
contextRenderingContext
Matter.Render.bodyIds
Draws body ids
Parameters
-
renderRender -
bodiesBody[] -
contextRenderingContext
Matter.Render.bodyPositions
Draws body positions
Parameters
-
renderRender -
bodiesBody[] -
contextRenderingContext
Matter.Render.bodyVelocity
Draws body velocity
Parameters
-
renderRender -
bodiesBody[] -
contextRenderingContext
Matter.Render.bodyWireframes
Optimised method for drawing body wireframes in one pass
Parameters
-
renderRender -
bodiesBody[] -
contextRenderingContext
Matter.Render.collisions
Description
Parameters
-
renderRender -
pairsPair[] -
contextRenderingContext
Matter.Render.constraints
Description
Parameters
-
constraintsConstraint[] -
contextRenderingContext
Matter.Render.create
Creates a new renderer. The options parameter is an object that specifies any properties you wish to override the defaults.
All properties have default values, and many are pre-calculated automatically based on other properties.
See the properties section below for detailed information on what you can pass via the options object.
Parameters
-
[options]Object optional
Returns
A new renderer
Matter.Render.endViewTransform
Resets all transforms on the render context.
Parameters
-
renderRender
Matter.Render.inspector
Description
Parameters
-
inspectorInspector -
contextRenderingContext
Matter.Render.lookAt
Positions and sizes the viewport around the given object bounds. Objects must have at least one of the following properties:
object.boundsobject.positionobject.minandobject.maxobject.xandobject.y
Matter.Render.mousePosition
Renders mouse position.
Parameters
-
renderRender -
mouseMouse -
contextRenderingContext
Matter.Render.performance
Renders engine and render performance information.
Parameters
-
renderRender -
contextRenderingContext
Matter.Render.run
Continuously updates the render canvas on the requestAnimationFrame event.
Parameters
-
renderRender
Matter.Render.separations
Description
Parameters
-
renderRender -
pairsPair[] -
contextRenderingContext
Matter.Render.setPixelRatio
Sets the pixel ratio of the renderer and updates the canvas.
To automatically detect the correct ratio, pass the string 'auto' for pixelRatio.
Matter.Render.setSize
Sets the render width and height.
Updates the canvas accounting for render.options.pixelRatio.
Updates the bottom right render bound render.bounds.max relative to the provided width and height.
The top left render bound render.bounds.min isn't changed.
Follow this call with Render.lookAt if you need to change the render bounds.
See also Render.setPixelRatio.
Parameters
Matter.Render.startViewTransform
Applies viewport transforms based on render.bounds to a render context.
Parameters
-
renderRender
Matter.Render.stats
Renders statistics about the engine and world useful for debugging.
Parameters
-
renderRender -
contextRenderingContext -
timeNumber
Matter.Render.status
Renders a label, indicator and a chart.
Parameters
Matter.Render.stop
Ends execution of Render.run on the given render, by canceling the animation frame request event loop.
Parameters
-
renderRender
Matter.Render.vertexNumbers
Renders body vertex numbers.
Parameters
-
renderRender -
bodiesBody[] -
contextRenderingContext
Properties / Options
The following properties if specified below are for objects created by Matter.Render.create and may be passed to it as options.
A Bounds object that specifies the drawing view region.
Rendering will be automatically transformed and scaled to fit within the canvas size (render.options.width and render.options.height).
This allows for creating views that can pan or zoom around the scene.
You must also set render.options.hasBounds to true to enable bounded rendering.
Render.canvas
HTMLCanvasElement
The canvas element to render to. If not specified, one will be created if render.element has been specified.
Default: null
Render.context
CanvasRenderingContext2D
The 2d rendering context from the render.canvas element.
Render.element
HTMLElement
A reference to the element where the canvas is to be inserted (if render.canvas has not been specified)
Default: null
A reference to the Matter.Engine instance to be used.
The mouse to render if render.options.showMousePosition is enabled.
Default: null
Render.options
The configuration options of the renderer.
Render.options.background
String
A CSS background color string to use when render.options.wireframes is disabled.
This may be also set to 'transparent' or equivalent.
Default: '#14151f'
Render.options.hasBounds
Boolean
A flag that specifies if render.bounds should be used when rendering.
Default: false
Render.options.height
Number
The target height in pixels of the render.canvas to be created.
See also the options.pixelRatio property to change render quality.
Default: 600
A flag to enable or disable the body angle debug overlay.
Default: false
Render.options.showAxes
Boolean
A flag to enable or disable the body axes debug overlay.
Default: false
Render.options.showBounds
Boolean
A flag to enable or disable the body bounds debug overlay.
Default: false
Render.options.showBroadphase
Boolean
A flag to enable or disable the collision broadphase debug overlay.
Default: false
Render.options.showCollisions
Boolean
A flag to enable or disable the body collisions debug overlay.
Default: false
Render.options.showConvexHulls
Boolean
A flag to enable or disable the body convex hulls debug overlay.
Default: false
Render.options.showDebug
Boolean
A flag to enable or disable all debug information overlays together.
This includes and has priority over the values of:
render.options.showStatsrender.options.showPerformance
Default: false
Render.options.showDebug
Boolean
A flag to enable or disable the debug information overlay.
Default: false
Render.options.showIds
Boolean
A flag to enable or disable the body and part ids debug overlay.
Default: false
Render.options.showInternalEdges
Boolean
A flag to enable or disable the body internal edges debug overlay.
Default: false
Render.options.showMousePosition
Boolean
A flag to enable or disable the mouse position debug overlay.
Default: false
Render.options.showPerformance
Boolean
A flag to enable or disable performance charts.
From left to right, the values shown are:
- average render frequency (e.g. 60 fps)
- exact engine delta time used for last update (e.g. 16.66ms)
- average updates per frame (e.g. 1)
- average engine execution duration (e.g. 5.00ms)
- average render execution duration (e.g. 0.40ms)
- average effective play speed (e.g. '1.00x' is 'real-time')
Each value is recorded over a fixed sample of past frames (60 frames).
A chart shown below each value indicates the variance from the average over the sample. The more stable or fixed the value is the flatter the chart will appear.
Default: false
Render.options.showPositions
Boolean
A flag to enable or disable the body positions debug overlay.
Default: false
Render.options.showSeparations
Boolean
A flag to enable or disable the collision resolver separations debug overlay.
Default: false
Render.options.showSleeping
Boolean
A flag to enable or disable sleeping bodies indicators.
Default: true
Render.options.showStats
Boolean
A flag to enable or disable the engine stats info overlay.
From left to right, the values shown are:
- body parts total
- body total
- constraints total
- composites total
- collision pairs total
Default: false
Render.options.showVelocity
Boolean
A flag to enable or disable the body velocity debug overlay.
Default: false
Render.options.showVertexNumbers
Boolean
A flag to enable or disable the body vertex numbers debug overlay.
Default: false
Render.options.width
Number
The target width in pixels of the render.canvas to be created.
See also the options.pixelRatio property to change render quality.
Default: 800
A CSS color string to use for background when render.options.wireframes is enabled.
This may be also set to 'transparent' or equivalent.
Default: '#14151f'
Render.options.wireframes
Boolean
A flag to toggle wireframe rendering otherwise solid fill rendering is used.
Default: true
A CSS color string to use for stroke when render.options.wireframes is enabled.
This may be also set to 'transparent' or equivalent.
Default: '#bbb'
Events
The following events are emitted by objects created by Matter.Render.create and received by objects that have subscribed using Matter.Events.on.