Html canvas: crop and text

I am working on a canvas drawing application and I want to give the user the ability to draw only in the selected area. for this I can use the clip () method. but if I want the user to be able to draw inside the letter as well - is there a way to use clip () for the text? is there any other way i can do this?

thanks

+7
source share
1 answer

You can do this, but not use the clip. A clip only works with paths, and text is not a path.

To achieve the effect, you will need a second canvas in memory (not on the page). Here's how:

  • Create a canvas in memory, set its width and height, which may contain text
  • Draw text on this canvas in mind
  • set the context in the memory of globalCompositeOperation to "source"
  • Draw the thing you want to copy to text
  • use drawImage(in-memory-canvas, x, y) to place the newly created effect on your regular canvas.
+11
source

All Articles