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-Surface'>/**
19 </span> * @class Ext.draw.Surface
22 * A Surface is an interface to render methods inside a draw {@link Ext.draw.Component}.
23 * A Surface contains methods to render sprites, get bounding boxes of sprites, add
24 * sprites to the canvas, initialize other graphic components, etc. One of the most used
25 * methods for this class is the `add` method, to add Sprites to the surface.
27 * Most of the Surface methods are abstract and they have a concrete implementation
28 * in VML or SVG engines.
30 * A Surface instance can be accessed as a property of a draw component. For example:
32 * drawComponent.surface.add({
40 * The configuration object passed in the `add` method is the same as described in the {@link Ext.draw.Sprite}
41 * class documentation.
45 * You can also add event listeners to the surface using the `Observable` listener syntax. Supported events are:
58 * drawComponent.surface.on({
59 * 'mousemove': function() {
60 * console.log('moving the mouse over the surface');
66 * drawComponent.surface.add([
103 * group: 'rectangles'
110 * group: 'rectangles'
114 * // Get references to my groups
115 * my circles = surface.getGroup('circles');
116 * my rectangles = surface.getGroup('rectangles');
118 * // Animate the circles down
126 * // Animate the rectangles across
127 * rectangles.animate({
134 Ext.define('Ext.draw.Surface', {
136 /* Begin Definitions */
139 observable: 'Ext.util.Observable'
142 requires: ['Ext.draw.CompositeSprite'],
143 uses: ['Ext.draw.engine.Svg', 'Ext.draw.engine.Vml'],
145 separatorRe: /[, ]+/,
148 <span id='Ext-draw-Surface-method-create'> /**
149 </span> * Create and return a new concrete Surface instance appropriate for the current environment.
150 * @param {Object} config Initial configuration for the Surface instance
151 * @param {Array} enginePriority Optional order of implementations to use; the first one that is
152 * available in the current environment will be used. Defaults to
153 * <code>['Svg', 'Vml']</code>.
155 create: function(config, enginePriority) {
156 enginePriority = enginePriority || ['Svg', 'Vml'];
159 len = enginePriority.length,
162 for (; i < len; i++) {
163 if (Ext.supports[enginePriority[i]]) {
164 return Ext.create('Ext.draw.engine.' + enginePriority[i], config);
171 /* End Definitions */
176 "clip-rect": "0 0 1e9 1e9",
177 cursor: "default",
180 'dominant-baseline': 'auto',
181 fill: "none",
182 "fill-opacity": 1,
183 font: '10px "Arial"',
184 "font-family": '"Arial"',
185 "font-size": "10",
186 "font-style": "normal",
187 "font-weight": 400,
188 gradient: "",
191 href: "http://sencha.com/",
193 path: "M0,0",
197 scale: "1 1",
199 stroke: "#000",
200 "stroke-dasharray": "",
201 "stroke-linecap": "butt",
202 "stroke-linejoin": "butt",
203 "stroke-miterlimit": 0,
204 "stroke-opacity": 1,
205 "stroke-width": 1,
206 target: "_blank",
208 "text-anchor": "middle",
209 title: "Ext Draw",
216 <span id='Ext-draw-Surface-cfg-height'> /**
217 </span> * @cfg {Number} height
218 * The height of this component in pixels (defaults to auto).
219 * <b>Note</b> to express this dimension as a percentage or offset see {@link Ext.Component#anchor}.
221 <span id='Ext-draw-Surface-cfg-width'> /**
222 </span> * @cfg {Number} width
223 * The width of this component in pixels (defaults to auto).
224 * <b>Note</b> to express this dimension as a percentage or offset see {@link Ext.Component#anchor}.
226 container: undefined,
232 constructor: function(config) {
234 config = config || {};
235 Ext.apply(me, config);
237 me.domRef = Ext.getDoc().dom;
239 me.customAttributes = {};
252 me.mixins.observable.constructor.call(me);
258 me.render(me.renderTo);
261 me.initBackground(config.background);
264 // @private called to initialize components in the surface
265 // this is dependent on the underlying implementation.
266 initSurface: Ext.emptyFn,
268 // @private called to setup the surface to render an item
269 //this is dependent on the underlying implementation.
270 renderItem: Ext.emptyFn,
273 renderItems: Ext.emptyFn,
276 setViewBox: Ext.emptyFn,
278 <span id='Ext-draw-Surface-method-addCls'> /**
279 </span> * Adds one or more CSS classes to the element. Duplicate classes are automatically filtered out.
283 * drawComponent.surface.addCls(sprite, 'x-visible');
285 * @param {Object} sprite The sprite to add the class to.
286 * @param {String/Array} className The CSS class to add, or an array of classes
291 <span id='Ext-draw-Surface-method-removeCls'> /**
292 </span> * Removes one or more CSS classes from the element.
296 * drawComponent.surface.removeCls(sprite, 'x-visible');
298 * @param {Object} sprite The sprite to remove the class from.
299 * @param {String/Array} className The CSS class to remove, or an array of classes
302 removeCls: Ext.emptyFn,
304 <span id='Ext-draw-Surface-method-setStyle'> /**
305 </span> * Sets CSS style attributes to an element.
309 * drawComponent.surface.setStyle(sprite, {
310 * 'cursor': 'pointer'
313 * @param {Object} sprite The sprite to add, or an array of classes to
314 * @param {Object} styles An Object with CSS styles.
317 setStyle: Ext.emptyFn,
320 initGradients: function() {
321 var gradients = this.gradients;
323 Ext.each(gradients, this.addGradient, this);
328 initItems: function() {
329 var items = this.items;
330 this.items = Ext.create('Ext.draw.CompositeSprite');
331 this.groups = Ext.create('Ext.draw.CompositeSprite');
338 initBackground: function(config) {
342 gradientId, gradient, backgroundSprite;
344 if (config.gradient) {
345 gradient = config.gradient;
346 gradientId = gradient.id;
347 me.addGradient(gradient);
348 me.background = me.add({
354 fill: 'url(#' + gradientId + ')'
356 } else if (config.fill) {
357 me.background = me.add({
365 } else if (config.image) {
366 me.background = me.add({
378 <span id='Ext-draw-Surface-method-setSize'> /**
379 </span> * Sets the size of the surface. Accomodates the background (if any) to fit the new size too.
383 * drawComponent.surface.setSize(500, 500);
385 * This method is generally called when also setting the size of the draw Component.
387 * @param {Number} w The new width of the canvas.
388 * @param {Number} h The new height of the canvas.
390 setSize: function(w, h) {
391 if (this.background) {
392 this.background.setAttributes({
401 scrubAttrs: function(sprite) {
407 // Narrow down attributes to the main set
408 if (this.translateAttrs.hasOwnProperty(i)) {
410 attrs[this.translateAttrs[i]] = sattr[i];
411 exclude[this.translateAttrs[i]] = true;
413 else if (this.availableAttrs.hasOwnProperty(i) && !exclude[i]) {
422 onClick: function(e) {
423 this.processEvent('click', e);
427 onMouseUp: function(e) {
428 this.processEvent('mouseup', e);
432 onMouseDown: function(e) {
433 this.processEvent('mousedown', e);
437 onMouseOver: function(e) {
438 this.processEvent('mouseover', e);
442 onMouseOut: function(e) {
443 this.processEvent('mouseout', e);
447 onMouseMove: function(e) {
448 this.fireEvent('mousemove', e);
452 onMouseEnter: Ext.emptyFn,
455 onMouseLeave: Ext.emptyFn,
457 <span id='Ext-draw-Surface-method-addGradient'> /**
458 </span> * Add a gradient definition to the Surface. Note that in some surface engines, adding
459 * a gradient via this method will not take effect if the surface has already been rendered.
460 * Therefore, it is preferred to pass the gradients as an item to the surface config, rather
461 * than calling this method, especially if the surface is rendered immediately (e.g. due to
462 * 'renderTo' in its config). For more information on how to create gradients in the Chart
463 * configuration object please refer to {@link Ext.chart.Chart}.
465 * The gradient object to be passed into this method is composed by:
468 * - **id** - string - The unique name of the gradient.
469 * - **angle** - number, optional - The angle of the gradient in degrees.
470 * - **stops** - object - An object with numbers as keys (from 0 to 100) and style objects as values.
474 drawComponent.surface.addGradient({
489 addGradient: Ext.emptyFn,
491 <span id='Ext-draw-Surface-method-add'> /**
492 </span> * Add a Sprite to the surface. See {@link Ext.draw.Sprite} for the configuration object to be passed into this method.
496 * drawComponent.surface.add({
506 var args = Array.prototype.slice.call(arguments),
510 var hasMultipleArgs = args.length > 1;
511 if (hasMultipleArgs || Ext.isArray(args[0])) {
512 var items = hasMultipleArgs ? args : args[0],
516 for (i = 0, ln = items.length; i < ln; i++) {
518 item = this.add(item);
524 sprite = this.prepareItems(args[0], true)[0];
525 this.normalizeSpriteCollection(sprite);
530 <span id='Ext-draw-Surface-method-normalizeSpriteCollection'> /**
532 * Insert or move a given sprite into the correct position in the items
533 * MixedCollection, according to its zIndex. Will be inserted at the end of
534 * an existing series of sprites with the same or lower zIndex. If the sprite
535 * is already positioned within an appropriate zIndex group, it will not be moved.
536 * This ordering can be used by subclasses to assist in rendering the sprites in
537 * the correct order for proper z-index stacking.
538 * @param {Ext.draw.Sprite} sprite
539 * @return {Number} the sprite's new index in the list
541 normalizeSpriteCollection: function(sprite) {
542 var items = this.items,
543 zIndex = sprite.attr.zIndex,
544 idx = items.indexOf(sprite);
546 if (idx < 0 || (idx > 0 && items.getAt(idx - 1).attr.zIndex > zIndex) ||
547 (idx < items.length - 1 && items.getAt(idx + 1).attr.zIndex < zIndex)) {
549 idx = items.findIndexBy(function(otherSprite) {
550 return otherSprite.attr.zIndex > zIndex;
555 items.insert(idx, sprite);
560 onAdd: function(sprite) {
561 var group = sprite.group,
562 draggable = sprite.draggable,
565 groups = [].concat(group);
567 for (i = 0; i < ln; i++) {
569 this.getGroup(group).add(sprite);
574 sprite.initDraggable();
578 <span id='Ext-draw-Surface-method-remove'> /**
579 </span> * Remove a given sprite from the surface, optionally destroying the sprite in the process.
580 * You can also call the sprite own `remove` method.
584 * drawComponent.surface.remove(sprite);
588 * @param {Ext.draw.Sprite} sprite
589 * @param {Boolean} destroySprite
590 * @return {Number} the sprite's new index in the list
592 remove: function(sprite, destroySprite) {
594 this.items.remove(sprite);
595 this.groups.each(function(item) {
599 if (destroySprite === true) {
605 <span id='Ext-draw-Surface-method-removeAll'> /**
606 </span> * Remove all sprites from the surface, optionally destroying the sprites in the process.
610 * drawComponent.surface.removeAll();
612 * @param {Boolean} destroySprites Whether to destroy all sprites when removing them.
613 * @return {Number} The sprite's new index in the list.
615 removeAll: function(destroySprites) {
616 var items = this.items.items,
619 for (i = ln - 1; i > -1; i--) {
620 this.remove(items[i], destroySprites);
624 onRemove: Ext.emptyFn,
626 onDestroy: Ext.emptyFn,
629 applyTransformations: function(sprite) {
630 sprite.bbox.transform = 0;
631 this.transform(sprite);
637 if (attr.translation.x != null || attr.translation.y != null) {
638 me.translate(sprite);
641 if (attr.scaling.x != null || attr.scaling.y != null) {
645 if (attr.rotation.degrees != null) {
650 sprite.bbox.transform = 0;
651 this.transform(sprite);
652 sprite.transformations = [];
657 rotate: function (sprite) {
659 deg = sprite.attr.rotation.degrees,
660 centerX = sprite.attr.rotation.x,
661 centerY = sprite.attr.rotation.y;
662 if (!Ext.isNumber(centerX) || !Ext.isNumber(centerY)) {
663 bbox = this.getBBox(sprite);
664 centerX = !Ext.isNumber(centerX) ? bbox.x + bbox.width / 2 : centerX;
665 centerY = !Ext.isNumber(centerY) ? bbox.y + bbox.height / 2 : centerY;
667 sprite.transformations.push({
668 type: "rotate",
676 translate: function(sprite) {
677 var x = sprite.attr.translation.x || 0,
678 y = sprite.attr.translation.y || 0;
679 sprite.transformations.push({
680 type: "translate",
687 scale: function(sprite) {
689 x = sprite.attr.scaling.x || 1,
690 y = sprite.attr.scaling.y || 1,
691 centerX = sprite.attr.scaling.centerX,
692 centerY = sprite.attr.scaling.centerY;
694 if (!Ext.isNumber(centerX) || !Ext.isNumber(centerY)) {
695 bbox = this.getBBox(sprite);
696 centerX = !Ext.isNumber(centerX) ? bbox.x + bbox.width / 2 : centerX;
697 centerY = !Ext.isNumber(centerY) ? bbox.y + bbox.height / 2 : centerY;
699 sprite.transformations.push({
700 type: "scale",
709 rectPath: function (x, y, w, h, r) {
711 return [["M", x + r, y], ["l", w - r * 2, 0], ["a", r, r, 0, 0, 1, r, r], ["l", 0, h - r * 2], ["a", r, r, 0, 0, 1, -r, r], ["l", r * 2 - w, 0], ["a", r, r, 0, 0, 1, -r, -r], ["l", 0, r * 2 - h], ["a", r, r, 0, 0, 1, r, -r], ["z"]];
713 return [["M", x, y], ["l", w, 0], ["l", 0, h], ["l", -w, 0], ["z"]];
717 ellipsePath: function (x, y, rx, ry) {
721 return [["M", x, y], ["m", 0, -ry], ["a", rx, ry, 0, 1, 1, 0, 2 * ry], ["a", rx, ry, 0, 1, 1, 0, -2 * ry], ["z"]];
725 getPathpath: function (el) {
730 getPathcircle: function (el) {
732 return this.ellipsePath(a.x, a.y, a.radius, a.radius);
736 getPathellipse: function (el) {
738 return this.ellipsePath(a.x, a.y,
739 a.radiusX || (a.width / 2) || 0,
740 a.radiusY || (a.height / 2) || 0);
744 getPathrect: function (el) {
746 return this.rectPath(a.x, a.y, a.width, a.height, a.r);
750 getPathimage: function (el) {
752 return this.rectPath(a.x || 0, a.y || 0, a.width, a.height);
756 getPathtext: function (el) {
757 var bbox = this.getBBoxText(el);
758 return this.rectPath(bbox.x, bbox.y, bbox.width, bbox.height);
761 createGroup: function(id) {
762 var group = this.groups.get(id);
764 group = Ext.create('Ext.draw.CompositeSprite', {
767 group.id = id || Ext.id(null, 'ext-surface-group-');
768 this.groups.add(group);
773 <span id='Ext-draw-Surface-method-getGroup'> /**
774 </span> * Returns a new group or an existent group associated with the current surface.
775 * The group returned is a {@link Ext.draw.CompositeSprite} group.
779 * var spriteGroup = drawComponent.surface.getGroup('someGroupId');
781 * @param {String} id The unique identifier of the group.
782 * @return {Object} The {@link Ext.draw.CompositeSprite}.
784 getGroup: function(id) {
785 if (typeof id == "string") {
786 var group = this.groups.get(id);
788 group = this.createGroup(id);
797 prepareItems: function(items, applyDefaults) {
798 items = [].concat(items);
799 // Make sure defaults are applied and item is initialized
801 for (i = 0, ln = items.length; i < ln; i++) {
803 if (!(item instanceof Ext.draw.Sprite)) {
804 // Temporary, just take in configs...
806 items[i] = this.createItem(item);
814 <span id='Ext-draw-Surface-method-setText'> /**
815 </span> * Changes the text in the sprite element. The sprite must be a `text` sprite.
816 * This method can also be called from {@link Ext.draw.Sprite}.
820 * var spriteGroup = drawComponent.surface.setText(sprite, 'my new text');
822 * @param {Object} sprite The Sprite to change the text.
823 * @param {String} text The new text to be set.
826 setText: Ext.emptyFn,
828 //@private Creates an item and appends it to the surface. Called
829 //as an internal method when calling `add`.
830 createItem: Ext.emptyFn,
832 <span id='Ext-draw-Surface-method-getId'> /**
833 </span> * Retrieves the id of this component.
834 * Will autogenerate an id if one has not already been set.
837 return this.id || (this.id = Ext.id(null, 'ext-surface-'));
840 <span id='Ext-draw-Surface-method-destroy'> /**
841 </span> * Destroys the surface. This is done by removing all components from it and
842 * also removing its reference to a DOM element.
846 * drawComponent.surface.destroy();
848 destroy: function() {