1 <!DOCTYPE html><html><head><title>Sencha Documentation Project</title><link rel="stylesheet" href="../reset.css" type="text/css"><link rel="stylesheet" href="../prettify.css" type="text/css"><link rel="stylesheet" href="../prettify_sa.css" type="text/css"><script type="text/javascript" src="../prettify.js"></script></head><body onload="prettyPrint()"><pre class="prettyprint"><pre><span id='Ext-draw.Sprite'>/**
2 </span> * @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'.
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);
178 <span id='Ext-draw.Sprite-property-dd'> /**
179 </span> * <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.createSprite(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);
197 <span id='Ext-draw.Sprite-method-setAttributes'> /**
198 </span> * 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 for (attr in custom) {
215 if (attrs.hasOwnProperty(attr) && typeof custom[attr] == "function") {
216 Ext.apply(attrs, custom[attr].apply(me, [].concat(attrs[attr])));
220 // Flag a change in hidden
221 if (!!attrs.hidden !== !!spriteAttrs.hidden) {
222 me.dirtyHidden = true;
226 for (i = 0; i < pathPropsLength; i++) {
228 if (attr in attrs && attrs[attr] !== spriteAttrs[attr]) {
234 // Flag zIndex change
235 if ('zIndex' in attrs) {
236 me.zIndexDirty = true;
239 // Flag font/text change
240 for (i = 0; i < fontPropsLength; i++) {
242 if (attr in attrs && attrs[attr] !== spriteAttrs[attr]) {
248 translate = attrs.translate;
249 translation = spriteAttrs.translation;
251 if ((translate.x && translate.x !== translation.x) ||
252 (translate.y && translate.y !== translation.y)) {
253 Ext.apply(translation, translate);
254 me.dirtyTransform = true;
256 delete attrs.translate;
259 rotate = attrs.rotate;
260 rotation = spriteAttrs.rotation;
262 if ((rotate.x && rotate.x !== rotation.x) ||
263 (rotate.y && rotate.y !== rotation.y) ||
264 (rotate.degrees && rotate.degrees !== rotation.degrees)) {
265 Ext.apply(rotation, rotate);
266 me.dirtyTransform = true;
272 scaling = spriteAttrs.scaling;
274 if ((scale.x && scale.x !== scaling.x) ||
275 (scale.y && scale.y !== scaling.y) ||
276 (scale.cx && scale.cx !== scaling.cx) ||
277 (scale.cy && scale.cy !== scaling.cy)) {
278 Ext.apply(scaling, scale);
279 me.dirtyTransform = true;
284 Ext.apply(spriteAttrs, attrs);
287 if (redraw === true && hasSurface) {
293 <span id='Ext-draw.Sprite-method-getBBox'> /**
294 </span> * Retrieve the bounding box of the sprite. This will be returned as an object with x, y, width, and height properties.
295 * @return {Object} bbox
297 getBBox: function() {
298 return this.surface.getBBox(this);
301 setText: function(text) {
302 return this.surface.setText(this, text);
305 <span id='Ext-draw.Sprite-method-hide'> /**
306 </span> * Hide the sprite.
307 * @param {Boolean} redraw Flag to immediatly draw the change.
308 * @return {Ext.draw.Sprite} this
310 hide: function(redraw) {
317 <span id='Ext-draw.Sprite-method-show'> /**
318 </span> * Show the sprite.
319 * @param {Boolean} redraw Flag to immediatly draw the change.
320 * @return {Ext.draw.Sprite} this
322 show: function(redraw) {
329 <span id='Ext-draw.Sprite-method-remove'> /**
330 </span> * Remove the sprite.
334 this.surface.remove(this);
340 onRemove: function() {
341 this.surface.onRemove(this);
344 <span id='Ext-draw.Sprite-method-destroy'> /**
345 </span> * Removes the sprite and clears all listeners.
347 destroy: function() {
349 if (me.fireEvent('beforedestroy', me) !== false) {
351 me.surface.onDestroy(me);
353 me.fireEvent('destroy');
357 <span id='Ext-draw.Sprite-method-redraw'> /**
358 </span> * Redraw the sprite.
359 * @return {Ext.draw.Sprite} this
362 this.surface.renderItem(this);
366 <span id='Ext-draw.Sprite-method-setStyle'> /**
367 </span> * Wrapper for setting style properties, also takes single object parameter of multiple styles.
368 * @param {String/Object} property The style property to be set, or an object of multiple styles.
369 * @param {String} value (optional) The value to apply to the given property, or null if an object was passed.
370 * @return {Ext.draw.Sprite} this
372 setStyle: function() {
373 this.el.setStyle.apply(this.el, arguments);
377 <span id='Ext-draw.Sprite-method-addCls'> /**
378 </span> * Adds one or more CSS classes to the element. Duplicate classes are automatically filtered out. Note this method
379 * is severly limited in VML.
380 * @param {String/Array} className The CSS class to add, or an array of classes
381 * @return {Ext.draw.Sprite} this
383 addCls: function(obj) {
384 this.surface.addCls(this, obj);
388 <span id='Ext-draw.Sprite-method-removeCls'> /**
389 </span> * Removes one or more CSS classes from the element.
390 * @param {String/Array} className The CSS class to remove, or an array of classes. Note this method
391 * is severly limited in VML.
392 * @return {Ext.draw.Sprite} this
394 removeCls: function(obj) {
395 this.surface.removeCls(this, obj);
399 </pre></pre></body></html>