phaser
Version:
A fast, free and fun HTML5 Game Framework for Desktop and Mobile web browsers from the team at Phaser Studio Inc.
79 lines (67 loc) • 2.09 kB
JavaScript
/**
* @author Richard Davey <rich@phaser.io>
* @author Igor Ognichenko <ognichenko.igor@gmail.com>
* @copyright 2013-2026 Phaser Studio Inc.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
/**
* @ignore
*/
var copy = function (out, a)
{
out[0] = a[0];
out[1] = a[1];
return out;
};
/**
* Takes a Polygon object and applies Chaikin's smoothing algorithm on its points.
*
* Chaikin's algorithm is a corner-cutting technique that replaces each edge between
* two consecutive points with two new points positioned along that edge, resulting in
* a smoother curve. Each iteration of the algorithm increases the total number of points
* in the polygon. The original first and last points are preserved, while all intermediate
* points are replaced by pairs of new points at 85% and 15% along each edge.
*
* The polygon is modified in-place and the same polygon object is returned.
*
* @function Phaser.Geom.Polygon.Smooth
* @since 3.13.0
*
* @generic {Phaser.Geom.Polygon} O - [polygon,$return]
*
* @param {Phaser.Geom.Polygon} polygon - The polygon to be smoothed. The polygon will be modified in-place and returned.
*
* @return {Phaser.Geom.Polygon} The input polygon.
*/
var Smooth = function (polygon)
{
var i;
var points = [];
var data = polygon.points;
for (i = 0; i < data.length; i++)
{
points.push([ data[i].x, data[i].y ]);
}
var output = [];
if (points.length > 0)
{
output.push(copy([ 0, 0 ], points[0]));
}
for (i = 0; i < points.length - 1; i++)
{
var p0 = points[i];
var p1 = points[i + 1];
var p0x = p0[0];
var p0y = p0[1];
var p1x = p1[0];
var p1y = p1[1];
output.push([ 0.85 * p0x + 0.15 * p1x, 0.85 * p0y + 0.15 * p1y ]);
output.push([ 0.15 * p0x + 0.85 * p1x, 0.15 * p0y + 0.85 * p1y ]);
}
if (points.length > 1)
{
output.push(copy([ 0, 0 ], points[points.length - 1]));
}
return polygon.setTo(output);
};
module.exports = Smooth;