Add Mask canvas renderer

This commit is contained in:
pandamicro
2018-04-07 18:54:49 +08:00
parent 368b13071b
commit 8f7e5618af
2 changed files with 100 additions and 2 deletions
@@ -29,8 +29,9 @@ let Sprite = require('../../../components/CCSprite');
let Label = require('../../../components/CCLabel');
let Mask = require('../../../components/CCMask');
let RichText = require('../../../components/CCRichText');
// the following should be placed into their own module folder
let Graphics = require('../../../graphics/graphics');
// the following should be placed into their own module folder
// let ParticleSystem = require('../../../../particle/CCParticleSystem');
// let TiledLayer = require('../../../../tilemap/CCTiledLayer');
// let Skeleton = require('../../../../../extensions/spine/Skeleton');
@@ -39,6 +40,7 @@ let Graphics = require('../../../graphics/graphics');
let spriteRenderer = require('./sprite');
let labelRenderer = require('./label');
let graphicsRenderer = require('./graphics');
let maskRenderer = require('./mask');
let map = {};
let postMap = {};
@@ -55,7 +57,7 @@ function addRenderer (Component, handler, postHandler) {
addRenderer(Sprite, spriteRenderer);
addRenderer(Label, labelRenderer);
addRenderer(Mask, null);
addRenderer(Mask, maskRenderer.beforeHandler, maskRenderer.afterHandler);
addRenderer(RichText, null);
addRenderer(Graphics, graphicsRenderer);
@@ -0,0 +1,96 @@
/****************************************************************************
Copyright (c) 2018 Xiamen Yaji Software Co., Ltd.
http://www.cocos.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated engine source code (the "Software"), a limited,
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
to use Cocos Creator solely to develop games on your target platforms. You shall
not use Cocos Creator software for developing other software or tools that's
used for developing games. You are not granted to publish, distribute,
sublicense, and/or sell copies of Cocos Creator.
The software or tools in this License Agreement are licensed, not sold.
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
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.
****************************************************************************/
const Node = require('../../../CCNode');
const Mask = require('../../../components/CCMask');
const Graphics = require('../../../graphics/graphics');
const graphicsHandler = require('./graphics');
// for nested mask, we might need multiple graphics component to avoid data conflict
let _graphicsPool = [];
function getGraphics () {
let graphics = _graphicsPool.pop();
if (!graphics) {
let graphicsNode = new Node();
graphics = graphicsNode.addComponent(Graphics);
graphics.lineWidth = 0;
}
return graphics;
}
let beforeHandler = {
updateGraphics (mask) {
let node = mask.node;
let graphics = mask._graphics;
// Share render data with graphics content
graphics.clear(false);
let width = node.width;
let height = node.height;
let x = -width * node.anchorX;
let y = -height * node.anchorY;
if (mask._type === Mask.Type.RECT) {
graphics.rect(x, y, width, height);
}
else if (mask._type === Mask.Type.ELLIPSE) {
let cx = x + width / 2,
cy = y + height / 2,
rx = width / 2,
ry = height / 2;
graphics.ellipse(cx, cy, rx, ry);
}
graphics.stroke();
},
draw (ctx, mask) {
let node = mask.node;
// Transform
let matrix = node._worldMatrix;
let a = matrix.m00, b = matrix.m01, c = matrix.m04, d = matrix.m05,
tx = matrix.m12, ty = matrix.m13;
ctx.save();
ctx.transform(a, b, c, d, tx, -ty);
// draw stencil
mask._graphics = getGraphics();
this.updateGraphics(mask);
graphicsHandler.draw(ctx, mask._graphics);
ctx.clip();
}
};
let afterHandler = {
draw (ctx, mask) {
ctx.restore();
}
};
module.exports = {
beforeHandler,
afterHandler
};