]>
Commit | Line | Data |
---|---|---|
6527f429 DM |
1 | /**\r |
2 | * @class Ext.draw.sprite.Image\r | |
3 | * @extends Ext.draw.sprite.Rect\r | |
4 | * \r | |
5 | * A sprite that represents an image.\r | |
6 | */\r | |
7 | Ext.define('Ext.draw.sprite.Image', {\r | |
8 | extend: 'Ext.draw.sprite.Rect',\r | |
9 | alias: 'sprite.image',\r | |
10 | type: 'image',\r | |
11 | statics: {\r | |
12 | imageLoaders: {}\r | |
13 | },\r | |
14 | \r | |
15 | inheritableStatics: {\r | |
16 | def: {\r | |
17 | processors: {\r | |
18 | /**\r | |
19 | * @cfg {String} [src=''] The image source of the sprite.\r | |
20 | */\r | |
21 | src: 'string'\r | |
22 | },\r | |
23 | defaults: {\r | |
24 | src: '',\r | |
25 | /**\r | |
26 | * @cfg {Number} [width=null] The width of the image.\r | |
27 | * For consistent image size on all devices the width must be explicitly set.\r | |
28 | * Otherwise the natural image width devided by the device pixel ratio\r | |
29 | * (for a crisp looking image) will be used as the width of the sprite.\r | |
30 | */\r | |
31 | width: null,\r | |
32 | /**\r | |
33 | * @cfg {Number} [height=null] The height of the image.\r | |
34 | * For consistent image size on all devices the height must be explicitly set.\r | |
35 | * Otherwise the natural image height devided by the device pixel ratio\r | |
36 | * (for a crisp looking image) will be used as the height of the sprite.\r | |
37 | */\r | |
38 | height: null\r | |
39 | }\r | |
40 | }\r | |
41 | },\r | |
42 | \r | |
43 | render: function (surface, ctx) {\r | |
44 | var me = this,\r | |
45 | attr = me.attr,\r | |
46 | mat = attr.matrix,\r | |
47 | src = attr.src,\r | |
48 | x = attr.x,\r | |
49 | y = attr.y,\r | |
50 | width = attr.width,\r | |
51 | height = attr.height,\r | |
52 | loadingStub = Ext.draw.sprite.Image.imageLoaders[src],\r | |
53 | imageLoader,\r | |
54 | image,\r | |
55 | i;\r | |
56 | \r | |
57 | if (loadingStub && loadingStub.done) {\r | |
58 | mat.toContext(ctx);\r | |
59 | image = loadingStub.image;\r | |
60 | ctx.drawImage(image, x, y,\r | |
61 | width || (image.naturalWidth || image.width) / surface.devicePixelRatio,\r | |
62 | height || (image.naturalHeight || image.height) / surface.devicePixelRatio);\r | |
63 | } else if (!loadingStub) {\r | |
64 | imageLoader = new Image();\r | |
65 | loadingStub = Ext.draw.sprite.Image.imageLoaders[src] = {\r | |
66 | image: imageLoader,\r | |
67 | done: false,\r | |
68 | pendingSprites: [me],\r | |
69 | pendingSurfaces: [surface]\r | |
70 | };\r | |
71 | imageLoader.width = width;\r | |
72 | imageLoader.height = height;\r | |
73 | imageLoader.onload = function () {\r | |
74 | if (!loadingStub.done) {\r | |
75 | loadingStub.done = true;\r | |
76 | for (i = 0; i < loadingStub.pendingSprites.length; i++) {\r | |
77 | loadingStub.pendingSprites[i].setDirty(true);\r | |
78 | }\r | |
79 | for (i in loadingStub.pendingSurfaces) {\r | |
80 | loadingStub.pendingSurfaces[i].renderFrame();\r | |
81 | }\r | |
82 | }\r | |
83 | };\r | |
84 | imageLoader.src = src;\r | |
85 | } else {\r | |
86 | Ext.Array.include(loadingStub.pendingSprites, me);\r | |
87 | Ext.Array.include(loadingStub.pendingSurfaces, surface);\r | |
88 | }\r | |
89 | \r | |
90 | //<debug>\r | |
91 | var debug = attr.debug || this.statics().debug || Ext.draw.sprite.Sprite.debug;\r | |
92 | if (debug) {\r | |
93 | debug.bbox && this.renderBBox(surface, ctx);\r | |
94 | }\r | |
95 | //</debug>\r | |
96 | }\r | |
97 | }); |