From 8605cf4d8f137c7fa7c768e8194f7add7623fb86 Mon Sep 17 00:00:00 2001 From: 2youyouo2 <501251991@qq.com> Date: Mon, 15 Aug 2016 11:45:12 +0800 Subject: [PATCH 1/2] fixed graphics arc --- cocos2d/core/graphics/graphics-canvas-cmd.js | 95 ++--------------- cocos2d/core/graphics/graphics-webgl-cmd.js | 103 ++----------------- cocos2d/core/graphics/helper.js | 100 ++++++++++++++++++ 3 files changed, 116 insertions(+), 182 deletions(-) create mode 100644 cocos2d/core/graphics/helper.js diff --git a/cocos2d/core/graphics/graphics-canvas-cmd.js b/cocos2d/core/graphics/graphics-canvas-cmd.js index 311731717d..3425e3d1e3 100644 --- a/cocos2d/core/graphics/graphics-canvas-cmd.js +++ b/cocos2d/core/graphics/graphics-canvas-cmd.js @@ -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) { diff --git a/cocos2d/core/graphics/graphics-webgl-cmd.js b/cocos2d/core/graphics/graphics-webgl-cmd.js index a8f7f4bf0b..ed72a464e6 100644 --- a/cocos2d/core/graphics/graphics-webgl-cmd.js +++ b/cocos2d/core/graphics/graphics-webgl-cmd.js @@ -30,14 +30,14 @@ 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; @@ -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) { diff --git a/cocos2d/core/graphics/helper.js b/cocos2d/core/graphics/helper.js new file mode 100644 index 0000000000..a073e0348f --- /dev/null +++ b/cocos2d/core/graphics/helper.js @@ -0,0 +1,100 @@ + +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; + +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, +}; From 32f92efa8ecb0debf8a16bfdb09ab54684bce8e9 Mon Sep 17 00:00:00 2001 From: 2youyouo2 <501251991@qq.com> Date: Mon, 15 Aug 2016 14:02:51 +0800 Subject: [PATCH 2/2] refine code --- cocos2d/core/graphics/graphics-webgl-cmd.js | 2 +- cocos2d/core/graphics/helper.js | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/cocos2d/core/graphics/graphics-webgl-cmd.js b/cocos2d/core/graphics/graphics-webgl-cmd.js index ed72a464e6..18c79e62f8 100644 --- a/cocos2d/core/graphics/graphics-webgl-cmd.js +++ b/cocos2d/core/graphics/graphics-webgl-cmd.js @@ -36,12 +36,12 @@ var Vec2 = cc.Vec2; var Js = cc.js; // Math -var PI = 3.14159265358979323846264338327; var INIT_VERTS_SIZE = 32; 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; diff --git a/cocos2d/core/graphics/helper.js b/cocos2d/core/graphics/helper.js index a073e0348f..e21bfd6879 100644 --- a/cocos2d/core/graphics/helper.js +++ b/cocos2d/core/graphics/helper.js @@ -1,6 +1,5 @@ -var PI = 3.14159265358979323846264338327; - +var PI = Math.PI; var min = Math.min; var max = Math.max; var cos = Math.cos;