Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
0ada4af
Properly consider the border thickness on the annotations' events
stockiNail Jan 10, 2022
c3457a8
adds test cases on label annotation
stockiNail Jan 10, 2022
9c45201
changes line label inRange calculation
stockiNail Jan 10, 2022
6da4d1f
uses testEvents to test events considering border and not
stockiNail Jan 10, 2022
952476d
removes decomposition of not used properties
stockiNail Jan 10, 2022
cc81827
reduces similar code on events test file
stockiNail Jan 10, 2022
7aa9a91
Merge remote-tracking branch 'origin/master' into eventsAndThickness
stockiNail Jan 12, 2022
97d77e7
refactoring of event test file
stockiNail Jan 12, 2022
3a406f2
applies tests one pixel in the other direction not triggering the hooks
stockiNail Jan 12, 2022
87bb9db
sets borderWidth:0
stockiNail Jan 12, 2022
f7d4e6b
adds additional common tests
stockiNail Jan 13, 2022
93dd210
optimizes the invocation of common code among the specs
stockiNail Jan 13, 2022
921b542
adds test cases removing borderWidth at runtime
stockiNail Jan 13, 2022
875d317
Merge remote-tracking branch 'origin/master' into eventsAndThickness
stockiNail Jan 14, 2022
3022e73
reduces test cases
stockiNail Jan 17, 2022
854fca1
refactoring of events test cases
stockiNail Jan 17, 2022
6f147a9
Merge remote-tracking branch 'origin/master' into eventsAndThickness
stockiNail Jan 17, 2022
dcda29f
remove comment
stockiNail Jan 17, 2022
d411d95
fixes ellipse spec test case
stockiNail Jan 17, 2022
8673e25
fixes annotation type
stockiNail Jan 17, 2022
183192d
fixes polygon event test case
stockiNail Jan 17, 2022
520ef65
adds test cases, removed before cleaning up
stockiNail Jan 17, 2022
eb6c36c
removes useless test case because internal
stockiNail Jan 17, 2022
68de971
adds test cases for an undefined borderwidth
stockiNail Jan 17, 2022
f4cfdca
remove useless options
stockiNail Jan 17, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions src/helpers/helpers.core.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@ export function clampAll(obj, from, to) {
return obj;
}

export function inPointRange(point, center, radius) {
export function inPointRange(point, center, radius, borderWidth) {
if (!point || !center || radius <= 0) {
return false;
}
return (Math.pow(point.x - center.x, 2) + Math.pow(point.y - center.y, 2)) <= Math.pow(radius, 2);
const hBorderWidth = borderWidth / 2 || 0;
return (Math.pow(point.x - center.x, 2) + Math.pow(point.y - center.y, 2)) <= Math.pow(radius + hBorderWidth, 2);
}

export function inBoxRange(mouseX, mouseY, {x, y, width, height}) {
return mouseX >= x &&
mouseX <= x + width &&
mouseY >= y &&
mouseY <= y + height;
export function inBoxRange(mouseX, mouseY, {x, y, width, height}, borderWidth) {
const hBorderWidth = borderWidth / 2 || 0;
return mouseX >= x - hBorderWidth &&
mouseX <= x + width + hBorderWidth &&
mouseY >= y - hBorderWidth &&
mouseY <= y + height + hBorderWidth;
}

export function getElementCenterPoint(element, useFinalPosition) {
Expand Down
2 changes: 1 addition & 1 deletion src/types/box.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {drawBox, drawLabel, getRelativePosition, measureLabelSize, getRectCenter

export default class BoxAnnotation extends Element {
inRange(mouseX, mouseY, useFinalPosition) {
return inBoxRange(mouseX, mouseY, this.getProps(['x', 'y', 'width', 'height'], useFinalPosition));
return inBoxRange(mouseX, mouseY, this.getProps(['x', 'y', 'width', 'height'], useFinalPosition), this.options.borderWidth);
}

getCenterPoint(useFinalPosition) {
Expand Down
7 changes: 4 additions & 3 deletions src/types/ellipse.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {getRectCenterPoint, getChartRect, setBorderStyle, setShadowStyle} from '
export default class EllipseAnnotation extends Element {

inRange(mouseX, mouseY, useFinalPosition) {
return pointInEllipse({x: mouseX, y: mouseY}, this.getProps(['x', 'y', 'width', 'height'], useFinalPosition), this.options.rotation);
return pointInEllipse({x: mouseX, y: mouseY}, this.getProps(['width', 'height'], useFinalPosition), this.options.rotation, this.options.borderWidth);
}

getCenterPoint(useFinalPosition) {
Expand Down Expand Up @@ -68,7 +68,7 @@ EllipseAnnotation.defaultRoutes = {
backgroundColor: 'color'
};

function pointInEllipse(p, ellipse, rotation) {
function pointInEllipse(p, ellipse, rotation, borderWidth) {
const {width, height} = ellipse;
const center = ellipse.getCenterPoint(true);
const xRadius = width / 2;
Expand All @@ -79,9 +79,10 @@ function pointInEllipse(p, ellipse, rotation) {
}
// https://stackoverflow.com/questions/7946187/point-and-ellipse-rotated-position-test-algorithm
const angle = toRadians(rotation || 0);
const hBorderWidth = borderWidth / 2 || 0;
const cosAngle = Math.cos(angle);
const sinAngle = Math.sin(angle);
const a = Math.pow(cosAngle * (p.x - center.x) + sinAngle * (p.y - center.y), 2);
const b = Math.pow(sinAngle * (p.x - center.x) - cosAngle * (p.y - center.y), 2);
return (a / Math.pow(xRadius, 2)) + (b / Math.pow(yRadius, 2)) <= 1;
return (a / Math.pow(xRadius + hBorderWidth, 2)) + (b / Math.pow(yRadius + hBorderWidth, 2)) <= 1;
}
2 changes: 1 addition & 1 deletion src/types/label.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {Element} from 'chart.js';
export default class LabelAnnotation extends Element {

inRange(mouseX, mouseY, useFinalPosition) {
return inBoxRange(mouseX, mouseY, this.getProps(['x', 'y', 'width', 'height'], useFinalPosition));
return inBoxRange(mouseX, mouseY, this.getProps(['x', 'y', 'width', 'height'], useFinalPosition), this.options.borderWidth);
}

getCenterPoint(useFinalPosition) {
Expand Down
5 changes: 3 additions & 2 deletions src/types/line.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ export default class LineAnnotation extends Element {
}
const {labelX, labelY, labelWidth, labelHeight, labelRotation} = this.getProps(['labelX', 'labelY', 'labelWidth', 'labelHeight', 'labelRotation'], useFinalPosition);
const {x, y} = rotated({x: mouseX, y: mouseY}, {x: labelX, y: labelY}, -labelRotation);
const w2 = labelWidth / 2;
const h2 = labelHeight / 2;
const hBorderWidth = this.options.label.borderWidth / 2 || 0;
const w2 = labelWidth / 2 + hBorderWidth;
const h2 = labelHeight / 2 + hBorderWidth;
return x >= labelX - w2 && x <= labelX + w2 &&
y >= labelY - h2 && y <= labelY + h2;
}
Expand Down
2 changes: 1 addition & 1 deletion src/types/point.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default class PointAnnotation extends Element {

inRange(mouseX, mouseY, useFinalPosition) {
const {width} = this.getProps(['width'], useFinalPosition);
return inPointRange({x: mouseX, y: mouseY}, this.getCenterPoint(useFinalPosition), width / 2 + this.options.borderWidth);
return inPointRange({x: mouseX, y: mouseY}, this.getCenterPoint(useFinalPosition), width / 2, this.options.borderWidth);
}

getCenterPoint(useFinalPosition) {
Expand Down
9 changes: 5 additions & 4 deletions src/types/polygon.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {setBorderStyle, resolvePointPosition, getElementCenterPoint, setShadowSt
export default class PolygonAnnotation extends Element {

inRange(mouseX, mouseY, useFinalPosition) {
const vertices = getVertices(this.getProps(['x', 'y'], useFinalPosition), this.options);
const vertices = getVertices(this.getProps(['x', 'y'], useFinalPosition), this.options, true);
return vertices && vertices.length > 0 && pointIsInPolygon(vertices, mouseX, mouseY);
}

Expand Down Expand Up @@ -81,15 +81,16 @@ PolygonAnnotation.defaultRoutes = {
backgroundColor: 'color'
};

function getVertices(point, options) {
function getVertices(point, options, useBorderWidth = false) {
const {sides, radius} = options;
const hBorderWidth = useBorderWidth ? options.borderWidth / 2 || 0 : 0;
let angle = (2 * PI) / sides;
let rad = options.rotation * RAD_PER_DEG;
const vertices = new Array();
addVertex(vertices, point, rad, radius);
addVertex(vertices, point, rad, radius + hBorderWidth);
for (let i = 0; i < sides; i++) {
rad += angle;
addVertex(vertices, point, rad, radius);
addVertex(vertices, point, rad, radius + hBorderWidth);
}
return vertices;
}
Expand Down
Loading