mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-01 15:52:40 +08:00
Merge pull request #856 from dabingnn/simplify_clippingNode
simplify clipping Node, support nest in canvas
This commit is contained in:
@@ -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;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user