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 * - **width** - (Number) Used in rectangle sprites, the width of the rectangle.
27 * - **height** - (Number) Used in rectangle sprites, the height of the rectangle.
28 * - **size** - (Number) Used in square sprites, the dimension of the square.
29 * - **radius** - (Number) Used in circle sprites, the radius of the circle.
30 * - **x** - (Number) The position along the x-axis.
31 * - **y** - (Number) The position along the y-axis.
32 * - **path** - (Array) Used in path sprites, the path of the sprite written in SVG-like path syntax.
33 * - **opacity** - (Number) The opacity of the sprite.
34 * - **fill** - (String) The fill color.
35 * - **stroke** - (String) The stroke color.
36 * - **stroke-width** - (Number) The width of the stroke.
37 * - **font** - (String) Used with text type sprites. The full font description. Uses the same syntax as the CSS `font` parameter.
38 * - **text** - (String) Used with text type sprites. The text itself.
40 * Additionally there are three transform objects that can be set with `setAttributes` which are `translate`, `rotate` and
43 * For translate, the configuration object contains x and y attributes that indicate where to
44 * translate the object. For example:
46 * sprite.setAttributes({
53 * For rotation, the configuration object contains x and y attributes for the center of the rotation (which are optional),
54 * and a `degrees` attribute that specifies the rotation in degrees. For example:
56 * sprite.setAttributes({
62 * For scaling, the configuration object contains x and y attributes for the x-axis and y-axis scaling. For example:
64 * sprite.setAttributes({
71 * Sprites can be created with a reference to a {@link Ext.draw.Surface}
73 * var drawComponent = Ext.create('Ext.draw.Component', options here...);
75 * var sprite = Ext.create('Ext.draw.Sprite', {
78 * surface: drawComponent.surface,
82 * Sprites can also be added to the surface as a configuration object:
84 * var sprite = drawComponent.surface.add({
90 * In order to properly apply properties and render the sprite we have to
91 * `show` the sprite setting the option `redraw` to `true`:
95 * The constructor configuration object of the Sprite can also be used and passed into the {@link Ext.draw.Surface}
96 * add method to append a new sprite to the canvas. For example:
98 * drawComponent.surface.add({
106 Ext.define('Ext.draw.Sprite', {
107 /* Begin Definitions */
110 observable: 'Ext.util.Observable',
111 animate: 'Ext.util.Animate'
114 requires: ['Ext.draw.SpriteDD'],
116 /* End Definitions */
120 dirtyTransform: false,
149 constructor: function(config) {
151 config = config || {};
152 me.id = Ext.id(null, 'ext-sprite-');
153 me.transformations = [];
154 Ext.copyTo(this, config, 'surface,group,type,draggable');
175 //delete not bucket attributes
176 delete config.surface;
179 delete config.draggable;
180 me.setAttributes(config);
192 me.mixins.observable.constructor.apply(this, arguments);
195 <span id='Ext-draw-Sprite-property-dd'> /**
196 </span> * <p>If this Sprite is configured {@link #draggable}, this property will contain
197 * an instance of {@link Ext.dd.DragSource} which handles dragging the Sprite.</p>
198 * The developer must provide implementations of the abstract methods of {@link Ext.dd.DragSource}
199 * in order to supply behaviour for each stage of the drag/drop process. See {@link #draggable}.
200 * @type Ext.dd.DragSource.
203 initDraggable: function() {
206 //create element if it doesn't exist.
208 me.surface.createSpriteElement(me);
210 me.dd = Ext.create('Ext.draw.SpriteDD', me, Ext.isBoolean(me.draggable) ? null : me.draggable);
211 me.on('beforedestroy', me.dd.destroy, me.dd);
214 <span id='Ext-draw-Sprite-method-setAttributes'> /**
215 </span> * Change the attributes of the sprite.
216 * @param {Object} attrs attributes to be changed on the sprite.
217 * @param {Boolean} redraw Flag to immediatly draw the change.
218 * @return {Ext.draw.Sprite} this
220 setAttributes: function(attrs, redraw) {
222 fontProps = me.fontProperties,
223 fontPropsLength = fontProps.length,
224 pathProps = me.pathProperties,
225 pathPropsLength = pathProps.length,
226 hasSurface = !!me.surface,
227 custom = hasSurface && me.surface.customAttributes || {},
228 spriteAttrs = me.attr,
229 attr, i, translate, translation, rotate, rotation, scale, scaling;
231 attrs = Ext.apply({}, attrs);
232 for (attr in custom) {
233 if (attrs.hasOwnProperty(attr) && typeof custom[attr] == "function") {
234 Ext.apply(attrs, custom[attr].apply(me, [].concat(attrs[attr])));
238 // Flag a change in hidden
239 if (!!attrs.hidden !== !!spriteAttrs.hidden) {
240 me.dirtyHidden = true;
244 for (i = 0; i < pathPropsLength; i++) {
246 if (attr in attrs && attrs[attr] !== spriteAttrs[attr]) {
252 // Flag zIndex change
253 if ('zIndex' in attrs) {
254 me.zIndexDirty = true;
257 // Flag font/text change
258 for (i = 0; i < fontPropsLength; i++) {
260 if (attr in attrs && attrs[attr] !== spriteAttrs[attr]) {
266 translate = attrs.translate;
267 translation = spriteAttrs.translation;
269 if ((translate.x && translate.x !== translation.x) ||
270 (translate.y && translate.y !== translation.y)) {
271 Ext.apply(translation, translate);
272 me.dirtyTransform = true;
274 delete attrs.translate;
277 rotate = attrs.rotate;
278 rotation = spriteAttrs.rotation;
280 if ((rotate.x && rotate.x !== rotation.x) ||
281 (rotate.y && rotate.y !== rotation.y) ||
282 (rotate.degrees && rotate.degrees !== rotation.degrees)) {
283 Ext.apply(rotation, rotate);
284 me.dirtyTransform = true;
290 scaling = spriteAttrs.scaling;
292 if ((scale.x && scale.x !== scaling.x) ||
293 (scale.y && scale.y !== scaling.y) ||
294 (scale.cx && scale.cx !== scaling.cx) ||
295 (scale.cy && scale.cy !== scaling.cy)) {
296 Ext.apply(scaling, scale);
297 me.dirtyTransform = true;
302 Ext.apply(spriteAttrs, attrs);
305 if (redraw === true && hasSurface) {
311 <span id='Ext-draw-Sprite-method-getBBox'> /**
312 </span> * Retrieve the bounding box of the sprite. This will be returned as an object with x, y, width, and height properties.
313 * @return {Object} bbox
315 getBBox: function() {
316 return this.surface.getBBox(this);
319 setText: function(text) {
320 return this.surface.setText(this, text);
323 <span id='Ext-draw-Sprite-method-hide'> /**
324 </span> * Hide the sprite.
325 * @param {Boolean} redraw Flag to immediatly draw the change.
326 * @return {Ext.draw.Sprite} this
328 hide: function(redraw) {
335 <span id='Ext-draw-Sprite-method-show'> /**
336 </span> * Show the sprite.
337 * @param {Boolean} redraw Flag to immediatly draw the change.
338 * @return {Ext.draw.Sprite} this
340 show: function(redraw) {
347 <span id='Ext-draw-Sprite-method-remove'> /**
348 </span> * Remove the sprite.
352 this.surface.remove(this);
358 onRemove: function() {
359 this.surface.onRemove(this);
362 <span id='Ext-draw-Sprite-method-destroy'> /**
363 </span> * Removes the sprite and clears all listeners.
365 destroy: function() {
367 if (me.fireEvent('beforedestroy', me) !== false) {
369 me.surface.onDestroy(me);
371 me.fireEvent('destroy');
375 <span id='Ext-draw-Sprite-method-redraw'> /**
376 </span> * Redraw the sprite.
377 * @return {Ext.draw.Sprite} this
380 this.surface.renderItem(this);
384 <span id='Ext-draw-Sprite-method-setStyle'> /**
385 </span> * Wrapper for setting style properties, also takes single object parameter of multiple styles.
386 * @param {String/Object} property The style property to be set, or an object of multiple styles.
387 * @param {String} value (optional) The value to apply to the given property, or null if an object was passed.
388 * @return {Ext.draw.Sprite} this
390 setStyle: function() {
391 this.el.setStyle.apply(this.el, arguments);
395 <span id='Ext-draw-Sprite-method-addCls'> /**
396 </span> * Adds one or more CSS classes to the element. Duplicate classes are automatically filtered out. Note this method
397 * is severly limited in VML.
398 * @param {String/Array} className The CSS class to add, or an array of classes
399 * @return {Ext.draw.Sprite} this
401 addCls: function(obj) {
402 this.surface.addCls(this, obj);
406 <span id='Ext-draw-Sprite-method-removeCls'> /**
407 </span> * Removes one or more CSS classes from the element.
408 * @param {String/Array} className The CSS class to remove, or an array of classes. Note this method
409 * is severly limited in VML.
410 * @return {Ext.draw.Sprite} this
412 removeCls: function(obj) {
413 this.surface.removeCls(this, obj);