First time at my blog? Check out the table of contents! x

May 2009 Entries

This afternoon I was impressed by an element on the Ceranco website that behaved like a wave of water. I started to wonder how I would be able to recreate the look of the wave animation in code. So I just dove in and started to play in Silverlight.

After about an hour I had this nice wave animation in Silverlight that I think would make a pretty sweet pre-loader:

image

Click here to view the animation.

Click here to download the source code. 

So how does it work?

The key to this animation is the use of a custom path that I made, it is the element that is filled with the blue gradient. No, I didn’t create this Path in Blend. I created it with code. The curved line across the top is a BezierSegment which has three points that get animated up and down throughout the animation using Math.Sin and Math.Cos.

Just like all the other animations I have done, I subscribe to the CompositionTarget.Rendering event. Inside this event is where all the heavy-lifting is done. A variable name degree is incremented every time the event fires, and degree is passed to the math functions to calculate the new positions of the animated elements.

If you want to know more, take a look at the code.

Thanks for reading!

 

This begins part two of my Silverlight animation series where I am recreating the animations that are described in the book Flash Math Creativity. Everything in part two of my series is from the second section of the book, which is authored by Glen Rhodes.

If you want to go back and view posts from part I of the series you can find a listing of all the posts in the Table of Contents.

Flowers Version 1

This first example demonstrates how you can layout instances of the flower petal at random scales and random angles of rotation. There really isn’t any animation yet, but at this point you’re only concerned with creating some sort of petal looking Path using Blend, and then getting them placed initially within the LayoutRoot.

I made the animation redraw the flower every 2 seconds so you can see that the petals are actually scaled and rotated randomly.

image

View version 1 here.

Download the source code here.

 

Flowers Version 2

In this second version of the flower I again randomly scale and rotate the petals. However, I don’t add them to the LayoutRoot in the order I create them. I first sorted the petals from largest scale to smallest scale, and then added them to LayoutRoot in that order. This caused the smallest petals to be in front of of the larger ones. Again, no real animation is happening yet.

image

View version 2 here.

Download the source code here.

Flowers Version 3

In version 3 I set the rotation and scale incrementally so that everything is spaced nicely. I also added the petals to the stage in order from largest scale to smallest so that the smaller petals were in front.

image 

View version 3 here.

Download the source code here.

 

 

Flowers Version 4

In version 4 I tweaked the fill and stroke of the petal Path. I also changed the RenderTransformOrigin to be "0.65,-0.5" so that it would cause the petal to rotate farter out from where the origin of rotation is.

image

View version 4 here.

Download the source code here.

 

Flower Version 5

In version 5 I changed the way I initialized the scale and rotation of the pedals. The scale and rotation in this example were calculated using the loop-control-variable (i) so that each has its own unique scale and rotation, but the values are not random.

image

View version 5 here.

Download the source code here.

 

Flowers Version 6

Finally some animation! In this animation each petal increments its rotation every frame. The smaller the petal is, the faster it rotates.

image

View version 6 here.

Download the source code here.

 

 

 

Flowers Version 7

The final animation for today is the same as version 6, but there is an additional scaling of the petals every frame. Once the petals reach a certain size, I have them return to a scale of 0, and I move them to the front using Canvas.SetZIndex.

image

View version 7 here.

Download the source code here.

 

More Flowers Coming Soon!

The next post will have the second half of Glen Rhodes’ flower animations. Some of them are pretty trippy, so keep an eye out!

Thanks for reading!

Edit: You can read the next post in the animation series here.

 

Technorati Tags: ,,

For those of you that haven’t been following along, I’ve been recreating the animations from the book Flash Math Creativity, using Silverlight. This is the final example from the first section of the book which is authored by Jamie MacDonald.

In this animation there is a grid of red balls. Each ball renders its color and scale based using Math.Sin, passing its current “degree” value to the function. This results in a number between –1 and 1. You use this value to calculate the current color and scale. By manipulating the balls’ initial degree value, you can change the direction that the wave of color moves.

image

View the animation here.

Download the source code here.

 

I learned a few things while creating this animation. First, if you ever need to keep an object (an ellipse in this case) centered when you apply a scale transform, you can set the RenderTransformOrigin property on the object to “0.5,0.5”. In previous animations I dealt with this problem by manually calculating and setting the centered position.

In this animation I was also able to tackle the manipulation of the color and opacity of the balls. In previous examples I wasn’t able to obtain the same color effects that MacDonald created using the ColorTransform class that ActionScript provides you.

Just for fun, I added a Slider to the top of the Silverlight movie that allows you to control the speed of the animation. This is nice because you can slow the animation down to get a better look at how the color and scale of the balls is changing.

In my next post I will begin recreating some really trippy spiral animations from the second part of the book. These animations are going to be wicked, so stick around!

 

Edit: View the next part of the Silverlight Math Creativity series here.


Technorati Tags: ,,

Today I bring you another Silverlight implementation of a Jamie MacDonald animation from the book Flash Math Creativity. This is the “Right To Left” animation which uses sine and cosine in order to oscillate the ball scale and horizontal-position. The combination of the horizontal and scale animation gives the illusion that the balls are moving toward you and away from you, or orbiting around a point in a 3d space.

image

View the animation here: Right To Left Animation

Download the source code here: Right To Left Animation Source Code

 

In my next post I will bring you the final MacDonald animation implemented in Silverlight. Check back soon!


Edit: View Part I.V of the Silverlight Math Creativity series here.

 

Technorati Tags: ,

For today’s Silverlight animation I am posting another experiment based Jamie Macdonald’s “In Line” animations from Flash Math creativity.

There are two things to note about this animation. First, the vertical oscillation of each ball is calculated using Sine and Cosine. One line of balls uses Sine, while the other line uses Cosine. This causes the lines to always be moving in opposite directions.

The second interesting thing in this animation is that I was able to animate the brightness of the ball color using a Sine wave. So far I am finding color manipulation a bit more tricky in Silverlight than in Flash. Actionscript provides a ColorTransform class that allows you to change the brightness of a color. With Silverlight there doesn’t seem to be a direct way to control brightness like that. You really have to just play around with the RGB values arbitrarily in order to color brightness.

Enjoy!

image

In Line Animation

In Line Animation Source Code


Edit: View Parth I.IV of the Silverlight Math Creativity series here.

 

Technorati Tags: ,,

Here is today’s Silverlight animation. I used a few classes that Lars Godejord posted that allow you to load bitmaps into a class which is readable and writable. This allowed me to randomly grab colors from van Gogh’s Starry Night, and drop 10 pixel diameter ellipses onto a canvas in their correct locations.

Kinda fun! (FYI, the bitmap image is 2mb, so it might take a minute to load)

image

View the animation here.

Download the source code here.

 

Technorati Tags:

 This is Day 3 of my Silverlight Math Creativity series. Today I am posting the final 8 variations of Jamie Macdonald’s “Toward Us” animations from the book Flash Math Creativity. What you are viewing here on my blog are Silverlight animations that I created based on the animations described in that book. They coded them in ActionScript. As an exercise, I am recreating them from scratch in Silverlight using C#.

Enjoy!

Toward Us 3

image

Toward Us 3

Toward Us 3 Source Code

 

Toward Us 4

image

Toward Us 4

Toward Us 4 Source Code

 

Toward Us 5

image

Toward Us 5

Toward Us 5 Source Code

 

Toward Us 6

image

Toward Us 6

Toward Us 6 Source Code

 

 

Toward Us 7

image

Toward Us 7

Toward Us 7 Source Code

 

Toward Us 8

image

Toward Us 8

Toward Us 8 Source Code

 

Toward Us 9

image

Toward Us 9

Toward Us 9 Source Code

 

Toward Us 10

image

Toward Us 10

Toward Us 10 Source Code

 

 Edit: View Part I.III of the Silverlight Math Creativity series here.


So how do you elegantly manage the different views or “pages” in your DotNetNuke module? This has to be one of the most frequently asked questions I see regarding DotNetNuke module development. Since module instances live on pages, possibly with instances of other modules, it can be tricky to switch between the pages (aka the “views”) within your module, while at the same time leaving the rest of the DotNetNuke page unchanged.

In the recently published book Professional DotNetNuke Programming by Mitchel Sellers, the author outlines the basics of several ways you can manage the views in your module. One of the methods he mentions involves using a MuiltiView control. This is the method I am going to demonstrate today. I am also going to share with you a helper class that I wrote that will allow you to more elegantly separate your views into UserControls that have all the same functionality of your standard PortalModuleBase control.

Let’s begin.

 

The Main View

When you are using the MultiView method to manage the views of your module, your module definition contains one main view that under most circumstances will not be navigated away from. In this example I simply named this control View.ascx.

image

If you aren’t familiar with the ASP.Net MultiView, the basic idea is that it allows you to show or hide a grouping of other controls and web content. Inside the single instance of a MultiView you declare several instance of the ASP.Net View control, each with an id. You tell the MultiView which View it should display, and only that View will be rendered and sent out to the browser.

In order to keep our module code clean, we will separate the code for each of our views into different UserControls. An instance of each control will then be placed inside its own ASP.Net View control inside the MultiView.

<asp:MultiView ID="MultiView1" runat="server">
    <asp:View ID="HomveView" runat="server">
        <h4>
            This is the home view</h4>
    </asp:View>
    <asp:View ID="View1" runat="server">
        <ke:Control1 ID="control1" runat="server">
        </ke:Control1>
    </asp:View>
    <asp:View ID="View2" runat="server">
        <ke:Control2 ID="control2" runat="server">
        </ke:Control2>
    </asp:View>
    <asp:View ID="View3" runat="server">
        <ke:Control3 ID="control3" runat="server">
        </ke:Control3>
    </asp:View>
</asp:MultiView>

Here we have 3 views inside our MultiView, one View control for each of our custom UserControls (Control1, Control2, and Control3).

 

Navigating Between The Views

Navigating between the views contained in the MultiView is as easy calling the SetActiveView on the MultiView, passing to it the view that you want to show. In the code-behind for the main View.ascx file of our module, we’ll read a query string parameter called ModuleView in order to determine which view to display.

switch (Request.Params["ModuleView"])
{
    case "view1":
        MultiView1.SetActiveView(View1);
        break;
    case "view2":
        MultiView1.SetActiveView(View2);
        break;
    case "view3":
        MultiView1.SetActiveView(View3);
        break;
    default:
        MultiView1.SetActiveView(HomveView);
        break;
}

Now that we have this functionality built into our module, you can manually switch between the views of the module by passing a parameter in the query string. Of course we can’t expect a user to do this, so we have to build some sort of navigation between the views of our module. We’ll create another control for this purpose that has 3 hyperlinks. Each hyperlink will link to the current page, passing the ModuleView parameter that corresponds to the view that the link should take the user to.

<%@ Control Language="C#" AutoEventWireup="true" 
    CodeBehind="ViewNavigation.ascx.cs"
    Inherits="Kemmis.Modules.ViewManagement.ViewNavigation" %>
<asp:HyperLink ID="HyperLink1" runat="server">
    Show View 1</asp:HyperLink>
<br />
<asp:HyperLink ID="HyperLink2" runat="server">
    Show View 2</asp:HyperLink>
<br />
<asp:HyperLink ID="HyperLink3" runat="server">
    Show View 3</asp:HyperLink>
<br />

Here we set the hyperlink navigateurl in the code-behind:

HyperLink1.NavigateUrl = 
    Globals.NavigateURL(TabId, "", "ModuleView=view1");
HyperLink2.NavigateUrl = 
    Globals.NavigateURL(TabId, "", "ModuleView=view2");
HyperLink3.NavigateUrl = 
    Globals.NavigateURL(TabId, "", "ModuleView=view3");

 

One Gotcha With A Solution

If you’re following along and you just tried to compile your module, you probably noticed a small problem. Our navigation control is an ordinary UserControl, so it doesn’t have direct access to DotNetNuke specific entities like TabID and the DNN Globals object. No worries, there’s an easy solution.

In order to give sub-controls in my modules access to all the functionality that the main module controls have, I’ve essentially made my sub-controls inherit from PortalModuleBase. However because of the way the DotNetNuke framework functions, sub-controls that inherit from PortalModuleBase do not get configured properly the way that main module controls do. You have to do this configuration in your own code. But don’t worry; I’ve done the work for you. You just need to inherit from the class I’m providing you called PortalModuleChild. This class inherits from PortalModuleBase and configures itself so that it can access the normal DotNetNuke integration points that main module controls have access to.

Here it is:

using System;
using System.Collections.Generic;
using System.Web;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Common;

namespace Kemmis.DNN
{
    public class PortalModuleChild :PortalModuleBase
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            System.Web.UI.Control parent = Parent as System.Web.UI.Control;
            while (!(parent is PortalModuleBase))
            {
                parent = parent.Parent;
            }
            ModuleConfiguration = ((PortalModuleBase)parent).ModuleConfiguration;
        }

        protected String CurrentView
        {
            get
            {
                return Request.Params["ModuleView"];
            }
        }

        protected string getEntityLink(int entityID)
        {
            return Globals.NavigateURL(TabId, "", 
                "ModuleView=EntityDetails&EntityID=" + entityID);
        }
    }
}

 

A Few More Things

One thing to keep in mind when working with the MultiView and View controls is that even though a View might not be the active View for the MultiView, its code is still executed normally inside the ASP.Net page life-cycle. This means that even though a View might not be visible, a control inside the View will still be doing things like querying the database or talking to your business logic layer, unless you explicitly tell it not to. So let’s take care of that problem.

You’ll notice that in my PortalModuleChild class I added a property called CurrentView. This property returns the value of the ModuleView query string parameter. This property can be used to check to see what view your module is in. Once you make your sub-controls inherit from PortalModuleChild, you’ll be able to use this property to determine if the module is in the view for which the sub-controls is required to run its code.

So inside your control, just wrap your code in an if statement like this to determine if the code should be executed:

protected void Page_Load(object sender, EventArgs e)
{
    if (CurrentView == "view1")
    {
        // do view 1 logic only if
        // module is showing this view
    }
}

 

And Another Tip

The PortalModuleChild class is a great place to insert data or functionality that you wish to make available to all of the sub-controls in your module. In my example you’ll see that I added (but didn’t use) a method called getEntityLink. This is a basic example of how you might provide a way for your sub-controls to quickly access the URL to a specific piece of content that your module can display. For example the “Entity” in your module might be a product. You could name your method getProductLink, and have it accept an SKU for the product. It would then return a URL that will allow the module to navigate to the particular view that displays products and accepts an SKU parameter in the query string.

These URL helper methods you can define once in your PortalModuleChild class, and use in any control that inherits from that class. There are plenty of other useful additions you can make to the PortalModuleChild class that I’m sure you can think of on your own. So go crazy!

 

In Summary

So managing the views of your module might not be an out-of-the-box functionality of the DotNetNuke framework, but it can be done quite elegantly using the ASP.Net MultiView control. Remember that all the code in your views will by default be executed, so don’t forget to add conditional statements in your views to check that the module is in the view for which your sub-control needs to execute its code.

And don’t forget to try out the PortalModuleChild class. In just about every module I’ve written I’ve found myself writing the same code over and over in each control. By using the PortalModuleChild class I’ve been able to write that code once, and still access it from within all of my controls.

That’s it! I hope you find this information helpful. You can access the example project code here.

This is the second post in my series of Silverlight animations, all based on the Flash animations published in the book Flash Math Creativity. Yesterday I posted Oscillating Ellipses.

Today’s Silverlight animation is a slight variation of the one I posted yesterday. In this animation there is no vertical or horizontal radius for the ball movement, so each ball appears to come straight at you.

Ellipses Toward Us

image

Like yesterday’s animation, today’s was also originally created by Jamie MacDonald using Actionscript. Here it is using Silverlight.

I did a bit more refactoring to the code for this animation. Now almost all of the animation parameters are contained within the ball class. This should allow me to create variations of the ball fairly easily in future animations.

Download the source code here.

Edit: View part I.II of the Silverlight Math Creativity series here.

 

Technorati Tags: ,,

So a friend of mine gave me an old Flash animation book called Flash Math Creativity. The book is published by FriendsofED, the same publisher that put out Foundation Silverlight 2 Animation. In Flash Math Creativity, several different ActionScript programmers demonstrate how to do some pretty complex animations, all from within code. Now implementing the examples from the book in ActionScript is a pretty trivial task. You just type out the code from the book an run it. Rewriting and running them in Silverlight though is a different story.

As an exercise to learn the best ways to do complex animations in Silverlight, I’ve begun implementing my own versions of the examples from Flash Math Creativity using Silverlight. I will be posting my results here on the blog.

And here is the first animation from the book!

Oscillating Ellipses

image

This animation was originally created by Jamie MacDonald using ActionScript . Here it is using Silverlight 2.

In this animation I used the DispatcherTimer class to create a new red ball every 100

milliseconds. The movement of the balls is done from within an event handler in each ball instance. In essence, there is a loose-coupling between the main Page class which creates the red balls, and the actual ball classes. Once the balls are created, each instance manages its own animation and removes itself from the LayoutRoot when its opacity becomes 0.

The other key to this animation is that each ball subscribes to the CompositionTarget.Rendering event. I found that using this event to animate on works much better because it runs on a separate thread. On the other hand the DispatcherTimer.Tick event is managed and dispatched from within the UI thread, and so it doesn’t keep pace as well as the CompositionTarget.Rendering event.

Download the source code here.

Edit: View part I.I of the Silverlight Math Creativity series here.

 

Who Is Rafe

rafe

Rafe Kemmis

I am an audacious web developer with a double bachelor of science in Computer Science and Mathematics. I specialize in Microsoft ASP.Net, Silverlight, and Adobe ActionScript.

Questions?

Always a thoughtful response. You may post your question on an article, or contact me directly.

Hire Me.

I provide custom solutions to complex problems. I can help your business no matter how large or small.

Contact me now.

Subscribe