A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: Flash stroke styles dynamically ?

  1. #1
    Senior Member
    Join Date
    Feb 2001
    Posts
    218

    Flash stroke styles dynamically ?

    The following will draw a line from (0, 0) to (100, 100). The line will be black, have a width of 1 pixel and 100% alpha
    Code:
    _root.createEmptyMovieClip("line_mc", 10);
    _root.line_mc.lineStyle(1, 0x000000, 100);
    _root.line_mc.lineTo(100, 100);
    Does anyone know how to alter the above code so that the line has a stroke style of any of the following: Dotted, Dashed, Ragged, Stipple, Hashed ? Like when you draw a line in flash and change the stroke style with the drop down box called 'Stroke Style'. Can you access these stroke styles with code ?

    Thanks,
    Stephen.

  2. #2
    Banned NTD's Avatar
    Join Date
    Feb 2004
    Posts
    3,438
    Hi,

    Not sure about the other line styles, but this works well for dotted or dashed lines..........

    code:

    MovieClip.prototype.dashTo = function(startx, starty, endx, endy, len, gap) {
    // ==============
    // mc.dashTo() - by Ric Ewing ([email protected]) - version 1.2 - 5.3.2002
    //
    // startx, starty = beginning of dashed line
    // endx, endy = end of dashed line
    // len = length of dash
    // gap = length of gap between dashes
    // ==============
    //
    // if too few arguments, bail
    if (arguments.length<6) {
    return false;
    }
    // init vars
    var seglength, deltax, deltay, segs, cx, cy;
    // calculate the legnth of a segment
    seglength = len+gap;
    // calculate the length of the dashed line
    deltax = endx-startx;
    deltay = endy-starty;
    delta = Math.sqrt((deltax*deltax)+(deltay*deltay));
    // calculate the number of segments needed
    segs = Math.floor(Math.abs(delta/seglength));
    // get the angle of the line in radians
    radians = Math.atan2(deltay, deltax);
    // start the line here
    cx = startx;
    cy = starty;
    // add these to cx, cy to get next seg start
    deltax = Math.cos(radians)*seglength;
    deltay = Math.sin(radians)*seglength;
    // loop through each seg
    for (var n = 0; n<segs; n++) {
    this.moveTo(cx, cy);
    this.lineTo(cx+Math.cos(radians)*len, cy+Math.sin(radians)*len);
    cx += deltax;
    cy += deltay;
    }
    // handle last segment as it is likely to be partial
    this.moveTo(cx, cy);
    delta = Math.sqrt((endx-cx)*(endx-cx)+(endy-cy)*(endy-cy));
    if (delta>len) {
    // segment ends in the gap, so draw a full dash
    this.lineTo(cx+Math.cos(radians)*len, cy+Math.sin(radians)*len);
    } else if (delta>0) {
    // segment is shorter than dash so only draw what is needed
    this.lineTo(cx+Math.cos(radians)*delta, cy+Math.sin(radians)*delta);
    }
    // move the pen to the end position
    this.moveTo(endx, endy);
    };
    function drawDash(startx, starty, endx, endy, len, gap) {
    _root.clear();
    _root.lineStyle(2, 0x6688AA);
    _root.dashTo(startx, starty, endx, endy, len, gap);
    updateAfterEvent();
    }
    drawDash(100, 100, 200, 100, 4, 8);


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center