annotate pyink/tween.py @ 1141:8f0ee167c5b2

Fix the issue of the new DOM implementation
author wycc
date Thu, 23 Dec 2010 00:00:46 +0800
parents d4dbcb93aee0
children e14ec6d1a661
rev   line source
1140
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
1 # -*- indent-tabs-mode: t; tab-width: 8; python-indent: 4; -*-
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
2 # vim: sw=4:ts=8:sts=4
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
3 import traceback
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
4 import math
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
5 class TweenObject:
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
6 def __init__(self,doc,dom):
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
7 self.document = doc
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
8 self.dom = dom
1141
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
9 try:
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
10 self.width = float(dom.getAttribute("width"))
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
11 self.height = float(dom.getAttribute("height"))
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
12 except:
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
13 self.width = 640
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
14 self.height = 480
1140
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
15
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
16 def updateMapping(self):
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
17 self.nodeToItem={}
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
18 root = self.dom
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
19 self.updateMappingNode(root)
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
20 def updateMappingNode(self,node):
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
21 for c in node.childList():
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
22 self.updateMappingNode(c)
1141
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
23 try:
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
24 self.nodeToItem[c.getAttribute("id")] = c
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
25 except:
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
26 pass
1140
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
27 def updateTweenContent(self,obj, typ, source,dest,cur):
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
28 """
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
29 Update the content of the duplicate scene group. We will use the (start,end) and cur to calculate the percentage of
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
30 the tween motion effect and then use it to update the transform matrix of the duplicated scene group.
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
31 """
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
32
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
33 start = source.idx
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
34 end = dest.idx
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
35 print cur,start,end
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
36 percent = (cur-start)*1.0/(end-start)
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
37 i = 0
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
38 s = source.ref.firstChild()
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
39 d = dest.ref.firstChild()
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
40 sources={}
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
41 dests={}
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
42
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
43 # Collect all objects
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
44 while d:
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
45 try:
1141
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
46 label = d.getAttribute("inkscape:label")
1140
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
47 except:
1141
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
48 d = d.next()
1140
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
49 continue
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
50 dests[label] = d
1141
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
51 d = d.next()
1140
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
52 # Check if the object in the source exists in the destination
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
53 s = source.ref.firstChild()
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
54 d = dest.ref.firstChild()
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
55 while s:
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
56 print s,d
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
57 try:
1141
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
58 label = s.getAttribute("inkscape:label")
1140
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
59 # Use i8nkscape:label to identidy the equipvalent objects
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
60 if label:
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
61 if dests.hasattr(label.value()):
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
62 self.updateTweenObject(obj,typ,s,dests[label.value()],percent)
1141
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
63 s = s.next()
1140
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
64 continue
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
65 except:
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
66 pass
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
67 # Search obejcts in the destination
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
68 while d:
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
69 try:
1141
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
70 d.getAttribute("inkscape:label")
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
71 d = d.next()
1140
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
72 continue
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
73 except:
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
74 pass
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
75 if s.name() == d.name():
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
76 self.updateTweenObject(obj,typ,s,d,percent)
1141
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
77 d = d.next()
1140
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
78 break
1141
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
79 d = d.next()
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
80 s = s.next()
1140
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
81
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
82 def parseTransform(self,obj):
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
83 """
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
84 Return the transform matrix of an object
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
85 """
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
86 try:
1141
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
87 t = obj.getAttribute("transform")
1140
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
88 print t
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
89 if t[0:9] == 'translate':
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
90 print "translate"
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
91 fields = t[10:].split(',')
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
92 x = float(fields[0])
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
93 fields = fields[1].split(')')
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
94 y = float(fields[0])
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
95 return [1,0,0,1,x,y]
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
96 elif t[0:6] == 'matrix':
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
97 print "matrix"
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
98 fields=t[7:].split(')')
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
99 fields = fields[0].split(',')
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
100 return [float(fields[0]),float(fields[1]),float(fields[2]),float(fields[3]),float(fields[4]),float(fields[5])]
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
101 except:
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
102 #traceback.print_exc()
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
103 return [1,0,0,1,0,0]
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
104
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
105 def invA(self,m):
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
106 d = m[0]*m[3]-m[2]*m[1]
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
107 return [m[3]/d, -m[1]/d, -m[2]/d, m[0]/d, (m[1]*m[5]-m[4]*m[3])/d, (m[4]*m[2]-m[0]*m[5])/d]
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
108
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
109 def mulA(self,a,b):
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
110 return [a[0]*b[0]+a[1]*b[2],
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
111 a[0]*b[1]+a[1]*b[3],
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
112 a[2]*b[0]+a[3]*b[2],
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
113 a[2]*b[1]+a[3]*b[3],
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
114 a[0]*b[4]+a[1]*b[5]+a[4],
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
115 a[2]*b[4]+a[3]*b[5]+a[5]]
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
116
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
117 def decomposition(self,m):
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
118 """
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
119 Decompose the affine matrix into production of translation,rotation,shear and scale.
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
120 The algorithm is documented at http://lists.w3.org/Archives/Public/www-style/2010Jun/0602.html
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
121 """
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
122 if m[0]*m[3] == m[1]*m[2]:
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
123 print "The affine matrix is singular"
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
124 return [1,0,0,1,0,0]
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
125 A=m[0]
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
126 B=m[2]
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
127 C=m[1]
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
128 D=m[3]
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
129 E=m[4]
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
130 F=m[5]
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
131 sx = math.sqrt(A*A+B*B)
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
132 A = A/sx
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
133 B = B/sx
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
134 shear = m[0]*m[1]+m[2]*m[3]
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
135 C = C - A*shear
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
136 D = D - B*shear
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
137 sy = math.sqrt(C*C+D*D)
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
138 C = C/sy
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
139 D = D/sy
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
140 r = A*D-B*C
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
141 if r == -1:
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
142 shear = -shear
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
143 sy = -sy
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
144 R = math.atan2(B,A)
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
145 return [sx,sy, R, E,F]
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
146
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
147
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
148 def updateTweenObject(self,obj,typ,s,d,p):
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
149 """
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
150 Generate tweened object in the @obj by using s and d in the @p percent
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
151 http://lists.w3.org/Archives/Public/www-style/2010Jun/0602.html
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
152 """
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
153 if typ == 'relocate':
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
154 newobj = s.duplicate(self.document)
1141
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
155 newobj.setAttribute("ref", s.getAttribute("id"))
1140
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
156 top = self.document.createElement("svg:g")
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
157 top.appendChild(newobj)
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
158 obj.appendChild(top)
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
159 if s.name() == 'svg:g':
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
160 # Parse the translate or matrix
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
161 sm = self.parseTransform(s)
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
162 dm = self.parseTransform(d)
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
163 top.setAttribute("transform","translate(%g,%g)" % ((dm[2]-sm[2])*p,(dm[5]-sm[5])*p))
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
164 else:
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
165 try:
1141
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
166 sx = float(s.getAttribute("x"))
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
167 sy = float(s.getAttribute("y"))
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
168 dx = float(d.getAttribute("x"))
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
169 dy = float(d.getAttribute("y"))
1140
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
170 tx = (dx-sx)*p
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
171 ty = (dy-sy)*p
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
172 print tx,ty
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
173 top.setAttribute("transform","translate(%g,%g)" % (tx,ty))
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
174 except:
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
175 traceback.print_exc()
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
176 pass
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
177 pass
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
178 elif typ == 'scale':
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
179 self.updateTweenObjectScale(obj,s,d,p)
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
180 pass
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
181 elif typ == 'normal':
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
182 newobj = s.duplicate(self.document)
1141
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
183 newobj.setAttribute("ref", s.getAttribute("id"))
1140
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
184 top = self.document.createElement("svg:g")
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
185 top.appendChild(newobj)
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
186 obj.appendChild(top)
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
187 pass
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
188
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
189 def updateTweenObjectScale(self,obj,s,d,p):
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
190 """
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
191 Generate a new group which contains the original group and then
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
192 add the transform matrix to generate a tween frame between the
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
193 origin and destination scene group.
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
194
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
195 We will parse the transform matrix of the @s and @d and then
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
196 generate the matrix which is (1-p) of @s and p percent of @d.
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
197 """
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
198 newobj = s.duplicate(self.document)
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
199 top = self.document.createElement("svg:g")
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
200 top.appendChild(newobj)
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
201 obj.appendChild(top)
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
202
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
203 if s.name() == 'svg:g':
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
204 # Parse the translate or matrix
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
205 #
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
206 # D = B inv(A)
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
207 try:
1141
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
208 item = self.nodeToItem[s.getAttribute("id")]
1140
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
209 (ox,oy) = item.getCenter()
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
210 except:
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
211 ox = 0
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
212 oy = 0
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
213 try:
1141
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
214 item = self.nodeToItem[d.getAttribute("id")]
1140
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
215 (dx,dy) = item.getCenter()
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
216 except:
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
217 dx = 0
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
218 dy = 0
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
219
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
220 sm = self.parseTransform(s)
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
221 ss = self.decomposition(sm)
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
222 dm = self.parseTransform(d)
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
223 dd = self.decomposition(dm)
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
224 sx = (ss[0]*(1-p)+dd[0]*p)/ss[0]
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
225 sy = (ss[1]*(1-p)+dd[1]*p)/ss[0]
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
226 a = ss[2]*(1-p)+dd[2]*p-ss[2]
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
227 tx = ox*(1-p)+dx*p-ox
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
228 ty = oy*(1-p)+dy*p-oy
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
229 m = [math.cos(a),math.sin(a),-math.sin(a),math.cos(a),0,0]
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
230 m = self.mulA([sx,0,0,sy,0,0],m)
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
231 m = self.mulA(m,[1,0,0,1,-ox,oy-self.height])
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
232 m = self.mulA([1,0,0,1,tx,self.height-ty],m)
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
233
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
234 top.setAttribute("transform","matrix(%g,%g,%g,%g,%g,%g)" % (m[0],m[2],m[1],m[3],m[4],m[5]))
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
235 else:
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
236 try:
1141
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
237 sw = float(s.getAttribute("width"))
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
238 sh = float(s.getAttribute("height"))
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
239 dw = float(d.getAttribute("width"))
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
240 dh = float(d.getAttribute("height"))
1140
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
241 try:
1141
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
242 item = self.nodeToItem[s.getAttribute("id")]
1140
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
243 (ox,oy) = item.getCenter()
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
244 except:
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
245 ox = 0
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
246 oy = 0
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
247 try:
1141
8f0ee167c5b2 Fix the issue of the new DOM implementation
wycc
parents: 1140
diff changeset
248 item = self.nodeToItem[d.getAttribute("id")]
1140
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
249 (dx,dy) = item.getCenter()
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
250 except:
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
251 dx = 0
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
252 dy = 0
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
253 try:
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
254 sm = self.parseTransform(s)
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
255 ss = self.decomposition(sm)
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
256 except:
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
257 ss = [1,1,0,0,0]
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
258 pass
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
259 try:
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
260 dm = self.parseTransform(d)
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
261 dd = self.decomposition(dm)
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
262 except:
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
263 dd = [1,1,0,0,0]
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
264 dd[0] = ss[0]*dw/sw
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
265 dd[1] = ss[1]*dh/sh
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
266 sx = (ss[0]*(1-p)+dd[0]*p)/ss[0]
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
267 sy = (ss[1]*(1-p)+dd[1]*p)/ss[1]
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
268 a = ss[2]*(1-p)+dd[2]*p-ss[2]
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
269 tx = ox*(1-p)+dx*p
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
270 ty = oy*(1-p)+dy*p
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
271 m = [math.cos(a),math.sin(a),-math.sin(a),math.cos(a),0,0]
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
272 m = self.mulA([sx,0,0,sy,0,0],m)
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
273 m = self.mulA(m,[1,0,0,1,-ox,oy-self.height])
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
274 m = self.mulA([1,0,0,1,tx,self.height-ty],m)
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
275
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
276 top.setAttribute("transform","matrix(%g,%g,%g,%g,%g,%g)" % (m[0],m[2],m[1],m[3],m[4],m[5]))
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
277 except:
d4dbcb93aee0 Separate the tween from the main module.
wycc
parents:
diff changeset
278 traceback.print_exc()