From 81abd63b046a4a1a920e1d2bb392f221c707de1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AF=B8=E5=B2=B3?= Date: Mon, 4 Nov 2019 16:06:38 +0800 Subject: [PATCH] fix(g-canvas): bbox calculation for path should consider angle of 0 and PI, close #254 --- packages/g-canvas/src/util/path.ts | 3 ++ .../g-canvas/tests/bugs/issue-254-spec.js | 30 +++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 packages/g-canvas/tests/bugs/issue-254-spec.js diff --git a/packages/g-canvas/src/util/path.ts b/packages/g-canvas/src/util/path.ts index 8800da1bc..d130c3e03 100644 --- a/packages/g-canvas/src/util/path.ts +++ b/packages/g-canvas/src/util/path.ts @@ -167,6 +167,9 @@ function getExtraFromSegmentWithAngle(segment, lineWidth) { const angleCurrent = Math.acos( (currentAndPre + currentAndNext - preAndNext) / (2 * Math.sqrt(currentAndPre) * Math.sqrt(currentAndNext)) ); + if (Math.sin(angleCurrent) === 0) { + return 0; + } // 这里不考虑在水平和垂直方向的投影,直接使用最大差值 // 由于上层统一加减了二分之一线宽,这里需要进行弥补 return lineWidth * (1 / Math.sin(angleCurrent / 2)) + lineWidth / 2 || 0; diff --git a/packages/g-canvas/tests/bugs/issue-254-spec.js b/packages/g-canvas/tests/bugs/issue-254-spec.js new file mode 100644 index 000000000..4a0e70781 --- /dev/null +++ b/packages/g-canvas/tests/bugs/issue-254-spec.js @@ -0,0 +1,30 @@ +const expect = require('chai').expect; +import Canvas from '../../src/canvas'; + +const dom = document.createElement('div'); +document.body.appendChild(dom); +dom.id = 'c1'; + +describe('#254', () => { + const canvas = new Canvas({ + container: dom, + width: 600, + height: 600, + }); + + it('bbox calculation for path should consider angle of 0 and PI', () => { + const shape = canvas.addShape({ + type: 'path', + attrs: { + path: [['M', 300, 500], ['L', 300, 375], ['L', 300, 500], ['Z']], + lineWidth: 5, + stroke: 'red', + }, + }); + const bbox = shape.getBBox(); + expect(bbox.minX).eqls(297.5); + expect(bbox.minY).eqls(372.5); + expect(bbox.maxX).eqls(302.5); + expect(bbox.maxY).eqls(502.5); + }); +});