`
cuixuxucui
  • 浏览: 350815 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

An Introduction to Degrafa

阅读更多

原文链接:http://www.insideria.com/2008/05/an-introduction-to-degrafa-1.html

 

One of the most important aspects of delivering a rich user experience is what a user sees and interacts with on the screen. Graphics, whether static or dynamic, comprise the face of an application. Static graphics can definitely suffice for creating the necessary visual parts of an application, but to change them you must replace them with other static graphics. Dynamic graphics, created with programmatic drawing are very powerful because it can be manipulated at run-time, but it can involve more advanced knowledge of the Drawing API. Degrafa is a declarative graphics framework that aims to offer the best of both worlds.

In this article you will be introduced to some of the theory behind Degrafa, the basics of composition, advanced implementations, and how the framework may fit into your Flex or AIR projects. There are many uses for Degrafa, all of which can’t be covered in this article, but the hope is that this information may reveal something useful for you.

Degrafa is an open-source project created by developers in the RIA community. One of the primary goals is to allow designers and developers to forego the complexities of ActionScript to create dynamic graphics in MXML markup. With Degrafa you can define fills, strokes, shapes, complex paths, and more in the same way you would define a Flex component in MXML. Degrafa can be used to create skins, data visualizations, maps, or meet any other graphics need.


Some of the features of Degrafa include:


• A library of pre-composed shapes for you to use as well as the ability to create your own.
• Use SVG path data to create complete reusable shapes.
• Flexible options for fills and strokes.
• Binding of properties for Fills, Strokes and Geometry.
• Advanced CSS support for layered backgrounds, complex border control and background image positioning based on CSS 3.
• Productive features like derivatives, composition reuse, shape libraries, repeaters, and other utilities that allow you to do more with less code.
• Light weight model-based architecture.
• Compatibility with Flex 2 and Flex 3.
• A "pay-as-you-go" implementation.
• Much, much more...


Terminology
In the following examples there will be terminology used that you may or may not be familiar with. Some terms revolve around graphics, Flex, or the tags used in Degrafa. Before you get started, here's some definitions of some of those terms you'll see used in the remainder of this article:

geometry - A general term referring to any shape, path or graphic.

graphics context - A drawing destination where vector drawing commands can occur and stores information on how commands get rendered like line styles or fill styles. Any objects derived from Shape, Sprite or Movie Clip have a graphics property. Anything visual is drawn to or stored within a graphics context.

graphicsTarget - A property available to Degrafa geometry, like Circle, RegularRectangle, Polygon, etc. It allows you to specify the target(s) as a destination for Degrafa to render to. Acceptable targets must have a graphics context.

Surface - A tag in Degrafa provided as a base UIComponent for composing Degrafa elements.

GeometryGroup - A tag is used within the Surface tag and allows you to group Degrafa objects together. GeometryGroup is based on Sprite, so you can take advantage of the properties and events supported by Sprite.

GeometryComposition - A Degrafa tag that works similarly to GeometryGroup, but it allows you to compose or group elements at any level within your MXML file, including the root. You can optionally render those graphics to any object containing a graphics property using the graphicsTarget property.

Fill - A fill is a graphics term for the treatment of the inner area of an object. Fills are applied to the shapes you create. Degrafa has a variety of fills you can take advantage of like Solid, Linear Gradient, Radial Gradient, Bitmap, Blend and Complex fills.

Stroke - A stroke is a graphics term for the treatment of the edge of an object, usually defined with a thickness. Degrafa provides Solid, Linear Gradient and Radial Gradients strokes.


Getting Started with Degrafa
To use Degrafa in your projects you have two options; you can either download the latest version of Degrafa as a compiled SWC, or tap in directly to the Degrafa SVN. Both of these options are hosted openly on

Google Code. For the purposes of this article you can use the compiled Degrafa SWC by following these steps:


1. Go to Google Code where the Degrafa code is hosted and download the latest version of the Degrafa SWC for the version of Flex you’re running. There is a version for Flex 2 and Flex 3. After you’ve downloaded the Degrafa SWC, open up Flex Builder and create a new project called DegrafaExample.

2. Once you’ve created your new Flex project, you need to drop the Degrafa SWC you just downloaded into the libs folder.

3. For the sake of keeping things clean in your MXML you can add a Degrafa XML namespace in the root MXML tag where you’ll be using Degrafa. For example, adding the Degrafa namespace in the Application tag of DegrafaExample.mxml could look like this:

Google Code. For the purposes of this article you can use the compiled Degrafa SWC by following these steps:


1. Go to Google Code where the Degrafa code is hosted and download the latest version of the Degrafa SWC for the version of Flex you’re running. There is a version for Flex 2 and Flex 3. After you’ve downloaded the Degrafa SWC, open up Flex Builder and create a new project called DegrafaExample.

2. Once you’ve created your new Flex project, you need to drop the Degrafa SWC you just downloaded into the libs folder.

3. For the sake of keeping things clean in your MXML you can add a Degrafa XML namespace in the root MXML tag where you’ll be using Degrafa. For example, adding the Degrafa namespace in the Application tag of DegrafaExample.mxml could look like this:

 

<?xml version="1.0" encoding="utf-8"?>
<mx:Application    
      xmlns:mx="http://www.adobe.com/2006/mxml"
      xmlns:Degrafa="http://www.degrafa.com/2007"    
      layout="absolute">     
</mx:Application>

The Basics of Composition By following those steps you can now access any of the Degrafa MXML tags for creating your graphics. Before you do though, there are a few fundamentals you’ll want to keep in mind when composing graphics. The following steps expose some of these fundamentals and walks through the basics of creating graphics with Degrafa resulting in a final composition of a single white Circle shown in Figure 1 and viewable here.

Figure 1 - Basic Circle

 

 

Figure 1 - Basic Circle
1. Graphic objects defined with Degrafa are drawn by a series of commands that require an object to be rendered upon. To define this you can either use a Degrafa GeometryGroup tag within a Surface tag, like this:

<Degrafa:Surface>    

    <Degrafa:GeometryGroup>        

    <!-- Add Degrafa Shapes Here -->    

    </Degrafa:GeometryGroup>

</Degrafa:Surface>

The Degrafa Surface tag is a UIComponent, which means you can set properties like verticalCenter, horizontalCenter, etc. and the GeometryGroup tag is a Sprite, which has fewer properties than UIComponent.

2. The other option you have for specifying an area to draw Degrafa graphics to is to use the graphicsTarget property with a GeometryComposition or other geometry tag like Circle, RegularRectangle, etc. This will allow you to define graphic objects outside of the component and target any graphics context, i.e., any component that accepts graphics, like a Canvas, Button, Image, etc. This will be the method used in this basic example.

3. Add a Canvas in the application MXML and give it an id of myCanvas. Also, add a GeometryComposition and specify myCanvas in the graphicsTarget property like this:

<Degrafa:GeometryComposition    

    graphicsTarget="{[ myCanvas ]}">    

    <!-- Add Degrafa Shapes Here -->

</Degrafa:GeometryComposition>    

<mx:Canvas   

    id="myCanvas"/>

4. Once you’ve specified the graphics context you want to draw to you can start drawing Degrafa geometry. There are a number of pre-created Degrafa geometry objects that you can use to create your compositions, like Circle, RegularRectangle, Path, LineRepeater, and more. These objects can be drawn within a GeometryGroup, GeometryComposition, or by themselves using the graphicsTarget property. For this example, add a Circle within the GeometryComposition:

<Degrafa:GeometryComposition    

    graphicsTarget="{[ myCanvas ]}">    

    <Degrafa:Circle         

        radius="100"        

        centerX="120"        

        centerY="120"/>

</Degrafa:GeometryComposition>     

<mx:Canvas    

    id="myCanvas"/>

5. That last bit of MXML draws a Circle, but you can’t see it because it needs to have a fill or stroke applied to it. Degrafa comes with a number of fill and stroke capabilities that are sure to meet the majority of your needs. There are Solid, Linear Gradient, Radial Gradient, Blend, and Complex Fills. Stroke types include Solid, Linear Gradient, and Radial Gradient options. To get the Circle in this example to show up you can apply a SolidFill within the Circle like this:

<Degrafa:GeometryComposition    

    graphicsTarget="{[ myCanvas ]}">    

    <Degrafa:Circle        

        radius="100"        

        centerX="120"        

        centerY="120" >        

        <Degrafa:fill>            

            <Degrafa:SolidFill                

                color="#FFF"/>        

        </Degrafa:fill>    

    </Degrafa:Circle>

</Degrafa:GeometryComposition>     

<mx:Canvas    

    id="myCanvas"/>

Note: With the binding features of Degrafa you can also specify Fills and Strokes outside of geometry and assign the fill or stroke by referencing its id. This is a great way to repurpose fills and strokes across geometry objects. The following MXML snippet shows the same fill used above, but pulled outside of the Circle tag and assigned using the fill’s id of myFill:

<Degrafa:fills>    

    <Degrafa:SolidFill        

        id="myFill"        

        color="#FFF"/>

</Degrafa:fill>

 

<Degrafa:Circle    

    radius="100"    

    centerX="120"    

    centerY="120"    

    fill="{myFill}"/>

View the final product or view the source.

Thoughts on Binding
One of the features I use the most in Degrafa is binding. Binding means creating a relationship between two properties such that when one updates the other is dynamically updated. This can be a very powerful feature when you think about binding properties like width, height, radius, fill, stroke, etc. between geometry objects to create some very dynamic relationships for a variety of purposes.

A simple example would be binding the angle of a LinearGradientFill to a HorizontalSlider so that the angle of the gradient would change dynamically based on the updated value of the slider (don’t forget to set live dragging on the slider). A more complex example would be a list of items whose backgrounds change based on dynamic data.

When you start to think of all the data you could use to influence graphics drawn with Degrafa it seems the possibilities are endless; interfaces that change based on cursor coordinates, buttons that change at different stages in a process, icons that change based on time of day, and charts that change shape based on new data. All it takes is a little experimentation with your own data sets, various public APIs, and user input. With binding in Degrafa you’ll start to see opportunities to create dynamic graphics in Flex.

A More Advanced Implementation

Now that we’ve covered some of the capabilities of Degrafa and the basics of composing graphics, let’s take a look at more involved example. The following steps will walk through creating a Flex Clock (Figure 2) to illustrate some of the features of Degrafa mentioned throughout this article and create a more advanced graphics composition. You can see the final product
here.

 

Figure 1 - Flex Clock

Figure 1 - Flex Clock


1. To get started, create a new Flex application within your DegrafaExample folder and name it FlexClock.mxml. For convenience, add the same XML namespace mentioned in the previous example to your Application tag:

xmlns:Degrafa="http://www.degrafa.com/2007"

2. For this example, you’ll be composing the different parts of the Flex Clock as GeometryGroups to take advantage of some of the properties of Sprite, like rotation for the clock hands.

The first thing you need to do is set up the Surface tag so you can start adding the parts of the clock as GeometryGroups. Set the x and y of the Surface to 100 to position the clock within your application.

3. Add a GeometryGroup tag within the Surface tag and give it an id of clockFace. Within this GeometryGroup you’ll create the face of the clock, which is made up of the dark gradient background and letters “Fx”.

To create the dark grey face of the clock you can use a RegularRectangle object inside the clockFace GeometryGroup. Set the height and width of the RegularRectangle to 360. You also need to create that dark grey radial gradient fill for the RegularRectangle, but at this point the clockFace MXML should look like this:

<Degrafa:GeometryGroup    

    id="clockFace">    

    <Degrafa:RegularRectangle        

        width="300"        

        height="300"/>

</Degrafa:GeometryGroup>


4. Outside the clockFace GeometryGroup, create a new fills tag. Within that fills tag, add a RadialGradientFill tag and set the cx property to 40, cy to 20, radius to 440 and give it an id of greyRadFill. The last thing you need to do to finish this fill is assign gradient stops within the fill.

Gradient stops are the attributes that make up a gradient and have their own set of properties, including color, alpha, and ratio. In Degrafa, you use a GradientStop tag to create a gradient stop within a radial or linear gradient fill. You can set as many gradient stops as you need to get the desired effect you’re looking for. For the purposes of this example, just create two gradient stops. Give the first gradient stop a color of #444 and the second a color of #000.

Note: You may have noticed that the colors for the gradient stops were defined using three digits rather than the six that Flex normally requires. This is similar to what is doable in HTML and another feature of Degrafa. In fact, you have several options for specifying colors within the color property. You can specify a color with a HEX value (#666), RGB value (60,60,60), color key (grey), or CMYK (0,0,0,60).

5. Now that you’ve created the dark grey fill, it must be applied to the RegularRectangle you created earlier. As mentioned previously in this article, you can apply a fill to an object using the fill property. The MXML for the RegularRectangle and RadialGradientFill should look like this:

<Degrafa:fills>    

    <Degrafa:RadialGradientFill        

        id="greyRadFill"        

        cx="40"        

        cy="20"        

        radius="340">        

        <Degrafa:GradientStop           

            color="#444"/>        

        <Degrafa:GradientStop            

            color="#000"/>    

    </Degrafa:RadialGradientFill>

</Degrafa:fills>     

<Degrafa:GeometryGroup    

    id="clockFace">   

    <Degrafa:RegularRectangle        

        width="300"        

        height="300"        

        fill="{ greyRadFill }"/>

</Degrafa:GeometryGroup>

6. If you run your application you should see the RegularRectangle you created filled with a dark grey radial gradient. There’s the base for the clock face.

Now you can create the remainder of the fills and strokes up front for the other elements of the clock and apply them as you go. You need to create a solid white fill for the “Fx” letters, a 10 pixel solid red stroke for the hour and minute hands, and a 2 pixel light grey stroke for the second hand. The fills tag has already been specified, but you need to add a strokes tag as well. Adding these new fills and strokes to the greyGradFill you already created should look like this in your MXML:

<Degrafa:fills>    

    <Degrafa:RadialGradientFill        

        id="greyRadFill"        

        cx="40"        

        cy="20"        

        radius="340">        

    <Degrafa:GradientStop             

        color="#444"/>        

    <Degrafa:GradientStop            

        color="#000"/>    

    </Degrafa:RadialGradientFill>    

    <Degrafa:SolidFill       

         id="whiteFill"        

        color="#FFF"/>

</Degrafa:fills>          

<Degrafa:strokes>    

    <Degrafa:SolidStroke        

        id="redStroke"        

        color="#C50"        

        weight="10"        

        caps="none"/>    

    <Degrafa:SolidStroke        

        id="greyStroke"        

        color="#666"        

        weight="2"        

        caps="none"/>

</Degrafa:strokes>


Note: You could expose Fill and Stroke properties to a designer to allow for customization without having them get involved with code.

7. With all the fills and strokes you’ll need for this example created, let’s add the remaining parts of the clock. You already have the dark grey background created, so now you need to finish the clock face and add the “Fx” letters. For these letters you will use a Polygon tag within the clockFace GeometryGroup.

For the “F” create a Polygon tag within the clockFace and assign whiteFill to the fill property. A Polygon takes shape by specifying a series of points, so rather than completely ending the Polygon tag, close it and set a points tag within. To specify the individual points, use a GraphicPoint tag for each point and specify the x and y values for each point. Instead of making you figure out the points to define the “F” I’ve defined them in the MXML block below along with the MXML for the “x”:

 

<!-- The White "F" Polygon -->

<Degrafa:Polygon    

    x="74"    

    y="90"    

    fill="{ whiteFill }">    

<Degrafa:points>       

    <Degrafa:GraphicPoint            

        x="1.716"           

        y=".868"/>         

    <Degrafa:GraphicPoint            

        x="91.638"            

        y=".868"/>        

    <Degrafa:GraphicPoint            

        x="92.329"            

        y="21.965"/>        

    <Degrafa:GraphicPoint            

        x="30.768"            

        y="21.965"/>        

    <Degrafa:GraphicPoint            

        x="30.768"            

        y="71.768"/>        

    <Degrafa:GraphicPoint            

        x="85.758"            

        y="71.768"/>        

    <Degrafa:GraphicPoint            

        x="85.758"            

        y="95.632"/>        

    <Degrafa:GraphicPoint            

        x="30.768"            

        y="95.632"/>        

    <Degrafa:GraphicPoint            

        x="30.768"            

        y="164.11"/>        

    <Degrafa:GraphicPoint            

        x="1.37"            

        y="164.11"/>    

    </Degrafa:points>

</Degrafa:Polygon>

<!-- The White "x" Polygon -->

<Degrafa:Polygon    

    x="178"    

    y="140"    

    fill="{ whiteFill }">    

    <Degrafa:points>        

    <Degrafa:GraphicPoint            

        x="1.107"            

        y=".147"/>        

    <Degrafa:GraphicPoint            

        x="32.926"            

        y=".147"/>        

    <Degrafa:GraphicPoint            

        x="54.022"            

        y="39.574"/>        

    <Degrafa:GraphicPoint            

        x="76.503"           

        y=".147"/>        

    <Degrafa:GraphicPoint            

        x="105.209"            

        y=".147"/>        

    <Degrafa:GraphicPoint           

        x="72.354"            

        y="54.446"/>        

    <Degrafa:GraphicPoint            

        x="108.322"            

        y="113.932"/>         

    <Degrafa:GraphicPoint            

        x="73.736"            

        y="113.932"/>        

    <Degrafa:GraphicPoint            

        x="52.256"            

        y="73.43"/>        

    <Degrafa:GraphicPoint            

        x="32.505"            

        y="113.241"/>        

    <Degrafa:GraphicPoint           

        x=".07"            

        y="113.241"/>        

    <Degrafa:GraphicPoint            

       

分享到:
评论

相关推荐

    关于DEGRAFA的简介

    DEGRAFA,全称为“基于SVG的矢量图形绘制框架”,是一个开源的、基于SVG(Scalable Vector Graphics)标准的图形绘制工具包,主要应用于Java平台。它为开发者提供了丰富的API,使得在Java应用程序中创建、编辑和展示...

    Degrafa画矢量曲线

    在 Degrafa 中,可以使用 MoveTo、LineTo、QuadTo、CubicTo 等方法来构建贝塞尔曲线。贝塞尔曲线是一种通过控制点来定义的曲线,分为线性贝塞尔(直线)、二次贝塞尔(两控制点)和三次贝塞尔(三个控制点)。通过...

    Degrafa帮助文档

    Degrafa是一款强大的图形绘制工具,专为Adobe Flex开发者设计,用于在Flex应用程序中创建复杂的矢量图形。这款工具的核心是其灵活的图形系统,它允许开发者通过代码控制图形的每一个细节,从而实现动态和交互式的...

    Degrafa3.1源码

    Degrafa 3.1 源码是一个用于FLEX应用程序开发的图形库,它提供了丰富的SVG(可缩放矢量图形)支持,使开发者能够创建复杂、动态且交互式的图形用户界面。在这个源码包中,我们主要关注的是如何使用 Degrafa 与 FLEX ...

    degrafa 的帮助文档

    flex开发的画图框架Degrafa 近来需在flex画些简单的图形,根据朋友推荐使用了Degrafa,但是官方没有提供离线的文档下载,于是自己制作了一下。 文档的所有版权均属http://www.degrafa.com 详细情况请查看degrafa的...

    关于Degrafa's Fills简介

    Degrafa's Fills是一个在Flex开发中用于图形渲染的开源库,它为开发者提供了丰富的图形填充效果。这篇博文的作者分享了如何将 Degrafa 的填充功能集成到其他Flex项目中的方法,通过一个名为“sign05.mxml”的示例...

    Degrafa.CHM

    flex开发的画图框架Degrafa 近来需在flex画些简单的图形,根据朋友推荐使用了Degrafa,但是官方没有提供离线的文档下载,于是自己制作了一下。 文档的所有版权均属http://www.degrafa.com 详细情况请查看degrafa的...

    degrafa绘图工具 svg操作

    DeGrafa是一款强大的开源绘图工具,专为创建和编辑SVG(Scalable Vector Graphics)图形而设计。SVG是一种基于XML的矢量图像格式,它允许开发者创建清晰、高质量的图形,无论放大多少倍都不会失真,广泛应用于网页...

    Degrafa学习一,(含SDK4.0兼容版本及源码,略微有所修改)

    Degrafa是一个图形化开发框架,它主要用于创建2D图形应用程序,特别是在Swing或JavaFX平台上。这个资源包包括了SDK的4.0兼容版本,这意味着你可以利用这个框架来开发与Java SDK 4.0相适应的应用。"含SDK4.0兼容版本...

    Degrafa(Beta3)

    Degrafa是一款强大的图形化开发框架,主要用于创建2D矢量图形和交互式应用程序。它在Beta3版本中提供了一些先进的特性和改进,为开发者带来了更丰富的功能和更好的用户体验。这款开源工具允许开发者通过编程方式设计...

    SvgToDegrafa.rar_SvgToDegrafa_degrafa_flex

    2. ** Degrafa图形API**: Degrafa 提供了一系列的图形类,如PathElement、Group、Shape等,可以创建和操作矢量图形。你需要把这些解析出来的SVG数据映射到 Degrafa 对象上,比如将SVG路径数据转化为PathElement对象...

    degrafa源代码

    Degrafa扩展就是在FLEX中增加了对矢量数据的动态处理,包括动态添加,删除和修改,并支持SVG的路径格式数据, 因此只需要对现有的SVG数据进行一些必要的转换(以后会有工具支持,但现在只能手工完成),就可以将SVG...

    深入SVG路径~~~在DEGRAFA中使用PATH

    在DEGRAFA中,SVG路径的使用是一个重要的主题,DEGRAFA是一个ActionScript 3.0库,专门用于图形渲染和矢量图形操作。 SVG路径由一系列命令和参数构成,这些命令指示浏览器如何绘制路径。主要的命令包括M(移动到)...

    一个隐藏式的登录窗口

    使用了“shade”和“degrafa”,这两个是ActionScript和Flex开发中的图形库和框架。 “shade”是一个图形效果库,它提供了丰富的视觉特效,如阴影、渐变、模糊等,可以用来增强用户界面的立体感和动态效果,使登录...

    degrafaFlex4.SWC

    基于Actionscript3.0,强大的矢量图形绘制类库

    flex图形报表控件源代码

    这里是源代码,但需要借助另外的开源框架Degrafa。我用的是Degrafaflex4.swf。 这里我直接将degrafaflex4.swf和birdeye本身自己写的vis打成一个swf文件。即birdeyelib.swf。 做图形报表框架直接用birdeyelib.swf即可...

    flex3做的流程图

    在Flex3中, Degrafa是一个图形库,专为创建复杂的矢量图形和图表而设计,尤其适用于制作流程图。 流程图是表示工作流程或系统操作过程的图形表示法,它通过标准化的图形符号来展示步骤和决策点。在Flex3中,利用 ...

    flex svg代码生成图片

    使用 Degrafa 框架,可以在 Flex 应用程序中动态显示和操作 SVG 文档。通过解析 SVG 文件,然后通过 Flex 生成图片。动态加载 SVG 图片可以实现大量的 SVG 图形资源直接在 Flex 应用程序中使用。 三、解析 SVG 文件...

Global site tag (gtag.js) - Google Analytics