Skip to content

Commit

Permalink
fix(issue-688): 当 marker symbol 没有内置时,method 为空,导致 crash
Browse files Browse the repository at this point in the history
  • Loading branch information
xinming committed Dec 21, 2020
1 parent f1c5445 commit 417cd90
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 5 deletions.
11 changes: 6 additions & 5 deletions packages/g-canvas/src/shape/marker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,13 @@ class Marker extends ShapeBase {
} else {
// 内置 symbol 的 path 都是绝对路径,直接绘制即可,不需要对 path 进行特殊处理
method = Marker.Symbols[symbol];
path = method(x, y, r);
}

if (!method) {
console.warn(`${symbol} marker is not supported.`);
return null;
if (!method) {
console.warn(`${symbol} marker is not supported.`);
return null;
}

path = method(x, y, r);
}

return path;
Expand Down
3 changes: 3 additions & 0 deletions packages/g-canvas/src/util/draw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ function checkElementRefresh(shape: IElement, region: Region): boolean {
// 绘制 path
export function drawPath(shape, context, attrs, arcParamsCache) {
const { path, startArrow, endArrow } = attrs;
if (!path) {
return;
}
let currentPoint = [0, 0]; // 当前图形
let startMovePoint = [0, 0]; // 开始 M 的点,可能会有多个
let distance = {
Expand Down
49 changes: 49 additions & 0 deletions packages/g-canvas/tests/bugs/issue-688-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const expect = require('chai').expect;
import { Canvas } from '../../src';
const container = document.createElement('canvas');
container.width = 300;
container.height = 300;
container.id = 'marker';
document.body.appendChild(container);

describe('#688', () => {
const canvas = new Canvas({
container,
width: 600,
height: 500,
});

canvas.addGroup();

const commonAttrs = {
r: 30,
lineWidth: 2,
stroke: '#F04864',
fill: '#1890FF',
};
const crossMarker = canvas.addShape('marker', {
attrs: {
...commonAttrs,
x: 100,
y: 100,
symbol: 'cross',
},
});

// 被影响了
const circleMarker = canvas.addShape('marker', {
attrs: {
...commonAttrs,
x: 300,
y: 100,
symbol: 'circle',
},
});

it('init', () => {
expect(crossMarker.attr('symbol')).eqls('cross');
expect(crossMarker.attr('path')).eqls(undefined);
expect(circleMarker.attr('symbol')).eqls('circle');
expect(circleMarker.attr('path')).not.eqls(null);
});
});

0 comments on commit 417cd90

Please sign in to comment.