Skip to content

Commit

Permalink
ffs
Browse files Browse the repository at this point in the history
  • Loading branch information
kurkle committed Jan 17, 2022
1 parent dd13774 commit de506f6
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 13 deletions.
20 changes: 11 additions & 9 deletions src/types/polygon.js
Expand Up @@ -3,8 +3,8 @@ import {PI, RAD_PER_DEG} from 'chart.js/helpers';
import {setBorderStyle, resolvePointPosition, getElementCenterPoint, setShadowStyle} from '../helpers';

export default class PolygonAnnotation extends Element {
inRange(x, y) {
return pointIsInPolygon(this.elements, x, y);
inRange(mouseX, mouseY, useFinalPosition) {
return this.elements.length > 1 && pointIsInPolygon(this.elements, mouseX, mouseY, useFinalPosition);
}

getCenterPoint(useFinalPosition) {
Expand Down Expand Up @@ -38,7 +38,7 @@ export default class PolygonAnnotation extends Element {
}

resolveElementProperties(chart, options) {
const {x, y} = resolvePointPosition(chart, options);
const {x, y, width, height} = resolvePointPosition(chart, options);
const {sides, radius, rotation, borderWidth} = options;
const halfBorder = borderWidth / 2;
const elements = [];
Expand All @@ -54,11 +54,11 @@ export default class PolygonAnnotation extends Element {
x: x + sin * radius,
y: y - cos * radius,
bX: x + sin * (radius + halfBorder),
by: y - cos * (radius + halfBorder)
bY: y - cos * (radius + halfBorder)
}
});
}
return {x, y, elements, initProperties: {x, y}};
return {x, y, width, height, elements, initProperties: {x, y}};
}
}

Expand Down Expand Up @@ -101,13 +101,15 @@ PolygonAnnotation.defaultRoutes = {
};


function pointIsInPolygon(points, x, y) {
function pointIsInPolygon(points, x, y, useFinalPosition) {
let isInside = false;
for (let i = 0, j = points.length - 1; i < points.length; j = i++) {
if ((points[i].bY > y) !== (points[j].bY > y) &&
x < (points[j].bX - points[i].bX) * (y - points[i].bY) / (points[j].nY - points[i].nY) + points[i].bX) {
let A = points[points.length - 1].getProps(['bX', 'bY'], useFinalPosition);
for (const point of points) {
const B = point.getProps(['bX', 'bY'], useFinalPosition);
if ((B.bY > y) !== (A.bY > y) && x < (A.bX - B.bX) * (y - B.bY) / (A.bY - B.bY) + B.bX) {
isInside = !isInside;
}
A = B;
}
return isInside;
}
4 changes: 0 additions & 4 deletions test/fixtures/box/labelPadding.js
Expand Up @@ -23,7 +23,6 @@ module.exports = {
yMax: 10,
backgroundColor: 'rgba(255, 99, 132, 0.5)',
borderColor: 'rgba(255, 99, 132)',
borderDash: [6, 6],
borderWidth: 5,
label: {
enabled: true,
Expand All @@ -40,7 +39,6 @@ module.exports = {
yMax: 15,
backgroundColor: 'rgba(255, 99, 132, 0.5)',
borderColor: 'rgba(255, 99, 132)',
borderDash: [6, 6],
borderWidth: 5,
label: {
enabled: true,
Expand All @@ -57,7 +55,6 @@ module.exports = {
yMax: 20,
backgroundColor: 'rgba(255, 99, 132, 0.5)',
borderColor: 'rgba(255, 99, 132)',
borderDash: [6, 6],
borderWidth: 5,
label: {
enabled: true,
Expand All @@ -74,7 +71,6 @@ module.exports = {
yMax: 9,
backgroundColor: 'rgba(255, 99, 132, 0.5)',
borderColor: 'rgba(255, 99, 132)',
borderDash: [6, 6],
borderWidth: 5,
label: {
enabled: true,
Expand Down
Binary file modified test/fixtures/box/labelPadding.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit de506f6

Please sign in to comment.