jsvg
jsvg copied to clipboard
Implement `repeatDur` property for animation elements
It seems setting repeatDur to <animateTransform> doesn't have an effect.
Using JSVG 2.0.0-RC, also 1.7.2.
animation-repeatDur.svg
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 120 120" width="120" height="120">
<g transform="translate(60,60)">
<g>
<path d="M 0,0 L 50,0 A 50,50 0 0 1 0,50 Z" fill="dodgerblue"/>
<line x1="0.5" x2="0.5" y2="50" stroke="crimson" stroke-width="1"/>
<animateTransform attributeName="transform" type="rotate"
from="0" to="360" dur="2s" repeatDur="4s"/>
</g>
</g>
</svg>
JSVGAnimationTest.java
import java.io.File;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Dimension2D;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import com.github.weisj.jsvg.SVGDocument;
import com.github.weisj.jsvg.parser.SVGLoader;
import com.github.weisj.jsvg.renderer.NullPlatformSupport;
import com.github.weisj.jsvg.renderer.animation.AnimationState;
import com.github.weisj.jsvg.renderer.impl.output.Graphics2DOutput;
public class JSVGAnimationTest {
public static void main(String[] args) throws Exception {
String fileName = "animation-repeatDur";
SVGDocument svg = new SVGLoader()
.load(JSVGAnimationTest.class.getResource(fileName + ".svg"));
if (svg == null)
System.exit(1);
for (float time : new float[] { 0, 1.1f, 2.1f, 3.9f }) {
ImageIO.write(renderAtTime(svg, time), "png",
new File(fileName + "-" + time + "s.png"));
}
System.out.println("Done.");
}
static BufferedImage renderAtTime(SVGDocument svg, float seconds) {
Dimension2D size = svg.size();
BufferedImage image = new BufferedImage((int) size.getWidth(),
(int) size.getHeight(),
BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
RenderingHints.VALUE_STROKE_PURE);
try {
Graphics2DOutput output = new Graphics2DOutput(g);
int timeMillis = Math.round(seconds * 1000);
System.out.println("snapshot[ms]: " + timeMillis);
svg.renderWithPlatform(NullPlatformSupport.INSTANCE,
output, null, new AnimationState(0, timeMillis));
output.dispose();
} finally {
g.dispose();
}
return image;
}
}
<animateTransform attributeName="transform" type="rotate"
from="0" to="360" dur="2s" repeatDur="4s"/>
| Actual | Expected | |
|---|---|---|
| 0s | ||
| 1.1s | ||
| 2.1s | ||
| 3.9s |
The repeatDur property isn't currently implemented.