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

fix: fix the defined bug in basis and monotone curve, closed #30 #37

Merged
merged 1 commit into from
Jun 14, 2023
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@visactor/vrender",
"comment": "fix the defined bug in basis and monotone curve",
"type": "patch"
}
],
"packageName": "@visactor/vrender"
}
116 changes: 92 additions & 24 deletions docs/demos/src/pages/line.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createStage, createLine, container, IGraphic } from '@visactor/vrender';
import { createStage, createLine, container, IGraphic, createSymbol } from '@visactor/vrender';
import { roughModule } from '@visactor/vrender-kits';
import { addShapesToStage, colorPools } from '../utils';

Expand Down Expand Up @@ -35,30 +35,98 @@ const points = [

export const page = () => {
const graphics: IGraphic[] = [];
['linear', 'step', 'stepBefore', 'stepAfter', 'basis', 'monotoneX', 'monotoneY'].forEach((type, i) => {
graphics.push(createLine({
points,
curveType: type as any,
x: (i * 300) % 900 + 100,
y: (Math.floor(i * 300 / 900)) * 200,
stroke: 'red'
}));
});
// ['linear', 'step', 'stepBefore', 'stepAfter', 'basis', 'monotoneX', 'monotoneY'].forEach((type, i) => {
// graphics.push(createLine({
// points,
// curveType: type as any,
// x: (i * 300) % 900 + 100,
// y: (Math.floor(i * 300 / 900)) * 200,
// stroke: 'red'
// }));
// // points.forEach(item => {
// // graphics.push(createSymbol({
// // x: (i * 300) % 900 + 100 + item.x,
// // y: (Math.floor(i * 300 / 900)) * 200 + item.y,
// // size: 10,
// // symbolType: 'circle',
// // fill: 'green'
// // }))
// // })
// });
const p = [
{
"x": 0,
"y": 17.142857142857142,
"defined": true,
},
{
"x": 31.142857142857142,
"y": 8.571428571428573,
"defined": true
},
{
"x": 62.285714285714285,
"y": 20,
"defined": false
},
{
"x": 93.42857142857143,
"y": 7.428571428571429,
"defined": true
},
{
"x": 124.57142857142857,
"y": 11.428571428571429,
"defined": true
},
{
"x": 155.71428571428572,
"y": 14.285714285714286,
"defined": true
},
{
"x": 186.85714285714286,
"y": 5.7142857142857135,
"defined": true
},
{
"x": 218,
"y": 0,
"defined": true
}
];
graphics.push(createLine({
"x": 100,
"y": 100,
"curveType": "basis",
"stroke": "#2E62F1",
"lineWidth": 2,
"points": p
}));
p.forEach(item => {
graphics.push(createSymbol({
x: item.x + 100,
y: item.y + 100,
fill: 'red',
symbolType: 'circle',
size: 10
}))
});

['linear', 'step', 'stepBefore', 'stepAfter', 'basis', 'monotoneX', 'monotoneY'].forEach((type, i) => {
i += 7;
graphics.push(createLine({
points,
curveType: type as any,
x: (i * 300) % 900 + 100,
y: (Math.floor(i * 300 / 900)) * 200,
segments: [
{ points: subP1, stroke: colorPools[3], lineWidth: 6 },
{ points: subP2, stroke: colorPools[2], lineWidth: 2, lineDash: [3, 3] }
],
stroke: 'red'
}));
});
// ['linear', 'step', 'stepBefore', 'stepAfter', 'basis', 'monotoneX', 'monotoneY'].forEach((type, i) => {
// i += 7;
// graphics.push(createLine({
// points,
// curveType: type as any,
// x: (i * 300) % 900 + 100,
// y: (Math.floor(i * 300 / 900)) * 200,
// segments: [
// { points: subP1, stroke: colorPools[3], lineWidth: 6 },
// { points: subP2, stroke: colorPools[2], lineWidth: 2, lineDash: [3, 3] }
// ],
// stroke: 'red'
// }));
// });


const stage = createStage({
Expand Down
12 changes: 7 additions & 5 deletions packages/vrender/src/common/segment/basis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ export function point(curveClass: Basis, x: number, y: number, defined: boolean)
}

export class Basis implements ICurvedSegment {
private _lastDefined?: boolean;
private _lastDefined1?: boolean;
private _lastDefined2?: boolean;
declare context: ISegPath2D;

protected startPoint?: IPointLike;
Expand Down Expand Up @@ -73,7 +74,7 @@ export class Basis implements ICurvedSegment {
this,
this._x1 * 6 - (this._x0 + 4 * this._x1),
this._y1 * 6 - (this._y0 + 4 * this._y1),
this._lastDefined !== false
this._lastDefined1 !== false && this._lastDefined2 !== false
); // falls through
// case 2: this.context.lineTo(this._x1, this._y1); break;
}
Expand All @@ -89,20 +90,21 @@ export class Basis implements ICurvedSegment {
case 0:
this._point = 1;
this._line
? this.context.lineTo(x, y, this._lastDefined !== false && p.defined !== false)
? this.context.lineTo(x, y, this._lastDefined1 !== false && this._lastDefined2 !== false)
: this.context.moveTo(x, y);
break;
case 1:
this._point = 2;
break;
// case 2: this._point = 3; this.context.lineTo((5 * this._x0 + this._x1) / 6, (5 * this._y0 + this._y1) / 6, i, defined1, defined2); // falls through
default:
point(this, x, y, this._lastDefined !== false && p.defined !== false);
point(this, x, y, this._lastDefined1 !== false && this._lastDefined2 !== false);
break;
}
(this._x0 = this._x1), (this._x1 = x);
(this._y0 = this._y1), (this._y1 = y);
this._lastDefined = p.defined;
this._lastDefined1 = this._lastDefined2;
this._lastDefined2 = p.defined;
}

tryUpdateLength(): number {
Expand Down
21 changes: 14 additions & 7 deletions packages/vrender/src/common/segment/monotone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ function point(curveClass: MonotoneX | MonotoneY, t0: number, t1: number, define
}

export class MonotoneX implements ICurvedSegment {
protected _lastDefined?: boolean;
protected _lastDefined1?: boolean;
protected _lastDefined2?: boolean;
declare context: ISegPath2D;
declare _t0: number;

Expand Down Expand Up @@ -93,10 +94,10 @@ export class MonotoneX implements ICurvedSegment {
lineEnd() {
switch (this._point) {
case 2:
this.context.lineTo(this._x1, this._y1, this._lastDefined !== false);
this.context.lineTo(this._x1, this._y1, this._lastDefined2 !== false);
break;
case 3:
point(this, this._t0, slope2(this, this._t0), this._lastDefined !== false);
point(this, this._t0, slope2(this, this._t0), this._lastDefined2 !== false);
break;
}
if (this._line || (this._line !== 0 && this._point === 1)) {
Expand All @@ -116,25 +117,31 @@ export class MonotoneX implements ICurvedSegment {
case 0:
this._point = 1;
this._line
? this.context.lineTo(x, y, this._lastDefined !== false && p.defined !== false)
? this.context.lineTo(x, y, this._lastDefined1 !== false && this._lastDefined2 !== false)
: this.context.moveTo(x, y);
break;
case 1:
this._point = 2;
break;
case 2:
this._point = 3;
point(this, slope2(this, (t1 = slope3(this, x, y))), t1, this._lastDefined !== false && p.defined !== false);
point(
this,
slope2(this, (t1 = slope3(this, x, y))),
t1,
this._lastDefined1 !== false && this._lastDefined2 !== false
);
break;
default:
point(this, this._t0, (t1 = slope3(this, x, y)), this._lastDefined !== false && p.defined !== false);
point(this, this._t0, (t1 = slope3(this, x, y)), this._lastDefined1 !== false && this._lastDefined2 !== false);
break;
}

(this._x0 = this._x1), (this._x1 = x);
(this._y0 = this._y1), (this._y1 = y);
this._t0 = t1;
this._lastDefined = p.defined !== false;
this._lastDefined1 = this._lastDefined2;
this._lastDefined2 = p.defined !== false;
}

tryUpdateLength(): number {
Expand Down