Wave or Lake effect control






4.60/5 (14 votes)
Jun 12, 2003
2 min read

86432

3504
Implementation of wave or lake effect in C#
Introduction
The WaveControl
control allows you to include the lake effect that has been available on web pages for the past 6 or 7 years, from groups like Anfy (www.anfy.com). With a name like Lyquidity, and corny as it is, liquid motion effects are used in splash screens and in some marketing materials. We wanted to include the wave or lake effect to the repertoire but were unable to find a C# implementation so we decided to create one ourselves.
The control
WaveControl
inherits from System.Windows.Forms.Control
and so implements its own painting to animate the rippling water effect. The actions taken by the control are to:
- Load an image
- Create an inverted copy of the image
- Create the frames for animation
- Run the animation
The real work is done in the DrawLakeEffect()
method. This method creates a frame of the animation at a time. There are plenty of notes in this method explaining what it does and how its done, so no explanation is included here (at least for now).
The animation is controlled by a timer and on each tick a frame is drawn onto the controls surface. Though the frame rate is fixed (you can change that), the user can control the number of frames generated and so the length of the animation. Other aspects of the animation that can be controlled include, the wave height, the number of ripples and the damping factor used.
A default image is displayed (mine), but an alternative image can be passed to the control. The control will size itself to the width of the image and 2 * the height.
VS.NET version
We use VS.NET 2003 for development work, so the .sln and .csproj files are in this format and cannot be opened in the earlier version of VS.NET. The good news is that no VS.NET 2003 specific features are used, so creating a project for the earlier will be straightforward. The one thing to bear in mind is that WaveControl.cs uses an unsafe {}
block while generating the animation frames, so you will need to set this configuration property to true before the code will compile.
Things that could be done
The Anfy control includes randomization features to simulate wind and to give a more natural appearance and adding such effects would be good. The use of a timer works just fine, but encapsulating the painting in a thread would be better.