SVG texturing

SVG textures are made of dynamic or predefined SVG patterns, images,masks and gradients. Patterns can be set in the “fill” and “stroke” property of an SVG object, masks have their own “mask” property. One object value can only have one pattern applied, one to the “fill” value and one to the “stroke” value. To create a multi layer pattern, it’s necessary to create multiple instances of the same object and stack them up.
Patterns,masks and gradients can be created and modified dynamically with javascript. Many of their values are animatable either via SMIL or javascript. This makes it possible to create dynamic textures, and sprite animations .


  Read the W3C recommendation about Patterns and Gradients here


To create patterns, they first need to be predefined in the <defs> tag. Once a pattern is created it can be reused as many times as we want, but always with the same parameters. Once the pattern changes, each object applied with this pattern will change too.
The values of the pattern x, y, width, height, patternUnits is defined inside the <pattern> tag, everything within the <pattern> tags will be used as texture.



To create linear or radial gradient, its also necessary to predefine them inside the <defs> tag. They can be used as fill or stroke value of an SVG object, as pattern or as mask.
The direction of gradients can be set inside the <Gradient> tag, by changing the values x1,y1 and x2,y2. The color range of the gradient is defined within the <Gradient> tag with <stop> tags. Each <stop> tag defines a color-stop.



Masks are also created inside the <defs> tag. Masks can be applied by setting the “mask” value of an SVG object accordingly. It masks out the texture that is applied to the same object as the mask itself.


sprite animation

Sprite animation are made of a animated pattern, which have a sprite sheet applied. This is a step by step animation defined inside the <animation> tag with the “calcMode = discrete” value. The steps are defined in the “values”, and the time to run is defined in the “keyTimes” value. The x and y direction of the sprite sheet needs to be animated separately to create the feel of a smooth motion.



It is really easy to create patterns, masks and gradients with SVG. They are easy to reuse, change, manipulate and animate. It’s possible to attach them to objects, text and strokes. It’s possible to use sprite sheets, and to create sprite animations.
A great way to create dynamic stuff, with a really simple syntax. There are plenty of options for any  HTML5 game or web designer.