获取组件可见区域的全局坐标

发布于 2024-09-28 00:50:44 字数 193 浏览 1 评论 0原文

我试图确定当前在舞台上渲染的可见矩形的全局坐标。

具体来说,如果画布具有明确的高度和宽度,并且是具有滚动条的面板的子级,则滚动条可以“隐藏”画布的一部分。 ContentToGlobal(x,y) 提供内容当时的全局位置,但内容坐标可以滚动到父面板的边界之外,并继续给出不可见的 x,y 坐标。

有没有办法确定没有被任何东西隐藏的可见矩形?

I am trying to determine the global coordinates of the visible rectangle that is currently rendered on the stage.

Specifically, if a canvas has an explicit height and width and is a child of a panel that has a scrollbar, the scrollbar can "hide" a part of the canvas. ContentToGlobal(x,y) provides the global position of the content at the time, but content coordinates can scroll off the boundaries of the parent panel and continue to give x,y coordinates that are not visible.

Is there a way to determine the viewable rectangle that is not hidden by anything?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花心好男孩 2024-10-05 00:50:44

事实证明, UIComponent 类有一个未记录的公共函数,它正是我所寻找的。

/**
 *  @private
 * 
 *  Get the bounds of this object that are visible to the user
 *  on the screen.
 * 
 *  @param targetParent The parent to stop at when calculating the visible
 *  bounds. If null, this object's system manager will be used as
 *  the parent.
 * 
 *  @return a <code>Rectangle</code> including the visible portion of the this 
 *  object. The rectangle is in global coordinates.
 */  
public function getVisibleRect(targetParent:DisplayObject = null):Rectangle

这是从类中提取出来的,并被记录为私有,但实际上是一个可公开访问的函数,可以在任何 UI 对象上使用。

由于这不在任何地方的文档中,我想它可能容易受到未来变化的影响。

It turns out that the UIComponent class has an undocumented public function that does exactly what I was looking for.

/**
 *  @private
 * 
 *  Get the bounds of this object that are visible to the user
 *  on the screen.
 * 
 *  @param targetParent The parent to stop at when calculating the visible
 *  bounds. If null, this object's system manager will be used as
 *  the parent.
 * 
 *  @return a <code>Rectangle</code> including the visible portion of the this 
 *  object. The rectangle is in global coordinates.
 */  
public function getVisibleRect(targetParent:DisplayObject = null):Rectangle

This is pulled from the class and is documented as private but is actually a publicly accessible function that can be used on any UI object.

Since this isn't in the documentation anywehere, I imagine it may be susceptible to future changes.

另类 2024-10-05 00:50:44

不,没有直接的解决方案。

您应该手动计算可见矩形:

    private function getVisibleRectangle(container:Container, child:UIComponent):Rectangle
    {
        var rect:Rectangle = child.getBounds(child.stage);
        var containerMetrics:EdgeMetrics = container.viewMetrics;
        var containerPoint:Point = container.localToGlobal(new Point(0, 0));
        var containerRect:Rectangle = new Rectangle(containerPoint.x + containerMetrics.left, 
            containerPoint.y + containerMetrics.top, 
            container.width - containerMetrics.left - containerMetrics.right, 
            container.height - containerMetrics.top - containerMetrics.bottom);

        if (rect.left >= containerRect.right ||
            rect.right <= containerRect.left ||
            rect.top >= containerRect.bottom ||
            rect.bottom <= containerRect.top)
            return null;

        rect.left = Math.max(rect.left, containerRect.left);
        rect.right = Math.min(rect.right, containerRect.right);
        rect.top = Math.max(rect.top, containerRect.top);
        rect.bottom = Math.min(rect.bottom, containerRect.bottom);
        return rect;
    }

使用示例:

<mx:Panel id="panel" width="200" height="200">
    <mx:Canvas backgroundColor="green" width="100" height="100"/>
    <mx:Canvas id="canvas" backgroundColor="red" width="500" height="400" 
        enterFrame="trace(getVisibleRectangle(panel, canvas));"/>
</mx:Panel>

No, there is no straightforward solution.

You should calculate the visible rectangle manually:

    private function getVisibleRectangle(container:Container, child:UIComponent):Rectangle
    {
        var rect:Rectangle = child.getBounds(child.stage);
        var containerMetrics:EdgeMetrics = container.viewMetrics;
        var containerPoint:Point = container.localToGlobal(new Point(0, 0));
        var containerRect:Rectangle = new Rectangle(containerPoint.x + containerMetrics.left, 
            containerPoint.y + containerMetrics.top, 
            container.width - containerMetrics.left - containerMetrics.right, 
            container.height - containerMetrics.top - containerMetrics.bottom);

        if (rect.left >= containerRect.right ||
            rect.right <= containerRect.left ||
            rect.top >= containerRect.bottom ||
            rect.bottom <= containerRect.top)
            return null;

        rect.left = Math.max(rect.left, containerRect.left);
        rect.right = Math.min(rect.right, containerRect.right);
        rect.top = Math.max(rect.top, containerRect.top);
        rect.bottom = Math.min(rect.bottom, containerRect.bottom);
        return rect;
    }

Example of usage:

<mx:Panel id="panel" width="200" height="200">
    <mx:Canvas backgroundColor="green" width="100" height="100"/>
    <mx:Canvas id="canvas" backgroundColor="red" width="500" height="400" 
        enterFrame="trace(getVisibleRectangle(panel, canvas));"/>
</mx:Panel>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文