Skip to content

Commit

Permalink
fix(g-canvas): bbox calculation for path should consider angle of 0 a…
Browse files Browse the repository at this point in the history
…nd PI, close #254
  • Loading branch information
dengfuping committed Nov 4, 2019
1 parent 106f04e commit 81abd63
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/g-canvas/src/util/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
30 changes: 30 additions & 0 deletions packages/g-canvas/tests/bugs/issue-254-spec.js
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit 81abd63

Please sign in to comment.