Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(polyline): support drawMode: lines #1098

Open
wants to merge 2 commits into
base: v6
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 68 additions & 14 deletions src/graphic/helper/poly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,90 @@ export function buildPath(
shape: {
points: VectorArray[],
smooth?: number
smoothConstraint?: VectorArray[]
smoothConstraint?: VectorArray[],
drawMode?: 'lineStrip' | 'lines'
},
closePath: boolean
) {
const smooth = shape.smooth;
const drawMode = shape.drawMode || 'lineStrip';
let points = shape.points;
if (points && points.length >= 2) {
if (smooth) {
const controlPoints = smoothBezier(
points, smooth, closePath, shape.smoothConstraint
);

ctx.moveTo(points[0][0], points[0][1]);
const len = points.length;
for (let i = 0; i < (closePath ? len : len - 1); i++) {
const cp1 = controlPoints[i * 2];
const cp2 = controlPoints[i * 2 + 1];
const p = points[(i + 1) % len];
ctx.bezierCurveTo(
cp1[0], cp1[1], cp2[0], cp2[1], p[0], p[1]
if (drawMode === 'lineStrip') {
buildBezierCurve(
points,
smooth,
closePath,
shape.smoothConstraint,
ctx
);
}
else {
let startIndex = 0;
while (startIndex < points.length - 1) {
// Find the position where the line start point is
// different from the end point
let endIndex = startIndex + 2;
const stripPoints = [points[startIndex], points[startIndex + 1]];
while (endIndex < points.length) {
if (points[endIndex - 1][0] !== points[endIndex][0]
|| points[endIndex - 1][1] !== points[endIndex][1]
) {
break;
}
endIndex += 2;
stripPoints.push(points[endIndex - 1]);
}
buildBezierCurve(
stripPoints,
smooth,
closePath,
shape.smoothConstraint,
ctx
);
startIndex = endIndex;
}
}
}
else {
ctx.moveTo(points[0][0], points[0][1]);
for (let i = 1, l = points.length; i < l; i++) {
ctx.lineTo(points[i][0], points[i][1]);
if (drawMode === 'lineStrip' || i % 2 === 1) {
ctx.lineTo(points[i][0], points[i][1]);
}
// If the new point is the same as the previous point, it can be ignored
else if (points[i][0] !== points[i - 1][0]
|| points[i][1] !== points[i - 1][1]
) {
ctx.moveTo(points[i][0], points[i][1]);
}
}
}

closePath && ctx.closePath();
}
}

function buildBezierCurve(
points: VectorArray[],
smooth: number,
closePath: boolean,
smoothConstraint: VectorArray[],
ctx: CanvasRenderingContext2D | PathProxy
) {
const controlPoints = smoothBezier(
points, smooth, closePath, smoothConstraint
);

ctx.moveTo(points[0][0], points[0][1]);
const len = points.length;
for (let i = 0; i < (closePath ? len : len - 1); i++) {
const cp1 = controlPoints[i * 2];
const cp2 = controlPoints[i * 2 + 1];
const p = points[(i + 1) % len];
ctx.bezierCurveTo(
cp1[0], cp1[1], cp2[0], cp2[1], p[0], p[1]
);
}
}
1 change: 1 addition & 0 deletions src/graphic/shape/Polyline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export class PolylineShape {
percent?: number = 1
smooth?: number = 0
smoothConstraint?: VectorArray[] = null
drawMode?: 'lineStrip' | 'lines' = 'lineStrip'
}

export interface PolylineProps extends PathProps {
Expand Down
128 changes: 128 additions & 0 deletions test/lineStrip.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>lineStrip</title>
<script src="../dist/zrender.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style>
html, body, #main {
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<div id="main"></div>
<script type="text/javascript">
var main = document.getElementById('main');
// 初始化zrender
var zr = zrender.init(main);

var polyline1 = new zrender.Polyline({
shape: {
points: [
[100, 50],
[200, 100],

[200, 200],
[300, 400],

[300, 400],
[200, 350],

[200, 350],
[100, 400]
],
drawMode: 'lines'
},
style: {
fill: 'red',
lineWidth: 2
}
});
zr.add(polyline1);

zr.add(new zrender.Text({
style: {
text: 'drawMode: lines',
x: 150,
y: 20
}
}));

var polyline2 = new zrender.Polyline({
shape: {
points: [
[400, 50],
[500, 100],

[500, 200],
[600, 400],

[600, 400],
[500, 350],

[500, 350],
[400, 400]
],
drawMode: 'lines',
smooth: 0.8
},
style: {
fill: 'blue',
lineWidth: 2
}
});
zr.add(polyline2);

zr.add(new zrender.Text({
style: {
text: 'drawMode: lines, with smooth',
x: 450,
y: 20
}
}));

var polyline3 = new zrender.Polyline({
shape: {
points: [
[700, 50],
[800, 100],
],
smooth: 0.8
},
style: {
fill: 'green',
lineWidth: 2
}
});
zr.add(polyline3);

var polyline4 = new zrender.Polyline({
shape: {
points: [
[800, 200],
[900, 400],
[800, 350],
[700, 400]
],
smooth: 0.8
},
style: {
fill: 'green',
lineWidth: 2
}
});
zr.add(polyline4);

zr.add(new zrender.Text({
style: {
text: 'drawMode: lineStrip, with smooth',
x: 750,
y: 20
}
}));
</script>
</body>
</html>
Loading