Fantasia Lite: New Silverlight Painting Tool

Jul 22, 2010

Edit: Fantasia Painter (full version), is now available here: http://fantasia.nokola.com

Edit 2: The Fantasia Lite version below, has been updated to include the new Rainbow brush (source code included too).

Here’s what I built tonight – a drawing tool that utilizes procedural brushes to enable almost everyone to create fluffy rabbits like this:

rabbit

Note that it took me about 2 minutes to “build the rabbit”. I’m sure someone else can do better. Supposedly, it lives in the hole on the right.

Here’s the live app:

As usual: Download source code

Note that this whole thing is inspired by http://mrdoob.com/120/Harmony (HTML5 – doesn’t work in IE8 as I write this) which was originally inspired by http://www.zefrank.com/scribbler/gallery/index_ran.html. By the way, make sure to check the funny videos “How To Dance Properly” on Ze’s page: http://www.zefrank.com/invite/swfs/index2.html

I talked way back (March or something) with Mr. Doob and he released his Harmony code under the MIT license. His latest source code is now released under GPL though. I didn’t look at it since I wanted my sources to be MS-PL license-able.

What Is A Procedural Brush?

A typical “brush” will just draw whatever it is supposed to draw (e.g. connect dots with lines). It just needs the current and previous mouse position.

A procedural brush is “smarter”, since it takes more variables into account.

Fantasia’s brushes make use of the following data to modify the drawing output:

  • mouse position
  • velocity (how fast the mouse moved)
  • time between 2 consecutive dots
  • distance between dots
  • slope (angle) of the line between the last and current dot
  • the last N previous dots (N varies between 2 and 100000)

The Fantasia brushes modify these outputs:

  • Color (e.g. color can be changed based on angle which can produce cool output – I haven’t published this brush yet)
  • Opacity (the faster you move, the less visible the trace is, such as in the OldPen sample
  • Stroke width (again can be controlled by speed, or maybe by slope (angle), which produces a nice caligraphy effect)
  • Drawing other random lines

Here is the source code of few of the brushes:

The Line Brush – same as a “regular” brush, just connects dots with straight lines:

public override void Stroke(double x, double y, double dx, double dy, double timeMsec, double distance)
{
    _surface.Render(new Line() { X1 = _prevX, Y1 = _prevY, X2 = x, Y2 = y, Stroke = _brush }, null);
}

The Old Pen brush, uses velocity = distance / time, to modify the color intensity:

public override void Stroke(double x, double y, double dx, double dy, double timeMsec, double distance)
{
    _surface.Render(new Line()
    {
        X1 = _prevX,
        Y1 = _prevY,
        X2 = x,
        Y2 = y,
        Stroke = new SolidColorBrush(
            new Color()
                {
                    A = (byte)(255 - distance / timeMsec * 100),
                    R = (byte) (_color.R * (distance / timeMsec * 100)),
                    G = (byte) (_color.G * (distance / timeMsec * 100)),
                    B = (byte) (_color.B * (distance / timeMsec * 100)),
                })
    }, null);
}

The History brush, most complex so far and used for the sketch and Furs:

public override void Stroke(double x, double y, double dx, double dy, double timeMsec, double distance)
{
    _points.Add(new Point(x, y));
    if ((Memory > 0) && (_points.Count > Memory)) _points.RemoveAt(0);

    _surface.Render(new Line() { X1 = _prevX, Y1 = _prevY, X2 = x, Y2 = y, Stroke = _solidBrush }, null);

    foreach (Point p in _points)
    {
        double cdx = p.X - x;
        double cdy = p.Y - y;

        double dist2 = cdx * cdx + cdy * cdy;

        if ((dist2 < _activationDistance2) && (_random.NextDouble() > (dist2 / (_activationDistance2 * ActivationChance))))
        {
            _surface.Render(new Line() { X1 = x + cdx * ReachSource, Y1 = y + cdy * ReachSource, X2 = p.X - cdx * ReachDest, Y2 = p.Y - cdy * ReachDest, Stroke = _stroke }, null);
        }
    }
}

The history brush can also do internal highlights if the _stroke is set to a LinearGradientBrush.

There is also a prototyping Test brush: I’m trying to “smoothen” the line as it is getting drawn. It’s using binaries by Charlez Petzold: http://www.charlespetzold.com/blog/2009/01/Canonical-Splines-in-WPF-and-Silverlight.html

Hope you like it! Please comment!

  

Comments

7/22/2010 2:11:59 PM #

Rene Schulte

Awesome!

Rene Schulte Germany | Reply

7/22/2010 2:29:12 PM #

nokola

Thanks! Smile

nokola United States | Reply

7/22/2010 2:39:30 PM #

pingback

Pingback from topsy.com

Twitter Trackbacks for
        
        Fantasia Lite: New Silverlight Painting Tool
        [nokola.com]
        on Topsy.com

topsy.com | Reply

7/22/2010 3:16:49 PM #

pingback

Pingback from alvinashcraft.com

Dew Drop – July 22, 2010 | Alvin Ashcraft's Morning Dew

alvinashcraft.com | Reply

7/22/2010 4:13:08 PM #

Marc Ziss

Wow,
All of a sudden I feel like an artist!(previously I was proud to draw a recognizable stick figure). Great Job!

Marc Ziss United States | Reply

7/22/2010 8:47:23 PM #

nokola

Thanks!
Yes, I was the same when I tried Harmony for the first time and wanted to do something similar in Silverlight since thenSmile

nokola | Reply

7/22/2010 4:28:14 PM #

urza

Lovely sir. When I first saw Mr.Doobs Harmony (a while back), I was thinking "it would be cool to port this to WPF". I have it in my TODO list, but havent got enought time yet. So it is good to see, you did it.

One thing that would be awasome - enable the history, so we can use "back" (like CTRL+Z). I know it is more challenging than with nonprocedural brushes, but it should be possible, what do you think?

urza Czech Republic | Reply

7/25/2010 11:57:04 AM #

nokola

Thanks! All Fantasia Lite brushes are now integrated in what was easypainter (now named Fantasia!). Check it out here: http://nokola.com/fantasia
It's not just undo and redo, you get all the other bells and whistles, like effects, cropping, flickr and transforms in the new Fantasia Painter.
I haven't hooked the keyboard yet  (Ctrl+Z), but clicking on the undo button works Smile
Thank you for the feedback! If you have more suggestions, I'll appreciate if you mention them!

nokola United States | Reply

7/22/2010 4:40:08 PM #

trackback

Fantasia Lite: интересный графический редактор

Thank you for submitting this cool story - Trackback from progg.ru

progg.ru | Reply

7/23/2010 2:22:49 AM #

Sl.ayer

Nice. I was just thinking about doing some work on natural looking brushes...
Anyway, here is my bunny: dl.dropbox.com/u/3528765/images/sketchbunny.jpg.

Sl.ayer United States | Reply

7/23/2010 2:39:57 AM #

nokola

Amazing! The bunny is pretty nice Smile Mine looks like some form of mutant compared to yours, nice job.

nokola United States | Reply

7/26/2010 8:07:50 AM #

Adam Kinney

Great job! Nice job on the procedural brushes, now if you could just add a plug-in model, maybe we could try and write our own Smile

Adam Kinney United States | Reply

8/14/2010 1:46:00 PM #

agriturismo marche

Silverlight painting tool is an all-purpose tool for dabbling with color schemes, and outputting web pages or color values for use in other software...

agriturismo marche United States | Reply

8/23/2010 2:20:42 AM #

pingback

Pingback from paintingpaintbrush.com

Fantasia Lite: New Silverlight Painting Tool | Painting and Paintbrush

paintingpaintbrush.com | Reply

8/26/2010 8:19:46 AM #

umi

THIS IS TOO GOOD TO BE TRUE! Thanks for sharing this! Smile
This awesome app + tablet/digital wacom is all designers need.

umi Indonesia | Reply

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading



nokola.com | Terms | Log in

Recent

About the author

Happy & enjoying life. Software enthusiast.
The opinions I express here and on nokola.com are mine and not my employeer's (Microsoft).
This is the official blog of nokola.com. You can find Silverlight samples, coding stuff, and hopefully other interesting things here.