Search code examples
javascriptphysics-enginematter.js

Why is this character required in matter.js?


I am trying to port matter.js to another language. Javascript is not my strongest language.

This function supposedly parses a string containing ordered x y pairs separated by spaces (and optionally commas) into a particular object:

 * @method fromPath
 * @param {string} path
 * @param {body} body
 * @return {vertices} vertices
 */
Vertices.fromPath = function(path, body) {
    var pathPattern = /L?\s*([\-\d\.e]+)[\s,]*([\-\d\.e]+)*/ig,
        points = [];

    path.replace(pathPattern, function(match, x, y) {
        points.push({ x: parseFloat(x), y: parseFloat(y) });
    });

    return Vertices.create(points, body);
};

Later, there is a call to this function where the following string is passed to Vertices.fromPath:

vertices: Vertices.fromPath('L 0 0 L ' + width + ' 0 L ' + width + ' ' + height + ' L 0 ' + height)

Where width and height are numbers. What is the purpose of the L character? I thought the method was supposed to split comma or space separated x,y pairs into numbers so I can't figure out the relevance of the L character.

Can anybody enlighten me?


Solution

  • L is a "Line To" command. Please refer to the SVG Path syntax (MDN, spec), which matter.js can accept as a path specification.