Bike Fit Analyzer

Home / Developers

bikefit-angles

v1.0.0·ES module, no dependencies·Browser and Node 18+

The measuring core of Bike Fit Analyzer as a package. The site runs this exact code. Give it 2D points from any pose model and it returns the five fit angles, finds the bottom of each pedal stroke, and grades the result against targets for six bike types.

Install

From npm:

npm i bikefit-angles

Or from GitHub Packages, which has the same code under a scoped name. Point the @doniwirawan scope at GitHub in your project's .npmrc, then install:

# .npmrc
@doniwirawan:registry=https://npm.pkg.github.com

npm i @doniwirawan/bikefit-angles

GitHub's registry asks for a token with read:packages even for public packages.

Without a bundler, import it straight from a CDN:

import { sideFrameAngles, analyzeSide } from 'https://cdn.jsdelivr.net/npm/bikefit-angles@1.0.0/index.js';

TypeScript types ship with the package.

Quick start

import { sideFrameAngles, analyzeSide } from 'bikefit-angles';

// One side's joints, in any consistent unit. visibility (0-1) is optional.
const angles = sideFrameAngles({
  shoulder: { x: 512, y: 210 }, elbow: { x: 590, y: 300 }, wrist: { x: 660, y: 330 },
  hip: { x: 400, y: 330 }, knee: { x: 470, y: 470 }, ankle: { x: 455, y: 600 },
});
// { knee_flexion_bdc, torso_from_horiz, elbow_flexion, shoulder_angle, hip_angle }

// A whole clip, one entry per frame
const report = analyzeSide(frames, { bikeType: 'road_endurance' });
// frames: [{ t: seconds, ankleY: ankle y in image coordinates, angles }, ...]
// report: { angles, grades, verdict, strokes, hipTop, bdcFrames, tdcFrames } or null

analyzeSide returns null when there are fewer than five usable frames or no clear pedal strokes. Check for it before reading the result.

With MediaPipe Pose

The helpers read MediaPipe's 33-point layout directly. Lock onto the side facing the camera using confidence summed over the whole clip, so one noisy frame can't switch the measurement to the far leg.

import { PoseLandmarker } from '@mediapipe/tasks-vision';
import { sidePoints, sideConfidence, sideFrameAngles, analyzeSide } from 'bikefit-angles';

const frames = [];
const confidence = { left: 0, right: 0 };

for (const { t, result } of everyFrame) {   // your loop over the video
  const lm = result.landmarks[0];
  if (!lm) continue;
  confidence.left += sideConfidence(lm, 'left');
  confidence.right += sideConfidence(lm, 'right');
  const side = confidence.left >= confidence.right ? 'left' : 'right';

  // MediaPipe gives 0-1 coordinates; pass the frame size to get pixels
  const p = sidePoints(lm, side, video.videoWidth, video.videoHeight);
  frames.push({ t, ankleY: p.ankle.y, angles: sideFrameAngles(p) });
}

const report = analyzeSide(frames, { bikeType: 'gravel' });
if (report) console.log(report.verdict, report.angles.knee_flexion_bdc, report.grades.knee_flexion_bdc);

Pass pixels, not 0-1 coordinates, whenever the frame isn't square. Normalised x and y are scaled differently, which bends every angle.

For good input, film the rider square to the side, with the camera at hip height, pedalling steadily for 20 to 30 seconds. How to film a bike fit video covers the details.

Other pose models

The angle functions only need six named points for one side, so any model works: MoveNet, YOLO-pose, OpenPose, or your own. Map its keypoints to shoulder, elbow, wrist, hip, knee and ankle, and pass the model's confidence as visibility.

// COCO-17 keypoints (MoveNet, YOLO-pose), left side
const k = (i) => ({ x: kp[i].x, y: kp[i].y, visibility: kp[i].score });
const p = { shoulder: k(5), elbow: k(7), wrist: k(9), hip: k(11), knee: k(13), ankle: k(15) };
frames.push({ t, ankleY: p.ankle.y, angles: sideFrameAngles(p) });

A joint below MIN_VISIBILITY (0.5) turns every angle that uses it into NaN, and the medians skip NaN. An occluded far-side limb never makes it into the result.

Targets and grading

Each angle is graded against a zone, written as [greenLow, greenHigh, redBelow, redAbove] in degrees. Inside the green range is GREEN, beyond either red edge is RED, and anything in between is AMBER. A missing reading is GRAY. The verdict is the worst of the four grades.

MeasurementKeyGreenRed outsideSource
Knee flexion at the bottom of the strokeknee_flexion_bdc30–40°28 / 42°Holmes method, raised ~8° for pedalling video
Elbow flexionelbow_flexion15–30°8 / 45°Holmes method
Shoulder (torso to upper arm)shoulder_angle80–95°70 / 105°Fitter's rule of thumb
Torso from horizontaltorso_from_horizBy bike typeBy bike typeFitter's rule of thumb
Hip at the top of the strokehipTop~55–65°Not gradedBurt (2014), road bikes, reported only

Only the torso target changes with the bike:

bikeTypeTorso greenRed outside
road_endurance (default)40–50°34 / 56°
road_race32–42°28 / 48°
tt_tri12–28°8 / 34°
gravel42–52°36 / 58°
mtb46–58°40 / 64°
city55–70°48 / 78°

To grade against your own targets, start from zonesFor() and override what you need:

import { zonesFor, analyzeSide } from 'bikefit-angles';

const zones = { ...zonesFor('road_race'), knee_flexion_bdc: [33, 43, 30, 46] };
const report = analyzeSide(frames, { zones });

Where each number comes from: the fit angles explained and the research notes.

Front and rear views (beta)

A clip filmed from the front or the back gives three readings. Knee tracking is the frontal-plane projection angle (FPPA), signed so that positive means toward the midline. Pelvic rock is the range of hip tilt. Symmetry is the left/right difference.

import { frontalFrameMetrics, analyzeFrontal, FRONTAL } from 'bikefit-angles';

const metrics = everyLandmarkSet.map((lm) => frontalFrameMetrics(lm, { width, height }));
const r = analyzeFrontal(metrics, { width });

if (!r.ok) console.log(r.reason);   // 'too-few-frames' or 'not-frontal'
else {
  const flagKnee = (leg) => leg && leg.peak > FRONTAL.fppa;
  console.log(flagKnee(r.left), flagKnee(r.right), r.rock > FRONTAL.rock);
}

not-frontal means the hips are less than 4% of the frame width apart, so the clip is really a side view. Flag thresholds sit at the top of the healthy range: knee tracking 10°, pelvic rock 12°, left/right difference 9°. These readings have been checked against the literature and against synthetic poses, but not yet against real footage. Treat them as hints, not verdicts.

API reference

A Point is { x, y, visibility? } in any consistent unit. A grade is one of 'GREEN', 'AMBER', 'RED', 'GRAY'.

sideFrameAngles(points, { minVisibility })

The five angles for one frame, in degrees.

points
{ shoulder, elbow, wrist, hip, knee, ankle } for one side.
minVisibility
Default 0.5. A joint below it gives NaN for every angle that uses it.
returns
{ knee_flexion_bdc, torso_from_horiz, elbow_flexion, shoulder_angle, hip_angle }. Knee and elbow are flexion, so 0° is a straight limb.

analyzeSide(frames, { bikeType, zones })

Finds the strokes, takes the median angles at the bottom of each one, and grades them.

frames
[{ t, ankleY, angles }]: time in seconds, ankle y in image coordinates (y grows downward, NaN if unseen), and sideFrameAngles() output.
bikeType
One of the six keys above. Default 'road_endurance'.
zones
Your own zones. Overrides bikeType.
returns
{ angles, grades, verdict, strokes, hipTop, bdcFrames, tdcFrames }, or null with fewer than five frames or no clear strokes. bdcFrames and tdcFrames are the frame indices of the bottom and top of each stroke.

sidePoints(landmarks, side, width, height)

Picks one side's six joints out of a 33-point MediaPipe / BlazePose array. Multiplies by width and height (default 1) and keeps visibility.

sideConfidence(landmarks, side)

The summed visibility of one side's six joints. Add it up over the clip and measure whichever side is larger.

frontalFrameMetrics(landmarks, { width, height, minVisibility })

One frame's frontal readings from a 33-point array: { L, R, hipTilt, hipSpan }. L and R are { fppa, medialSign }, or null when that leg is not visible.

analyzeFrontal(frames, { width })

Summarises a front or rear clip. Returns { ok: false, reason }, or { ok: true, left, right, rock, frames }, where left and right are { peak, median } (peak is the 90th percentile).

grade(value, zone) · worst(...grades)

Grade one value against a zone, and pick the worst of several grades (RED > AMBER > GREEN > GRAY).

angleAt(a, b, c) · torsoFromHorizontal(hip, shoulder)

The interior angle at b of the path a, b, c (NaN if a segment has no length), and the torso angle from horizontal, 0–90° whichever way the rider faces.

findPeaks(y, minDist, prominence) · median(values)

Indices of local maxima at least minDist apart that stand at least prominence above the lower of their two neighbouring minima. Median skips non-finite values and returns null when none are left.

Constants

ZONES
The default side-view zones.
BIKE_TYPES
Torso zone per bike type.
zonesFor(bikeType)
ZONES with that bike type's torso zone.
HIP_TOP_REFERENCE
[55, 65], for display only.
FRONTAL
{ fppa: 10, rock: 12, sym: 9 }, the beta flag thresholds.
SIDE_LANDMARKS
MediaPipe indices for each side's six joints.
MIN_VISIBILITY
0.5.

Limits

These are 2D angles from a single camera. A clip filmed off-axis stretches or squashes them, and reach suffers most. Knee flexion is the most reliable reading, and shoulder and reach are the least. The results are a starting point for adjusting a bike or booking a fitter, not a replacement for a professional fit, and not medical advice. The limits of 2D analysis goes further.

Licence

Source-available under PolyForm Shield 1.0.0. You may use it, including commercially, but not to build or provide a product that competes with Bike Fit Analyzer. It is not open source.