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.Surface'>/**
2 </span> * @class Ext.draw.Surface
5 * A Surface is an interface to render methods inside a draw {@link Ext.draw.Component}.
6 * A Surface contains methods to render sprites, get bounding boxes of sprites, add
7 * sprites to the canvas, initialize other graphic components, etc. One of the most used
8 * methods for this class is the `add` method, to add Sprites to the surface.
10 * Most of the Surface methods are abstract and they have a concrete implementation
11 * in VML or SVG engines.
13 * A Surface instance can be accessed as a property of a draw component. For example:
15 * drawComponent.surface.add({
23 * The configuration object passed in the `add` method is the same as described in the {@link Ext.draw.Sprite}
24 * class documentation.
28 * You can also add event listeners to the surface using the `Observable` listener syntax. Supported events are:
41 drawComponent.surface.on({
42 'mousemove': function() {
43 console.log('moving the mouse over the surface');
47 Ext.define('Ext.draw.Surface', {
49 /* Begin Definitions */
52 observable: 'Ext.util.Observable'
55 requires: ['Ext.draw.CompositeSprite'],
56 uses: ['Ext.draw.engine.Svg', 'Ext.draw.engine.Vml'],
61 <span id='Ext-draw.Surface-method-create'> /**
62 </span> * Create and return a new concrete Surface instance appropriate for the current environment.
63 * @param {Object} config Initial configuration for the Surface instance
64 * @param {Array} enginePriority Optional order of implementations to use; the first one that is
65 * available in the current environment will be used. Defaults to
66 * <code>['Svg', 'Vml']</code>.
68 create: function(config, enginePriority) {
69 enginePriority = enginePriority || ['Svg', 'Vml'];
72 len = enginePriority.length,
75 for (; i < len; i++) {
76 if (Ext.supports[enginePriority[i]]) {
77 return Ext.create('Ext.draw.engine.' + enginePriority[i], config);
89 "clip-rect": "0 0 1e9 1e9",
90 cursor: "default",
93 'dominant-baseline': 'auto',
94 fill: "none",
95 "fill-opacity": 1,
96 font: '10px "Arial"',
97 "font-family": '"Arial"',
98 "font-size": "10",
99 "font-style": "normal",
100 "font-weight": 400,
101 gradient: "",
104 href: "http://sencha.com/",
106 path: "M0,0",
110 scale: "1 1",
112 stroke: "#000",
113 "stroke-dasharray": "",
114 "stroke-linecap": "butt",
115 "stroke-linejoin": "butt",
116 "stroke-miterlimit": 0,
117 "stroke-opacity": 1,
118 "stroke-width": 1,
119 target: "_blank",
121 "text-anchor": "middle",
122 title: "Ext Draw",
129 <span id='Ext-draw.Surface-cfg-height'> /**
130 </span> * @cfg {Number} height
131 * The height of this component in pixels (defaults to auto).
132 * <b>Note</b> to express this dimension as a percentage or offset see {@link Ext.Component#anchor}.
134 <span id='Ext-draw.Surface-cfg-width'> /**
135 </span> * @cfg {Number} width
136 * The width of this component in pixels (defaults to auto).
137 * <b>Note</b> to express this dimension as a percentage or offset see {@link Ext.Component#anchor}.
139 container: undefined,
145 constructor: function(config) {
147 config = config || {};
148 Ext.apply(me, config);
150 me.domRef = Ext.getDoc().dom;
152 me.customAttributes = {};
165 me.mixins.observable.constructor.call(me);
171 me.render(me.renderTo);
174 me.initBackground(config.background);
177 // @private called to initialize components in the surface
178 // this is dependent on the underlying implementation.
179 initSurface: Ext.emptyFn,
181 // @private called to setup the surface to render an item
182 //this is dependent on the underlying implementation.
183 renderItem: Ext.emptyFn,
186 renderItems: Ext.emptyFn,
189 setViewBox: Ext.emptyFn,
191 <span id='Ext-draw.Surface-property-addCls'> /**
192 </span> * Adds one or more CSS classes to the element. Duplicate classes are automatically filtered out.
196 * drawComponent.surface.addCls(sprite, 'x-visible');
198 * @param {Object} sprite The sprite to add the class to.
199 * @param {String/Array} className The CSS class to add, or an array of classes
203 <span id='Ext-draw.Surface-property-removeCls'> /**
204 </span> * Removes one or more CSS classes from the element.
208 * drawComponent.surface.removeCls(sprite, 'x-visible');
210 * @param {Object} sprite The sprite to remove the class from.
211 * @param {String/Array} className The CSS class to remove, or an array of classes
213 removeCls: Ext.emptyFn,
215 <span id='Ext-draw.Surface-property-setStyle'> /**
216 </span> * Sets CSS style attributes to an element.
220 * drawComponent.surface.setStyle(sprite, {
221 * 'cursor': 'pointer'
224 * @param {Object} sprite The sprite to add, or an array of classes to
225 * @param {Object} styles An Object with CSS styles.
227 setStyle: Ext.emptyFn,
230 initGradients: function() {
231 var gradients = this.gradients;
233 Ext.each(gradients, this.addGradient, this);
238 initItems: function() {
239 var items = this.items;
240 this.items = Ext.create('Ext.draw.CompositeSprite');
241 this.groups = Ext.create('Ext.draw.CompositeSprite');
248 initBackground: function(config) {
253 height = this.height;
255 if (config.gradient) {
256 gradient = config.gradient;
257 gradientId = gradient.id;
258 this.addGradient(gradient);
259 this.background = this.add({
265 fill: 'url(#' + gradientId + ')'
267 } else if (config.fill) {
268 this.background = this.add({
276 } else if (config.image) {
277 this.background = this.add({
289 <span id='Ext-draw.Surface-method-setSize'> /**
290 </span> * Sets the size of the surface. Accomodates the background (if any) to fit the new size too.
294 * drawComponent.surface.setSize(500, 500);
296 * This method is generally called when also setting the size of the draw Component.
298 * @param {Number} w The new width of the canvas.
299 * @param {Number} h The new height of the canvas.
301 setSize: function(w, h) {
302 if (this.background) {
303 this.background.setAttributes({
312 scrubAttrs: function(sprite) {
318 // Narrow down attributes to the main set
319 if (this.translateAttrs.hasOwnProperty(i)) {
321 attrs[this.translateAttrs[i]] = sattr[i];
322 exclude[this.translateAttrs[i]] = true;
324 else if (this.availableAttrs.hasOwnProperty(i) && !exclude[i]) {
333 onClick: function(e) {
334 this.processEvent('click', e);
338 onMouseUp: function(e) {
339 this.processEvent('mouseup', e);
343 onMouseDown: function(e) {
344 this.processEvent('mousedown', e);
348 onMouseOver: function(e) {
349 this.processEvent('mouseover', e);
353 onMouseOut: function(e) {
354 this.processEvent('mouseout', e);
358 onMouseMove: function(e) {
359 this.fireEvent('mousemove', e);
363 onMouseEnter: Ext.emptyFn,
366 onMouseLeave: Ext.emptyFn,
368 <span id='Ext-draw.Surface-property-addGradient'> /**
369 </span> * Add a gradient definition to the Surface. Note that in some surface engines, adding
370 * a gradient via this method will not take effect if the surface has already been rendered.
371 * Therefore, it is preferred to pass the gradients as an item to the surface config, rather
372 * than calling this method, especially if the surface is rendered immediately (e.g. due to
373 * 'renderTo' in its config). For more information on how to create gradients in the Chart
374 * configuration object please refer to {@link Ext.chart.Chart}.
376 * The gradient object to be passed into this method is composed by:
379 * - **id** - string - The unique name of the gradient.
380 * - **angle** - number, optional - The angle of the gradient in degrees.
381 * - **stops** - object - An object with numbers as keys (from 0 to 100) and style objects as values.
385 drawComponent.surface.addGradient({
398 addGradient: Ext.emptyFn,
400 <span id='Ext-draw.Surface-method-add'> /**
401 </span> * Add a Sprite to the surface. See {@link Ext.draw.Sprite} for the configuration object to be passed into this method.
405 * drawComponent.surface.add({
415 var args = Array.prototype.slice.call(arguments),
419 var hasMultipleArgs = args.length > 1;
420 if (hasMultipleArgs || Ext.isArray(args[0])) {
421 var items = hasMultipleArgs ? args : args[0],
425 for (i = 0, ln = items.length; i < ln; i++) {
427 item = this.add(item);
433 sprite = this.prepareItems(args[0], true)[0];
434 this.normalizeSpriteCollection(sprite);
439 <span id='Ext-draw.Surface-method-normalizeSpriteCollection'> /**
441 * Insert or move a given sprite into the correct position in the items
442 * MixedCollection, according to its zIndex. Will be inserted at the end of
443 * an existing series of sprites with the same or lower zIndex. If the sprite
444 * is already positioned within an appropriate zIndex group, it will not be moved.
445 * This ordering can be used by subclasses to assist in rendering the sprites in
446 * the correct order for proper z-index stacking.
447 * @param {Ext.draw.Sprite} sprite
448 * @return {Number} the sprite's new index in the list
450 normalizeSpriteCollection: function(sprite) {
451 var items = this.items,
452 zIndex = sprite.attr.zIndex,
453 idx = items.indexOf(sprite);
455 if (idx < 0 || (idx > 0 && items.getAt(idx - 1).attr.zIndex > zIndex) ||
456 (idx < items.length - 1 && items.getAt(idx + 1).attr.zIndex < zIndex)) {
458 idx = items.findIndexBy(function(otherSprite) {
459 return otherSprite.attr.zIndex > zIndex;
464 items.insert(idx, sprite);
469 onAdd: function(sprite) {
470 var group = sprite.group,
471 draggable = sprite.draggable,
474 groups = [].concat(group);
476 for (i = 0; i < ln; i++) {
478 this.getGroup(group).add(sprite);
483 sprite.initDraggable();
487 <span id='Ext-draw.Surface-method-remove'> /**
488 </span> * Remove a given sprite from the surface, optionally destroying the sprite in the process.
489 * You can also call the sprite own `remove` method.
493 * drawComponent.surface.remove(sprite);
497 * @param {Ext.draw.Sprite} sprite
498 * @param {Boolean} destroySprite
499 * @return {Number} the sprite's new index in the list
501 remove: function(sprite, destroySprite) {
503 this.items.remove(sprite);
504 this.groups.each(function(item) {
508 if (destroySprite === true) {
514 <span id='Ext-draw.Surface-method-removeAll'> /**
515 </span> * Remove all sprites from the surface, optionally destroying the sprites in the process.
519 * drawComponent.surface.removeAll();
521 * @param {Boolean} destroySprites Whether to destroy all sprites when removing them.
522 * @return {Number} The sprite's new index in the list.
524 removeAll: function(destroySprites) {
525 var items = this.items.items,
528 for (i = ln - 1; i > -1; i--) {
529 this.remove(items[i], destroySprites);
533 onRemove: Ext.emptyFn,
535 onDestroy: Ext.emptyFn,
538 applyTransformations: function(sprite) {
539 sprite.bbox.transform = 0;
540 this.transform(sprite);
546 if (attr.translation.x != null || attr.translation.y != null) {
547 me.translate(sprite);
550 if (attr.scaling.x != null || attr.scaling.y != null) {
554 if (attr.rotation.degrees != null) {
559 sprite.bbox.transform = 0;
560 this.transform(sprite);
561 sprite.transformations = [];
566 rotate: function (sprite) {
568 deg = sprite.attr.rotation.degrees,
569 centerX = sprite.attr.rotation.x,
570 centerY = sprite.attr.rotation.y;
571 if (!Ext.isNumber(centerX) || !Ext.isNumber(centerY)) {
572 bbox = this.getBBox(sprite);
573 centerX = !Ext.isNumber(centerX) ? bbox.x + bbox.width / 2 : centerX;
574 centerY = !Ext.isNumber(centerY) ? bbox.y + bbox.height / 2 : centerY;
576 sprite.transformations.push({
577 type: "rotate",
585 translate: function(sprite) {
586 var x = sprite.attr.translation.x || 0,
587 y = sprite.attr.translation.y || 0;
588 sprite.transformations.push({
589 type: "translate",
596 scale: function(sprite) {
598 x = sprite.attr.scaling.x || 1,
599 y = sprite.attr.scaling.y || 1,
600 centerX = sprite.attr.scaling.centerX,
601 centerY = sprite.attr.scaling.centerY;
603 if (!Ext.isNumber(centerX) || !Ext.isNumber(centerY)) {
604 bbox = this.getBBox(sprite);
605 centerX = !Ext.isNumber(centerX) ? bbox.x + bbox.width / 2 : centerX;
606 centerY = !Ext.isNumber(centerY) ? bbox.y + bbox.height / 2 : centerY;
608 sprite.transformations.push({
609 type: "scale",
618 rectPath: function (x, y, w, h, r) {
620 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"]];
622 return [["M", x, y], ["l", w, 0], ["l", 0, h], ["l", -w, 0], ["z"]];
626 ellipsePath: function (x, y, rx, ry) {
630 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"]];
634 getPathpath: function (el) {
639 getPathcircle: function (el) {
641 return this.ellipsePath(a.x, a.y, a.radius, a.radius);
645 getPathellipse: function (el) {
647 return this.ellipsePath(a.x, a.y, a.radiusX, a.radiusY);
651 getPathrect: function (el) {
653 return this.rectPath(a.x, a.y, a.width, a.height, a.r);
657 getPathimage: function (el) {
659 return this.rectPath(a.x || 0, a.y || 0, a.width, a.height);
663 getPathtext: function (el) {
664 var bbox = this.getBBoxText(el);
665 return this.rectPath(bbox.x, bbox.y, bbox.width, bbox.height);
668 createGroup: function(id) {
669 var group = this.groups.get(id);
671 group = Ext.create('Ext.draw.CompositeSprite', {
674 group.id = id || Ext.id(null, 'ext-surface-group-');
675 this.groups.add(group);
680 <span id='Ext-draw.Surface-method-getGroup'> /**
681 </span> * Returns a new group or an existent group associated with the current surface.
682 * The group returned is a {@link Ext.draw.CompositeSprite} group.
686 * var spriteGroup = drawComponent.surface.getGroup('someGroupId');
688 * @param {String} id The unique identifier of the group.
689 * @return {Object} The {@link Ext.draw.CompositeSprite}.
691 getGroup: function(id) {
692 if (typeof id == "string") {
693 var group = this.groups.get(id);
695 group = this.createGroup(id);
704 prepareItems: function(items, applyDefaults) {
705 items = [].concat(items);
706 // Make sure defaults are applied and item is initialized
708 for (i = 0, ln = items.length; i < ln; i++) {
710 if (!(item instanceof Ext.draw.Sprite)) {
711 // Temporary, just take in configs...
713 items[i] = this.createItem(item);
721 <span id='Ext-draw.Surface-property-setText'> /**
722 </span> * Changes the text in the sprite element. The sprite must be a `text` sprite.
723 * This method can also be called from {@link Ext.draw.Sprite}.
727 * var spriteGroup = drawComponent.surface.setText(sprite, 'my new text');
729 * @param {Object} sprite The Sprite to change the text.
730 * @param {String} text The new text to be set.
732 setText: Ext.emptyFn,
734 //@private Creates an item and appends it to the surface. Called
735 //as an internal method when calling `add`.
736 createItem: Ext.emptyFn,
738 <span id='Ext-draw.Surface-method-getId'> /**
739 </span> * Retrieves the id of this component.
740 * Will autogenerate an id if one has not already been set.
743 return this.id || (this.id = Ext.id(null, 'ext-surface-'));
746 <span id='Ext-draw.Surface-method-destroy'> /**
747 </span> * Destroys the surface. This is done by removing all components from it and
748 * also removing its reference to a DOM element.
752 * drawComponent.surface.destroy();
754 destroy: function() {
758 });</pre></pre></body></html>