Skip to content

Commit

Permalink
animator: add fade event for SVG images
Browse files Browse the repository at this point in the history
  • Loading branch information
felipepiovezan committed Jan 7, 2022
1 parent c253fed commit dcada1b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
7 changes: 7 additions & 0 deletions svganimator/SvgUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def filter_tag(root: ET.Element, tag):
path_tag = svg_namespace + 'path'
rectangle_tag = svg_namespace + 'rect'
svg_tag = svg_namespace + 'svg'
image_tag = svg_namespace + 'use'


def svg_elements_named(name: str, root: ET.Element):
Expand All @@ -44,6 +45,12 @@ def is_circle(root: ET.Element):
return root.tag == circle_tag


def is_image(root: ET.Element):
"""Returns true if root is an image use."""

return root.tag == image_tag


def is_group(root: ET.Element):
"""Returns true if root is an SVG group."""

Expand Down
5 changes: 5 additions & 0 deletions svganimator/SvgVisitors.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ def visit_root(self, node: ET.ElementTree):
def _visit(self, node: ET.ElementTree):
if SvgUtils.is_circle(node):
return self._visit_circle(node)
if SvgUtils.is_image(node):
return self._visit_image(node)
if SvgUtils.is_path(node):
return self._visit_path(node)
if SvgUtils.is_group(node):
Expand All @@ -72,6 +74,9 @@ def _visit(self, node: ET.ElementTree):
def _visit_circle(self, node: ET.ElementTree):
return FadeEvent(node, self.out, 1)

def _visit_image(self, node: ET.ElementTree):
return FadeEvent(node, self.out, 600)

def _visit_path(self, node: ET.ElementTree):
return PathEvent(node, self.out)

Expand Down
7 changes: 6 additions & 1 deletion tests/test_SvgVisitors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from svganimator.ParallelEventContainer import ParallelEventContainer
from svganimator.PathEvent import PathEvent
from svganimator.SequentialEventContainer import SequentialEventContainer
from svganimator.FadeEvent import FadeEvent
from svganimator.SvgVisitors import SimpleVisitor
import io
import unittest
Expand Down Expand Up @@ -89,13 +90,14 @@ def test_flat_root(self):
y="20.0"
width="20.0"
height="20.0"/>
<use id="use46"/>
</svg>''')

def test_grouped_root(self):
out = io.StringIO()
visitor = SimpleVisitor(out)
events = visitor.visit_root(TestSimpleVisitor.nested_root)
self.assertEqual(len(events), 3)
self.assertEqual(len(events), 4)

cam_event = events[0]
self.assertIsInstance(cam_event, CameraEvent)
Expand All @@ -122,6 +124,9 @@ def test_grouped_root(self):
self.assertEqual(cam_event2.old_cam, [10.0, 20.0, 30.0, 40.0])
self.assertEqual(cam_event2.new_cam, [20.0, 20.0, 20.0, 20.0])

image_event = events[3]
self.assertIsInstance(image_event, FadeEvent)


if __name__ == '__main__':
unittest.main()

0 comments on commit dcada1b

Please sign in to comment.