forked from cocos2d/cocos2d-html5
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCCSpriteCanvasRenderCmd.js
249 lines (214 loc) · 9.78 KB
/
CCSpriteCanvasRenderCmd.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/****************************************************************************
Copyright (c) 2013-2014 Chukong Technologies Inc.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
(function() {
cc.Sprite.CanvasRenderCmd = function (renderable) {
cc.Node.CanvasRenderCmd.call(this, renderable);
this._needDraw = true;
this._textureCoord = {
renderX: 0, //the x of texture coordinate for render, when texture tinted, its value doesn't equal x.
renderY: 0, //the y of texture coordinate for render, when texture tinted, its value doesn't equal y.
x: 0, //the x of texture coordinate for node.
y: 0, //the y of texture coordinate for node.
width: 0,
height: 0,
validRect: false
};
this._blendFuncStr = "source-over";
this._colorized = false;
this._canUseDirtyRegion = true;
this._textureToRender = null;
};
var proto = cc.Sprite.CanvasRenderCmd.prototype = Object.create(cc.Node.CanvasRenderCmd.prototype);
proto.constructor = cc.Sprite.CanvasRenderCmd;
proto.setDirtyRecursively = function (value) {};
proto._setTexture = function (texture) {
var node = this._node;
if (node._texture !== texture) {
if (texture) {
node._textureLoaded = texture._textureLoaded;
}else{
node._textureLoaded = false;
}
node._texture = texture;
this._updateColor();
}
};
proto._setColorDirty = function () {
this.setDirtyFlag(cc.Node._dirtyFlags.colorDirty | cc.Node._dirtyFlags.opacityDirty);
};
proto.isFrameDisplayed = function (frame) { //TODO there maybe has a bug
var node = this._node;
if (frame.getTexture() !== node._texture)
return false;
return cc.rectEqualToRect(frame.getRect(), node._rect);
};
proto.updateBlendFunc = function (blendFunc) {
this._blendFuncStr = cc.Node.CanvasRenderCmd._getCompositeOperationByBlendFunc(blendFunc);
};
proto._setBatchNodeForAddChild = function (child) {
return true;
};
proto._handleTextureForRotatedTexture = function (texture, rect, rotated, counterclockwise) {
if (rotated && texture.isLoaded()) {
var tempElement = texture.getHtmlElementObj();
tempElement = cc.Sprite.CanvasRenderCmd._cutRotateImageToCanvas(tempElement, rect, counterclockwise);
var tempTexture = new cc.Texture2D();
tempTexture.initWithElement(tempElement);
tempTexture.handleLoadedTexture();
texture = tempTexture;
rect.x = rect.y = 0;
this._node._rect = cc.rect(0, 0, rect.width, rect.height);
}
return texture;
};
proto._checkTextureBoundary = function (texture, rect, rotated) {
if (texture && texture.url) {
var _x = rect.x + rect.width, _y = rect.y + rect.height;
if (_x > texture.width)
cc.error(cc._LogInfos.RectWidth, texture.url);
if (_y > texture.height)
cc.error(cc._LogInfos.RectHeight, texture.url);
}
};
proto.rendering = function (ctx, scaleX, scaleY) {
var node = this._node;
var locTextureCoord = this._textureCoord, alpha = (this._displayedOpacity / 255);
var texture = this._textureToRender || node._texture;
if ((texture && (locTextureCoord.width === 0 || locTextureCoord.height === 0|| !texture._textureLoaded)) || alpha === 0)
return;
var wrapper = ctx || cc._renderContext, context = wrapper.getContext();
var locX = node._offsetPosition.x, locHeight = node._rect.height, locWidth = node._rect.width,
locY = -node._offsetPosition.y - locHeight, image;
wrapper.setTransform(this._worldTransform, scaleX, scaleY);
wrapper.setCompositeOperation(this._blendFuncStr);
wrapper.setGlobalAlpha(alpha);
if(node._flippedX || node._flippedY)
wrapper.save();
if (node._flippedX) {
locX = -locX - locWidth;
context.scale(-1, 1);
}
if (node._flippedY) {
locY = node._offsetPosition.y;
context.scale(1, -1);
}
var sx, sy, sw, sh, x, y, w, h;
if (this._colorized) {
sx = 0;
sy = 0;
}else{
sx = locTextureCoord.renderX;
sy = locTextureCoord.renderY;
}
sw = locTextureCoord.width;
sh = locTextureCoord.height;
x = locX;
y = locY;
w = locWidth;
h = locHeight;
if (texture && texture._htmlElementObj) {
image = texture._htmlElementObj;
if (texture._pattern !== "") {
wrapper.setFillStyle(context.createPattern(image, texture._pattern));
context.fillRect(x, y, w, h);
} else {
context.drawImage(image,
sx, sy, sw, sh,
x, y, w, h);
}
} else {
var contentSize = node._contentSize;
if (locTextureCoord.validRect) {
var curColor = this._displayedColor;
wrapper.setFillStyle("rgba(" + curColor.r + "," + curColor.g + "," + curColor.b + ",1)");
context.fillRect(x, y, contentSize.width * scaleX, contentSize.height * scaleY);
}
}
if(node._flippedX || node._flippedY)
wrapper.restore();
cc.g_NumberOfDraws++;
};
proto._updateColor = function(){
var node = this._node;
var texture = node._texture, rect = this._textureCoord;
var dColor = this._displayedColor;
if(texture){
if(dColor.r !== 255 || dColor.g !== 255 || dColor.b !== 255){
this._textureToRender = texture._generateColorTexture(dColor.r, dColor.g, dColor.b, rect);
this._colorized = true;
}else if(texture){
this._textureToRender = texture;
this._colorized = false;
}
}
};
proto._textureLoadedCallback = function (sender) {
var node = this;
if (node._textureLoaded)
return;
node._textureLoaded = true;
var locRect = node._rect, locRenderCmd = this._renderCmd;
if (!locRect) {
locRect = cc.rect(0, 0, sender.width, sender.height);
} else if (cc._rectEqualToZero(locRect)) {
locRect.width = sender.width;
locRect.height = sender.height;
}
node.texture = sender;
node.setTextureRect(locRect, node._rectRotated);
//set the texture's color after the it loaded
var locColor = locRenderCmd._displayedColor;
if (locColor.r !== 255 || locColor.g !== 255 || locColor.b !== 255)
locRenderCmd._updateColor();
// by default use "Self Render".
// if the sprite is added to a batchnode, then it will automatically switch to "batchnode Render"
node.setBatchNode(node._batchNode);
node.dispatchEvent("load");
};
proto._setTextureCoords = function (rect, needConvert) {
if (needConvert === undefined)
needConvert = true;
var locTextureRect = this._textureCoord,
scaleFactor = needConvert ? cc.contentScaleFactor() : 1;
locTextureRect.renderX = locTextureRect.x = 0 | (rect.x * scaleFactor);
locTextureRect.renderY = locTextureRect.y = 0 | (rect.y * scaleFactor);
locTextureRect.width = 0 | (rect.width * scaleFactor);
locTextureRect.height = 0 | (rect.height * scaleFactor);
locTextureRect.validRect = !(locTextureRect.width === 0 || locTextureRect.height === 0 || locTextureRect.x < 0 || locTextureRect.y < 0);
};
cc.Sprite.CanvasRenderCmd._cutRotateImageToCanvas = function (texture, rect, counterclockwise) {
if (!texture)
return null;
if (!rect)
return texture;
counterclockwise = counterclockwise == null? true: counterclockwise; // texture package is counterclockwise, spine is clockwise
var nCanvas = document.createElement("canvas");
nCanvas.width = rect.width;
nCanvas.height = rect.height;
var ctx = nCanvas.getContext("2d");
ctx.translate(nCanvas.width / 2, nCanvas.height / 2);
if(counterclockwise)
ctx.rotate(-1.5707963267948966);
else
ctx.rotate(1.5707963267948966);
ctx.drawImage(texture, rect.x, rect.y, rect.height, rect.width, -rect.height / 2, -rect.width / 2, rect.height, rect.width);
return nCanvas;
};
})();