From 56e8927e96b7701b1a20d3baca0b4db2bf52daf6 Mon Sep 17 00:00:00 2001 From: Hatem Alimam Date: Fri, 12 Feb 2021 10:07:45 +0300 Subject: [PATCH] adjust text collisions --- src/components/Airport/index.js | 55 +++++++++++++++++++++++++++++++- src/components/RunwayId/index.js | 16 ++++++++-- 2 files changed, 67 insertions(+), 4 deletions(-) diff --git a/src/components/Airport/index.js b/src/components/Airport/index.js index 4f54db6..3de183f 100644 --- a/src/components/Airport/index.js +++ b/src/components/Airport/index.js @@ -13,6 +13,9 @@ class Airport extends React.Component { this.onTooltipShow = this.onTooltipShow.bind(this); this.onTooltipHide = this.onTooltipHide.bind(this); + this.adjustTextCollisions = this.adjustTextCollisions.bind(this); + + this.adjusted = {}; this.state = { showTooltip: false, @@ -22,6 +25,56 @@ class Airport extends React.Component { }; } + adjustTextCollisions() { + const runwaysTexts = document.getElementsByClassName('runway-id'); + for (let i = 0; i < runwaysTexts.length; i++) { + for (let j = 0; j < runwaysTexts.length; j++) { + const self = runwaysTexts[i]; + const selfBoundingRect = self.getBoundingClientRect(); + const that = runwaysTexts[j]; + if (self != that) { + const thatBoundingRect = that.getBoundingClientRect(); + if ( + !( + thatBoundingRect.left > selfBoundingRect.right || + thatBoundingRect.right < selfBoundingRect.left || + thatBoundingRect.top > selfBoundingRect.bottom || + thatBoundingRect.bottom < selfBoundingRect.top + ) + ) { + const id = that.getAttribute('data-id'); + if (!this.adjusted[id]) { + const style = window.getComputedStyle(that); + const matrix = new WebKitCSSMatrix(style.transform); + + const isNegative = Math.sign(matrix.m42) === -1; + + const transform = that.getAttribute('transform'); + const hasRotate = transform.includes('rotate(180)'); + + let translate; + if (!isNegative) { + translate = matrix.m42 + 20; + } else { + translate = matrix.m42 - 20; + } + + if (hasRotate) + that.setAttribute('transform', `translate(0, ${translate}) rotate(180)`); + else that.setAttribute('transform', `translate(0, ${translate})`); + this.adjusted[id] = true; + this.adjusted[self.getAttribute('data-id')] = true; + } + } + } + } + } + } + + componentDidMount() { + this.adjustTextCollisions(); + } + onTooltipShow(event, text) { if (text) { this.setState({ @@ -95,7 +148,7 @@ class Airport extends React.Component { const minDimension = Math.min(VIEW_BOX_WIDTH, VIEW_BOX_HEIGHT); // ratio of the runways - this.ratio = (1 / maxRunwayLen) * (minDimension - 200); + this.ratio = (1 / maxRunwayLen) * (minDimension - 190); } render() { diff --git a/src/components/RunwayId/index.js b/src/components/RunwayId/index.js index b67ce74..3ecdcaf 100644 --- a/src/components/RunwayId/index.js +++ b/src/components/RunwayId/index.js @@ -46,10 +46,20 @@ class RunwayId extends React.Component { if (idx === 1) transform = `translate(0, -${t}) rotate(180)`; - const textX = idx === 1 ? (-1 * width) / 4 : (-1 * width) / 1; + const textX = idx === 1 ? (-1 * width) / 4 : (-1 * width) / 4; return ( - - + this.adjustOverlapedTexts()} + data-id={`${name}-${idx}`} + > + {name} {showPatternIndicator && (