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

Fill free drawing path as user is drawing (lasso tool) #7441

Closed
michaelsboost opened this issue Oct 19, 2021 · 3 comments
Closed

Fill free drawing path as user is drawing (lasso tool) #7441

michaelsboost opened this issue Oct 19, 2021 · 3 comments

Comments

@michaelsboost
Copy link

In fabric.js, we can draw free hand paths (http://fabricjs.com/freedrawing) (http://fabricjs.com/fabric-intro-part-4#free_drawing)

(Yes I've already seen this post and it doesn't solve or reference my problem)

What I'm trying to achieve is showing the fill as the user is drawing. Not just when the path is created (as seen in the demo below) which it also sets that fill for all the other paths drawn which I don't want. I ONLY want the fill to apply only to the path that's being drawn and show the fill as it's being drawn.

My question is, "How can I showing the fill as the user is drawing using free drawing in fabric.js?"

var canvas = new fabric.Canvas('c', {
  isDrawingMode: true
});
 
 canvas.on('path:created', function() {
  canvas.getObjects().forEach(o => {
    o.fill = '#000'
  });
  canvas.renderAll();
});
canvas {
  border: 1px solid #ccc;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.6.3/fabric.js"></script>

<canvas id="c" width="600" height="400"></canvas>
@ShaMan123
Copy link
Member

What you are looking for is a custom brush.
Take a look at PencilBrush and BaseBrush.
It should be relatively straight forward extending PencilBrush and adding some logic to closePath on context.
The gist:

  1. When a user is drawing the brush is in charge of rendering what you see.
  2. The brush uses the top context, bottom context is used by fabric.Canvas.
  3. Once drawing is done a path is created and added to canvas.
  4. Top context is cleared.

This is done for performance reasons. Top context is there for that reason.
BTW, EraserBrush is exactly what I have described here, but ever more complicated.

@ShaMan123
Copy link
Member

Take into account there are excellent path utils that you may want to look at when trying to create the lasso.

@michaelsboost
Copy link
Author

I solved my problem.

Brief simplified explanation of the solution:
I created a new class called LassoBrush (very similar to the PencilBrush class except stroke width value is 0, stroke color = null and fill I changed from null to this.color (for which is whatever the user sets) and then is called like so....

  canvas.freeDrawingBrush = new fabric.LassoBrush(canvas);
  canvas.freeDrawingBrush.color = '#000';
  canvas.isDrawingMode = true;

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

No branches or pull requests

2 participants