将小图像中的rect映射到较大的图像(以便进行copyPixels操作)

时间:2010-06-16 15:18:08

标签: math actionscript

这是(我认为)一个相对简单的数学问题,但我花了一天时间撞击它,只有凹痕而没有解决方案......

我在动作3中编码 - 功能是:

  1. 运行时加载的大图像。存储了bitmapData并创建了一个较小的版本以显示在可用的屏幕区域上(我可能最终只是缩放大图像,因为它仍在内存中)。

  2. 用户可以在较小的图像上创建一个矩形热点(功能将更复杂:多个透明度:例如带孔的圆环形状等)

  3. 3当用户点击热点时,热点的矩形将映射到较大的图像,并使用较大的位图数据创建新的位图“callout”。这样做的原因是“标注”将比仅扩大热点区域的质量更好。

    下图显示了我到目前为止的位置 - 蓝色矩形是点击的热点。左上角是“标注” - 从较大的图像复制。我的宽高比是正确的,但我没有正确映射到更大的图像。

    下面的丑陋代码...对不起,这篇文章太长了 - 我只是觉得我应该提供尽可能多的信息。感谢您的任何提示!

    - 跟踪我的数据值

    *源BitmapDada 1152 864

    缩放到800 600

    缩放BitmapData 800 600

    选择BitmapData 58 56

    比例选择83 80

    比率1.44

    之前(x = 544,y = 237,w = 58,h = 56)

    (x = 544,y = 237,w = 225.04,h = 217.28) *

    图片在这里:http://i795.photobucket.com/albums/yy237/skinnyTOD/exampleST.jpg

    public function onExpandCallout(event:MouseEvent):void{
        if (maskBitmapData.getPixel32(event.localX, event.localY) != 0){
            var maskClone:BitmapData = maskBitmapData.clone();
    
            //amount to scale callout - this will vary/can be changed by user
            var  scale:Number =150 //scale percentage
            var normalizedScale :Number = scale/=100;
    
            var w:Number = maskBitmapData.width*normalizedScale;
            var h:Number = maskBitmapData.height*normalizedScale;
    
            var ratio:Number =  (sourceBD.width /targetRect.width);
    
            //creat bmpd of the scaled size to copy source into
           var scaledBitmapData:BitmapData = new BitmapData(maskBitmapData.width * ratio, maskBitmapData.height * ratio, true, 0xFFFFFFFF);
    
           trace("source BitmapDada " + sourceBD.width, sourceBD.height);
           trace("scaled to rect " + targetRect.width, targetRect.height);
           trace("scaled BitmapData", bkgnImageSprite.width, bkgnImageSprite.height);
           trace("selection BitmapData", maskBitmapData.width, maskBitmapData.height);
           trace("scaled selection", scaledBitmapData.width, scaledBitmapData.height);
           trace("ratio", ratio);
    
           var scaledBitmap:Bitmap = new Bitmap(scaledBitmapData);
    
           var scaleW:Number = sourceBD.width / scaledBitmapData.width;
           var scaleH:Number = sourceBD.height / scaledBitmapData.height;
    
           var scaleMatrix:Matrix = new Matrix();   
           scaleMatrix.scale(ratio,ratio);
    
           var sRect:Rectangle = maskSprite.getBounds(bkgnImageSprite);
           var sR:Rectangle = sRect.clone();
           var ss:Sprite = new Sprite();
           ss.graphics.lineStyle(8, 0x0000FF);
           //ss.graphics.beginFill(0x000000, 1);
           ss.graphics.drawRect(sRect.x, sRect.y, sRect.width, sRect.height);
           //ss.graphics.endFill();
           this.addChild(ss);
    
           trace("before " + sRect);
    
           w = uint(sRect.width * scaleW);
           h = uint(sRect.height  * scaleH);
    
           sRect.inflate(maskBitmapData.width * ratio, maskBitmapData.height * ratio);
           sRect.offset(maskBitmapData.width * ratio, maskBitmapData.height * ratio);
    
           trace(sRect);
    
           scaledBitmapData.copyPixels(sourceBD, sRect, new Point());
           addChild(scaledBitmap);
           scaledBitmap.x = offsetPt.x;
           scaledBitmap.y = offsetPt.y;
        }
    }
    

1 个答案:

答案 0 :(得分:0)

谢谢!

public function onExpandCallout(event:MouseEvent):void{
// TODO: build this on startup or only on click? Speed vs memory

if (calloutState == true) return;

if (maskBitmapData.getPixel32(event.localX, event.localY) != 0){

    calloutState = true;

    //create bitmap from source using scaled selection rect
    var ratio:Number =  (sourceBMD.width /targetRect.width);
    var sRect:Rectangle = hotSpotSprite.getBounds(bkgnImageSprite);
    var destRect:Rectangle = new Rectangle(sRect.x * ratio, sRect.y * ratio, sRect.width * ratio, sRect.height * ratio);
    calloutBitmapData = new BitmapData(destRect.width, destRect.height, true, 0xFFFFFFFF);
    calloutBitmap = new Bitmap(calloutBitmapData);

    //-- scale alpha mask
    var scaledMaskBitmapData:BitmapData =  new BitmapData(destRect.width, destRect.height, true, 0x00000000);
    var maskScale:Number = scaledMaskBitmapData.width / maskBitmapData.width;
    var mMatrix:Matrix = new Matrix(maskScale, 0, 0, maskScale);
    scaledMaskBitmapData.draw(maskBitmapData,mMatrix,null,null,null, false);

    // copy source with scaled alpha  
    calloutBitmapData.copyPixels(sourceBMD, destRect, new Point(), scaledMaskBitmapData, new Point());

    scaledMaskBitmapData = null;

    // apply filter to bitmap
    var myDropShadowFilter:DropShadowFilter = new DropShadowFilter();
    myDropShadowFilter.distance = 12;
    myDropShadowFilter.alpha = .3
    myDropShadowFilter.strength = 1;
    myDropShadowFilter.blurX = 8;
    myDropShadowFilter.blurY = 8;
    calloutBitmap.filters = [myDropShadowFilter];

    //place on screen
    calloutSprite = new Sprite();
    calloutSprite.addChild(calloutBitmap)
    calloutSprite.x = offsetPt.x;
    calloutSprite.y = offsetPt.y;
    // ADD TO PARENT DisplayContainer
    calloutLayer.addChild(calloutSprite);

    //              calloutSprite.scaleX = 2;
    //              calloutSprite.scaleY = 2;
    calloutSprite.doubleClickEnabled = true;

    calloutSprite.addEventListener(MouseEvent.DOUBLE_CLICK, onCollapseCallout); 
    calloutSprite.addEventListener(MouseEvent.MOUSE_DOWN, onStartDrag); 
    calloutSprite.addEventListener(MouseEvent.MOUSE_UP, onStopDrag);    
}

}