From cab0fc0f2c1f822afa7384b7071d038f331b2396 Mon Sep 17 00:00:00 2001 From: "Huabing.Xu" Date: Thu, 30 Jun 2016 15:00:45 +0800 Subject: [PATCH] simplify clipping Node, support nest in canvas --- .../CCClippingNodeCanvasRenderCmd.js | 113 +++++------------- 1 file changed, 28 insertions(+), 85 deletions(-) diff --git a/cocos2d/clipping-nodes/CCClippingNodeCanvasRenderCmd.js b/cocos2d/clipping-nodes/CCClippingNodeCanvasRenderCmd.js index efd7ee1257..834b1f8ec5 100644 --- a/cocos2d/clipping-nodes/CCClippingNodeCanvasRenderCmd.js +++ b/cocos2d/clipping-nodes/CCClippingNodeCanvasRenderCmd.js @@ -27,11 +27,9 @@ cc.ClippingNode.CanvasRenderCmd = function(renderable){ _ccsg.Node.CanvasRenderCmd.call(this, renderable); this._needDraw = false; - this._godhelpme = false; this._clipElemType = false; - this._rendererSaveCmd = new cc.CustomRenderCmd(this, this._saveCmdCallback); - this._rendererClipCmd = new cc.CustomRenderCmd(this, this._clipCmdCallback); + this._rendererClipCmd = new cc.CustomRenderCmd(this, this._drawStencilCommand); this._rendererRestoreCmd = new cc.CustomRenderCmd(this, this._restoreCmdCallback); }; var proto = cc.ClippingNode.CanvasRenderCmd.prototype = Object.create(_ccsg.Node.CanvasRenderCmd.prototype); @@ -75,34 +73,8 @@ proto.setStencil = function(stencil){ } }; }else{ - stencil._parent = this._node; - } -}; - -proto._saveCmdCallback = function(ctx, scaleX, scaleY) { - var wrapper = ctx || cc._renderContext, context = wrapper.getContext(); - - if (this._clipElemType) { - context.save(); - context.globalAlpha = this._displayedOpacity/ 255; - var locCache = cc.ClippingNode.CanvasRenderCmd._getSharedCache(); - var canvas = context.canvas; - if (locCache.width !== canvas.width) - locCache.width = canvas.width; - if (locCache.height !== canvas.height) - locCache.height = canvas.height; //note: on some browser, it can't clear the canvas, e.g. baidu - var locCacheCtx = locCache.getContext("2d"); - locCacheCtx.drawImage(canvas, 0, 0); //save the result to shareCache canvas - } else { - wrapper.save(); - context.beginPath(); //save for clip - //Because drawNode's content size is zero - wrapper.setTransform(this._worldTransform, scaleX, scaleY); - - if (this._node.inverted) { - context.rect(0, 0, context.canvas.width, -context.canvas.height); - context.clip(); - } + cc.error('do not support other type as stencil'); + //stencil._parent = this._node; } }; @@ -121,33 +93,10 @@ proto._setStencilCompositionOperation = function(stencil){ } }; -proto._clipCmdCallback = function(ctx) { - var node = this._node; - var wrapper = ctx || cc._renderContext, context = wrapper.getContext(); - - if (this._clipElemType) { - //hack - this._setStencilCompositionOperation(node._stencil); - } else { - context.clip(); - } -}; - proto._restoreCmdCallback = function (ctx) { var locCache = cc.ClippingNode.CanvasRenderCmd._getSharedCache(); var wrapper = ctx || cc._renderContext, context = wrapper.getContext(); - if (this._clipElemType) { - // Redraw the cached canvas, so that the clipped area shows the background etc. - context.save(); - context.setTransform(1, 0, 0, 1, 0, 0); - context.globalCompositeOperation = "destination-over"; - context.drawImage(locCache, 0, 0); - context.restore(); - this._dirtyFlag = 0; - context.restore(); - } else { - wrapper.restore(); //use for restore clip operation - } + wrapper.restore(); }; proto.transform = function(parentCmd, recursive){ @@ -157,10 +106,24 @@ proto.transform = function(parentCmd, recursive){ node._stencil._renderCmd.transform(this, recursive); }; -proto._cangodhelpme = function (godhelpme) { - if (godhelpme === true || godhelpme === false) - cc.ClippingNode.CanvasRenderCmd.prototype._godhelpme = godhelpme; - return cc.ClippingNode.CanvasRenderCmd.prototype._godhelpme; +proto._drawStencilCommand = function (ctx, scaleX, scaleY) { + var wrapper = ctx || cc._renderContext, context = wrapper.getContext(); + wrapper.save(); + context.beginPath(); //save for clip + wrapper.setTransform(this._worldTransform, scaleX, scaleY); + + //draw elements + var stencilBuffer = this._node._stencil._buffer; + for(var index = 0; index < stencilBuffer.length; ++index) { + var vertices = stencilBuffer[index].verts; + if(vertices.length < 3) continue; + context.moveTo(vertices[0].x * scaleX, -vertices[0].y * scaleY); + for(var vIndex = 1; vIndex < vertices.length; ++vIndex) { + context.lineTo(vertices[vIndex].x * scaleX, -vertices[vIndex].y * scaleY); + } + } + //end draw elements + context.clip(); }; proto.visit = function(parentCmd){ @@ -173,43 +136,23 @@ proto.visit = function(parentCmd){ parentCmd = parentCmd || this.getParentRenderCmd(); if( parentCmd) this._curLevel = parentCmd._curLevel + 1; - var transformRenderCmd = this; - - // Composition mode, costy but support texture stencil - this._clipElemType = !(!this._cangodhelpme() && node._stencil instanceof cc.DrawNode); - if (!node._stencil || !node._stencil.visible) { - if (this.inverted) - _ccsg.Node.CanvasRenderCmd.prototype.visit.call(this, parentCmd); // draw everything - return; - } this._syncStatus(parentCmd); - cc.renderer.pushRenderCommand(this._rendererSaveCmd); - if(this._clipElemType){ - // Draw everything first using node visit function - _ccsg.Node.CanvasRenderCmd.prototype.visit.call(this, parentCmd); - }else{ - node._stencil.visit(this); + if(this._node._stencil) { + cc.renderer.pushRenderCommand(this._rendererClipCmd); } - cc.renderer.pushRenderCommand(this._rendererClipCmd); - - if(this._clipElemType){ - node._stencil.visit(transformRenderCmd); - }else{ var i, children = node._children; - // Clip mode doesn't support recursive stencil, so once we used a clip stencil, - // so if it has ClippingNode as a child, the child must uses composition stencil. - this._cangodhelpme(true); + var len = children.length; if (len > 0) { node.sortAllChildren(); for (i = 0; i < len; i++) children[i]._renderCmd.visit(this); } - this._cangodhelpme(false); - } - cc.renderer.pushRenderCommand(this._rendererRestoreCmd); + if(this._node._stencil) { + cc.renderer.pushRenderCommand(this._rendererRestoreCmd); + } this._dirtyFlag = 0; };