I'm working on a paint app using canvas, and i want to let the user the option to draw only in a selected area. for that I can use the clip() method. but if I want the user to be able to draw inside letters also - is there any way to u开发者_运维百科se clip() for text? is there another way I can do it?
thanks
You can do this but not using clip. Clip only works with paths and text is not a path.
You will need to use a second in-memory (not on the page) canvas in order to achieve the effect. Here is how:
- Make an in-memory canvas, set it to a width and height capable of containing the text
- Draw the text to that in-memory canvas
- set the in-memory context's globalCompositeOperation to 'source-in'
- Draw the thing you want clipped to the text
- use
drawImage(in-memory-canvas, x, y)
to put the newly created effect onto your normal canvas
精彩评论