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

ctx.globalCompositeOperation not working as expected #695

Closed
twlite opened this issue Jul 25, 2023 · 4 comments · Fixed by #755
Closed

ctx.globalCompositeOperation not working as expected #695

twlite opened this issue Jul 25, 2023 · 4 comments · Fixed by #755
Assignees
Labels
bug Something isn't working

Comments

@twlite
Copy link
Contributor

twlite commented Jul 25, 2023

Version

  • "@napi-rs/canvas": "^0.1.41"
  • Node.js: v20.0.0
  • OS: Windows 11

How to reproduce?

Let us consider the following as an example for @napi-rs/canvas

import { createCanvas } from '@napi-rs/canvas';
import { writeFile } from 'fs/promises';

async function main() {
  const width = 500,
    height = 500;

  const canvas = createCanvas(width, height);
  const ctx = canvas.getContext('2d');

  ctx.fillStyle = 'red';
  ctx.fillRect(0, 0, width, height);

  ctx.globalCompositeOperation = 'destination-in';
  ctx.beginPath();
  ctx.arc(width / 2, height / 2, width / 2, 0, Math.PI * 2);
  ctx.closePath();
  ctx.fill();

  const buffer = await canvas.encode('png');
  await writeFile(`${__dirname}/output.png`, buffer);
}

main();

and the following for web browser:

<canvas id="canvas" width="500" height="500"></canvas>
const width = 500, height = 500;
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");

ctx.fillStyle = 'red';
ctx.fillRect(0, 0, width, height);

ctx.globalCompositeOperation = 'destination-in';
ctx.beginPath();
ctx.arc(width / 2, height / 2, width / 2, 0, Math.PI * 2);
ctx.closePath();
ctx.fill();

Expectation

The output of the browser looks like this:

image

Result

The output of @napi-rs/canvas looks like this:

image

@Brooooooklyn Brooooooklyn self-assigned this Jul 26, 2023
@Brooooooklyn Brooooooklyn added the bug Something isn't working label Jul 26, 2023
@nieyuyao
Copy link
Contributor

It seems to be the expected behavior for blending of geometries? https://skia.org/docs/user/api/skblendmode_overview/

@Brooooooklyn
Copy link
Owner

It seems to be the expected behavior for blending of geometries?

yes, but we need to align the behavior with the browser

@nieyuyao
Copy link
Contributor

nieyuyao commented Dec 4, 2023

I found that the skia-canvas library solves the similar issue with SkPictureRecorder. https://github.com/meihuanyu/skia-canvas/blob/e60e197f34df3da9f03af4f905acdd553807f511/src/context/mod.rs#L208
Can we refer to it to fix the issue?

@Brooooooklyn
Copy link
Owner

Can we refer to it to fix the issue?

@nieyuyao yes sure

nieyuyao added a commit to nieyuyao/canvas that referenced this issue Dec 6, 2023
Brooooooklyn pushed a commit that referenced this issue Dec 12, 2023
#755)

* wip: fix issue #695

* chore: modify render_canvas to associated function

* test: add some tests

* chore: format codes

* fix: fix clippy errors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants