support canvas graphics

This commit is contained in:
yinkaile
2018-04-02 14:40:30 +08:00
parent 687f9a77dc
commit ac3d195f6d
4 changed files with 207 additions and 3 deletions
+6
View File
@@ -72,6 +72,7 @@ let Graphics = cc.Class({
},
set (value) {
this._lineWidth = value;
this._impl.lineWidth = value;
}
},
@@ -89,6 +90,7 @@ let Graphics = cc.Class({
},
set (value) {
this._lineJoin = value;
this._impl.lineJoin = value;
},
type: LineJoin
},
@@ -107,6 +109,7 @@ let Graphics = cc.Class({
},
set (value) {
this._lineCap = value;
this._impl.lineCap = value;
},
type: LineCap
},
@@ -125,6 +128,7 @@ let Graphics = cc.Class({
},
set (value) {
this._strokeColor = cc.color(value);
this._impl.strokeColor = value;
}
},
@@ -142,6 +146,7 @@ let Graphics = cc.Class({
},
set (value) {
this._fillColor = cc.color(value);
this._impl.fillColor = value;
}
},
@@ -159,6 +164,7 @@ let Graphics = cc.Class({
},
set (value) {
this._miterLimit = value;
this._impl.miterLimit = value;
}
}
},
@@ -0,0 +1,131 @@
const Helper = require('../../../../graphics/helper');
const Types = require('../../../../graphics/types');
const PointFlags = Types.PointFlags;
const LineJoin = Types.LineJoin;
const LineCap = Types.LineCap;
class Impl {
constructor () {
this.cmds = [];
this.style = {
strokeStyle: 'black',
fillStyle: 'white',
lineCap: 'butt',
lineJoin: 'miter',
miterLimit: 10
};
}
set strokeColor (v) {
var strokeStyle = 'rgba(' + (0 | v.r) + ',' + (0 | v.g) + ',' + (0 | v.b) + ',' + v.a / 255 + ')';
this.cmds.push(['strokeStyle', strokeStyle]);
this.style.strokeStyle = strokeStyle;
}
set fillColor (v) {
var fillStyle = 'rgba(' + (0 | v.r) + ',' + (0 | v.g) + ',' + (0 | v.b) + ',' + v.a / 255 + ')';
this.cmds.push(['fillStyle', fillStyle]);
this.style.fillStyle = fillStyle;
}
set lineWidth (v) {
this.cmds.push(['lineWidth', v]);
this.style.lineWidth = v;
}
set lineCap (v) {
var lineCap = 'butt';
if (v === LineCap.BUTT) {
lineCap = 'butt';
}
else if (v === LineCap.ROUND) {
lineCap = 'round';
}
else if (v === LineCap.SQUARE) {
lineCap = 'square';
}
this.cmds.push(['lineCap', lineCap]);
this.style.lineCap = lineCap;
}
set lineJoin (v) {
var lineJoin = 'bevel';
if (v === LineJoin.BEVEL) {
lineJoin = 'bevel';
}
else if (v === LineJoin.ROUND) {
lineJoin = 'round';
}
else if (v === LineJoin.MITER) {
lineJoin = 'miter';
}
this.cmds.push(['lineJoin', lineJoin]);
this.style.lineJoin = lineJoin;
}
set miterLimit (v) {
this.cmds.push(['miterLimit', v]);
this.style.miterLimit = v;
}
moveTo (x, y) {
this.cmds.push(['moveTo', [x, y]]);
}
lineTo (x, y) {
this.cmds.push(['lineTo', [x, y]]);
}
bezierCurveTo (c1x, c1y, c2x, c2y, x, y) {
this.cmds.push(['bezierCurveTo', [c1x, c1y, c2x, c2y, x, y]]);
}
quadraticCurveTo (cx, cy, x, y) {
this.cmds.push(['quadraticCurveTo', [cx, cy, x, y]]);
}
arc (cx, cy, r, startAngle, endAngle, counterclockwise) {
Helper.arc(this, cx, cy, r, startAngle, endAngle, counterclockwise);
}
ellipse (cx, cy, rx, ry) {
Helper.ellipse(this, cx, cy, rx, ry);
}
circle (cx, cy, r) {
Helper.ellipse(this, cx, cy, r, r);
}
rect (x, y, w, h) {
this.moveTo(x, y);
this.lineTo(x, y + h);
this.lineTo(x + w, y + h);
this.lineTo(x + w, y);
this.close();
}
roundRect (x, y, w, h, r) {
Helper.roundRect(this, x, y, w, h, r);
}
clear (comp, clean) {
this.cmds.length = 0;
}
close () {
this.cmds.push(['closePath', []]);
}
stroke () {
this.cmds.push(['stroke', []]);
}
fill () {
this.cmds.push(['fill', []]);
}
}
module.exports = Impl;
@@ -0,0 +1,65 @@
const Impl = require('./impl');
module.exports = {
createImpl () {
return new Impl();
},
draw (ctx, comp) {
let node = comp.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.transform(a, b, c, d, tx, -ty);
ctx.save();
ctx.scale(1, -1);
// TODO: handle blend function
// opacity
ctx.glphaAlpha = node.opacity / 255;
let style = comp._impl.style;
ctx.strokeStyle = style.strokeStyle;
ctx.fillStyle = style.fillStyle;
ctx.lineWidth = style.lineWidth;
ctx.lineJoin = style.lineJoin;
ctx.miterLimit = style.miterLimit;
let endPath = true;
let cmds = comp._impl.cmds;
for (let i = 0, l = cmds.length; i < l; i++) {
let cmd = cmds[i];
let ctxCmd = cmd[0], args = cmd[1];
if (ctxCmd === 'moveTo' && endPath) {
ctx.beginPath();
endPath = false;
}
else if (ctxCmd === 'fill' || ctxCmd === 'stroke' || ctxCmd === 'fillRect') {
endPath = true;
}
if (typeof ctx[ctxCmd] === 'function') {
ctx[ctxCmd].apply(ctx, args);
}
else {
ctx[ctxCmd] = args;
}
}
ctx.restore();
return 1;
},
stroke (comp) {
comp._impl.stroke();
},
fill (comp) {
comp._impl.fill();
}
}
@@ -30,14 +30,15 @@ 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');
let Graphics = require('../../../graphics/graphics');
// let ParticleSystem = require('../../../../particle/CCParticleSystem');
// let TiledLayer = require('../../../../tilemap/CCTiledLayer');
// let Skeleton = require('../../../../../extensions/spine/Skeleton');
// let Armature = require('../../../../../extensions/dragonbones/ArmatureDisplay');
let spriteRenderer = require('./sprite/');
let labelRenderer = require('./label/');
let spriteRenderer = require('./sprite');
let labelRenderer = require('./label');
let graphicsRenderer = require('./graphics');
let map = {};
let postMap = {};
@@ -56,6 +57,7 @@ addRenderer(Sprite, spriteRenderer);
addRenderer(Label, labelRenderer);
addRenderer(Mask, null);
addRenderer(RichText, null);
addRenderer(Graphics, graphicsRenderer);
module.exports = {
map,