Two Flavors of Page Flip
The Page Flip technique has been around for several years in Flash.
However, not all implementations are equal. Usually, the premium page
flip components that are sold to developers are substantially different
than the free page flip tutorials you might find online. The biggest
difference is in the range of motion of the page corner being flipped.
Most free page flip tutorials have a fixed radius
motion path instead of a wider range of motion. In other words, no
matter where you drag your mouse, the page corner will stay fixed to a
curve as it travels from one side to the other. The math behind this
technique is rather straightforward and variations of its
implementation are found all over the place. The problem, though, is
that the premium
page flip technique allows a far greater range
of motion while flipping the page (as shown above), which yields a much
richer experience. However, nobody ever talks about how this one is
done!
The Page Flip in Silverlight
So what we're going to do today is
explain the mathematics behind a premium page flip experience
engineered in Silverlight. The Silverlight implementation shown above
was solved using trigonometry, clever masking and a little "smoke and
mirrors" magic for the shadows. As with many of my blog posts, the
effect can usually be split into the mechanics of the effect(the math
part) and the presentation (the rendering tricks). Since Silverlight
and Flash handle the mechanics of masking, clipping and rendering
transforms differently, this tutorial is aimed primarily at Silverlight
developers. However, the mathematical secrets behind the Page Flip
technique are applicable to interactive developers in any language and
this tutorial focuses primarily on explaining the rotational mechanics
of the solution.
Step 1: Follow the Mouse, Constrain the Corner Point
[ Download BlogPageFlipStudy01.zip
]
PageFlipStudy01
sets up the framework for what we need to do.
Please review it carefully, since the next 3 studies are built on top
of each other. If you open it up, you'll notice the solution only has
MainPage.xaml and a Dot.xaml control I created to visualize the
solution variables so far. If you look over the MainPage.xaml.cs code,
you'll see that the loaded() function mainly initializes variables and
setups up the Mouse event handlers. All the logic happens in the
CompositeTarget.Rendering() animation loop. Here are the key variables,
as illustrated above:
M:
The position of the Mouse
as set by the MouseMove() event
F:
The mouse Follower
that allows us to gracefully ease toward the raw mouse position
C:
The Constraint
point which defines where the page corner will eventually be
R1:
The Radius
point on the constraining circumference of the arc that defines the maximum page width allowed.
SC:
Spine Center
SB:
Spine Bottom
EB:
Edge Bottom
And here is the main constraint logic in the animation loop:
The most important variable at this point is our calculated constraint (dot C
above). I never use the positional data from the Mouse
events directly, but usually create a Follower
variable that constantly eases toward the raw mouse position. However, for the page flip effect to work, the Constraint
point that positions the page corner needs to respect the maximum width of the page. In other words, the line SB > C
can never be longer than the line SB > EB
.
Step 2: Adding the Second Constraint and Defining the Critical Triangle
[ Download BlogPageFlipStudy02.zip
]
In PageFlipStudy02
, we need to add an additional radius constraint R2
to define the maximum distance the page corner can be from the spine top (dot ST
above). In other words, the line ST > C
cannot be longer than the line that would connect ST
and R2
. Here is the additional constraint code for the second constraining radius:
Now that the corner point C
has been constrained
between the two radii, we can get to the heart of the solution; the
critical triangle. The critical triangle is creating by taking the
bisector of line C > EB
, which is marked by dot T0
. Shoot out a line perpendicular to the bisector toward the page bottom, and you get T2
. You then close up the triangle with T1
.
This critical triangle will be used to control both the location and
rotation of the Page object and the Rectangle that can serve as your
clipping boundary. Here is the logic that calculates the tangent:
You may want to experiment with this skeleton and then
experiment with the original page flip solution at the start of this
article and see if you figure out how the properties of the critical
triangle are used in a rendered solution.
Step 3: Adding the Page and Rotating the Corner
[ Download BlogPageFlipStudy03.zip
]
The biggest change in PageFlipStudy03
is the addition
of a pageBack.xaml control that contains the page graphic we want to
display. If you look at the control, you'll notice that both the
positional and rotational point of the control is located at its bottom
left corner. This makes sense, since when the page is finally flipped,
the corner you're trying to flip is its bottom left corner. In
MainPage.xaml.cs, the positioning and rotating of the page is
calculated like so:
The important thing to realize is that the angle defined by T2
to C
defines the amount of rotation needed for the page control if you anchor it to the constrained corner point C
.
You can determine the angle in Radians by performing a Math.atan2()
operation on the variables shown above. Once you have this tangent
value, you can set the page control's rotation angle to this value
after converting it from Radians to Degrees (by multiplying by
Math.PI/180.0), and the Page being flipped will always intersect with T2
.
Step 4: Defining the Clipping Region
[ Download BlogPageFlipStudy04.zip
]
Up to this point, you have all the logic required to constrain and
rotate the page properly. The last essential step, however, is defining
a clipping region that will also be animated to define what portion of
the page should be visible during the flip. Since the focus of this
tutorial is to understand the underlying math of the technique, though,
I'll keep the rectangle visible so you can see exactly what is going
on.
The red Rectangle above that can give me the points for a
clipping path was created in such a way that it's renderTransformOrigin
lines up to wherever T2
is. You calculate its position and rotation like so:
Inevitably, though, when you want to implement this solution, the red Rectangle
object used above will need to be converted to a collection of Path
figure segments within a PathGeometry that defines the clipping region
for the page control (it's easier doing it this way than trying to
perform a complex rotation on a clipping path, believe me). The good
news is that Silverlight's GeneralTransform object makes this very
easy:
That's it! The purple region above where the blue page and the red
rectangle overlap defines how much of the page is visible as your flip
it. Using the calculated points above, you can convert this rectangle
to a proper pathGeometry that clips the page control, making it look
like the page is being revealed by being flipped over. This
trigonometry defines the mathematical underpinnings of the premium page
flip effect.
From here, you can use this structure to add your shadows and add
multiple pages. Flipping from left to right is simply a reversal of the
geometry and flipping from the top is a basic inversion. However, I'll
leave it for you to add the shading and multiple page logic. Have fun!
分享到:
相关推荐
本程序主要是实现了两个图表控件向四个方向翻转的效果. 开发环境:vs2010,silverlight 4,blend 4。
Silverlight:手把手教你写俄罗斯方块
Silverlight 翻转效果 ,由于发现好多翻转动画都是靠后台代码实现,所以我做了纯动画翻转,无代码,适合所有人群
silverlight的behavior,利用blend来使用导入的behavior,可以实现页面左右翻转,上下翻转,斜角翻转等效果。切换时特别炫
silverlight实现图片幻灯片效果
silverlight入门教程,入门Silverlight的开始,值得拥有
Silverlight实现的登陆注册翻转 数据库没加 太大了:)
(1):创建一个基本的Silverlight应用 (2):基本控件 (3):界面布局 (4):鼠标事件处理 (5):实现简单的拖放功能 (6):键盘事件处理 (7):全屏模式支持 (8):使用样式封装控件观感 (9):...
Silverlight中文版教程 实例教程 silverlight
一个SilverLight4实现右键菜单效果的实例
Silverlight教程 · 第一部分:使用Silverlight 2 和 VS 2008创建“Hello World”程序 · 第二部分:使用布局管理 (木野狐译) · 第三部分:使用 Networking取回数据并填充DataGrid · 第四部分:使用 Style ...
Silverlight实现的图片放大效果
Silverlight页面跳转及页面间传递参数。
Silverlight超炫页面展示,各种展示效果,超炫!
Silverlight 2教程中文版,入门级学习Silverlight。
通过参考别人的一些例子,自己总结了下。实现了利用silverlight 操作cookie 和 用户可自定义页面的语言类型(主要实现了中英文之间的切换)
silverlight入门教程,让您软松入门silverlight.
silverlight中文版教程,很详尽,我开始学silverlight的时候是零基础,这篇教程通俗易懂,看完后基本可以上手应付工作。。
无代码实现倒影效果无代码实现倒影效果无代码实现倒影效果无代码实现倒影效果无代码实现倒影效果无代码实现倒影效果无代码实现倒影效果
SILVERLIGHT SILVERLIGHT 完美入门教程