github.com/cayleygraph/cayley@v0.7.7/svg/svg.py (about)

     1  # Copyright 2014 The Cayley Authors. All rights reserved.
     2  #
     3  # Licensed under the Apache License, Version 2.0 (the "License");
     4  # you may not use this file except in compliance with the License.
     5  # You may obtain a copy of the License at
     6  #
     7  #     http://www.apache.org/licenses/LICENSE-2.0
     8  #
     9  # Unless required by applicable law or agreed to in writing, software
    10  # distributed under the License is distributed on an "AS IS" BASIS,
    11  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  # See the License for the specific language governing permissions and
    13  # limitations under the License.
    14  
    15  import svgwrite
    16  import math
    17  import random
    18  
    19  dwg = svgwrite.Drawing((210,130))
    20  
    21  
    22  node_radius = 15
    23  big_radius = 50
    24  fan_dist = 90
    25  center = (105,65)
    26  edge_stroke = 2.5
    27  edge_color = '#555555'
    28  fan_color = '#aa0000'
    29  
    30  b = '#4285F4'
    31  r = '#DB4437'
    32  g = '#0F9D58'
    33  y = '#F4B400'
    34  n = '#999999'
    35  
    36  center_colors = [b, r, y, b, r, y]
    37  edge_color = n
    38  fan_color = n
    39  
    40  def center_list(center, big_r):
    41      x, y = center
    42      out = []
    43      for i in range(0,6):
    44          ox = x + (math.cos(2 * math.pi * i / 6.0) * big_r)
    45          oy = y - (math.sin(2 * math.pi * i / 6.0) * big_r)
    46          out.append((ox, oy))
    47      return out
    48  
    49  cx, cy = center
    50  ring_centers = center_list(center, big_radius)
    51  outer_left = (cx - fan_dist, cy)
    52  outer_right = (cx + fan_dist, cy)
    53  left = ring_centers[3]
    54  right = ring_centers[0]
    55  
    56  all_lines = []
    57  l = dwg.add(dwg.line(outer_left, left))
    58  l.stroke(edge_color, edge_stroke)
    59  all_lines.append(l)
    60  l = dwg.add(dwg.line(outer_right, right))
    61  l.stroke(edge_color, edge_stroke)
    62  all_lines.append(l)
    63  
    64  for i, c in enumerate(ring_centers):
    65      for j, d in enumerate(ring_centers):
    66          if i > j or i == j:
    67              continue
    68          if (i % 3) == (j % 3):
    69              continue
    70          if (i % 3) == 1 and (j % 3) == 2:
    71              continue
    72          if (j % 3) == 1 and (i % 3) == 2:
    73              continue
    74          if i == 0 and j == 3:
    75              continue
    76          if i == 3 and j == 0:
    77              continue
    78          l = dwg.add(dwg.line(c,d))
    79          l.stroke(edge_color, edge_stroke)
    80          all_lines.append(l)
    81  
    82  circle_elems = []
    83  for i, c in enumerate(ring_centers):
    84      elem = dwg.add(dwg.circle(c, node_radius, fill=center_colors[i]))
    85      circle_elems.append(elem)
    86  
    87  left_circle = dwg.add(dwg.circle(outer_left, node_radius, fill=fan_color))
    88  right_circle = dwg.add(dwg.circle(outer_right, node_radius, fill=fan_color))
    89  
    90  
    91  anims = []
    92  def flash(element, orig_color, start, is_line=False):
    93      prop = "fill"
    94      if is_line:
    95          prop = "stroke"
    96  
    97      a = svgwrite.animate.Animate(prop, href=element)
    98      a['from'] = orig_color
    99      a['to'] = g
   100      a['begin'] = "+%0.2fs" % start
   101      a['dur'] = "1.0s"
   102      dwg.add(a)
   103      anims.append(a)
   104  
   105      a = svgwrite.animate.Animate(prop, href=element)
   106      a['from'] = g
   107      a['to'] = orig_color
   108      a['begin'] = "+%0.2fs" % (start + 1.0)
   109      a['dur'] = "1.2s"
   110  
   111      dwg.add(a)
   112      anims.append(a)
   113      return a
   114  
   115  dwg.saveas("cayley.svg")
   116  first = flash(left_circle, n, 0)
   117  flash(all_lines[0], n, 0.5, True)
   118  flash(all_lines[7], n, 1.0, True)
   119  flash(all_lines[3], n, 1.5, True)
   120  flash(all_lines[9], n, 1.0, True)
   121  flash(all_lines[5], n, 1.5, True)
   122  flash(all_lines[1], n, 2.0, True)
   123  flash(right_circle, n, 2.5)
   124  flash(left_circle, n, 3.5)
   125  flash(all_lines[0], n, 4.0, True)
   126  flash(all_lines[6], n, 4.5, True)
   127  flash(all_lines[4], n, 5.0, True)
   128  flash(all_lines[8], n, 4.5, True)
   129  flash(all_lines[2], n, 5.0, True)
   130  flash(all_lines[1], n, 5.5, True)
   131  final = flash(right_circle, n, 6.0)
   132  
   133  for anim in anims:
   134      anim["begin"] = anim["begin"] + "; " + final.get_id() + ".end" + anim["begin"]
   135  
   136  dwg.saveas("cayley_active.svg")