关于flex中图形的有点奇怪的问题

发布于 2024-10-19 05:47:28 字数 286 浏览 1 评论 0原文

假设您有一个带有填充和描边的通用 Graphics 对象。是否有一种简单的方法可以对该对象进行切片(例如垂直切片)并获取代表其子集的 Graphics 对象?

换句话说,下面的函数切片会是什么样子?

var gOriginal:Graphics;
var gNew:Graphics;
gNew = slice(gOriginal, "vertical", "0.5);

那么如果 gOriginal 是一个椭圆我现在得到半个椭圆?

谢谢

Let's say you have a generic Graphics object with a fill and a a stroke. Is there an easy way to slice that object, for instance vertically, and get a Graphics object that represents a subset of it?

In other words, what would function slice below look like?

var gOriginal:Graphics;
var gNew:Graphics;
gNew = slice(gOriginal, "vertical", "0.5);

so that if gOriginal was an ellipse I now get half an ellipse?

thank you

f

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

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

发布评论

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

评论(3

尤怨 2024-10-26 05:47:28

我基本上是在标记丹尼尔的概念(除了我有一些根深蒂固的、无法解释的、对 degrafa 的蔑视),你应该能够做到这一点:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
               xmlns:s="library://ns.adobe.com/flex/spark"
               xmlns:mx="library://ns.adobe.com/flex/mx"
               minWidth="955"
               minHeight="600"
               creationComplete="application1_creationCompleteHandler(event)">
    <s:layout>
        <s:VerticalLayout/>
    </s:layout>
    <fx:Script>
        <![CDATA[
            import mx.core.UIComponent;
            import mx.events.FlexEvent;

            import spark.components.Group;

            public var someGraphics:UIComponent;
            [Bindable]
            public var bd:BitmapData;
            [Bindable]
            public var finalBD:BitmapData;
            override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
            {
                super.updateDisplayList(unscaledWidth,unscaledHeight);
                if(someGraphics)
                {
                    someGraphics.graphics.beginFill(0xFF0000);
                    someGraphics.graphics.drawEllipse(0,0,100,20);
                    someGraphics.graphics.endFill();

                    bd.draw(someGraphics);
                    finalBD.copyPixels(bd,new Rectangle(0,0,bd.width*.5,bd.height),new Point());
                }
            }

            protected function application1_creationCompleteHandler(event:FlexEvent):void
            {
                someGraphics = new UIComponent();
                bd = new BitmapData(100,100);
                finalBD = new BitmapData(100,100);
                invalidateDisplayList();
            }

        ]]>
    </fx:Script>
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <s:BitmapImage source="{bd}" width="100" height="100"/>
    <s:BitmapImage source="{finalBD}" width="100" height="100"/>
</s:Application>

这显然不是一个漂亮的解决方案,但只是将图形制作到一些具有图形属性,然后使用 BitmapData 绘制方法将像素放入位图数据中,然后将这些像素的一部分复制到另一个 BitmapData 中进行显示,这可能比 Jeremy 的方法占用更多内存,但占用的处理器更少。另外,我正在使用 UIComponent,绘制方法的要求是该类实现 IBitmapDrawable,因此无论最轻的实现是什么,都可能是您想要使用的,UIComponent 是一个猜测。

I'm tagging onto Daniel's concept basically (except I have some deeply rooted, unexplained, disdain for degrafa), you should be able to do this:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
               xmlns:s="library://ns.adobe.com/flex/spark"
               xmlns:mx="library://ns.adobe.com/flex/mx"
               minWidth="955"
               minHeight="600"
               creationComplete="application1_creationCompleteHandler(event)">
    <s:layout>
        <s:VerticalLayout/>
    </s:layout>
    <fx:Script>
        <![CDATA[
            import mx.core.UIComponent;
            import mx.events.FlexEvent;

            import spark.components.Group;

            public var someGraphics:UIComponent;
            [Bindable]
            public var bd:BitmapData;
            [Bindable]
            public var finalBD:BitmapData;
            override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
            {
                super.updateDisplayList(unscaledWidth,unscaledHeight);
                if(someGraphics)
                {
                    someGraphics.graphics.beginFill(0xFF0000);
                    someGraphics.graphics.drawEllipse(0,0,100,20);
                    someGraphics.graphics.endFill();

                    bd.draw(someGraphics);
                    finalBD.copyPixels(bd,new Rectangle(0,0,bd.width*.5,bd.height),new Point());
                }
            }

            protected function application1_creationCompleteHandler(event:FlexEvent):void
            {
                someGraphics = new UIComponent();
                bd = new BitmapData(100,100);
                finalBD = new BitmapData(100,100);
                invalidateDisplayList();
            }

        ]]>
    </fx:Script>
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <s:BitmapImage source="{bd}" width="100" height="100"/>
    <s:BitmapImage source="{finalBD}" width="100" height="100"/>
</s:Application>

This is obviously far from a pretty solution but just doing the graphics into some object that has a graphics property, then using the BitmapData draw method to get the pixels into a bitmap data then copying out a section of those pixels to another BitmapData for display, this is probably more memory but less processor intensive than Jeremy's. Also I'm using UIComponent the requirement for the draw method is that the class implements IBitmapDrawable so whatever is the lightest implementation of that is probably what you want to use, UIComponent is a guess.

故事灯 2024-10-26 05:47:28

您可以将其绘制()到位图对象中并选择要包含的矩形,这可以具有相同的效果,但没有简单的方法可以做到这一点。

您还可以查看 degrafa 但这可能会变得太复杂

you could draw() it into a bitmap object and select a rectangle to include which could have the same effect, but there is no simple way of doing that.

you can also look into degrafa but that could become too complicated

双马尾 2024-10-26 05:47:28

没有用于删除 Graphics 对象部分的内置方法。在“切片”方面,您很可能需要独特地对待每种类型的形状(矩形、椭圆形、多边形)。例如,要对椭圆进行切片,与从饼图中切出切片类似,您需要使用一点三角函数来绘制形状以创建楔形。

以下是绘制楔形的基本函数:

protected function drawWedge():void 
{
    var g:Graphics = shape.graphics;    
    g.clear();
    g.lineStyle( 1, 0xFFFFFF );
    g.beginFill( 0xCCCCCC );

    var radiusX:Number = 100;
    var radiusY:Number = 100;
    var angle:Number = 0;
    var xpos:Number = 0;
    var ypos:Number = 0;
    var segments:Number = 25;
    var degrees:Number = 45;
    var inc:Number = degrees / ( segments );

        for( var i:int = 0; i <= segments; i++ )
        {
            xpos =  Math.cos( Math.PI * angle / 180 ) * radiusX;
            ypos =   Math.sin( Math.PI * angle / 180 ) * radiusY;

            g.lineTo( xpos, ypos );

            angle += inc;
        }

        g.endFill();
}

在初始化时调整 angleXangleY 变量将开始在圆周上的不同点绘制楔形。如果将更改为 360,您将看到一个完整的圆,如果更改为 180,您将看到半圆。更改 radiusXradiusY 以创建椭圆形。此外,segments 控制绘图的分辨率。提高它,你会得到更平滑的曲线。

希望这有帮助。

There isn't a built in method for removing portions of a Graphics object. You will most likely need to treat each type of shape ( rect, ellipse, poly ) uniquely, in regard to "slicing" it. For instance to slice an elipse, similarly to taking a slice out of a pie chart, you would need to draw the shapes using a little trig to create a wedge.

Here is a basic function for drawing wedges:

protected function drawWedge():void 
{
    var g:Graphics = shape.graphics;    
    g.clear();
    g.lineStyle( 1, 0xFFFFFF );
    g.beginFill( 0xCCCCCC );

    var radiusX:Number = 100;
    var radiusY:Number = 100;
    var angle:Number = 0;
    var xpos:Number = 0;
    var ypos:Number = 0;
    var segments:Number = 25;
    var degrees:Number = 45;
    var inc:Number = degrees / ( segments );

        for( var i:int = 0; i <= segments; i++ )
        {
            xpos =  Math.cos( Math.PI * angle / 180 ) * radiusX;
            ypos =   Math.sin( Math.PI * angle / 180 ) * radiusY;

            g.lineTo( xpos, ypos );

            angle += inc;
        }

        g.endFill();
}

Adjusting the angleX and angleY vars when they are initialized will begin drawing the wedge at a different point along the circumference. If you change degrees to 360 you will see a complete circle, and at 180 you will get a half circle. Change radiusX and radiusY to create an oval. Also, segments controls the resolution of the drawing. Bump it up and you get a smoother curve.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文