Variable Line Resolution

Actionscript:
  1. var loc:Vector.<Point> = new Vector.<Point>();
  2. var lifts:Vector.<int> = new Vector.<int>();
  3. var index:int = 0;
  4. var resolution:int = 1;
  5. var down:Boolean;
  6. stage.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
  7. stage.addEventListener(MouseEvent.MOUSE_UP, onUp);
  8. stage.addEventListener(KeyboardEvent.KEY_UP, onKeyReleased);
  9. addEventListener(Event.ENTER_FRAME, onLoop);
  10. function onKeyReleased(evt:KeyboardEvent):void{
  11.     if (evt.keyCode == Keyboard.RIGHT){
  12.         resolution -= 1;
  13.         if (resolution <1) resolution = 1;
  14.     }else
  15.     if (evt.keyCode == Keyboard.LEFT){
  16.         resolution += 1;
  17.     }
  18. }
  19. function onDown(evt:MouseEvent):void{
  20.     down = true;
  21.     lifts.push(index);
  22. }
  23. function onUp(evt:MouseEvent):void{
  24.     down = false;
  25. }
  26. function onLoop(evt:Event):void {
  27.     if (down){
  28.         loc[index] = new Point(mouseX, mouseY);
  29.         index++;
  30.     }
  31.     graphics.clear();
  32.     graphics.lineStyle(0,0x000000);
  33.     var liftIndex:int = 0;
  34.     for (var i:int = 0; i<loc.length; i++){
  35.         if (liftIndex == lifts.length) liftIndex = 0;
  36.         if (i == lifts[liftIndex]){
  37.             graphics.moveTo(loc[i].x, loc[i].y);
  38.             liftIndex++;
  39.         }else{
  40.             if (i % resolution == 1 || resolution == 1){
  41.                 graphics.lineTo(loc[i].x, loc[i].y);
  42.             }
  43.         }
  44.     }
  45. }

This snippet shows a simple approach to creating variable resolution graphics.

Below is a drawing done using this snippet. The left arrow key is used to decrease resolution and the right arrow key is used to increase resolution:


Click to view swf...

This entry was posted in Graphics, Vector and tagged , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

3 Comments

  1. Posted July 8, 2009 at 2:00 am | Permalink

    hi,
    great blog, thanks a lot for sharing.

    I simply wanted to mention another approach adressed here: http://www.motiondraw.com/blog/?p=50&cpage=1#comment-16005
    atb

  2. Posted July 8, 2009 at 7:06 am | Permalink

    very cool

  3. bijoy
    Posted February 16, 2010 at 12:51 am | Permalink

    how can i highlight each drawn lines on click or rollover.

    pls help me with this

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*