Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Zero pointBorderWidth still drawing border #5180

Closed
wants to merge 8 commits into from

Conversation

jhaenchen
Copy link
Contributor

@jhaenchen jhaenchen commented Jan 24, 2018

This PR fixes the fact that a pointBorderWidth property of zero still draws a border around the point. It does this by checking if the pointBorderWidth is zero. If yes, it changes the stroke of the point to be completely transparent.

Before with a zero width red point border:
https://codepen.io/anon/pen/xpNbbN
image

After with a zero width red point border:
image

@jhaenchen jhaenchen changed the title If zero borderWidth set stroke to opaque Fix Zero pointBorderWidth still drawing border Jan 24, 2018
@jhaenchen
Copy link
Contributor Author

The reason we have to do this is because of this line which tries to set the canvas context line width to 0, which is not a valid value and so is ignored.

@etimberg etimberg added this to the Version 2.8 milestone Jan 25, 2018
@@ -79,7 +79,11 @@ module.exports = Element.extend({
return;
}

ctx.strokeStyle = vm.borderColor || defaultColor;

ctx.strokeStyle = helpers.valueOrDefault(vm.borderWidth, defaults.global.elements.point.borderWidth) === 0 ?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would only compute the line width once which improves performance and minification

etimberg
etimberg previously approved these changes Jan 25, 2018
@jhaenchen
Copy link
Contributor Author

Sorry about that @etimberg just want to put in a comment because I was confused when assigning 0 and seeing 1 remain.

@jhaenchen
Copy link
Contributor Author

This should be good to merge.

etimberg
etimberg previously approved these changes Feb 1, 2018
@benmccann
Copy link
Contributor

@jhaenchen would you be able to rebase this PR? thanks!

@benmccann
Copy link
Contributor

@jhaenchen just a quick reminder that this PR will need to be rebased

ctx.strokeStyle = vm.borderColor || defaultColor;
ctx.lineWidth = helpers.valueOrDefault(vm.borderWidth, defaults.global.elements.point.borderWidth);
var finalLineWidth = helpers.valueOrDefault(vm.borderWidth, defaults.global.elements.point.borderWidth);
ctx.strokeStyle = finalLineWidth === 0 ? 'rgba(0,0,0,0)' : vm.borderColor || defaultColor;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jhaenchen these lines don't line up. you have two spaces here where there should be a tab

@@ -79,8 +79,9 @@ module.exports = Element.extend({

// Clipping for Points.
if (chartArea === undefined || (model.x >= chartArea.left && chartArea.right * errMargin >= model.x && model.y >= chartArea.top && chartArea.bottom * errMargin >= model.y)) {
ctx.strokeStyle = vm.borderColor || defaultColor;
ctx.lineWidth = helpers.valueOrDefault(vm.borderWidth, defaults.global.elements.point.borderWidth);
var finalLineWidth = helpers.valueOrDefault(vm.borderWidth, defaults.global.elements.point.borderWidth);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you need an intermediate finalLineWidth variable here? can you do this instead:

ctx.lineWidth = helpers.valueOrDefault(vm.borderWidth, defaults.global.elements.point.borderWidth);
ctx.strokeStyle = ctx.lineWidth === 0 ? 'rgba(0,0,0,0)' : vm.borderColor || defaultColor;

@benmccann
Copy link
Contributor

The PR description says "it changes the stroke of the point to be completely opaque". I'm guessing you meant "transparent" instead? Why draw it transparently instead of just not drawing it at all?

@benmccann
Copy link
Contributor

@jhaenchen a reminder to take another look at this PR when you get a chance

@jhaenchen
Copy link
Contributor Author

@benmccann I'll take a look tonight, sorry.

@jhaenchen
Copy link
Contributor Author

jhaenchen commented Aug 21, 2018

@benmccann

I'm guessing you meant "transparent" instead?

Yep, sorry.

Why draw it transparently instead of just not drawing it at all?

You cannot set a stroke width of zero hence the bug in the first place (stroke width zero is ignored), and so no matter what you have a stroke when drawing a point. Therefore, in order to draw the point, we also have to draw the stroke, so the next best thing is a completely transparent stroke.

Also, I've fixed your other comments. Thank you for your patience!

@benmccann
Copy link
Contributor

It seems like we would need this fix in element.rectangle.js and element.arc.js as well

@jhaenchen
Copy link
Contributor Author

@benmccann would you like me to add that to this PR?

@benmccann
Copy link
Contributor

Yes, that would be great if you can do that

Copy link
Member

@simonbrunel simonbrunel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jhaenchen I think the latest code doesn't work (see my comment), can you please verify? Ideally, we will also need unit tests.

@@ -79,7 +79,7 @@ module.exports = Element.extend({

// Clipping for Points.
if (chartArea === undefined || (model.x >= chartArea.left && chartArea.right * errMargin >= model.x && model.y >= chartArea.top && chartArea.bottom * errMargin >= model.y)) {
ctx.strokeStyle = vm.borderColor || defaultColor;
ctx.strokeStyle = ctx.lineWidth === 0 ? 'rgba(0,0,0,0)' : vm.borderColor || defaultColor;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are trying to access ctx.lineWidth before assigning it a value (next line), is that expected?

Though, I think @benmccann suggestion was wrong because according the MDN:

lineWidth: A number specifying the line width in space units. Zero, negative, Infinity and NaN values are ignored.

image

So ctx.lineWidth will never match ctx.lineWidth === 0 (because ignored) so I would keep your first approach and cache the lineWidth value (and check for lineWidth === null or undefined using !lineWidth):

// ...
var errMargin = 1.01;
var lineWidth;

//...

lineWidth = helpers.valueOrDefault(vm.borderWidth, defaults.global.elements.point.borderWidth);
ctx.strokeStyle = !lineWidth ? 'rgba(0,0,0,0)' : vm.borderColor || defaultColor;
ctx.lineWidth = lineWidth;

@benmccann
Copy link
Contributor

@jhaenchen would you be able to address Simon's comments?

@benmccann
Copy link
Contributor

@jhaenchen just a reminder that there's a comment that will need to be addressed before we can merge

@benmccann
Copy link
Contributor

@jhaenchen this PR will need to be rebased. Don't forget about Simon's comment either. We'd love to get this merged

@simonbrunel simonbrunel removed this from the Version 2.8 milestone Mar 1, 2019
@benmccann
Copy link
Contributor

@jhaenchen we'd love to merge this, but haven't gotten any response, so are going to close this PR as inactive. Please let us know if you're able to find the time to revisit and we can reopen this and work with you to get it in

@benmccann benmccann closed this Apr 4, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants