TwelvestoneFlash

changing coords of bitmapdata relative to the bitmap


Sign in

  • Waiting for Godot ( 730 k posts )
    Just conversation.
  • Thunder Dome ( 23 k posts )
    Photoshop Tennis and Collabs.
  • Photography ( 5.1 k posts )
    For all you shutterbugs, sh...
  • Flash ( 18 k posts )
    ActionScripting to tweens, ...
  • Front End ( 5.9 k posts )
    general front end design an...
  • Back End ( 9.7 k posts )
    serverside scripting, progr...
  • Projects and Theory ( 12 k posts )
    This forum is for discussio...
  • FAQ ( 269 posts )
    All those nagging questions...
  • Design ( 17 k posts )
    graphics & all aspects of g...
  • Purgatory ( 3.6 k posts )
    12stone Jail, feel free to ...
baron ruhstoff
 
2008-06-24

Scaling a 600 x 300 bitmap to fit in a rectangle 300 x 300 is easy. But say that instead of getting a 300x150 bitmap, you wanted it 300x300 with the 300x150 bitmapdata centered vertically within the rectangle? Any suggestions?

Here's where I'm at so far...

public static function createThumb(bd:BitmapData, maxW:Number, maxH:Number, doCenter:Boolean = true):BitmapData{
    // make the bitmap we'll use to get the dimensions
    var bmp:Bitmap = new Bitmap(bd);

    // store the new dimensions
    var nW:int = Math.round(bmp.width);
    var nH:int = Math.round(bmp.height);

    // get the proportions for resizing
    var ratio:Number = nW/nH;

    // set the width
    if (nW > maxW) {
        nW = maxW;
        nH = Math.round(nW / ratio);
    }

    // set the height
    if (nH > maxH) {
        nH = maxH;
        nW = Math.round(nH * ratio);
    }

    // create the matrix we'll use to set scale and position
    var matrix:Matrix = new Matrix();

    // center the bitmapdata within the given width and height
    if (doCenter){
        var dx:Number = (maxW - nW)/2;
        var dy:Number = (maxH - nH)/2;
        matrix.translate(dx, dy);
    }

    // scale the thing
    matrix.scale(nW / bmp.width, nH / bmp.height);

    // define the bitmapdata
    var bmpd:BitmapData = new BitmapData(maxW, maxH, true, 0xFFFFFFFF); // transparent (just in case)               
    bmpd.draw(bmp,matrix);

    return bmpd;
}
baron ruhstoff
 
2008-06-24

Looks like setting matrix.scale prior to matrix.translate took care of it.

Arsis
 
2008-06-25

do the matrix scale before you do the translate...

..oh... nm k

Sorry, you must be a member to post to a conversation. Either log in or sign up to get involved.
TwelvestoneFlash

changing coords of bitmapdata relative to the bitmap