Merge pull request #1035 from 2youyou2/graphics

fixed graphics arc
This commit is contained in:
Wang Nan
2016-08-16 10:54:06 +08:00
committed by GitHub
3 changed files with 116 additions and 183 deletions
+8 -87
View File
@@ -27,16 +27,7 @@ var Js = cc.js;
var LineCap = require('./types').LineCap;
var LineJoin = require('./types').LineJoin;
var PI = 3.14159265358979323846264338327;
var min = Math.min;
var max = Math.max;
var cos = Math.cos;
var sin = Math.sin;
var abs = Math.abs;
var sign = Math.sign;
var KAPPA90 = 0.5522847493;
var Helper = require('./helper');
var CanvasRenderCmd = function (renderable) {
_ccsg.Node.CanvasRenderCmd.call(this, renderable);
@@ -94,12 +85,12 @@ _p.rendering = function (ctx, scaleX, scaleY) {
// draw api
Js.mixin(_p, {
setStrokeColor: function (v) {
var fillStyle = "rgba(" + (0 | v.r) + "," + (0 | v.g) + "," + (0 | v.b) + "," + v.a / 255 + ")";
var fillStyle = 'rgba(' + (0 | v.r) + ',' + (0 | v.g) + ',' + (0 | v.b) + ',' + v.a / 255 + ')';
this.cmds.push(['strokeStyle', fillStyle]);
},
setFillColor: function (v) {
var fillStyle = "rgba(" + (0 | v.r) + "," + (0 | v.g) + "," + (0 | v.b) + "," + v.a / 255 + ")";
var fillStyle = 'rgba(' + (0 | v.r) + ',' + (0 | v.g) + ',' + (0 | v.b) + ',' + v.a / 255 + ')';
this.cmds.push(['fillStyle', fillStyle]);
},
@@ -157,69 +148,16 @@ Js.mixin(_p, {
},
//
arc: function (cx, cy, r, a0, a1, counterclockwise) {
// this.cmds.push(['arc', arguments]);
var a = 0, da = 0, hda = 0, kappa = 0;
var dx = 0, dy = 0, x = 0, y = 0, tanx = 0, tany = 0;
var px = 0, py = 0, ptanx = 0, ptany = 0;
var i, ndivs;
// Clamp angles
da = a1 - a0;
if (!counterclockwise) {
if (abs(da) >= PI * 2) {
da = PI * 2;
} else {
while (da < 0) da += PI * 2;
}
} else {
if (abs(da) >= PI * 2) {
da = -PI * 2;
} else {
while (da > 0) da -= PI * 2;
}
}
// Split arc into max 90 degree segments.
ndivs = max(1, min(abs(da) / (PI * 0.5) + 0.5, 5)) | 0;
hda = da / ndivs / 2.0;
kappa = abs(4.0 / 3.0 * (1 - cos(hda)) / sin(hda));
if (counterclockwise) kappa = -kappa;
for (i = 0; i <= ndivs; i++) {
a = a0 + da * (i / ndivs);
dx = cos(a);
dy = sin(a);
x = cx + dx * r;
y = cy + dy * r;
tanx = -dy * r * kappa;
tany = dx * r * kappa;
if (i === 0) {
this.moveTo(x, y);
} else {
this.bezierCurveTo(px + ptanx, py + ptany, x - tanx, y - tany, x, y);
}
px = x;
py = y;
ptanx = tanx;
ptany = tany;
}
arc: function (cx, cy, r, startAngle, endAngle, counterclockwise) {
Helper.arc(this, cx, cy, r, startAngle, endAngle, counterclockwise);
},
ellipse: function (cx, cy, rx, ry) {
this.moveTo(cx - rx, cy);
this.bezierCurveTo(cx - rx, cy + ry * KAPPA90, cx - rx * KAPPA90, cy + ry, cx, cy + ry);
this.bezierCurveTo(cx + rx * KAPPA90, cy + ry, cx + rx, cy + ry * KAPPA90, cx + rx, cy);
this.bezierCurveTo(cx + rx, cy - ry * KAPPA90, cx + rx * KAPPA90, cy - ry, cx, cy - ry);
this.bezierCurveTo(cx - rx * KAPPA90, cy - ry, cx - rx, cy - ry * KAPPA90, cx - rx, cy);
this.close();
Helper.ellipse(this, cx, cy, rx, ry);
},
circle: function (cx, cy, r) {
this.ellipse(cx, cy, r, r);
this.close();
Helper.ellipse(this, cx, cy, r, r);
},
rect: function (x, y, w, h) {
@@ -231,24 +169,7 @@ Js.mixin(_p, {
},
roundRect: function (x, y, w, h, r) {
if (r < 0.1) {
this.rect(x, y, w, h);
return;
} else {
var rx = min(r, abs(w) * 0.5) * sign(w),
ry = min(r, abs(h) * 0.5) * sign(h);
this.moveTo(x, y + ry);
this.lineTo(x, y + h - ry);
this.bezierCurveTo(x, y + h - ry * (1 - KAPPA90), x + rx * (1 - KAPPA90), y + h, x + rx, y + h);
this.lineTo(x + w - rx, y + h);
this.bezierCurveTo(x + w - rx * (1 - KAPPA90), y + h, x + w, y + h - ry * (1 - KAPPA90), x + w, y + h - ry);
this.lineTo(x + w, y + ry);
this.bezierCurveTo(x + w, y + ry * (1 - KAPPA90), x + w - rx * (1 - KAPPA90), y, x + w - rx, y);
this.lineTo(x + rx, y);
this.bezierCurveTo(x + rx * (1 - KAPPA90), y, x, y + ry * (1 - KAPPA90), x, y + ry);
this.close();
}
Helper.roundRect(this, x, y, w, h, r);
},
fillRect: function (x, y, w, h) {
+9 -96
View File
@@ -30,18 +30,18 @@ var LineJoin = require('./types').LineJoin;
var Earcut = require('./earcut');
var Helper = require('./Helper');
var Vec2 = cc.Vec2;
var Js = cc.js;
// Math
var PI = 3.14159265358979323846264338327;
var PI_2 = PI*2;
var INIT_VERTS_SIZE = 32;
var KAPPA90 = 0.5522847493;
var VERTS_FLOAT_LENGTH = 2;
var VERTS_BYTE_LENGTH = 8;
var PI = Math.PI;
var min = Math.min;
var max = Math.max;
var ceil = Math.ceil;
@@ -50,7 +50,6 @@ var cos = Math.cos;
var sin = Math.sin;
var atan2 = Math.atan2;
var abs = Math.abs;
var sign = Math.sign;
function clamp (v, min, max) {
if (v < min) {
@@ -206,86 +205,17 @@ Js.mixin(_p, {
},
//
arc: function (cx, cy, r, a0, a1, counterclockwise) {
var a = 0, da = 0, hda = 0, kappa = 0;
var dx = 0, dy = 0, x = 0, y = 0, tanx = 0, tany = 0;
var px = 0, py = 0, ptanx = 0, ptany = 0;
var i, ndivs;
// Clamp angles
da = a1 - a0;
if (!counterclockwise) {
if (abs(da) >= PI * 2) {
da = PI * 2;
} else {
while (da < 0) da += PI * 2;
}
} else {
if (abs(da) >= PI * 2) {
da = -PI * 2;
} else {
while (da > 0) da -= PI * 2;
}
}
// Split arc into max 90 degree segments.
ndivs = max(1, min(abs(da) / (PI * 0.5) + 0.5, 5)) | 0;
hda = da / ndivs / 2.0;
kappa = abs(4.0 / 3.0 * (1 - cos(hda)) / sin(hda));
if (counterclockwise) kappa = -kappa;
for (i = 0; i <= ndivs; i++) {
a = a0 + da * (i / ndivs);
dx = cos(a);
dy = sin(a);
x = cx + dx * r;
y = cy + dy * r;
tanx = -dy * r * kappa;
tany = dx * r * kappa;
if (i === 0) {
this.moveTo(x, y);
} else {
this.bezierCurveTo(px + ptanx, py + ptany, x - tanx, y - tany, x, y);
}
px = x;
py = y;
ptanx = tanx;
ptany = tany;
}
// this.close();
// this._curPath.complex = false;
arc: function (cx, cy, r, startAngle, endAngle, counterclockwise) {
Helper.arc(this, cx, cy, r, startAngle, endAngle, counterclockwise);
},
ellipse: function (cx, cy, rx, ry) {
this.moveTo(cx - rx, cy);
this.bezierCurveTo(cx - rx, cy + ry * KAPPA90, cx - rx * KAPPA90, cy + ry, cx, cy + ry);
this.bezierCurveTo(cx + rx * KAPPA90, cy + ry, cx + rx, cy + ry * KAPPA90, cx + rx, cy);
this.bezierCurveTo(cx + rx, cy - ry * KAPPA90, cx + rx * KAPPA90, cy - ry, cx, cy - ry);
this.bezierCurveTo(cx - rx * KAPPA90, cy - ry, cx - rx, cy - ry * KAPPA90, cx - rx, cy);
this.close();
Helper.ellipse(this, cx, cy, rx, ry);
this._curPath.complex = false;
},
circle: function (cx, cy, r) {
// this.ellipse(cx, cy, r, r);
var divs = this._curveDivs(r, PI_2, this._tessTol);
var step = PI_2 / divs;
this.moveTo(cx, cy + r);
for (var i = 1; i < divs; i++) {
var angle = step * i;
var x = r * sin(angle);
var y = r * cos(angle);
this.lineTo(cx + x, cy + y);
}
this.close();
Helper.ellipse(this, cx, cy, r, r);
this._curPath.complex = false;
},
@@ -299,25 +229,8 @@ Js.mixin(_p, {
},
roundRect: function (x, y, w, h, r) {
if (r < 0.1) {
this.rect(x, y, w, h);
return;
} else {
var rx = min(r, abs(w) * 0.5) * sign(w),
ry = min(r, abs(h) * 0.5) * sign(h);
this.moveTo(x, y + ry);
this.lineTo(x, y + h - ry);
this.bezierCurveTo(x, y + h - ry * (1 - KAPPA90), x + rx * (1 - KAPPA90), y + h, x + rx, y + h);
this.lineTo(x + w - rx, y + h);
this.bezierCurveTo(x + w - rx * (1 - KAPPA90), y + h, x + w, y + h - ry * (1 - KAPPA90), x + w, y + h - ry);
this.lineTo(x + w, y + ry);
this.bezierCurveTo(x + w, y + ry * (1 - KAPPA90), x + w - rx * (1 - KAPPA90), y, x + w - rx, y);
this.lineTo(x + rx, y);
this.bezierCurveTo(x + rx * (1 - KAPPA90), y, x, y + ry * (1 - KAPPA90), x, y + ry);
this.close();
this._curPath.complex = false;
}
Helper.roundRect(this, x, y, w, h, r);
this._curPath.complex = false;
},
fillRect: function (x, y, w, h) {
+99
View File
@@ -0,0 +1,99 @@
var PI = Math.PI;
var min = Math.min;
var max = Math.max;
var cos = Math.cos;
var sin = Math.sin;
var abs = Math.abs;
var sign = Math.sign;
var KAPPA90 = 0.5522847493;
function arc (ctx, cx, cy, r, startAngle, endAngle, counterclockwise) {
counterclockwise = counterclockwise || false;
var a = 0, da = 0, hda = 0, kappa = 0;
var dx = 0, dy = 0, x = 0, y = 0, tanx = 0, tany = 0;
var px = 0, py = 0, ptanx = 0, ptany = 0;
var i, ndivs;
// Clamp angles
da = endAngle - startAngle;
if (counterclockwise) {
if (abs(da) >= PI * 2) {
da = PI * 2;
} else {
while (da < 0) da += PI * 2;
}
} else {
if (abs(da) >= PI * 2) {
da = -PI * 2;
} else {
while (da > 0) da -= PI * 2;
}
}
// Split arc into max 90 degree segments.
ndivs = max(1, min(abs(da) / (PI * 0.5) + 0.5, 5)) | 0;
hda = da / ndivs / 2.0;
kappa = abs(4.0 / 3.0 * (1 - cos(hda)) / sin(hda));
if (!counterclockwise) kappa = -kappa;
for (i = 0; i <= ndivs; i++) {
a = startAngle + da * (i / ndivs);
dx = cos(a);
dy = sin(a);
x = cx + dx * r;
y = cy + dy * r;
tanx = -dy * r * kappa;
tany = dx * r * kappa;
if (i === 0) {
ctx.moveTo(x, y);
} else {
ctx.bezierCurveTo(px + ptanx, py + ptany, x - tanx, y - tany, x, y);
}
px = x;
py = y;
ptanx = tanx;
ptany = tany;
}
}
function ellipse (ctx, cx, cy, rx, ry) {
ctx.moveTo(cx - rx, cy);
ctx.bezierCurveTo(cx - rx, cy + ry * KAPPA90, cx - rx * KAPPA90, cy + ry, cx, cy + ry);
ctx.bezierCurveTo(cx + rx * KAPPA90, cy + ry, cx + rx, cy + ry * KAPPA90, cx + rx, cy);
ctx.bezierCurveTo(cx + rx, cy - ry * KAPPA90, cx + rx * KAPPA90, cy - ry, cx, cy - ry);
ctx.bezierCurveTo(cx - rx * KAPPA90, cy - ry, cx - rx, cy - ry * KAPPA90, cx - rx, cy);
ctx.close();
}
function roundRect (ctx, x, y, w, h, r) {
if (r < 0.1) {
ctx.rect(x, y, w, h);
return;
} else {
var rx = min(r, abs(w) * 0.5) * sign(w),
ry = min(r, abs(h) * 0.5) * sign(h);
ctx.moveTo(x, y + ry);
ctx.lineTo(x, y + h - ry);
ctx.bezierCurveTo(x, y + h - ry * (1 - KAPPA90), x + rx * (1 - KAPPA90), y + h, x + rx, y + h);
ctx.lineTo(x + w - rx, y + h);
ctx.bezierCurveTo(x + w - rx * (1 - KAPPA90), y + h, x + w, y + h - ry * (1 - KAPPA90), x + w, y + h - ry);
ctx.lineTo(x + w, y + ry);
ctx.bezierCurveTo(x + w, y + ry * (1 - KAPPA90), x + w - rx * (1 - KAPPA90), y, x + w - rx, y);
ctx.lineTo(x + rx, y);
ctx.bezierCurveTo(x + rx * (1 - KAPPA90), y, x, y + ry * (1 - KAPPA90), x, y + ry);
ctx.close();
}
}
module.exports = {
arc: arc,
ellipse: ellipse,
roundRect: roundRect,
};