Skip to content

context2D closePath on arc closes path to centre x point and not to drawn line start #3293

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

Closed
chinanwu opened this issue Oct 8, 2021 · 5 comments · Fixed by #3304
Closed

Comments

@chinanwu
Copy link

chinanwu commented Oct 8, 2021

I have read and understood the contribution guidelines.

Version: ^2.4.0

The closePath() the context2D seems to be closing on the centre x point of the arc rather than the actual drawn arc line.

Simple example (This can be copied and pasted into the playground https://raw.githack.com/MrRio/jsPDF/master/#):

var doc = new jsPDF();
const canvas = doc.canvas;
const ctx = canvas.getContext('2d')
ctx.beginPath();
ctx.arc(80, 80, 65, 0, Math.PI);
ctx.closePath();
ctx.lineWidth = 6;
ctx.stroke();

Which is grabbed from this page: https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/closePath#closing_just_one_sub-path (You can see the correct behaviour here)
template

Slightly more complex example (can also be copied into the playground):

var doc = new jsPDF();
const canvas = doc.canvas;
const ctx = canvas.getContext('2d')
ctx.beginPath();
ctx.arc(
    106,
    130,
    100,
    -1.5707963267948966,
    -0.3141592653589793,
    false);
ctx.arc(
    106,
    130,
    62.5,
    -0.3141592653589793, 
    -1.5707963267948966, 
    true);
ctx.closePath();
ctx.stroke();

template-2

Here is the jsfiddle page for the expected behaviour: https://jsfiddle.net/p7vmqu6r/1/

@Bl4sio
Copy link
Contributor

Bl4sio commented Oct 9, 2021

Hello!
I'd like to work on this issue.

@Bl4sio
Copy link
Contributor

Bl4sio commented Oct 10, 2021

The main problem, is that the closePath function tries to connect to the x, y coordinates of the first drawn object, but the arc method stores the origin as x and y.
In the second example there is an other problem: the arc won't connect the start point to the previous endpoint.
I think the solution should be to call a drawLine to the beginning of the arc.

@Bl4sio
Copy link
Contributor

Bl4sio commented Oct 10, 2021

The results:
Example1:
first
Example2:
second

The solution is not perfect, as the connection between the line and the beginning of the arc is ugly.
After further investigation I figured that the fill method isn't working as expected:
Actual:
actual
Expected:
expected
Test code:

var doc = new jsPDF();
const canvas = doc.canvas;
const ctx = canvas.getContext('2d')

ctx.beginPath();
ctx.moveTo(80, 80);
ctx.lineTo(80, 100);
ctx.arc(80, 80, 65, 0, Math.PI * 0.8);
ctx.closePath()
ctx.lineWidth = 6;
ctx.fill()
ctx.stroke();

I belive it should be fixed in an other issue.

@HackbrettXXX
Copy link
Collaborator

Thanks for the bug report and the PR. closePath seems to be generally broken, so I'm OK with fixing that in another PR. It seems that closePath doesn't call the PDF close path operator.

Could it be that the fill issue is also related to the "fake closing"?

@Bl4sio
Copy link
Contributor

Bl4sio commented Oct 23, 2021

I don't really know the pdf syntax. But I think this two issue are related.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants