I'm using AS3 (Flash) to read SVG files and display them. The problem is correctly displaying a linear/radial gradient.
Following is an example of a simple linear gradient from Red to Blue, in SVG.
<linearGradient id="GRADIENT_1" gradientUnits="userSpaceOnUse"
x1="107.3938" y1="515.5684" x2="105.2488" y2="428.8614"
gradientTransform="matrix(0.9988 0.0485 0.0485 -0.9988 0.8489 711.6634)">
<stop offset="0" style="stop-color:red"/>
<stop offset="1" style="stop-color:blue"/>
</linearGradient>
I've been able to read the colors from the gradient (stop's styles), and their spacing through the gradient (stop's offsets).
Okay, so now I've got a nice gradient with the right colors and right amount of space between them, but with NO rotation or scale, and its off position!.
The answer is to feed a Matrix into Flash's gradient function.
Method 1: Use the supplied matrix
The Matrix supplied by SVG, gradientTransform="matrix(a,b,c,d,e,f)" appears to be effectively useless since when you feed that matrix into Flash's beginGradientFill() function, it displays the gradient stretched over the shape so much so only the center color of the gradient is visible, like a solid fill.
Method 2: Generate a Matrix using 2 gradient points
- The x1, y1 tags might be the point on screen where the gradient begins at.
- The x2, y2 tags, might be the point on screen where the gradient ends.
When I say "2 gradient points", I'm referring to x1,y1 and x2,y2, which are mostly telling you where on the shape does the gradient start & end.
There's the Matrix.createGradientBox() function which creates a matrix for use with gradients, but the parameters are very different than what SVG gives me. It wants:
- Width --- used the relative distance between the 2 gradient points here (X axis)
- Height --- used the relative distance between the 2 gradient points here (Y axis), but got incorrect results --- also tried using the distance between the 2 gradient points here, but I didn't know what to fill into Width!
- Rotation angle -- used the angle of the 2 gradient points, in radians, with incorrect results!
- Translation x -- tried 1st gradient point's X, also tried 0, with incorrect results!
- Translation y -- tried 1st gradient point's Y, also tried 0, with incorrect results!
What SVG data can I use to generate a gradient Matrix for Flash?
(Look at the SVG snippet above, that's all the data I have describing the gradient)