The below example shows a simple paint application in Silverlight. You can select from 10 colours, change the brush size and the opacity.

To get this effect I run some event handlers to check when the mouse is being pressed. While the mouse is pressed, every time the mouse is moved a new ‘Line’ element is produced and plots the co-ordinates from the previous mouse position to the current position. This ‘Line’ element is then styled up based on the selection from the colour palette and sliders. Finally I set Line1.StrokeStartLineCap = PenLineCap.Round; and Line1.StrokeEndLineCap = PenLineCap.Round; to ensure the joins between each ‘Line’ element fits snugly.

Possible improvements

Firstly it would be nice to add a feature to erase paint – I haven’t spent much time thinking about how to do this yet as I’m sure it’s going to be quite tricky.

It would be quite easy to add tools to draw rectangles and circles and even reasonably easy to add a gradient tool to colour these up.

Text would be an interesting addition.

The ability to save your master piece to either return at a later point or export to an image would be cool.

Get the code

As always, feel free to grab the code here and start playing!

The below example shows a dragable ball that when released will bounce around with a simulated gravity effect. There is also a box (‘Move me’) that you can drag that the bouncy ball will bounce off / on.

In a previous post (here) I dabbled in making a dragable bouncy ball that simulated a gravity effect when released. I have taken that project and cleaned it up a bit to get rid of most of the bugs.

I have also set up all of the controlling factors as variables so you can get in and play with the effects easier, see below for your ‘built in’ options:

//Declare the limits of where the ball can bounce
private double BounceBoundaryLeft = 10;
private double BounceBoundaryTop = 10;
private double BounceBoundaryRight = 490;
private double BounceBoundaryBottom = 390;
//Set Margin for dragging the Ball around the edge of the screen
private double DragMargin = 30;
//Declare Ball properties
private double BouncyBallWeight = 0.7;
private double BounceLevel = 0.8;
// Declare Shadow properties
private double ShadowOffset = 10;
private double ShadowHeightLimit = 300;

Get the code

Feel free to grab the code here and start playing!

So below is a first attempt on implementing gravity on a dragable object. There are a couple minor bugs still but the core idea generally works.

Use the mouse to click and drag the ball, move the mouse and let go of the mouse button as you’re moving the mouse. The ball will pick up on the direction you were moving and continue in the same direction.

While the ball continues in this direction a force will pull the ball towards the ground, effectively trying to simulate gravity.

If the ball hits the ground the momentum built up by the ball will be reflected partially and rebound the ball back upwards.

If the ball hits the left, top or right boundary then the ball will be reflected partially and rebound in the opposite direction.

If the ball is dragged outside the of the boundaries of the Silverlight area then the ball will continue to move in the direction it was going in at the last point of contact, bouncing off the related boundary.

I have featured 2 Sliders to allow you to adjust the variables slightly to change the effect on the ball.

Future Enhancements

There are still a few bugs, every so often the ball will react a bit crazy – this is due to my coding no doubt, I would like to address this.

I would like to implement a dragable block that the ball would react and bounce off.

I would like to be able to have multiple balls on the stage with different properties that can additionally interact/bounce off each other

Get the code

As always you can grab the code here, please pull it apart and enjoy!