|
782 | 782 | // then draw everything in results |
783 | 783 | // get all text elements in the svg in case collision detection is needed |
784 | 784 | var textList = Polymer.dom(svgToDraw).querySelectorAll('text') |
785 | | - textArray = Array.prototype.slice.call(textList) |
786 | | - var currY = y - ((results.length - 1) * vertParam + 1) * (this.lineGap + this.fullHeight) * this.textSize |
787 | | - if (!avoidCollision || resultWidths.every(function (width, index) { |
| 785 | + var oldTextArray = Array.prototype.slice.call(textList) |
| 786 | + // then store all new labels in newTextArray for collision detection |
| 787 | + var newTextArray = [] |
| 788 | + var currY = y - (results.length - 1) * vertParam * |
| 789 | + (this.lineGap + this.fullHeight) * this.textSize |
| 790 | + results.forEach(function (line, index) { |
| 791 | + newTextArray.push(this.drawText( |
| 792 | + x, currY, line, textAnchor, params, svgToDraw |
| 793 | + )) |
788 | 794 | currY += (this.lineGap + this.fullHeight) * this.textSize |
789 | | - var left = x |
790 | | - var right = x |
791 | | - switch (textAnchor) { |
792 | | - case 'start': |
793 | | - right += width |
794 | | - break |
795 | | - case 'end': |
796 | | - left -= width |
797 | | - break |
798 | | - case 'middle': |
799 | | - right += width / 2 |
800 | | - left -= width / 2 |
801 | | - break |
802 | | - } |
803 | | - return textArray.every(function (text, indexT) { |
804 | | - var rect = text.getBBox() |
805 | | - return (rect.x >= right || (rect.x + rect.width) <= left || |
806 | | - rect.y > (currY + (this.lineGap + this.fullHeight) * this.textSize) || |
807 | | - (rect.y + rect.height) < currY) |
| 795 | + }, this) |
| 796 | + |
| 797 | + // collision detection, if collision is detected, remove all new labels |
| 798 | + // because there should not be many text elements within the labeling |
| 799 | + // area, a brute-force approach should be enough |
| 800 | + if (avoidCollision && newTextArray.some(function (newNode) { |
| 801 | + var newBox = newNode.getBBox() |
| 802 | + return oldTextArray.some(function (oldNode) { |
| 803 | + var oldBox = oldNode.getBBox() |
| 804 | + return (newBox.x + newBox.width > oldBox.x) && |
| 805 | + (oldBox.x + oldBox.width > newBox.x) && |
| 806 | + (newBox.y + newBox.height > oldBox.y) && |
| 807 | + (oldBox.y + oldBox.height > newBox.y) |
808 | 808 | }, this) |
809 | 809 | }, this)) { |
810 | | - currY = y - (results.length - 1) * vertParam * (this.lineGap + this.fullHeight) * this.textSize |
811 | | - results.forEach(function (line, index) { |
812 | | - this.drawText(x, currY, line, textAnchor, params, svgToDraw) |
813 | | - currY += (this.lineGap + this.fullHeight) * this.textSize |
| 810 | + // collision found, remove everything from the newTextArray |
| 811 | + newTextArray.forEach(function (textElem) { |
| 812 | + this.removeElement(textElem, svgToDraw) |
814 | 813 | }, this) |
815 | 814 | } |
816 | 815 | }, |
|
0 commit comments