Flash scribble pad using Ming

All content copyright (c) 2003 Walter Ogburn

This is written using the basic C library version of Ming but wrapped for Matlab. Sorry, please look past the strange syntax - since it's almost all ActionScript it should be easy enough to translate to PHP or whatever else. Using the ActionScript 6 code this is very simple and the resulting SWF file is only ~750 bytes.

This flash animation was created using Ming 0.2a by Opaque Industries. It is an open source flash SWF library available at ming.sourceforge.net.


Scribble pad - click and drag to draw

(Requires Flash Player 6)


The Matlab code


% Ming SWF code for a scribble pad 
%
% RWO Feb 6 2003
%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Constants
%

outpath = './';
moviename = 'ming_draw';

MINLENGTH = 1;                  % Don't draw line segments shorter than this
MAXCOLOR = hex2dec('ffffff');   % Largest possible color
EPSILON = MINLENGTH;            % When pen goes down, draw at least this -
                                % want a dot to show up if mouse is clicked.

              
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Setup
%

Ming_init;
Ming_setScale (20);
Ming_useSWFVersion (5);         % Change to 6 after saving.
m = newSWFMovie;
SWFMovie_setDimension (m, 320, 240);
SWFMovie_setRate (m, 30.0);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Body
%

c = newSWFMovieClip;            % This movie clip will contain only
                                % ActionScript, which will draw everything.

% First frame : set up all variables, define methods to respond
% to MouseUp and MouseDown events.

SWFMovieClip_add (c, compileSWFActionCode ([ ...
        'var pen_down = 0;' ... 
        'var last_x;' ...               % Endpoint of last
        'var last_y;' ...               % line segment.
        'var new_x;' ...                % Endpoint of next
        'var new_y;' ...                % line segment.
        'this.onMouseUp = function ()' ...
        '{' ...
        '       pen_down = 0;' ...              % Raise pen,
        '       last_x = undefined;' ...        % stop connecting.
        '};' ...
        'this.onMouseDown = function ()' ...
        '{' ...
        '       pen_down = 1;' ...              % Lower pen,
                                             ...% Set new line style & color.
        '       this.lineStyle (2, random (' int2str(MAXCOLOR+1) '));' ...
        '};' ...
]));

SWFMovieClip_nextFrame (c);

% Second frame : do the drawing.
%

SWFMovieClip_add (c, compileSWFActionCode ([ ...
        'if (pen_down == 1)' ...
        '{' ...
        '       new_x = this._xmouse;' ...
        '       new_y = this._ymouse;' ...
        '       if (last_x == undefined)' ...   % Starting a new line.
        '       {' ...                          % Get ready to connect.
        '               last_x = new_x;' ...
        '               last_y = new_y;' ...
        '               this.moveTo (last_x+' int2str(EPSILON) ', last_y);' ...
        '               this.lineTo (new_x, new_y);' ...
        '               this.moveTo (last_x, last_y);' ...
        '       }' ...
                                             ...% Check whether new line
                                             ...% segment is long enouth
                                             ...% to draw.
        '       else if (Math.abs(new_x-last_x)+Math.abs(new_y-last_y)' ...
        '               > ' int2str(MINLENGTH) ')' ...
        '       {' ...
        '               this.lineTo (new_x, new_y);' ...
        '               last_x = new_x;' ...
        '               last_y = new_y;' ...
        '       }' ...
        '}' ...
        '' ...
]));

SWFMovieClip_nextFrame (c);

% Third frame : loop back.
%

SWFMovieClip_add (c, compileSWFActionCode ([ ...
        'this.gotoAndPlay (2);' ...
]));

d = SWFMovie_add (m, c);
SWFMovie_nextFrame (m);
SWFMovie_add (m, compileSWFActionCode ([ ...
        'stop();' ...
]));
SWFMovieClip_nextFrame (c);
SWFMovie_nextFrame (m);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Output & play
%

SWFMovie_save (m, [outpath moviename '.swf']);
destroySWFMovie (m);
destroySWFMovieClip (c);

% This line zaps the saved file to change its version number to 6.
% If you are under Unix, can do something like
%
%       xxd in.swf | replace "0000000: 4657 5305" "0000000: 4657 5306"
%                | xxd -r > out.swf
%

SWF_force_v6 ([outpath moviename '.swf']);
flashplay ([outpath moviename '.swf']);