Skip to content

Commit

Permalink
Improve magic number handling
Browse files Browse the repository at this point in the history
  • Loading branch information
otacke committed Aug 6, 2024
1 parent 386f4ba commit 085c1e2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/scripts/components/map/map.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Util from '@services/util.js';
import Path from './path.js';
import { MATH_HUNDRED } from '@services/constants.js';
import './map.scss';

export default class Map {
Expand Down Expand Up @@ -178,7 +177,8 @@ export default class Map {
this.dom.style.getPropertyValue('--stage-height')
);

const fontSize = clientRect.height / MATH_HUNDRED * heightPercentage;
// eslint-disable-next-line no-magic-numbers
const fontSize = clientRect.height / 100 * heightPercentage;

this.dom.style.setProperty(
'--stage-font-size', `calc(${Map.STAGE_BORDER_RADIUS} * ${fontSize}px)`
Expand Down
40 changes: 22 additions & 18 deletions src/scripts/components/map/path.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Util from '@services/util.js';
import { MATH_TWO, MATH_HUNDRED } from '@services/constants.js';
import './path.scss';

export default class Path {
Expand Down Expand Up @@ -189,12 +188,18 @@ export default class Path {
const toX = this.params.telemetryTo.x;
const toY = this.params.telemetryTo.y;

const fromXPx = parseFloat(fromX) / MATH_HUNDRED * params.mapSize.width;
const fromYPx = parseFloat(fromY) / MATH_HUNDRED * params.mapSize.height;
const toXPx = parseFloat(toX) / MATH_HUNDRED * params.mapSize.width;
const toYPx = parseFloat(toY) / MATH_HUNDRED * params.mapSize.height;
const width = parseFloat(fromWidth) / MATH_HUNDRED * params.mapSize.width;
const height = parseFloat(fromHeight) / MATH_HUNDRED * params.mapSize.height;
// eslint-disable-next-line no-magic-numbers
const fromXPx = parseFloat(fromX) / 100 * params.mapSize.width;
// eslint-disable-next-line no-magic-numbers
const fromYPx = parseFloat(fromY) / 100 * params.mapSize.height;
// eslint-disable-next-line no-magic-numbers
const toXPx = parseFloat(toX) / 100 * params.mapSize.width;
// eslint-disable-next-line no-magic-numbers
const toYPx = parseFloat(toY) / 100 * params.mapSize.height;
// eslint-disable-next-line no-magic-numbers
const width = parseFloat(fromWidth) / 100 * params.mapSize.width;
// eslint-disable-next-line no-magic-numbers
const height = parseFloat(fromHeight) / 100 * params.mapSize.height;

const deltaXPx = fromXPx - toXPx;
const deltaYPx = fromYPx - toYPx;
Expand All @@ -204,10 +209,10 @@ export default class Path {

// Distance from center to border
const offsetToBorder = {
x: width / MATH_TWO * Math.cos(angle) *
MATH_HUNDRED / params.mapSize.width,
y: height / MATH_TWO * Math.sin(angle) *
MATH_HUNDRED / params.mapSize.height
// eslint-disable-next-line no-magic-numbers
x: width / 2 * Math.cos(angle) * 100 / params.mapSize.width,
// eslint-disable-next-line no-magic-numbers
y: height / 2 * Math.sin(angle) * 100 / params.mapSize.height
};

// Border width
Expand All @@ -216,18 +221,17 @@ export default class Path {
width * Path.MAX_FACTOR
);

const offsetPathStroke =
strokeWidth / MATH_TWO * MATH_HUNDRED / params.mapSize.height;
// eslint-disable-next-line no-magic-numbers
const offsetPathStroke = strokeWidth / 2 * 100 / params.mapSize.height;

// Position + offset for centering + offset for border (+ stroke offset)
const x = parseFloat(fromX) +
parseFloat(fromWidth) / MATH_TWO +
offsetToBorder.x;
// eslint-disable-next-line no-magic-numbers
parseFloat(fromWidth) / 2 + offsetToBorder.x;

const y = parseFloat(fromY) +
parseFloat(fromHeight) / MATH_TWO +
offsetToBorder.y -
offsetPathStroke;
// eslint-disable-next-line no-magic-numbers
parseFloat(fromHeight) / 2 + offsetToBorder.y - offsetPathStroke;

// Good old Pythagoras
const length = Math.sqrt(
Expand Down
5 changes: 0 additions & 5 deletions src/scripts/services/constants.js

This file was deleted.

0 comments on commit 085c1e2

Please sign in to comment.