4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-draw-Sprite'>/**
19 </span> * @class Ext.draw.Sprite
22 * A Sprite is an object rendered in a Drawing surface. There are different options and types of sprites.
23 * The configuration of a Sprite is an object with the following properties:
25 * - **type** - (String) The type of the sprite. Possible options are 'circle', 'path', 'rect', 'text', 'square', 'image'.
26 * - **group** - (String/Array) The group that this sprite belongs to, or an array of groups. Only relevant when added to a {@link Ext.draw.Surface}.
27 * - **width** - (Number) Used in rectangle sprites, the width of the rectangle.
28 * - **height** - (Number) Used in rectangle sprites, the height of the rectangle.
29 * - **size** - (Number) Used in square sprites, the dimension of the square.
30 * - **radius** - (Number) Used in circle sprites, the radius of the circle.
31 * - **x** - (Number) The position along the x-axis.
32 * - **y** - (Number) The position along the y-axis.
33 * - **path** - (Array) Used in path sprites, the path of the sprite written in SVG-like path syntax.
34 * - **opacity** - (Number) The opacity of the sprite.
35 * - **fill** - (String) The fill color.
36 * - **stroke** - (String) The stroke color.
37 * - **stroke-width** - (Number) The width of the stroke.
38 * - **font** - (String) Used with text type sprites. The full font description. Uses the same syntax as the CSS `font` parameter.
39 * - **text** - (String) Used with text type sprites. The text itself.
40 * - **translate** - (Object) Defines a translation for the Sprite. There's more information on this property below.
41 * - **rotate** - (Object) Defines a rotation for the Sprite. There's more information on this property below.
42 * - **scale** - (Object) Defines a scaling for the Sprite. There's more information on this property below.
47 * For translate, the configuration object contains x and y attributes that indicate where to
48 * translate the object. For example:
50 * sprite.setAttributes({
60 * For rotation, the configuration object contains x and y attributes for the center of the rotation (which are optional),
61 * and a `degrees` attribute that specifies the rotation in degrees. For example:
63 * sprite.setAttributes({
69 * That example will create a 90 degrees rotation using the centroid of the Sprite as center of rotation, whereas:
71 * sprite.setAttributes({
79 * will create a rotation around the `(0, 0)` axis.
84 * For scaling, the configuration object contains x and y attributes for the x-axis and y-axis scaling. For example:
86 * sprite.setAttributes({
93 * You can also specify the center of scaling by adding `cx` and `cy` as properties:
95 * sprite.setAttributes({
104 * That last example will scale a sprite taking as centers of scaling the `(0, 0)` coordinate.
107 * ## Creating and adding a Sprite to a Surface
109 * Sprites can be created with a reference to a {@link Ext.draw.Surface}
111 * var drawComponent = Ext.create('Ext.draw.Component', options here...);
113 * var sprite = Ext.create('Ext.draw.Sprite', {
116 * surface: drawComponent.surface,
120 * Sprites can also be added to the surface as a configuration object:
122 * var sprite = drawComponent.surface.add({
128 * In order to properly apply properties and render the sprite we have to
129 * `show` the sprite setting the option `redraw` to `true`:
133 * The constructor configuration object of the Sprite can also be used and passed into the {@link Ext.draw.Surface}
134 * add method to append a new sprite to the canvas. For example:
136 * drawComponent.surface.add({
144 Ext.define('Ext.draw.Sprite', {
146 <span id='Ext-draw-Sprite-cfg-type'> /**
147 </span> * @cfg {String} type The type of the sprite. Possible options are 'circle', 'path', 'rect', 'text', 'square', 'image'
150 <span id='Ext-draw-Sprite-cfg-width'> /**
151 </span> * @cfg {Number} width Used in rectangle sprites, the width of the rectangle
154 <span id='Ext-draw-Sprite-cfg-height'> /**
155 </span> * @cfg {Number} height Used in rectangle sprites, the height of the rectangle
158 <span id='Ext-draw-Sprite-cfg-size'> /**
159 </span> * @cfg {Number} size Used in square sprites, the dimension of the square
162 <span id='Ext-draw-Sprite-cfg-radius'> /**
163 </span> * @cfg {Number} radius Used in circle sprites, the radius of the circle
166 <span id='Ext-draw-Sprite-cfg-x'> /**
167 </span> * @cfg {Number} x The position along the x-axis
170 <span id='Ext-draw-Sprite-cfg-y'> /**
171 </span> * @cfg {Number} y The position along the y-axis
174 <span id='Ext-draw-Sprite-cfg-path'> /**
175 </span> * @cfg {Array} path Used in path sprites, the path of the sprite written in SVG-like path syntax
178 <span id='Ext-draw-Sprite-cfg-opacity'> /**
179 </span> * @cfg {Number} opacity The opacity of the sprite
182 <span id='Ext-draw-Sprite-cfg-fill'> /**
183 </span> * @cfg {String} fill The fill color
186 <span id='Ext-draw-Sprite-cfg-stroke'> /**
187 </span> * @cfg {String} stroke The stroke color
190 <span id='Ext-draw-Sprite-cfg-stroke'> /**
191 </span> * @cfg {Number} stroke-width The width of the stroke
194 <span id='Ext-draw-Sprite-cfg-font'> /**
195 </span> * @cfg {String} font Used with text type sprites. The full font description. Uses the same syntax as the CSS font parameter
198 <span id='Ext-draw-Sprite-cfg-text'> /**
199 </span> * @cfg {String} text Used with text type sprites. The text itself
202 <span id='Ext-draw-Sprite-cfg-group'> /**
203 </span> * @cfg {String/Array} group The group that this sprite belongs to, or an array of groups. Only relevant when added to a
204 * {@link Ext.draw.Surface}
207 /* Begin Definitions */
210 observable: 'Ext.util.Observable',
211 animate: 'Ext.util.Animate'
214 requires: ['Ext.draw.SpriteDD'],
216 /* End Definitions */
220 dirtyTransform: false,
249 constructor: function(config) {
251 config = config || {};
252 me.id = Ext.id(null, 'ext-sprite-');
253 me.transformations = [];
254 Ext.copyTo(this, config, 'surface,group,type,draggable');
275 //delete not bucket attributes
276 delete config.surface;
279 delete config.draggable;
280 me.setAttributes(config);
292 me.mixins.observable.constructor.apply(this, arguments);
295 <span id='Ext-draw-Sprite-property-dd'> /**
296 </span> * <p>If this Sprite is configured {@link #draggable}, this property will contain
297 * an instance of {@link Ext.dd.DragSource} which handles dragging the Sprite.</p>
298 * The developer must provide implementations of the abstract methods of {@link Ext.dd.DragSource}
299 * in order to supply behaviour for each stage of the drag/drop process. See {@link #draggable}.
300 * @type Ext.dd.DragSource.
303 initDraggable: function() {
306 //create element if it doesn't exist.
308 me.surface.createSpriteElement(me);
310 me.dd = Ext.create('Ext.draw.SpriteDD', me, Ext.isBoolean(me.draggable) ? null : me.draggable);
311 me.on('beforedestroy', me.dd.destroy, me.dd);
314 <span id='Ext-draw-Sprite-method-setAttributes'> /**
315 </span> * Change the attributes of the sprite.
316 * @param {Object} attrs attributes to be changed on the sprite.
317 * @param {Boolean} redraw Flag to immediatly draw the change.
318 * @return {Ext.draw.Sprite} this
320 setAttributes: function(attrs, redraw) {
322 fontProps = me.fontProperties,
323 fontPropsLength = fontProps.length,
324 pathProps = me.pathProperties,
325 pathPropsLength = pathProps.length,
326 hasSurface = !!me.surface,
327 custom = hasSurface && me.surface.customAttributes || {},
328 spriteAttrs = me.attr,
329 attr, i, translate, translation, rotate, rotation, scale, scaling;
331 attrs = Ext.apply({}, attrs);
332 for (attr in custom) {
333 if (attrs.hasOwnProperty(attr) && typeof custom[attr] == "function") {
334 Ext.apply(attrs, custom[attr].apply(me, [].concat(attrs[attr])));
338 // Flag a change in hidden
339 if (!!attrs.hidden !== !!spriteAttrs.hidden) {
340 me.dirtyHidden = true;
344 for (i = 0; i < pathPropsLength; i++) {
346 if (attr in attrs && attrs[attr] !== spriteAttrs[attr]) {
352 // Flag zIndex change
353 if ('zIndex' in attrs) {
354 me.zIndexDirty = true;
357 // Flag font/text change
358 for (i = 0; i < fontPropsLength; i++) {
360 if (attr in attrs && attrs[attr] !== spriteAttrs[attr]) {
366 translate = attrs.translate;
367 translation = spriteAttrs.translation;
369 if ((translate.x && translate.x !== translation.x) ||
370 (translate.y && translate.y !== translation.y)) {
371 Ext.apply(translation, translate);
372 me.dirtyTransform = true;
374 delete attrs.translate;
377 rotate = attrs.rotate;
378 rotation = spriteAttrs.rotation;
380 if ((rotate.x && rotate.x !== rotation.x) ||
381 (rotate.y && rotate.y !== rotation.y) ||
382 (rotate.degrees && rotate.degrees !== rotation.degrees)) {
383 Ext.apply(rotation, rotate);
384 me.dirtyTransform = true;
390 scaling = spriteAttrs.scaling;
392 if ((scale.x && scale.x !== scaling.x) ||
393 (scale.y && scale.y !== scaling.y) ||
394 (scale.cx && scale.cx !== scaling.cx) ||
395 (scale.cy && scale.cy !== scaling.cy)) {
396 Ext.apply(scaling, scale);
397 me.dirtyTransform = true;
402 Ext.apply(spriteAttrs, attrs);
405 if (redraw === true && hasSurface) {
411 <span id='Ext-draw-Sprite-method-getBBox'> /**
412 </span> * Retrieve the bounding box of the sprite. This will be returned as an object with x, y, width, and height properties.
413 * @return {Object} bbox
415 getBBox: function() {
416 return this.surface.getBBox(this);
419 setText: function(text) {
420 return this.surface.setText(this, text);
423 <span id='Ext-draw-Sprite-method-hide'> /**
424 </span> * Hide the sprite.
425 * @param {Boolean} redraw Flag to immediatly draw the change.
426 * @return {Ext.draw.Sprite} this
428 hide: function(redraw) {
435 <span id='Ext-draw-Sprite-method-show'> /**
436 </span> * Show the sprite.
437 * @param {Boolean} redraw Flag to immediatly draw the change.
438 * @return {Ext.draw.Sprite} this
440 show: function(redraw) {
447 <span id='Ext-draw-Sprite-method-remove'> /**
448 </span> * Remove the sprite.
452 this.surface.remove(this);
458 onRemove: function() {
459 this.surface.onRemove(this);
462 <span id='Ext-draw-Sprite-method-destroy'> /**
463 </span> * Removes the sprite and clears all listeners.
465 destroy: function() {
467 if (me.fireEvent('beforedestroy', me) !== false) {
469 me.surface.onDestroy(me);
471 me.fireEvent('destroy');
475 <span id='Ext-draw-Sprite-method-redraw'> /**
476 </span> * Redraw the sprite.
477 * @return {Ext.draw.Sprite} this
480 this.surface.renderItem(this);
484 <span id='Ext-draw-Sprite-method-setStyle'> /**
485 </span> * Wrapper for setting style properties, also takes single object parameter of multiple styles.
486 * @param {String/Object} property The style property to be set, or an object of multiple styles.
487 * @param {String} value (optional) The value to apply to the given property, or null if an object was passed.
488 * @return {Ext.draw.Sprite} this
490 setStyle: function() {
491 this.el.setStyle.apply(this.el, arguments);
495 <span id='Ext-draw-Sprite-method-addCls'> /**
496 </span> * Adds one or more CSS classes to the element. Duplicate classes are automatically filtered out. Note this method
497 * is severly limited in VML.
498 * @param {String/Array} className The CSS class to add, or an array of classes
499 * @return {Ext.draw.Sprite} this
501 addCls: function(obj) {
502 this.surface.addCls(this, obj);
506 <span id='Ext-draw-Sprite-method-removeCls'> /**
507 </span> * Removes one or more CSS classes from the element.
508 * @param {String/Array} className The CSS class to remove, or an array of classes. Note this method
509 * is severly limited in VML.
510 * @return {Ext.draw.Sprite} this
512 removeCls: function(obj) {
513 this.surface.removeCls(this, obj);