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

context.stroke() & context.fill = multi rewrite #168

Open
Nours312 opened this issue Jan 3, 2020 · 2 comments
Open

context.stroke() & context.fill = multi rewrite #168

Nours312 opened this issue Jan 3, 2020 · 2 comments
Labels

Comments

@Nours312
Copy link

Nours312 commented Jan 3, 2020

When I do more than one rect or path, all previous forms are re-write with new style definition :

// red rectangle
ctx.fillStyle = "rgba(255, 0, 0, 0.3)";
ctx.strokeStyle = "rgba(255, 0, 0, 1)";
ctx.rect(x1, y1, w1, h1);
ctx.stroke();
ctx.fill();

// green rectangle
ctx.fillStyle = "rgba(0, 255, 0, 0.3)";
ctx.strokeStyle = "rgba(0, 255, 0, 1)";
ctx.rect(x2, y2, w2, h2);
ctx.stroke();
ctx.fill();

the result is : in first, the red rectangle is drawed, and after one green rectangle overload the first red and a second is drawed.

to obtain a good result I need to use fillRec and strokeRec like this :

// red rectangle
ctx.fillStyle = "rgba(255, 0, 0, 0.3)";
ctx.strokeStyle = "rgba(255, 0, 0, 1)";
ctx.fillRect(x1, y1, w1, h1);
ctx.strokeRect(x1, y1, w1, h1);

// green rectangle
ctx.fillStyle = "rgba(0, 255, 0, 0.3)";
ctx.strokeStyle = "rgba(0, 255, 0, 1)";
ctx.fillRect(x2, y2, w2, h2);
ctx.strokeRect(x2, y2, w2, h2);

same issue with path, arc and other draw function who need fill and stroke, all path are re drawed all times with last defined styles.

@iddan iddan added the bug label Jan 3, 2020
@iddan
Copy link
Owner

iddan commented Jan 3, 2020

Thank you for the extensive report. Can you provide a codesandbox with the expected behaviour? PRs are welcome.

@nora-soderlund
Copy link
Contributor

Not a bug. This is how CanvasRenderingContext2D is logically meant to work. What you should've done is used paths, e.g.:

const canvas = document.getElementById("canvas");
const context = canvas.getContext("2d");

context.beginPath();
context.fillStyle = "rgba(255, 0, 0, 0.3)";
context.strokeStyle = "rgba(255, 0, 0, 1)";
context.rect(0, 0, 50, 50);
context.stroke();
context.fill();
context.closePath();

// green rectangle
context.beginPath();
context.fillStyle = "rgba(0, 255, 0, 0.3)";
context.strokeStyle = "rgba(0, 255, 0, 1)";
context.rect(25, 25, 50, 50);
context.stroke();
context.fill();
context.closePath();

Suggest we close this issue.

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

No branches or pull requests

3 participants