comparison pyink/tween.py @ 1296:7f0a16125eeb

When we animate from a use, we should not consider its matrix because the matrix of the destination group is apply to the whole <svg:use> include its matrix. Seperate scale tween into three functions to reduce its size.
author wycc
date Sun, 16 Jan 2011 10:10:37 +0800
parents cbcb91b196fa
children 0e27e3aa21ed
comparison
equal deleted inserted replaced
1295:71118bff7d61 1296:7f0a16125eeb
214 newobj = s.duplicate(self._doc) 214 newobj = s.duplicate(self._doc)
215 newobj.setAttribute("ref", s.getAttribute("id")) 215 newobj.setAttribute("ref", s.getAttribute("id"))
216 obj.appendChild(newobj) 216 obj.appendChild(newobj)
217 pass 217 pass
218 218
219 def updateTweenObjectScale_Group(self,s,d,top):
220 # Parse the translate or matrix
221 #
222 # D = B inv(A)
223 try:
224 (ox,oy) = s.spitem.getCenter()
225 except:
226 ox = 0
227 oy = 0
228 pass
229
230 try:
231 (dx,dy) = d.spitem.getCenter()
232 except:
233 dx = 0
234 dy = 0
235 pass
236
237 try:
238 start_opacity = parse_opacity(s)
239 except:
240 start_opacity = 1
241 pass
242
243 try:
244 end_opacity =parse_opacity( d)
245 except:
246 end_opacity = 1
247 pass
248
249
250 cur_opacity = start_opacity*(1-p)+end_opacity*p
251 change_opacity(newobj,cur_opacity)
252 sm = self.parseTransform(s)
253 ss = self.decomposition(sm)
254 dm = self.parseTransform(d)
255 dd = self.decomposition(dm)
256 sx = (ss[0]*(1-p)+dd[0]*p)/ss[0]
257 sy = (ss[1]*(1-p)+dd[1]*p)/ss[1]
258 a = ss[2]*(1-p)+dd[2]*p-ss[2]
259 tx = ox*(1-p)+dx*p
260 ty = oy*(1-p)+dy*p
261 m = [math.cos(a),math.sin(a),-math.sin(a),math.cos(a),0,0]
262 m = self.mulA([sx,0,0,sy,0,0],m)
263 m = self.mulA(m,[1,0,0,1,-ox,oy-self.height])
264 m = self.mulA([1,0,0,1,tx,self.height-ty],m)
265
266 top.setAttribute("transform","matrix(%g,%g,%g,%g,%g,%g)" % (m[0],m[2],m[1],m[3],m[4],m[5]))
267 pass
268
269 def updateTweenObjectScale_Use(self,s,d,top):
270 # Parse the translate or matrix
271 #
272 # D = B inv(A)
273 try:
274 (ox,oy) = s.spitem.getCenter()
275 except:
276 ox = 0
277 oy = 0
278 pass
279
280 try:
281 (dx,dy) = d.spitem.getCenter()
282 except:
283 dx = 0
284 dy = 0
285 pass
286
287 try:
288 start_opacity = parse_opacity(s)
289 except:
290 start_opacity = 1
291 pass
292
293 try:
294 end_opacity =parse_opacity( d)
295 except:
296 end_opacity = 1
297 pass
298
299 cur_opacity = start_opacity*(1-p)+end_opacity*p
300 change_opacity(newobj,cur_opacity)
301 dm = self.parseTransform(d)
302 dd = self.decomposition(dm)
303 sx = 1-(1-dd[0])*p
304 sy = 1-(1-dd[1])*p
305 a = dd[2]*p
306 tx = ox*(1-p)+dx*p
307 ty = oy*(1-p)+dy*p
308 m = [math.cos(a),math.sin(a),-math.sin(a),math.cos(a),0,0]
309 m = self.mulA([sx,0,0,sy,0,0],m)
310 m = self.mulA(m,[1,0,0,1,-ox,oy-self.height])
311 m = self.mulA([1,0,0,1,tx,self.height-ty],m)
312
313 top.setAttribute("transform","matrix(%g,%g,%g,%g,%g,%g)" % (m[0],m[2],m[1],m[3],m[4],m[5]))
314 pass
315
316 def updateTweenObjectScale_Primitive(self,s,d,top):
317 try:
318 if d.name() == "svg:use":
319 sw = 1
320 sh = 1
321 dw = 1
322 dh = 1
323 else:
324 try:
325 sw = float(s.getAttribute("width"))
326 except:
327 sw = 1
328 try:
329 sh = float(s.getAttribute("height"))
330 except:
331 sh = 1
332
333 try:
334 dw = float(d.getAttribute("width"))
335 except:
336 dw = 1
337 try:
338 dh = float(d.getAttribute("height"))
339 except:
340 dh = 1
341 pass
342
343 try:
344 start_opacity = parse_opacity(s)
345 except:
346 start_opacity = 1
347 pass
348
349 try:
350 end_opacity =parse_opacity( d)
351 if d.name() == "svg:use":
352 end_opacity = end_opacity * start_opacity
353 pass
354 pass
355 except:
356 if d.name() == "svg:use":
357 end_opacity = start_opacity
358 else:
359 end_opacity = 1
360 pass
361
362 cur_opacity = start_opacity*(1-p)+end_opacity*p
363 change_opacity(newobj,cur_opacity)
364
365 try:
366 (ox,oy) = s.spitem.getCenter()
367 except:
368 ox = 0
369 oy = 0
370 try:
371 (dx,dy) = d.spitem.getCenter()
372 except:
373 dx = 0
374 dy = 0
375 try:
376 sm = self.parseTransform(s)
377 ss = self.decomposition(sm)
378 except:
379 ss = [1,1,0,0,0]
380 pass
381 try:
382 dm = self.parseTransform(d)
383 dd = self.decomposition(dm)
384 except:
385 dd = [1,1,0,0,0]
386 dd[0] = dd[0]*dw/sw
387 dd[1] = dd[1]*dh/sh
388 sx = (ss[0]*(1-p)+dd[0]*p)/ss[0]
389 sy = (ss[1]*(1-p)+dd[1]*p)/ss[1]
390 a = ss[2]*(1-p)+dd[2]*p-ss[2]
391 tx = ox*(1-p)+dx*p
392 ty = oy*(1-p)+dy*p
393 m = [math.cos(a),math.sin(a),-math.sin(a),math.cos(a),0,0]
394 m = self.mulA([sx,0,0,sy,0,0],m)
395 m = self.mulA(m,[1,0,0,1,-ox,oy-self.height])
396 m = self.mulA([1,0,0,1,tx,self.height-ty],m)
397
398 top.setAttribute("transform","matrix(%g,%g,%g,%g,%g,%g)" % (m[0],m[2],m[1],m[3],m[4],m[5]))
399 except:
400 traceback.print_exc()
401 pass
402 pass
403
219 def updateTweenObjectScale(self,obj,s,d,p,newobj): 404 def updateTweenObjectScale(self,obj,s,d,p,newobj):
220 """ 405 """
221 Generate a new group which contains the original group and then 406 Generate a new group which contains the original group and then
222 add the transform matrix to generate a tween frame between the 407 add the transform matrix to generate a tween frame between the
223 origin and destination scene group. 408 origin and destination scene group.
244 else: 429 else:
245 top = newobj 430 top = newobj
246 newobj = newobj.firstChild() 431 newobj = newobj.firstChild()
247 pass 432 pass
248 print s.name() 433 print s.name()
249 if s.name() == 'svg:g' or s.name() == 'svg:use': 434 if s.name() == 'svg:g':
250 # Parse the translate or matrix 435 self.updateTweenObjectScale_Group(s,d,top)
251 # 436 elif s.name() == 'svg:use':
252 # D = B inv(A) 437 self.updateTweenObjectScale_Use(s,d,top)
253 try:
254 (ox,oy) = s.spitem.getCenter()
255 except:
256 ox = 0
257 oy = 0
258 try:
259 (dx,dy) = d.spitem.getCenter()
260 except:
261 dx = 0
262 dy = 0
263 try:
264 start_opacity = parse_opacity(s)
265 except:
266 start_opacity = 1
267
268 try:
269 end_opacity =parse_opacity( d)
270 except:
271 end_opacity = 1
272
273
274 cur_opacity = start_opacity*(1-p)+end_opacity*p
275 change_opacity(newobj,cur_opacity)
276 sm = self.parseTransform(s)
277 ss = self.decomposition(sm)
278 dm = self.parseTransform(d)
279 dd = self.decomposition(dm)
280 sx = (ss[0]*(1-p)+dd[0]*p)/ss[0]
281 sy = (ss[1]*(1-p)+dd[1]*p)/ss[1]
282 a = ss[2]*(1-p)+dd[2]*p-ss[2]
283 tx = ox*(1-p)+dx*p
284 ty = oy*(1-p)+dy*p
285 m = [math.cos(a),math.sin(a),-math.sin(a),math.cos(a),0,0]
286 m = self.mulA([sx,0,0,sy,0,0],m)
287 m = self.mulA(m,[1,0,0,1,-ox,oy-self.height])
288 m = self.mulA([1,0,0,1,tx,self.height-ty],m)
289
290 top.setAttribute("transform","matrix(%g,%g,%g,%g,%g,%g)" % (m[0],m[2],m[1],m[3],m[4],m[5]))
291 else: 438 else:
292 try: 439 self.updateTweenObjectScale_Primitive(s,d,top)
293 if d.name() == "svg:use":
294 sw = 1
295 sh = 1
296 dw = 1
297 dh = 1
298 else:
299 try:
300 sw = float(s.getAttribute("width"))
301 except:
302 sw = 1
303 try:
304 sh = float(s.getAttribute("height"))
305 except:
306 sh = 1
307
308 try:
309 dw = float(d.getAttribute("width"))
310 except:
311 dw = 1
312 try:
313 dh = float(d.getAttribute("height"))
314 except:
315 dh = 1
316 pass
317
318 try:
319 start_opacity = parse_opacity(s)
320 except:
321 start_opacity = 1
322 pass
323
324 try:
325 end_opacity =parse_opacity( d)
326 if d.name() == "svg:use":
327 end_opacity = end_opacity * start_opacity
328 pass
329 pass
330 except:
331 if d.name() == "svg:use":
332 end_opacity = start_opacity
333 else:
334 end_opacity = 1
335 pass
336
337 cur_opacity = start_opacity*(1-p)+end_opacity*p
338 change_opacity(newobj,cur_opacity)
339
340 try:
341 (ox,oy) = s.spitem.getCenter()
342 except:
343 ox = 0
344 oy = 0
345 try:
346 (dx,dy) = d.spitem.getCenter()
347 except:
348 dx = 0
349 dy = 0
350 try:
351 sm = self.parseTransform(s)
352 ss = self.decomposition(sm)
353 except:
354 ss = [1,1,0,0,0]
355 pass
356 try:
357 dm = self.parseTransform(d)
358 dd = self.decomposition(dm)
359 except:
360 dd = [1,1,0,0,0]
361 dd[0] = dd[0]*dw/sw
362 dd[1] = dd[1]*dh/sh
363 sx = (ss[0]*(1-p)+dd[0]*p)/ss[0]
364 sy = (ss[1]*(1-p)+dd[1]*p)/ss[1]
365 a = ss[2]*(1-p)+dd[2]*p-ss[2]
366 tx = ox*(1-p)+dx*p
367 ty = oy*(1-p)+dy*p
368 m = [math.cos(a),math.sin(a),-math.sin(a),math.cos(a),0,0]
369 m = self.mulA([sx,0,0,sy,0,0],m)
370 m = self.mulA(m,[1,0,0,1,-ox,oy-self.height])
371 m = self.mulA([1,0,0,1,tx,self.height-ty],m)
372
373 top.setAttribute("transform","matrix(%g,%g,%g,%g,%g,%g)" % (m[0],m[2],m[1],m[3],m[4],m[5]))
374 except:
375 traceback.print_exc()
376 pass
377 pass
378 pass 440 pass
379 pass 441 pass
380 442
381 ## \brief Providing capability of showing scenes. 443 ## \brief Providing capability of showing scenes.
382 # 444 #