执行特效
三个核心类的最后一个是 Ken Burns Effects。这些特效通过 Animation 对象应用于图像,如 清单 6 所示。
清单 6. KenBurnsAnimations.js
function KenBurnsFader( img, windowSize ) { this.img = img; this.windowSize = windowSize; }
KenBurnsFader.prototype.apply = function( percent ) { var opacity = 100;
if ( percent <= this.windowSize ) opacity = ( percent / this.windowSize ) * 100; else if ( percent >= ( 100 - this.windowSize ) ) opacity = ( ( 100 - percent ) / this.windowSize ) * 100;
this.img.set_opacity( opacity ); }
function KenBurnsZoomer( img, start, end, cw, ch ) { this.start = start; this.end = end; this.img = img;
var wr = this.img.width / cw; var nw = this.img.width * wr; var nh = this.img.height * wr;
this.sw = ( nw * ( this.start / 100 ) ); this.ew = ( nw * ( this.end / 100 ) ); this.sh = ( nh * ( this.start / 100 ) ); this.eh = ( nh * ( this.end / 100 ) ); this.dw = ( this.ew - this.sw ) / 100; this.dh = ( this.eh - this.sh ) / 100; }
KenBurnsZoomer.prototype.apply = function( percent ) { this.img.set_size( this.sw + ( this.dw * percent ), this.sh + ( this.dh * percent ) ); }
function KenBurnsMover( img, sx, sy, ex, ey, cw, ch ) { this.img = img; this.sx = sx / 100; this.ex = ex / 100; this.sy = sy / 100; this.ey = ey / 100; this.cw = cw; this.ch = ch; this.wr = this.img.width / this.cw; }
KenBurnsMover.prototype.apply = function( percent ) { var nw = this.img.current_width * this.wr; var nh = this.img.current_height * this.wr;
var cntw = ( ( this.cw / 2 ) - ( nw / 2 ) ); var cnth = ( ( this.ch / 2 ) - ( nh / 2 ) );
var sx = ( nw * this.sx ); var ex = ( nw * this.ex ); var sy = ( nh * this.sy ); var ey = ( nh * this.ey ); var dx = ( ex - sx ) / 100; var dy = ( ey - sy ) / 100; var x = cntw + sx + ( dx * percent ); var y = cntw + sy + ( dy * percent );
this.img.set_position( x, y ); }
这三个类分别处理应用于图像的不同特效。KenBurnsFader 类使用不透明度处理图像的淡入淡出。KenBurnsZoomer 类处理图像的缩放,从最初的大小到最终的大小。KenBurnsMover 类处理图像的移动,从起点到终点(用图像的百分比指定)。
经过一些试验后,我发现最吸引人的移动特效是相对于窗口中心从一个角移动到另一个角。KenBurnsMover 类的 apply 方法包含一些复杂的数学运算,不仅相对于包含图像的 <div> 标记的中心来移动,还要计算图像和 <div> 标记的相对大小,这样在小窗口中移动的距离就小,在大窗口中移动的距离就大。放大倍数根据窗口的高度确定。
出处:IBM developerWorks
责任编辑:moby
上一页 用DHTML与XML制作Ajax幻灯片 [4] 下一页 用DHTML与XML制作Ajax幻灯片 [6]
◎进入论坛网络编程版块参加讨论
|