grommet
Version:
focus on the essential experience
35 lines (34 loc) • 1.68 kB
JavaScript
var POST_DECIMAL_DIGITS = 10;
export var baseUnit = 24;
export var polarToCartesian = function polarToCartesian(centerX, centerY, radius, angleInDegrees) {
var angleInRadians = (angleInDegrees - 90) * Math.PI / 180.0;
return {
x: centerX + radius * Math.cos(angleInRadians),
y: centerY + radius * Math.sin(angleInRadians)
};
};
export var arcCommands = function arcCommands(centerX, centerY, radius, startAngle, endAngle) {
// handle that we can't draw a complete circle
var normalizedEndAngle = endAngle;
/*
added endAngle - startAngle >= 360
for SemiCircle the endAngle will never be greater then startAngle
since it starts with a startAngle of 270.
*/
if (endAngle > startAngle && endAngle - startAngle >= 360) {
normalizedEndAngle = startAngle + 359.99;
}
var start = polarToCartesian(centerX, centerY, radius, normalizedEndAngle);
var end = polarToCartesian(centerX, centerY, radius, startAngle);
var arcSweep = normalizedEndAngle - startAngle <= 180 ? '0' : '1';
var d = ['M', start.x.toFixed(POST_DECIMAL_DIGITS), start.y.toFixed(POST_DECIMAL_DIGITS), 'A', radius.toFixed(POST_DECIMAL_DIGITS), radius.toFixed(POST_DECIMAL_DIGITS), 0, arcSweep, 0, end.x.toFixed(POST_DECIMAL_DIGITS), end.y.toFixed(POST_DECIMAL_DIGITS)].join(' ');
return d;
};
/* TranslatedEngAngle will now take the value of the
startAngle + anglePer * value and mod by 360. This was added
to take account the startAngle not being 0. So no matter the
value it will be % 360 to get the correct angle.
*/
export var translateEndAngle = function translateEndAngle(startAngle, anglePer, value) {
return Math.max(0, startAngle + anglePer * value) % 360;
};