0
votes

I'm trying to draw a video(webcam) output on a Bitmapdata, it works if the position of the video is at (0,0) but when I shift the video to (200,0), the drawn output also shifted.

The code is pretty simple

// omitted for brevity

var videoContainer:Sprite = new Sprite();

// Video is added
videoContainer.addChild(video)

// position is offsetted
videoContainer.x = 200;
videoContainer.y = 200;
this.addChild(videoContainer)

// Generate Bitmap
var bd:BitmapData = new BitmapData(video.width, video.height, false, 0x0);
bd.x = video.x;
bd.y = video.y + video.height;
this.addChild(bd);

this.addEventListener(Event.ENTER_FRAME, onEnterFrame);

function onEnterFrame(e:Event):void {
    // start drawing
    bd.lock()
    bd.draw(video);
    bd.unlock();
}

enter image description here

I've tried to reposition it with a matrix, but apparently the entire original source had changed.

Is there a way to ensure that the output remains at (0,0) even if the videoContainer were offsetted from (0,0)?

1

1 Answers

1
votes

First of all, it looks like you have a type in your code:

var bd:Bitmap = new BitmapData(video.width, video.height, false, 0x0);

It should throw an error, because an instance of BitmapData couldn't be assigned to a variable with the type Bitmap. But, I guess, it's just a type.

Secondly, it looks like your bitmap is shifted, because you shift it =)

bd.x = video.x;

I would advice to remove/comment this code.

Upd:

Also, please, could you try the next method for getting a BitmapData instance of the DisplayObject with applied transform changes:

function getBitmapDataFromDisplayObject(
    displayObject:DisplayObject,
    transparent:Boolean = true,
    fillColor:uint = 0x00000000,
    smoothing:Boolean = true,
    customRect:Rectangle = null):BitmapData
{
    var bitmapData:BitmapData;
    try
    {
        var tempRect:Rectangle = displayObject.getRect(displayObject);
        if (customRect)
        {
            tempRect = customRect;
        }

        var matrix:Matrix = new Matrix();
        matrix.tx = -tempRect.x;
        matrix.ty = -tempRect.y;

        //bitmapData = new BitmapData(tempRect.right, tempRect.bottom, transparent, fillColor);
        bitmapData = new BitmapData(tempRect.width, tempRect.height, transparent, fillColor);
        bitmapData.draw(displayObject, matrix, null, null, null, smoothing);

    }catch (error:Error)
    {
        bitmapData = null;
    }

    return bitmapData
}