4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../resources/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> * A Sprite is an object rendered in a Drawing surface.
23 * For translate, the configuration object contains x and y attributes that indicate where to
24 * translate the object. For example:
26 * 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 * That example will create a 90 degrees rotation using the centroid of the Sprite as center of rotation, whereas:
47 * sprite.setAttributes({
55 * will create a rotation around the `(0, 0)` axis.
60 * For scaling, the configuration object contains x and y attributes for the x-axis and y-axis scaling. For example:
62 * sprite.setAttributes({
69 * You can also specify the center of scaling by adding `cx` and `cy` as properties:
71 * sprite.setAttributes({
80 * That last example will scale a sprite taking as centers of scaling the `(0, 0)` coordinate.
83 * # Creating and adding a Sprite to a Surface
85 * Sprites can be created with a reference to a {@link Ext.draw.Surface}
87 * var drawComponent = Ext.create('Ext.draw.Component', options here...);
89 * var sprite = Ext.create('Ext.draw.Sprite', {
92 * surface: drawComponent.surface,
96 * Sprites can also be added to the surface as a configuration object:
98 * var sprite = drawComponent.surface.add({
104 * In order to properly apply properties and render the sprite we have to
105 * `show` the sprite setting the option `redraw` to `true`:
109 * The constructor configuration object of the Sprite can also be used and passed into the {@link Ext.draw.Surface}
110 * add method to append a new sprite to the canvas. For example:
112 * drawComponent.surface.add({
120 Ext.define('Ext.draw.Sprite', {
122 /* Begin Definitions */
125 observable: 'Ext.util.Observable',
126 animate: 'Ext.util.Animate'
129 requires: ['Ext.draw.SpriteDD'],
131 /* End Definitions */
133 <span id='Ext-draw-Sprite-cfg-type'> /**
134 </span> * @cfg {String} type The type of the sprite. Possible options are 'circle', 'path', 'rect', 'text', 'square', 'image'
137 <span id='Ext-draw-Sprite-cfg-width'> /**
138 </span> * @cfg {Number} width Used in rectangle sprites, the width of the rectangle
141 <span id='Ext-draw-Sprite-cfg-height'> /**
142 </span> * @cfg {Number} height Used in rectangle sprites, the height of the rectangle
145 <span id='Ext-draw-Sprite-cfg-size'> /**
146 </span> * @cfg {Number} size Used in square sprites, the dimension of the square
149 <span id='Ext-draw-Sprite-cfg-radius'> /**
150 </span> * @cfg {Number} radius Used in circle sprites, the radius of the circle
153 <span id='Ext-draw-Sprite-cfg-x'> /**
154 </span> * @cfg {Number} x The position along the x-axis
157 <span id='Ext-draw-Sprite-cfg-y'> /**
158 </span> * @cfg {Number} y The position along the y-axis
161 <span id='Ext-draw-Sprite-cfg-path'> /**
162 </span> * @cfg {Array} path Used in path sprites, the path of the sprite written in SVG-like path syntax
165 <span id='Ext-draw-Sprite-cfg-opacity'> /**
166 </span> * @cfg {Number} opacity The opacity of the sprite
169 <span id='Ext-draw-Sprite-cfg-fill'> /**
170 </span> * @cfg {String} fill The fill color
173 <span id='Ext-draw-Sprite-cfg-stroke'> /**
174 </span> * @cfg {String} stroke The stroke color
177 <span id='Ext-draw-Sprite-cfg-stroke'> /**
178 </span> * @cfg {Number} stroke-width The width of the stroke
181 <span id='Ext-draw-Sprite-cfg-font'> /**
182 </span> * @cfg {String} font Used with text type sprites. The full font description. Uses the same syntax as the CSS font parameter
185 <span id='Ext-draw-Sprite-cfg-text'> /**
186 </span> * @cfg {String} text Used with text type sprites. The text itself
189 <span id='Ext-draw-Sprite-cfg-group'> /**
190 </span> * @cfg {String/String[]} group The group that this sprite belongs to, or an array of groups. Only relevant when added to a
191 * {@link Ext.draw.Surface}
194 <span id='Ext-draw-Sprite-cfg-draggable'> /**
195 </span> * @cfg {Boolean} draggable True to make the sprite draggable.
200 dirtyTransform: false,
229 constructor: function(config) {
231 config = config || {};
232 me.id = Ext.id(null, 'ext-sprite-');
233 me.transformations = [];
234 Ext.copyTo(this, config, 'surface,group,type,draggable');
255 //delete not bucket attributes
256 delete config.surface;
259 delete config.draggable;
260 me.setAttributes(config);
272 me.mixins.observable.constructor.apply(this, arguments);
275 <span id='Ext-draw-Sprite-property-dd'> /**
276 </span> * @property {Ext.dd.DragSource} dd
277 * If this Sprite is configured {@link #draggable}, this property will contain
278 * an instance of {@link Ext.dd.DragSource} which handles dragging the Sprite.
280 * The developer must provide implementations of the abstract methods of {@link Ext.dd.DragSource}
281 * in order to supply behaviour for each stage of the drag/drop process. See {@link #draggable}.
284 initDraggable: function() {
287 //create element if it doesn't exist.
289 me.surface.createSpriteElement(me);
291 me.dd = Ext.create('Ext.draw.SpriteDD', me, Ext.isBoolean(me.draggable) ? null : me.draggable);
292 me.on('beforedestroy', me.dd.destroy, me.dd);
295 <span id='Ext-draw-Sprite-method-setAttributes'> /**
296 </span> * Change the attributes of the sprite.
297 * @param {Object} attrs attributes to be changed on the sprite.
298 * @param {Boolean} redraw Flag to immediatly draw the change.
299 * @return {Ext.draw.Sprite} this
301 setAttributes: function(attrs, redraw) {
303 fontProps = me.fontProperties,
304 fontPropsLength = fontProps.length,
305 pathProps = me.pathProperties,
306 pathPropsLength = pathProps.length,
307 hasSurface = !!me.surface,
308 custom = hasSurface && me.surface.customAttributes || {},
309 spriteAttrs = me.attr,
310 attr, i, translate, translation, rotate, rotation, scale, scaling;
312 attrs = Ext.apply({}, attrs);
313 for (attr in custom) {
314 if (attrs.hasOwnProperty(attr) && typeof custom[attr] == "function") {
315 Ext.apply(attrs, custom[attr].apply(me, [].concat(attrs[attr])));
319 // Flag a change in hidden
320 if (!!attrs.hidden !== !!spriteAttrs.hidden) {
321 me.dirtyHidden = true;
325 for (i = 0; i < pathPropsLength; i++) {
327 if (attr in attrs && attrs[attr] !== spriteAttrs[attr]) {
333 // Flag zIndex change
334 if ('zIndex' in attrs) {
335 me.zIndexDirty = true;
338 // Flag font/text change
339 for (i = 0; i < fontPropsLength; i++) {
341 if (attr in attrs && attrs[attr] !== spriteAttrs[attr]) {
347 translate = attrs.translate;
348 translation = spriteAttrs.translation;
350 if ((translate.x && translate.x !== translation.x) ||
351 (translate.y && translate.y !== translation.y)) {
352 Ext.apply(translation, translate);
353 me.dirtyTransform = true;
355 delete attrs.translate;
358 rotate = attrs.rotate;
359 rotation = spriteAttrs.rotation;
361 if ((rotate.x && rotate.x !== rotation.x) ||
362 (rotate.y && rotate.y !== rotation.y) ||
363 (rotate.degrees && rotate.degrees !== rotation.degrees)) {
364 Ext.apply(rotation, rotate);
365 me.dirtyTransform = true;
371 scaling = spriteAttrs.scaling;
373 if ((scale.x && scale.x !== scaling.x) ||
374 (scale.y && scale.y !== scaling.y) ||
375 (scale.cx && scale.cx !== scaling.cx) ||
376 (scale.cy && scale.cy !== scaling.cy)) {
377 Ext.apply(scaling, scale);
378 me.dirtyTransform = true;
383 Ext.apply(spriteAttrs, attrs);
386 if (redraw === true && hasSurface) {
392 <span id='Ext-draw-Sprite-method-getBBox'> /**
393 </span> * Retrieves the bounding box of the sprite.
394 * This will be returned as an object with x, y, width, and height properties.
395 * @return {Object} bbox
397 getBBox: function() {
398 return this.surface.getBBox(this);
401 setText: function(text) {
402 return this.surface.setText(this, text);
405 <span id='Ext-draw-Sprite-method-hide'> /**
406 </span> * Hides the sprite.
407 * @param {Boolean} redraw Flag to immediatly draw the change.
408 * @return {Ext.draw.Sprite} this
410 hide: function(redraw) {
417 <span id='Ext-draw-Sprite-method-show'> /**
418 </span> * Shows the sprite.
419 * @param {Boolean} redraw Flag to immediatly draw the change.
420 * @return {Ext.draw.Sprite} this
422 show: function(redraw) {
429 <span id='Ext-draw-Sprite-method-remove'> /**
430 </span> * Removes the sprite.
434 this.surface.remove(this);
440 onRemove: function() {
441 this.surface.onRemove(this);
444 <span id='Ext-draw-Sprite-method-destroy'> /**
445 </span> * Removes the sprite and clears all listeners.
447 destroy: function() {
449 if (me.fireEvent('beforedestroy', me) !== false) {
451 me.surface.onDestroy(me);
453 me.fireEvent('destroy');
457 <span id='Ext-draw-Sprite-method-redraw'> /**
458 </span> * Redraws the sprite.
459 * @return {Ext.draw.Sprite} this
462 this.surface.renderItem(this);
466 <span id='Ext-draw-Sprite-method-setStyle'> /**
467 </span> * Wrapper for setting style properties, also takes single object parameter of multiple styles.
468 * @param {String/Object} property The style property to be set, or an object of multiple styles.
469 * @param {String} value (optional) The value to apply to the given property, or null if an object was passed.
470 * @return {Ext.draw.Sprite} this
472 setStyle: function() {
473 this.el.setStyle.apply(this.el, arguments);
477 <span id='Ext-draw-Sprite-method-addCls'> /**
478 </span> * Adds one or more CSS classes to the element. Duplicate classes are automatically filtered out. Note this method
479 * is severly limited in VML.
480 * @param {String/String[]} className The CSS class to add, or an array of classes
481 * @return {Ext.draw.Sprite} this
483 addCls: function(obj) {
484 this.surface.addCls(this, obj);
488 <span id='Ext-draw-Sprite-method-removeCls'> /**
489 </span> * Removes one or more CSS classes from the element.
490 * @param {String/String[]} className The CSS class to remove, or an array of classes. Note this method
491 * is severly limited in VML.
492 * @return {Ext.draw.Sprite} this
494 removeCls: function(obj) {
495 this.surface.removeCls(this, obj);