MCPcopy Create free account
hub / github.com/Code-Bullet/Jump-King / getPath

Function getPath

libraries/p5.js:4538–4606  ·  view source on GitHub ↗
(points)

Source from the content-addressed store, hash-verified

4536
4537// Convert the TrueType glyph outline to a Path.
4538function getPath(points) {
4539 var p = new path.Path();
4540 if (!points) {
4541 return p;
4542 }
4543
4544 var contours = getContours(points);
4545 for (var i = 0; i < contours.length; i += 1) {
4546 var contour = contours[i];
4547 var firstPt = contour[0];
4548 var lastPt = contour[contour.length - 1];
4549 var curvePt;
4550 var realFirstPoint;
4551 if (firstPt.onCurve) {
4552 curvePt = null;
4553 // The first point will be consumed by the moveTo command,
4554 // so skip it in the loop.
4555 realFirstPoint = true;
4556 } else {
4557 if (lastPt.onCurve) {
4558 // If the first point is off-curve and the last point is on-curve,
4559 // start at the last point.
4560 firstPt = lastPt;
4561 } else {
4562 // If both first and last points are off-curve, start at their middle.
4563 firstPt = { x: (firstPt.x + lastPt.x) / 2, y: (firstPt.y + lastPt.y) / 2 };
4564 }
4565
4566 curvePt = firstPt;
4567 // The first point is synthesized, so don't skip the real first point.
4568 realFirstPoint = false;
4569 }
4570
4571 p.moveTo(firstPt.x, firstPt.y);
4572
4573 for (var j = realFirstPoint ? 1 : 0; j < contour.length; j += 1) {
4574 var pt = contour[j];
4575 var prevPt = j === 0 ? firstPt : contour[j - 1];
4576 if (prevPt.onCurve && pt.onCurve) {
4577 // This is a straight line.
4578 p.lineTo(pt.x, pt.y);
4579 } else if (prevPt.onCurve && !pt.onCurve) {
4580 curvePt = pt;
4581 } else if (!prevPt.onCurve && !pt.onCurve) {
4582 var midPt = { x: (prevPt.x + pt.x) / 2, y: (prevPt.y + pt.y) / 2 };
4583 p.quadraticCurveTo(prevPt.x, prevPt.y, midPt.x, midPt.y);
4584 curvePt = pt;
4585 } else if (!prevPt.onCurve && pt.onCurve) {
4586 // Previous point off-curve, this point on-curve.
4587 p.quadraticCurveTo(curvePt.x, curvePt.y, pt.x, pt.y);
4588 curvePt = null;
4589 } else {
4590 throw new Error('Invalid state.');
4591 }
4592 }
4593
4594 if (firstPt !== lastPt) {
4595 // Connect the last and first points

Callers 1

buildPathFunction · 0.85

Calls 1

getContoursFunction · 0.85

Tested by

no test coverage detected