4 Replies Latest reply on May 2, 2010 9:51 PM by MythProd2010

    Element width vs Height

    MythProd2010 Level 1

      Hi guys, I'm a bit stumped on this one. I'm writing jsfl script where I'm setting an element width and height. When I go to run the script, my element's width and height are off by what I set it to. For example, I set the width and height to 800, the results I get for width and height are 799.4, 799.0 respectively. I'm not understanding why my element's width and height aren't exactly what I typed in?


      Btw, I can go into the "size and position" property UI and set them to 800 x 800 and those values stick. It's only when I set the width and height via the element.width and element.height that my values change on me. Maybe I'm asking for a lot here but I'd like my values to be what I give them, not some other number "close" to what I gave them!?!


      Any clue, help or comments welcome.


      - MythProd

        • 1. Re: Element width vs Height
          davebelais Level 1

          Width and height aren't static attributes, when you set the width or height properties Flash measures the bounding box of the Element, then sets the transformation matrix to compensate. Flash does so in a native code operation ( C++ or objective C depending on OS ), and I expect the C class that translates the javascript value is trying to be clever by saving an infinitesimal amount of memory using a less precise number type.

          The solution to your problem is to perform your own math in javascript, so that you circumvent the problematic function. You can use document.scaleSelection(), document.transformSelection(), or set the element's matrix attribute directly. Setting the matrix attribute directly you will find to be the most accurate, but a little tricky since the matrices in flash are treated differently according to Element type.

          • 2. Re: Element width vs Height
            MythProd2010 Level 1

            Thanks, David. Can you give me an example of how I would calculate the matrix for width and height for a movieclip (I'm only using movieclips)? I've been reading over a senocular tutorial here (http://www.senocular.com/flash/tutorials/transformmatrix/) and it sounds like "Scale" may be the portion of the page I want to pay attention to.

            • 3. Re: Element width vs Height
              davebelais Level 1

              First get the scale values you need to transform the movie clip by:


              var widthMultiple=desiredWidth/element.width;

              var heightMultiple=desiredHeight/element.height;


              You probably don't need to set the matrix directly, I expect using fl.getDocumentDOM().scaleSelection(widthMultiple, heightMultiple) will do the job, however if you want to do the matrix calculations, I can explain in them briefly:


              In flash a transformation matrix is really a 2x2 matrix ( the default being {a:1,b:0,c:0,d:1} )  with a registration point ({tx:0,ty:0}).

              (matrix.a,matrix.b) is the vector for the x axis. (matrix.c,matrix.d) is the vector defining the y axis.

              That just means that moving 1 unit along the x axis is equal to moving a units along the x axis and b units along the y axis in the element's parent's coordinate space, and that moving 1 unit along the y axis is equal to moving c units along the x axis and d units along the y axis in the element's parent's coordinate space. The registration point (tx,ty) is the location of the elements origin/registration point in it's parent's coordinate space.

              So long as the vectors (a,b) & (c,d) are tangent to one another, flash will show a rotation value in the transformation panel, otherwise flash will display a skew value. Scale is determined by the length of (a,b) and (c,d). So the square root of ( a2 + b2) is scaleX and the square root of ( c2 + d2) is scaleY. The scaleX and scaleY values of an element are not necessarily directly proportional to it's width and height, however, which are measured in the element's parent's coordinate space.


              What you want to do in order to scale the matrix in it's parent's coordinate space is to concatenate it with a new matrix which has just the desired transformation. An untransformed matrix is {a:1,b:0,c:0,d:1,tx:0,ty:0}, so to double the width of an element you would concatenate it's matrix with {a:2,b:0,c:0,d:1,tx:0,ty:0}. To double the height, concatenate it with {a:1,b:0,c:0,d:2,tx:0,ty:0}. You can use fl.Math.concatMatrix() for this. Your calculation would be:




              You can also use document.setSelectionBounds() to set the width and height.



              1 person found this helpful
              • 4. Re: Element width vs Height
                MythProd2010 Level 1

                Brilliant! I appreciate the great info on matrix calculations as well as the heads up on fl.getDocumentDOM().scaleSelection(widthMultiple, heightMultiple). That worked a treat!


                Being new to Adobe's new forum software I meant to mark this answer as "correct" instead of just helpful. My apologies on that. If it's possible to change that after the fact let me know and I will.


                Thanks a bunch!