I have an HTML5 Canvas. I am using the KineticJS(KonvaJS) canvas library. On a blank canvas I dram an image as shown in the figure below. Now I want to create a circle Shape which can be used to erase parts of the image. The red circle in the image is the eraser.
How can I erase parts of an Image on HTML5 Canvas?
You can use Compositing to "erase" pixels.
Specifically you use
destination-out
compositing.KineticJS does not support compositing, but you still have a couple of options:
(Note: KineticJS has become KonvaJS and I haven't checked whether KonvaJs supports compositing. If it now does, just use
destination-out
compositing inside KonvaJS)Option#1: Use a native canvas element as your Kinetic.Image source
Create an in-memory html5 canvas using
var c=document.createElement
,Resize the canvas to image size,
drawImage
your image onto the canvas,Create a
Kinetic.Image
and set its image property to a reference to the native canvas. The Kinetic.Image will display whatever is drawn onto the native canvas.Set the canvas Compositing to cause new drawings to "erase" existing pixels:
Listen for drag events on your circle-eraser. Use those events to draw a circle on the canvas that move just like the Kinetic circle-eraser moves. Since the canvas's compositing is set to "erase", new drawings of the circle on the canvas will erase the image on the canvas.
Your Kinetic.Image exactly reflects its canvas source (var c), so your Kinetic.Image will also display the image being erased in response to the Kinetic circle-eraser movements.
Option#2: Use a Kinetic.Shape
You can do the same operation as Option#1 by creating a Kinetic.Shape on a separate layer and getting a reference to the native canvas context using:
This is a weaker option because KineticJS will redraw the shape--causing your erasing to be undone. Therefore you must do the additional step of saving all your circle-eraser's movements and replaying those movements (in
drawFunc
) to redo your erasing.In plain JavaScript this is pretty straight forward.
First get your canvas and drawing context ready:
Now you want to draw the image to the context:
Now, when you want to erase part of your image, just draw over the canvas:
This only simulates erasing, however. If you want what you erase to be transparent afterwards, you might look into
context.clearRect
, but I'm not sure how you would do that with a circle.Thanks for markE for his detailed answer,
I have tried to get the context from the
Konva.Layer()
and it worked.and I have used the
Konva.Shape
to erase by"destination-out"
and draw free draw by custom"source-over"
: