avo has asked for the wisdom of the Perl Monks concerning the following question:

Good day great monks! I have been playing around with Tk::Canvas... today and I came up with a question: How to mask a canvas (layer with a mask). To keep it short I put the question visual here Please have a look ... to get the idea. Any suggestions are welcome. Hint, I've been thinking of creating a second canvas and then using ->Place(x,y) to position it where it is supposed to be. but.. I guess there should be a better way to do this. Thanks for your time on this puzzle.

Replies are listed 'Best First'.
Re: Masking Tk Canvas
by zentara (Cardinal) on Sep 17, 2005 at 11:57 UTC
    You are not very clear as to what your ultimate objective is. From your graphic, you could just make a rectangle on the exiting canvas, and put your text into it. You can set the -tags of all the canvas objects, then layer them by using lower and raise. Do you really need 2 canvases?

    When you place a new object on a canvas, it will obscure what is below it, and what is below, will reappear when the new object is removed.


    I'm not really a human, but I play one on earth. flash japh
      Ok, I agree. I wasn't quite clear. So.. let me explain it in more details. Imagine you have a canvas, on which you just create a pattern of PNG files with Alpha transparency (ei. background is transparent). Then you want to create a second pattern that is like - a second layer with another PNG. But you want to mask it (ie crop the layer with an rectangular shape) so you can move the contents of it inside the mask. Perhaps because my English is not great, I just created a small Flash demonstration that will give you an idea... I am not sure whether a normal canvas can mask like this.... have a look here
        I looked at your flash, and wasn't sure exactly what it was supposed to do, different from what I suggested earlier. Also the text in the "floater" seemed to move around inside the rectangle as the rectangle moved. I don't know if that was one of the things you want to accomplish, probably not. Also, I did not see any "transparency effects" in your flash.

        But setting aside the transparency and alpha-layering stuff, which I'm not sure about in Tk, you could simulate your flash in Tk, by just creating an a new 'image' on your existing canvas, and setting tags on it, to make it "draggable".

        Perhaps, if you told us what this is supposed to perform, it would be easier. Are you trying to "custom-drag" images to positions on a canvas, to build a new composite image?

        I have seen one other thing which may be what you are looking for. It's in the TkZinc demo, and is called the "magic lens", (it is #6 under Small Applications).


        I'm not really a human, but I play one on earth. flash japh