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.
24 * One way to create a draw component is:
27 * var drawComponent = Ext.create('Ext.draw.Component', {
38 * Ext.create('Ext.Window', {
42 * items: [drawComponent]
45 * In this case we created a draw component and added a sprite to it.
46 * The *type* of the sprite is *circle* so if you run this code you'll see a yellow-ish
47 * circle in a Window. When setting `viewBox` to `false` we are responsible for setting the object's position and
48 * dimensions accordingly.
50 * You can also add sprites by using the surface's add method:
52 * drawComponent.surface.add({
60 * For more information on Sprites, the core elements added to a draw component's surface,
61 * refer to the Ext.draw.Sprite documentation.
63 Ext.define('Ext.draw.Component', {
65 /* Begin Definitions */
69 extend: 'Ext.Component',
73 'Ext.layout.component.Draw'
79 * @cfg {String[]} enginePriority
80 * Defines the priority order for which Surface implementation to use. The first
81 * one supported by the current environment will be used.
83 enginePriority: ['Svg', 'Vml'],
85 baseCls: Ext.baseCSSPrefix + 'surface',
87 componentLayout: 'draw',
90 * @cfg {Boolean} viewBox
91 * Turn on view box support which will scale and position items in the draw component to fit to the component while
92 * maintaining aspect ratio. Note that this scaling can override other sizing settings on yor items. Defaults to true.
97 * @cfg {Boolean} autoSize
98 * Turn on autoSize support which will set the bounding div's size to the natural size of the contents. Defaults to false.
103 * @cfg {Object[]} gradients (optional) Define a set of gradients that can be used as `fill` property in sprites.
104 * The gradients array is an array of objects with the following properties:
106 * - `id` - string - The unique name of the gradient.
107 * - `angle` - number, optional - The angle of the gradient in degrees.
108 * - `stops` - object - An object with numbers as keys (from 0 to 100) and style objects as values
139 * Then the sprites can use `gradientId` and `gradientId2` by setting the fill attributes to those ids, for example:
141 * sprite.setAttributes({
142 * fill: 'url(#gradientId)'
145 initComponent: function() {
146 this.callParent(arguments);
161 * Create the Surface on initial render
163 onRender: function() {
165 viewBox = me.viewBox,
166 autoSize = me.autoSize,
167 bbox, items, width, height, x, y;
168 me.callParent(arguments);
170 if (me.createSurface() !== false) {
171 items = me.surface.items;
173 if (viewBox || autoSize) {
174 bbox = items.getBBox();
176 height = bbox.height;
180 me.surface.setViewBox(x, y, width, height);
184 me.autoSizeSurface();
191 autoSizeSurface: function() {
193 items = me.surface.items,
194 bbox = items.getBBox(),
196 height = bbox.height;
197 items.setAttributes({
200 //Opera has a slight offset in the y axis.
201 y: -bbox.y + (+Ext.isOpera)
205 me.setSize(width, height);
206 me.surface.setSize(width, height);
209 me.surface.setSize(width, height);
211 me.el.setSize(width, height);
215 * Create the Surface instance. Resolves the correct Surface implementation to
216 * instantiate based on the 'enginePriority' config. Once the Surface instance is
217 * created you can use the handle to that instance to add sprites. For example:
219 * drawComponent.surface.add(sprite);
221 createSurface: function() {
222 var surface = Ext.draw.Surface.create(Ext.apply({}, {
226 }, this.initialConfig));
228 // In case we cannot create a surface, return false so we can stop
231 this.surface = surface;
234 function refire(eventName) {
236 this.fireEvent(eventName, e);
242 mouseup: refire('mouseup'),
243 mousedown: refire('mousedown'),
244 mousemove: refire('mousemove'),
245 mouseenter: refire('mouseenter'),
246 mouseleave: refire('mouseleave'),
247 click: refire('click')
255 * Clean up the Surface instance on component destruction
257 onDestroy: function() {
258 var surface = this.surface;
262 this.callParent(arguments);