3 This file is part of Ext JS 4
5 Copyright (c) 2011 Sencha Inc
7 Contact: http://www.sencha.com/contact
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
16 * @class Ext.draw.Component
17 * @extends Ext.Component
19 * The Draw Component is a surface in which sprites can be rendered. The Draw Component
20 * manages and holds a `Surface` instance: an interface that has
21 * an SVG or VML implementation depending on the browser capabilities and where
22 * Sprites can be appended.
23 * {@img Ext.draw.Component/Ext.draw.Component.png Ext.draw.Component component}
24 * One way to create a draw component is:
26 * var drawComponent = Ext.create('Ext.draw.Component', {
37 * Ext.create('Ext.Window', {
41 * items: [drawComponent]
44 * In this case we created a draw component and added a sprite to it.
45 * The *type* of the sprite is *circle* so if you run this code you'll see a yellow-ish
46 * circle in a Window. When setting `viewBox` to `false` we are responsible for setting the object's position and
47 * dimensions accordingly.
49 * You can also add sprites by using the surface's add method:
51 * drawComponent.surface.add({
59 * For more information on Sprites, the core elements added to a draw component's surface,
60 * refer to the Ext.draw.Sprite documentation.
62 Ext.define('Ext.draw.Component', {
64 /* Begin Definitions */
68 extend: 'Ext.Component',
72 'Ext.layout.component.Draw'
78 * @cfg {Array} enginePriority
79 * Defines the priority order for which Surface implementation to use. The first
80 * one supported by the current environment will be used.
82 enginePriority: ['Svg', 'Vml'],
84 baseCls: Ext.baseCSSPrefix + 'surface',
86 componentLayout: 'draw',
89 * @cfg {Boolean} viewBox
90 * Turn on view box support which will scale and position items in the draw component to fit to the component while
91 * maintaining aspect ratio. Note that this scaling can override other sizing settings on yor items. Defaults to true.
96 * @cfg {Boolean} autoSize
97 * Turn on autoSize support which will set the bounding div's size to the natural size of the contents. Defaults to false.
102 * @cfg {Array} gradients (optional) Define a set of gradients that can be used as `fill` property in sprites.
103 * The gradients array is an array of objects with the following properties:
106 * <li><strong>id</strong> - string - The unique name of the gradient.</li>
107 * <li><strong>angle</strong> - number, optional - The angle of the gradient in degrees.</li>
108 * <li><strong>stops</strong> - object - An object with numbers as keys (from 0 to 100) and style objects
144 Then the sprites can use `gradientId` and `gradientId2` by setting the fill attributes to those ids, for example:
147 sprite.setAttributes({
148 fill: 'url(#gradientId)'
154 initComponent: function() {
155 this.callParent(arguments);
170 * Create the Surface on initial render
172 onRender: function() {
174 viewBox = me.viewBox,
175 autoSize = me.autoSize,
176 bbox, items, width, height, x, y;
177 me.callParent(arguments);
181 items = me.surface.items;
183 if (viewBox || autoSize) {
184 bbox = items.getBBox();
186 height = bbox.height;
190 me.surface.setViewBox(x, y, width, height);
194 me.autoSizeSurface();
200 autoSizeSurface: function() {
202 items = me.surface.items,
203 bbox = items.getBBox(),
205 height = bbox.height;
206 items.setAttributes({
209 //Opera has a slight offset in the y axis.
210 y: -bbox.y + (+Ext.isOpera)
214 me.setSize(width, height);
215 me.surface.setSize(width, height);
218 me.surface.setSize(width, height);
220 me.el.setSize(width, height);
224 * Create the Surface instance. Resolves the correct Surface implementation to
225 * instantiate based on the 'enginePriority' config. Once the Surface instance is
226 * created you can use the handle to that instance to add sprites. For example:
229 drawComponent.surface.add(sprite);
232 createSurface: function() {
233 var surface = Ext.draw.Surface.create(Ext.apply({}, {
237 }, this.initialConfig));
238 this.surface = surface;
240 function refire(eventName) {
242 this.fireEvent(eventName, e);
248 mouseup: refire('mouseup'),
249 mousedown: refire('mousedown'),
250 mousemove: refire('mousemove'),
251 mouseenter: refire('mouseenter'),
252 mouseleave: refire('mouseleave'),
253 click: refire('click')
261 * Clean up the Surface instance on component destruction
263 onDestroy: function() {
264 var surface = this.surface;
268 this.callParent(arguments);