使displayObject的嵌套其他的DisplayObject内的真实视觉克隆,并克隆添加到舞台层在相同的位置,旋转,等等 [英] make visual clone of displayObject that's nested within other displayObjects, and add the clone to the stage layer in the same location, rotation, etc

查看:138
本文介绍了使displayObject的嵌套其他的DisplayObject内的真实视觉克隆,并克隆添加到舞台层在相同的位置,旋转,等等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够抓住嵌套的是一个DisplayObject的副本等的转换的的DisplayObject(旋转,缩放,伸展对象),并能够回来戳成相同的视觉位置的,但在舞台层上。本质上,能够使一个嵌套的DisplayObject的克隆,但能够克隆添加到舞台层,但有它完美地对齐(目视)与原来的(相同的位置,缩放,旋转)

I want to be able to grab a copy of a DisplayObject that is nested within other transformed DisplayObjects (rotated, scaled, stretched objects), and be able to stamp it back into the same visual location, but on the stage layer. Essentially, being able to make a clone of a nested DisplayObject, but be able to add the clone to the stage layer, yet have it perfectly align (visually) with the original (same position, scale, rotation)

我一直在与线沿线的东西:

I have been working with something along the lines of:

// draw the pixels of a displayobject into a new bitmap object
var bitmapData:BitmapData = new BitmapData(nestedSprite.width, nestedSprite.height, true, 0xFFFFFF);
var bitmap:Bitmap = new Bitmap(bitmapData);
bitmapData.draw(nestedSprite);

// put the copy on the top most layer
stage.addChild(bitmap);

// position the copy to perfectly overlay the original, but on the top stage layer
var point:Point = nestedSprite.localToGlobal(new Point(0, 0));
bitmap.x = point.x;
bitmap.y = point.y;

但这只很适合的DisplayObject的父母都没有改变;和displayObjetcs是perectly在(0,0)的起源。它分崩离析居中对齐对象或缩放的父母等。

But this only works well for displayObjects whose parents are not transformed; and for displayObjetcs that are perectly at the (0,0) origin. It falls apart for centered aligned objects or scaled parents, etc.

我知道,我可以添加一个矩阵参数的 .draw()的方法,以及剪辑rectngle,后来扩展我的位图,或设置一个对象转换到另一个,或使用 .transform.concatenatedMatrix ,或使用 nestedObject.getBounds(空) nestedSprite.getBounds(nestedSprite)等,但我不幸地陷入这样的的试验和错误的编程的就这一个,并与一些许多变数,这是永远不会解决编程问题的好办法。

I am aware that I can add a matrix param to the .draw() method, as well as a clipping rectngle, and scale my bitmap afterwards, or setting the transform of one object to another, or use .transform.concatenatedMatrix, or use nestedObject.getBounds(null), or nestedSprite.getBounds(nestedSprite), etc. But I have unfortunately fallen into doing trial and error programming on this one, and with some many variables, this is never a good way to solve a programming problem.

推荐答案

我认为这个功能应该工作,唯一的额外步骤被抵消了连接矩阵,使目标与它的顶部留在画(0,0)上位图,即使它的起源是在别的地方。希望剩下的就是自我解释,但我可以添加更多的意见,如果任何事情没有意义。

I believe this function should work, the only extra step was offsetting the concatenated matrix so that the target would draw with its top left at (0, 0) on the Bitmap even if its origin was somewhere else. Hopefully the rest is self explanatory, but I can add more comments if anything doesn't make sense.

function createBitmapClone(target:DisplayObject):Bitmap {
    var targetTransform:Matrix = target.transform.concatenatedMatrix;
    var targetGlobalBounds:Rectangle = target.getBounds(target.stage);
    var targetGlobalPos:Point = target.localToGlobal(new Point());

    // Calculate difference between target origin and top left.
    var targetOriginOffset:Point = new Point(targetGlobalPos.x - targetGlobalBounds.left, targetGlobalPos.y - targetGlobalBounds.top);

    // Move transform matrix so that top left of target will be at (0, 0).
    targetTransform.tx = targetOriginOffset.x;
    targetTransform.ty = targetOriginOffset.y;

    var cloneData:BitmapData = new BitmapData(targetGlobalBounds.width, targetGlobalBounds.height, true, 0x00000000);
    cloneData.draw(target, targetTransform);
    var clone:Bitmap = new Bitmap(cloneData);

    // Move clone to target's global position, minus the origin offset.
    clone.x = targetGlobalPos.x - targetOriginOffset.x;
    clone.y = targetGlobalPos.y - targetOriginOffset.y;

    return clone;
}

<打击>不幸的是,pixelBounds似乎回到了原点(0,0),如果有对的DisplayObject,这显然打破了事情的任何过滤器。

编辑:的替换 target.transform.pixelBounds target.getBounds(target.stage)作为一个略有改善。这样可以使位置正确,如果有过滤器,但仍然不会被包括在母公司的DisplayObject过滤器目标过滤器可以重叠位图的边缘。我不知道,如果有一个可以解决的简单方法。

Replaced target.transform.pixelBounds with target.getBounds(target.stage) as a slight improvement. This keeps the position correct if there are filters, but filters on parent DisplayObjects still won't be included, and filters on the target can overlap the edges of the Bitmap. I'm not sure if there's a simple way to work around that.

更新:的Jimmi Heiserman发现,如果SWF进行缩放此功能被打破。如果没有在Stage.scaleMode = StageScaleMode.NO_SCALE时; 虽然, stageWidth stageHeight 参数似乎保持不变,所以唯一的(而哈克)的解决方法,我发现是添加一个未缩放的测试雪碧和使用的及其 concatenatedMatrix 调整克隆的位置和规模:

Update: Jimmi Heiserman spotted that this function is broken if the swf is scaled. Without stage.scaleMode = StageScaleMode.NO_SCALE; though, the stageWidth and stageHeight parameters seem to stay unchanged, so the only (rather hacky) workaround I've found is to add an "unscaled" test Sprite and use its concatenatedMatrix to adjust the clone's position and scale:

function createScaledBitmapClone(target:DisplayObject):Bitmap {
    var targetTransform:Matrix = target.transform.concatenatedMatrix;
    var targetGlobalBounds:Rectangle = target.getBounds(target.stage);
    var targetGlobalPos:Point = target.localToGlobal(new Point());

    // Calculate difference between target origin and top left.
    var targetOriginOffset:Point = new Point(targetGlobalPos.x - targetGlobalBounds.left, targetGlobalPos.y - targetGlobalBounds.top);

    // Create a test Sprite to check if the stage is scaled.
    var testSprite:Sprite = new Sprite();
    target.stage.addChild(testSprite);
    var testMatrix:Matrix = testSprite.transform.concatenatedMatrix;
    target.stage.removeChild(testSprite);

    // Move transform matrix so that top left of target will be at (0, 0).
    targetTransform.tx = targetOriginOffset.x * testMatrix.a;
    targetTransform.ty = targetOriginOffset.y * testMatrix.d;

    var cloneData:BitmapData = new BitmapData(targetGlobalBounds.width * testMatrix.a, targetGlobalBounds.height * testMatrix.d, true, 0x00000000);
    cloneData.draw(target, targetTransform);
    var clone:Bitmap = new Bitmap(cloneData);

    // Move clone to target's global position, minus the origin offset, and cancel out stage scaling.
    clone.x = targetGlobalPos.x - targetOriginOffset.x;
    clone.y = targetGlobalPos.y - targetOriginOffset.y;
    clone.scaleX = 1 / testMatrix.a;
    clone.scaleY = 1 / testMatrix.d;

    return clone;
}

这篇关于使displayObject的嵌套其他的DisplayObject内的真实视觉克隆,并克隆添加到舞台层在相同的位置,旋转,等等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆