index.mustache revision 8a9c8f46081091689310524284394f06d22fcbfe
315ce5be539bfe8bc7777ab0654499c49583cea2Timo Sirainen<div class="intro">
315ce5be539bfe8bc7777ab0654499c49583cea2Timo Sirainen <img src="{{componentAssets}}/img/ship.png" alt="Screencapture of ship drawn with Graphics" style="border: 1px solid #bfbfbf; float:right; height:150px; margin: 0 0 8px 8px; width:275px;">
315ce5be539bfe8bc7777ab0654499c49583cea2Timo Sirainen The Graphics module provides a JavaScript API for creating shapes in a variety of formats across all A-grade browsers. Based on device and browser capabilities, Graphics leverages SVG, HTML Canvas and VML to render its graphical elements.
315ce5be539bfe8bc7777ab0654499c49583cea2Timo Sirainen The Graphics module features a `Graphic` class that allows you to easily create and manage shapes. Currently, a `Graphic` instance can be used to create predifined shapes and free-form polygons with fill and stroke properties.
92c27c7db84150bff9e77271f19efc5aec4f214aTimo Sirainen{{>getting-started}}
315ce5be539bfe8bc7777ab0654499c49583cea2Timo Sirainen<h2 id="using">Using the Graphics module</h2>
315ce5be539bfe8bc7777ab0654499c49583cea2Timo Sirainen<p>The `Graphic` class acts a factory and container for shapes. You need at least one `Graphic` instance to create shapes for your application.</p>
315ce5be539bfe8bc7777ab0654499c49583cea2Timo Sirainen<h3 id="instantiating">Instantiating A Graphic instance</h3>
315ce5be539bfe8bc7777ab0654499c49583cea2Timo Sirainen<p>All you need to instantiate a Graphic instance is an HTML element in which to render. Alternatively, you can attach your instance to the body of your page.</p>
315ce5be539bfe8bc7777ab0654499c49583cea2Timo Sirainen#mygraphiccontainer {
92c27c7db84150bff9e77271f19efc5aec4f214aTimo Sirainen width: 600px;
315ce5be539bfe8bc7777ab0654499c49583cea2Timo Sirainen height: 400px;
315ce5be539bfe8bc7777ab0654499c49583cea2Timo Sirainen<div id="mygraphiccontainer"></div>
315ce5be539bfe8bc7777ab0654499c49583cea2Timo Sirainen<h4>JavaScript</h4>
92c27c7db84150bff9e77271f19efc5aec4f214aTimo Sirainen// Instantiate a graphic instance
458acd7b39c84bae0d18c36ff9ddff9a49b4ae4aTimo Sirainenvar mygraphic = new Y.Graphic({
458acd7b39c84bae0d18c36ff9ddff9a49b4ae4aTimo Sirainen render: "#mygraphiccontainer"
92c27c7db84150bff9e77271f19efc5aec4f214aTimo Sirainen <p>By default, `Graphic` will size to its parent container. The API also provides the option of explicitly setting its `width` and `height` attributes. Additionally, the Graphic class provides an `autoSize` attribute. When set to true, the Graphic instance will expand to fit its contents.
315ce5be539bfe8bc7777ab0654499c49583cea2Timo Sirainen<h3>Creating shapes</h3>
315ce5be539bfe8bc7777ab0654499c49583cea2Timo Sirainen<p>Shapes are created using the `addShape` method. The `addShape` method takes a config parameter that defines the shape and its properties. When creating a shape, the shape is determined by the `type`
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainenattribute. The `Graphics` module includes four pre-defined shapes. They can be created by passing a `String` reference.</p>
315ce5be539bfe8bc7777ab0654499c49583cea2Timo Sirainen <th>shape</th>
315ce5be539bfe8bc7777ab0654499c49583cea2Timo Sirainen <td>circle</td>
315ce5be539bfe8bc7777ab0654499c49583cea2Timo Sirainen <td>ellipse</td>
315ce5be539bfe8bc7777ab0654499c49583cea2Timo Sirainen <td>rect</td>
315ce5be539bfe8bc7777ab0654499c49583cea2Timo Sirainen <td>path</td>
d176f84ce5ca2073f4dfbafb457b9c74f6bf0d76Timo Sirainen<p>Alternatively, you can create your own custom class and pass it directly through the `type` attribute.</p>
578ef2538ccf42e2a48234c24a8b709397101d88Timo Sirainen<p>The below code would create a 300x200 rectangle with a blue fill and a red border.</p>
578ef2538ccf42e2a48234c24a8b709397101d88Timo Sirainenvar mygraphic = new Y.Graphic({render:"#mygraphiccontainer"}),
4d4d6d4745682790c20d759ba93dbea46b812c5dTimo Sirainen type: "rect",
315ce5be539bfe8bc7777ab0654499c49583cea2Timo Sirainen color: "#0000ff"
315ce5be539bfe8bc7777ab0654499c49583cea2Timo Sirainen color: "#ff0000"
var mygraphic = new Y.Graphic({render:"#mygraphiccontainer"}),
myrect = mygraphic.addShape({
type: Y.MyCustomShape,
<p>The `Graphics` module uses different technologies based on browser capabilities. The `Graphics` module normalizes these different technologies with a consistent API. Ideally, you should not
have to interact directly with the underlying technologies or their corresponding HTML elements. Both the `Graphic` and `Shape` classes provide APIs for sizing, positioning and customization.</p>
<td>Unique identifier for the `Graphic` instance. If not explicity set, one will be generated.</td></tr>
<td>Object containing size and coordinate data for the content of a Graphic in relation to the coordinate space of the `Graphic` instance. The following values are included: `top`, `right`, `bottom`, `left`, `width` and `height`.</td>
<td>Determines how the size of instance is calculated. If true, the width and height are determined by the size of the contents. If false, the width and height values are either explicitly set or determined by the size of the parent node's dimensions. The default value is false.</td>
<td>The `contentBounds` will resize to greater values but not to smaller values. (for performance) When resizing the `contentBounds` down is desirable, set the resizeDown value to true. The default value is false.</td>
<td>Indicates whether or not the instance will automatically redraw after a change is made to a shape. When performing multiple operations, such adding many shapes, `autoDraw` can be set to false. Calling `_redraw` will force a redraw when `autoDraw` is `false`.</td>
<td>Returns an array containing the current position of the graphic instance in page coordinates.</td>
<p>Each shape shares a common set of attributes. Attributes shared across all shapes are listed below:</p>
<td>Unique identifier for the `Shape` instance. If not explicity set, one will be generated.</td></tr>
<td>A string containing, in order, transform operations applied to the shape instance. The `transform` string can contain the following values:
Applying transforms through the transform attribute will reset the transform matrix and apply a new transform. The shape class also contains corresponding methods for each transform
that will apply the transform to the current matrix. The below code illustrates how you might use the `transform` attribute to instantiate a recangle with a rotation of 45 degrees.
var myRect = new Y.Rect({
myRect.set("transform", "translate(40, 50) rotate(45)");
<td>Gets the current position of the shape in page coordinates. Returns an array, `[x, y,]`, with the coordinates.</td>
<td>Sets the current position of the shape in page coordinates. Accepts an array, `[x, y]`, with the coordinates.</td>
<p>Unlike the other included shapes, the `Path` class is not pre-defined. Setting the size, fill and/or stroke of a pre-defined shape will render the shape. This is not true with the `Path`. To render
a `Path` instance, its drawing methods need to be leveraged. These drawing methods can also be leveraged when creating custom shapes. Available drawing methods include:
<dt>quadraticCurveTo</dt><dd>Draws a quadratic curve based on a start point, end point and two control points.</dd>
<dt>end</dt><dd>Ends a drawing operation. The path or custom shape will draw after end is called.</dd>
<p>All `Shape` instances contain `stroke` and `fill` attributes. They are used to define the colors for a `Shape`.</p>
<dt>opacity</dt><dd>Number between 0 and 1 that indicates the opacity of the stroke. The default value is 1.</dd>
<dt>dashstyle</dt>Indicates whether to draw a dashed stroke. When set to "none", a solid stroke is drawn. When set to an array, the first index indicates the
<dt>miter limit</dt><dd>An integer specifying the miter limit of a miter linejoin. If you want to specify a linejoin of miter, you simply specify the limit as opposed to having
myshape.set("stroke", {
myshape.set("stroke", {
<dt>opacity</dt><dd>Number between 0 and 1 that indicates the opacity of the fill. The default value is 1.</dd>
<p>If a `linear` or `radial` is specified as the fill type. The following additional property is used:
<dt>opacity</dt><dd>Number between 0 and 1 that indicates the opacity of the stop. The default value is 1. Note: No effect for IE <= 8</dd>
<dt>rotation</dt><dd>Linear gradients flow left to right by default. The rotation property allows you to change the flow by rotation. (e.g. A rotation of 180 would make the gradient pain from right to left.)</dd>
<dt>cx</dt><dd>The x-coordinate of the center of the gradient circle. Determines where the color stop begins. The default value 0.5.
<dt>cy</dt><dd>The y-coordinate of the center of the gradient circle. Determines where the color stop begins. The default value 0.5.
<p>Gradients need more need more normalization across technologies. Certain gradient types have limitations on different browsers.
<li>Radial gradients contain the properties `cx` and `cy`. These properties currently have no impact on Android or IE 6 - 8.</li>