2 * @class Ext.draw.Sprite
5 * A Sprite is an object rendered in a Drawing surface. There are different options and types of sprites.
6 * The configuration of a Sprite is an object with the following properties:
8 * - **type** - (String) The type of the sprite. Possible options are 'circle', 'path', 'rect', 'text', 'square', 'image'.
9 * - **width** - (Number) Used in rectangle sprites, the width of the rectangle.
10 * - **height** - (Number) Used in rectangle sprites, the height of the rectangle.
11 * - **size** - (Number) Used in square sprites, the dimension of the square.
12 * - **radius** - (Number) Used in circle sprites, the radius of the circle.
13 * - **x** - (Number) The position along the x-axis.
14 * - **y** - (Number) The position along the y-axis.
15 * - **path** - (Array) Used in path sprites, the path of the sprite written in SVG-like path syntax.
16 * - **opacity** - (Number) The opacity of the sprite.
17 * - **fill** - (String) The fill color.
18 * - **stroke** - (String) The stroke color.
19 * - **stroke-width** - (Number) The width of the stroke.
20 * - **font** - (String) Used with text type sprites. The full font description. Uses the same syntax as the CSS `font` parameter.
21 * - **text** - (String) Used with text type sprites. The text itself.
23 * Additionally there are three transform objects that can be set with `setAttributes` which are `translate`, `rotate` and
26 * For translate, the configuration object contains x and y attributes that indicate where to
27 * translate the object. For example:
29 * sprite.setAttributes({
36 * For rotation, the configuration object contains x and y attributes for the center of the rotation (which are optional),
37 * and a `degrees` attribute that specifies the rotation in degrees. For example:
39 * sprite.setAttributes({
45 * For scaling, the configuration object contains x and y attributes for the x-axis and y-axis scaling. For example:
47 * sprite.setAttributes({
54 * Sprites can be created with a reference to a {@link Ext.draw.Surface}
56 * var drawComponent = Ext.create('Ext.draw.Component', options here...);
58 * var sprite = Ext.create('Ext.draw.Sprite', {
61 * surface: drawComponent.surface,
65 * Sprites can also be added to the surface as a configuration object:
67 * var sprite = drawComponent.surface.add({
73 * In order to properly apply properties and render the sprite we have to
74 * `show` the sprite setting the option `redraw` to `true`:
78 * The constructor configuration object of the Sprite can also be used and passed into the {@link Ext.draw.Surface}
79 * add method to append a new sprite to the canvas. For example:
81 * drawComponent.surface.add({
89 Ext.define('Ext.draw.Sprite', {
90 /* Begin Definitions */
93 observable: 'Ext.util.Observable',
94 animate: 'Ext.util.Animate'
97 requires: ['Ext.draw.SpriteDD'],
103 dirtyTransform: false,
132 constructor: function(config) {
134 config = config || {};
135 me.id = Ext.id(null, 'ext-sprite-');
136 me.transformations = [];
137 Ext.copyTo(this, config, 'surface,group,type,draggable');
158 //delete not bucket attributes
159 delete config.surface;
162 delete config.draggable;
163 me.setAttributes(config);
175 me.mixins.observable.constructor.apply(this, arguments);
179 * <p>If this Sprite is configured {@link #draggable}, this property will contain
180 * an instance of {@link Ext.dd.DragSource} which handles dragging the Sprite.</p>
181 * The developer must provide implementations of the abstract methods of {@link Ext.dd.DragSource}
182 * in order to supply behaviour for each stage of the drag/drop process. See {@link #draggable}.
183 * @type Ext.dd.DragSource.
186 initDraggable: function() {
189 //create element if it doesn't exist.
191 me.surface.createSpriteElement(me);
193 me.dd = Ext.create('Ext.draw.SpriteDD', me, Ext.isBoolean(me.draggable) ? null : me.draggable);
194 me.on('beforedestroy', me.dd.destroy, me.dd);
198 * Change the attributes of the sprite.
199 * @param {Object} attrs attributes to be changed on the sprite.
200 * @param {Boolean} redraw Flag to immediatly draw the change.
201 * @return {Ext.draw.Sprite} this
203 setAttributes: function(attrs, redraw) {
205 fontProps = me.fontProperties,
206 fontPropsLength = fontProps.length,
207 pathProps = me.pathProperties,
208 pathPropsLength = pathProps.length,
209 hasSurface = !!me.surface,
210 custom = hasSurface && me.surface.customAttributes || {},
211 spriteAttrs = me.attr,
212 attr, i, translate, translation, rotate, rotation, scale, scaling;
214 attrs = Ext.apply({}, attrs);
215 for (attr in custom) {
216 if (attrs.hasOwnProperty(attr) && typeof custom[attr] == "function") {
217 Ext.apply(attrs, custom[attr].apply(me, [].concat(attrs[attr])));
221 // Flag a change in hidden
222 if (!!attrs.hidden !== !!spriteAttrs.hidden) {
223 me.dirtyHidden = true;
227 for (i = 0; i < pathPropsLength; i++) {
229 if (attr in attrs && attrs[attr] !== spriteAttrs[attr]) {
235 // Flag zIndex change
236 if ('zIndex' in attrs) {
237 me.zIndexDirty = true;
240 // Flag font/text change
241 for (i = 0; i < fontPropsLength; i++) {
243 if (attr in attrs && attrs[attr] !== spriteAttrs[attr]) {
249 translate = attrs.translate;
250 translation = spriteAttrs.translation;
252 if ((translate.x && translate.x !== translation.x) ||
253 (translate.y && translate.y !== translation.y)) {
254 Ext.apply(translation, translate);
255 me.dirtyTransform = true;
257 delete attrs.translate;
260 rotate = attrs.rotate;
261 rotation = spriteAttrs.rotation;
263 if ((rotate.x && rotate.x !== rotation.x) ||
264 (rotate.y && rotate.y !== rotation.y) ||
265 (rotate.degrees && rotate.degrees !== rotation.degrees)) {
266 Ext.apply(rotation, rotate);
267 me.dirtyTransform = true;
273 scaling = spriteAttrs.scaling;
275 if ((scale.x && scale.x !== scaling.x) ||
276 (scale.y && scale.y !== scaling.y) ||
277 (scale.cx && scale.cx !== scaling.cx) ||
278 (scale.cy && scale.cy !== scaling.cy)) {
279 Ext.apply(scaling, scale);
280 me.dirtyTransform = true;
285 Ext.apply(spriteAttrs, attrs);
288 if (redraw === true && hasSurface) {
295 * Retrieve the bounding box of the sprite. This will be returned as an object with x, y, width, and height properties.
296 * @return {Object} bbox
298 getBBox: function() {
299 return this.surface.getBBox(this);
302 setText: function(text) {
303 return this.surface.setText(this, text);
308 * @param {Boolean} redraw Flag to immediatly draw the change.
309 * @return {Ext.draw.Sprite} this
311 hide: function(redraw) {
320 * @param {Boolean} redraw Flag to immediatly draw the change.
321 * @return {Ext.draw.Sprite} this
323 show: function(redraw) {
335 this.surface.remove(this);
341 onRemove: function() {
342 this.surface.onRemove(this);
346 * Removes the sprite and clears all listeners.
348 destroy: function() {
350 if (me.fireEvent('beforedestroy', me) !== false) {
352 me.surface.onDestroy(me);
354 me.fireEvent('destroy');
360 * @return {Ext.draw.Sprite} this
363 this.surface.renderItem(this);
368 * Wrapper for setting style properties, also takes single object parameter of multiple styles.
369 * @param {String/Object} property The style property to be set, or an object of multiple styles.
370 * @param {String} value (optional) The value to apply to the given property, or null if an object was passed.
371 * @return {Ext.draw.Sprite} this
373 setStyle: function() {
374 this.el.setStyle.apply(this.el, arguments);
379 * Adds one or more CSS classes to the element. Duplicate classes are automatically filtered out. Note this method
380 * is severly limited in VML.
381 * @param {String/Array} className The CSS class to add, or an array of classes
382 * @return {Ext.draw.Sprite} this
384 addCls: function(obj) {
385 this.surface.addCls(this, obj);
390 * Removes one or more CSS classes from the element.
391 * @param {String/Array} className The CSS class to remove, or an array of classes. Note this method
392 * is severly limited in VML.
393 * @return {Ext.draw.Sprite} this
395 removeCls: function(obj) {
396 this.surface.removeCls(this, obj);