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

Free drawing over non-transparent .PNG file parts #6964

Closed
PhilCliv opened this issue Mar 30, 2021 · 5 comments
Closed

Free drawing over non-transparent .PNG file parts #6964

PhilCliv opened this issue Mar 30, 2021 · 5 comments
Labels
stale Issue marked as stale by the stale bot

Comments

@PhilCliv
Copy link

The globalCompositeOperation in DrawingMode seems not to be working the way I expect. Or my approach is wrong.
I'm trying to do free drawing only on non-transparent parts of a PNG image.
I've been doing deep searches on how to make it work but with no success. Here is what I have so far:

var canvas = new fabric.Canvas('canvas');
canvas.backgroundColor = '#ffffff';

fabric.Image.fromURL('https://i.pinimg.com/originals/49/af/b1/49afb1d21ae594cb7ac3534a15383711.png', function (myImg) {
    var img1 = myImg.set({ left: 0, top: 0, width: 503, height: 343 });
    canvas.add(img1);
});

canvas.isDrawingMode = 1;
canvas.getContext().globalCompositeOperation = 'source-atop';
canvas.freeDrawingBrush.color = "purple";
canvas.freeDrawingBrush.width = 10;
canvas.renderAll();

Here is an example of the result I would like to have, but it's working in native canvas (without Fabric.js) https://stackoverflow.com/a/49228110/6660372
Any suggestions for a solution using Fabric.js? Thank you!

@PhilCliv PhilCliv changed the title Hand drawing over non-transparent .PNG file parts Free drawing over non-transparent .PNG file parts Mar 30, 2021
@asturur
Copy link
Member

asturur commented Mar 31, 2021

No globalComposite operation won't work because the drawing happens on a separate layer and gets merged later.
You can still do it using the image as a clipPath object.
But i would need to write an example because i m not sure is easy to get there or supported out of the box.
So trying alone could be a huge use of time with uncertain results.
We have at least another issue asking the same thing.

@PhilCliv
Copy link
Author

Thank you for the answer. I tried clipping with the image but it doesn't seem to work like this. Here is the jsfiddle link https://jsfiddle.net/PhilCliv/dpf840qn/14/
Is it something like this you were talking about?

var canvas;

$(function () {
    canvas = window._canvas = new fabric.Canvas('canvas');
    canvas.backgroundColor = '#efefef';
    canvas.isDrawingMode= 1;
    canvas.freeDrawingBrush.color = "purple";
    canvas.freeDrawingBrush.width = 10;
     var imgInstance = fabric.Image.fromURL('https://i.pinimg.com/originals/49/af/b1/49afb1d21ae594cb7ac3534a15383711.png', function (img) {
        canvas.add(img);
        canvas.renderAll();
    });
 
    canvas.renderAll();

    canvas.on("path:created", (e) => {
      var path = e.path;
      path.set({selectable:false})
      path.clipPath = imgInstance;
    });
});

@PhilCliv
Copy link
Author

Or would this approach work as well with Fabric.js ?
"Keep two layers: the one the user is drawing to, and the rendering one.
Draw the user's drawings on the first layer (offscreen canvas) and then do your compositing with the png image on top of this layer on a second canvas, the rendering one
."
https://stackoverflow.com/a/49228110/6660372

@stale
Copy link

stale bot commented Apr 16, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale Issue marked as stale by the stale bot label Apr 16, 2021
@ShaMan123
Copy link
Contributor

#6994

@ShaMan123 ShaMan123 mentioned this issue Apr 21, 2021
@stale stale bot closed this as completed Apr 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale Issue marked as stale by the stale bot
Projects
None yet
Development

No branches or pull requests

3 participants