1
+ /****************************************************************************
2
+ Copyright (c) 2013-2014 Chukong Technologies Inc.
3
+
4
+ http://www.cocos2d-x.org
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in
14
+ all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ THE SOFTWARE.
23
+ ****************************************************************************/
24
+
25
+ cc . ClippingNode . CanvasSaveRenderCmd = function ( renderableObject ) {
26
+ cc . Node . CanvasRenderCmd . call ( this , renderableObject ) ;
27
+ this . _needDraw = true ;
28
+ } ;
29
+
30
+ cc . ClippingNode . CanvasSaveRenderCmd . prototype . rendering = function ( ctx , scaleX , scaleY ) {
31
+ var node = this . _node ;
32
+ var context = ctx || cc . _renderContext ;
33
+
34
+ if ( node . _clipElemType ) {
35
+ var locCache = cc . ClippingNode . _getSharedCache ( ) ;
36
+ var canvas = context . canvas ;
37
+ locCache . width = canvas . width ;
38
+ locCache . height = canvas . height ;
39
+ var locCacheCtx = locCache . getContext ( "2d" ) ;
40
+ locCacheCtx . drawImage ( canvas , 0 , 0 ) ;
41
+ context . save ( ) ;
42
+ } else {
43
+ node . transform ( ) ;
44
+ var t = node . _transformWorld ;
45
+ context . save ( ) ;
46
+ context . save ( ) ;
47
+ context . transform ( t . a , t . c , t . b , t . d , t . tx * scaleX , - t . ty * scaleY ) ;
48
+ }
49
+ } ;
50
+
51
+ cc . ClippingNode . CanvasSaveRenderCmd . prototype = Object . create ( cc . Node . CanvasRenderCmd . prototype ) ;
52
+ cc . ClippingNode . CanvasSaveRenderCmd . prototype . constructor = cc . ClippingNode . CanvasSaveRenderCmd ;
53
+
54
+ cc . ClippingNode . CanvasClipRenderCmd = function ( renderableObject ) {
55
+ cc . Node . CanvasRenderCmd . call ( this , renderableObject ) ;
56
+ this . _needDraw = true ;
57
+ } ;
58
+
59
+ cc . ClippingNode . CanvasClipRenderCmd . prototype . rendering = function ( ctx , scaleX , scaleY ) {
60
+ var node = this . _node ;
61
+ var context = ctx || cc . _renderContext ;
62
+
63
+ if ( node . _clipElemType ) {
64
+ context . globalCompositeOperation = node . inverted ? "destination-out" : "destination-in" ;
65
+ var t = node . _transformWorld ;
66
+ context . transform ( t . a , t . c , t . b , t . d , t . tx * scaleX , - t . ty * scaleY ) ;
67
+ } else {
68
+ context . restore ( ) ;
69
+ if ( node . inverted ) {
70
+ var canvas = context . canvas ;
71
+ context . save ( ) ;
72
+
73
+ context . setTransform ( 1 , 0 , 0 , 1 , 0 , 0 ) ;
74
+
75
+ context . moveTo ( 0 , 0 ) ;
76
+ context . lineTo ( 0 , canvas . height ) ;
77
+ context . lineTo ( canvas . width , canvas . height ) ;
78
+ context . lineTo ( canvas . width , 0 ) ;
79
+ context . lineTo ( 0 , 0 ) ;
80
+
81
+ context . restore ( ) ;
82
+ }
83
+ context . clip ( ) ;
84
+ }
85
+ } ;
86
+
87
+ cc . ClippingNode . CanvasClipRenderCmd . prototype = Object . create ( cc . Node . CanvasRenderCmd . prototype ) ;
88
+ cc . ClippingNode . CanvasClipRenderCmd . prototype . constructor = cc . ClippingNode . CanvasClipRenderCmd ;
89
+
90
+ cc . ClippingNode . CanvasRestoreRenderCmd = function ( renderableObject ) {
91
+ cc . Node . CanvasRenderCmd . call ( this , renderableObject ) ;
92
+ this . _needDraw = true ;
93
+ } ;
94
+
95
+ cc . ClippingNode . CanvasRestoreRenderCmd . prototype . rendering = function ( ctx , scaleX , scaleY ) {
96
+ var node = this . _node ;
97
+ var locCache = cc . ClippingNode . _getSharedCache ( ) ;
98
+ var context = ctx || cc . _renderContext ;
99
+ if ( node . _clipElemType ) {
100
+ context . restore ( ) ;
101
+
102
+ // Redraw the cached canvas, so that the cliped area shows the background etc.
103
+ context . save ( ) ;
104
+ context . setTransform ( 1 , 0 , 0 , 1 , 0 , 0 ) ;
105
+ context . globalCompositeOperation = "destination-over" ;
106
+ context . drawImage ( locCache , 0 , 0 ) ;
107
+ context . restore ( ) ;
108
+ } else {
109
+ context . restore ( ) ;
110
+ }
111
+ } ;
112
+
113
+ cc . ClippingNode . CanvasRestoreRenderCmd . prototype = Object . create ( cc . Node . CanvasRenderCmd . prototype ) ;
114
+ cc . ClippingNode . CanvasRestoreRenderCmd . prototype . constructor = cc . ClippingNode . CanvasRestoreRenderCmd ;
115
+
116
+ cc . ClippingNode . WebGLRenderCmd = function ( renderableObject , callback ) {
117
+ cc . Node . WebGLRenderCmd . call ( this , renderableObject ) ;
118
+ this . _needDraw = true ;
119
+ this . _callback = callback ;
120
+ } ;
121
+
122
+ cc . ClippingNode . WebGLRenderCmd . prototype = Object . create ( cc . Node . CanvasRenderCmd . prototype ) ;
123
+ cc . ClippingNode . WebGLRenderCmd . prototype . constructor = cc . ClippingNode . WebGLRenderCmd ;
124
+
125
+ cc . ClippingNode . WebGLRenderCmd . prototype . rendering = function ( ctx ) {
126
+ if ( ! this . _callback )
127
+ return ;
128
+ this . _callback . call ( this . _node , ctx ) ;
129
+ } ;
0 commit comments