Outils pour utilisateurs

Outils du site


issue73:inkscape

Ceci est une ancienne révision du document !


One thing you've probably noticed about Inkscape throughout the course of this series is that it operates with a stack of objects placed one above another—the “z-order.” You can introduce some overall structure by putting objects into groups and layers, but it's simply not possible to create an element that passes both over and under another object.

Because we're mostly interested in creating an artistic result, the solution is simply to put the object on top, and then remove the part that would be hidden behind our second object. One way to remove the hidden part is to edit the paths that make up our problem object, perhaps using Boolean operations to cut out sections. But what if the object is complex, such as a group of many individual elements that would each have to be cut independently? A better option in this case would be to tell Inkscape that it should draw only certain areas, and leave the others transparent for the background object to show through. This is done by creating a path and applying it as a “clip path.” Any part of the object inside the path will be visible, whereas the parts that are outside it are not drawn at all.

As an example let's resurrect the snowman from earlier in this series, and give him a scarf. We could do this simply by drawing the front part of the scarf onto the snowman, but, in this contrived case, our scarf already has a back section that we need to clip out of the final image.

Despite its name, a clip path doesn't have to be a path. It can be any closed shape—a rectangle, oval, star or polygon. It can also be a complex path which contains multiple sub-paths (more on that later). Whatever shape you use, however, it must be a single object rather than a group or a selection of several elements.

Defining a clip path is simple: just draw the path or shape you want, ensuring that it sits higher in the z-order than the object or group you want to clip. Now select both the clip path and the object you wish to clip, and use the “Set Clip” option on the right-click context menu. The Object > Clip > Set menu item has the same effect. Here, I've created an oval on top of the scarf. I usually draw my clipping paths with a bright green stroke and no fill—so that they're easy to spot against the item I'm clipping. The color is irrelevant, as is the thickness of the stroke: it's purely the shape of the path that is used to define the clip.

Unfortunately the result of this clip is the opposite of the effect we're looking for. Instead of removing the back of the scarf, we're left with nothing but the back of the scarf!

Remember, clipping always leaves the content inside the clip path visible, and hides everything outside it. There's no way to invert this behaviour to hide the content of the path, so, instead you have to work around it by creating a more complex path that does the job for you. This is an ideal opportunity to use the Boolean operations that were introduced in Part 7 of this series. In this case, you just draw a rectangle that's large enough to encompass the whole of the scarf then, ensuring the oval is on top, use Path > Difference to “subtract” the oval from the rectangle.

At first glance this may appear to simply be a rectangle with an oval on top of it, but, in practice, this is now a single complex path. “Complex” simply means that it's made up of separate sub-paths. The rectangle is one sub-path, and the oval is a second sub-path, but the whole object is still considered to be a single path and can therefore be used for clipping. Once again, only the area inside the path will remain, but in this case “inside” consists of the space between the oval and the rectangle; “outside” is anything beyond the rectangle, or within the oval. Try setting a fill if the difference between inside and outside isn't clear. Clipping with this complex path gets us much closer to our desired result.

Remember, this is a purely visual effect. The original scarf remains the same, it's just that parts of it are not being drawn. You can undo the effect at any time by selecting a clipped object then choosing Release Clip from the context menu, or Object > Clip > Release from the main menu bar. This will return the object to its normal appearance, and also make the clipping path visible again.

If we add the scarf to the snowman you can see that we're close to the effect we're looking for, but there are still parts of the scarf that are being drawn in front of the character's head.

The problem is that the clip path we've used was just an approximation, and needs to be tweaked to more closely match the section that we want to hide. In earlier versions of Inkscape, the only way to do this was to release the clip, adjust the path, then re-clip. Even with the latest version, that's still the only option if your clip “path” is actually a rectangle, star, oval or other primitive object. Thankfully, our Boolean operation has turned our combination of an oval and a rectangle into a fully fledged path, which can be edited without releasing the clip since the 0.48 version of Inkscape.

To switch to editing mode, you have to select the clipped object (as usual keep an eye on the status bar where a clipped object will be described as such), then switch to the node tool—using the icon on the tool palette or by pressing “n” or F2. You should now see your clipping path rendered as a single pixel green path, regardless of the color or thickness of the original path. The green color in this mode is the reason I choose bright green when drawing my path in the first place—it helps to reinforce the mental link between green paths and clipping. If you don't see the green path, make sure the “Show clipping path(s) of selected object(s)” button (in the tool control palette) is active.

From this point you can just edit the path using the node tools that were described in Part 6. You may find it easier to reduce the opacity of your clipped object so that you can see how your clipping path relates to the objects behind. The changes to the clipping path will be applied live, so you can see the result as you work. Once you've finished editing, return your object's opacity to 100% if you need to, and change to the selection tool to hide the green clipping path and see your results. Remember that the spacebar can be used as a convenient shortcut to switch to the selection tool and then back to the node tool during editing, if you find that the nodes are obscuring your view.

With a little time spent on node editing, you should be able to adjust the clipping path to follow the head of the snowman and make the scarf seem to disappear behind it.

Sometimes you will be able to get away with a simple rectangle or oval as a clipping path, but usually it's worth pressing CTRL-SHIFT-C (or using Path > Object to Path) before setting the clip, simply for the advantage of being able to edit the path without having to un-set then re-set the clip.

Because clipping always results in a hard edge, it can be used to create some effects that would otherwise be very difficult to produce. Wherever you have to stop a blur from spilling over an edge, clipping can provide a solution. Consider something like a semicircle which should only be blurred on the curved edge and cut cleanly on the straight edge. Without clipping this would be a very difficult image to create.

The ability to cut out parts of a blurred object can lead to some particularly interesting effects when used with text. For each of these examples, I've created two copies of a text object. One of them is blurred, whilst the other has been converted to a path and used to clip the blurred version. Different arrangements of clip path, blur, text, background and color choices can produce a variety of results.

Clipping can also be used to work around the limited selection of gradient types. Inkscape follows the SVG standard in supporting only linear and radial gradients, but other types can often be emulated through careful use of blurred objects and clip paths. In this example, a few blurred segments and a donut-shaped clipping path makes an acceptable substitute for a conical gradient when drawing a CD.

Although clipping has many uses, and is a technique that's well worth getting to grips with, sometimes you need a little more subtlety than the hard edges it creates. Next time, we'll look at masking—a related technique that lets you fade objects out gradually, rather than just stopping them dead at the edge of a path.

issue73/inkscape.1370529887.txt.gz · Dernière modification : 2013/06/06 16:44 de andre_domenech