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: change effect for connectedType, closed #1660 #1673

Merged
merged 1 commit into from
Jan 10, 2025
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-core",
"comment": "feat: change effect for connectedType, closed #1660",
"type": "none"
}
],
"packageName": "@visactor/vrender-core"
}
265 changes: 49 additions & 216 deletions packages/vrender-core/src/common/render-area.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,8 @@ export function drawAreaSegments(
offsetY?: number;
offsetZ?: number;
direction?: IDirection;
drawConnect?: boolean; // 是否是绘制connect区域的效果
mode?: 'none' | 'connect' | 'zero';
zeroX?: number;
zeroY?: number;
}
) {
const { drawConnect = false, mode = 'none' } = params || {};
if (drawConnect && mode === 'none') {
return;
}
// let needMoveTo: boolean = true;
const { top, bottom } = segPath;
// 如果top和bottom的curves数量不同,那么就跳过
Expand All @@ -44,90 +36,26 @@ export function drawAreaSegments(
const topList: ICurve<IPoint>[] = [];
const bottomList: ICurve<IPoint>[] = [];
let lastDefined: boolean = true;
if (drawConnect) {
let defined0 = true;
let lastCurve: ICurve<IPoint>;
let lastBottomCurve: ICurve<IPoint>;
const n = top.curves.length;
top.curves.forEach((curve, i) => {
// step的逻辑
const bototmCurve = bottom.curves[n - i - 1];
let currentTopCurve = curve;
let currentBottomCurve = bototmCurve;
if (curve.originP1 === curve.originP2) {
lastCurve = curve;
lastBottomCurve = bototmCurve;
return;
}
if (lastCurve && lastCurve.originP1 === lastCurve.originP2) {
currentTopCurve = lastCurve;
currentBottomCurve = lastBottomCurve;
}
if (curve.defined) {
// 非法变合法需要lineTo,合法变非法需要moveTo,初始非法需要moveTo
if (!defined0) {
topList.push(currentTopCurve);
bottomList.push(currentBottomCurve);
drawAreaConnectBlock(path, topList, bottomList, params);
topList.length = 0;
bottomList.length = 0;
defined0 = !defined0;
}
for (let i = 0, n = top.curves.length; i < n; i++) {
const topCurve = top.curves[i];
if (lastDefined !== topCurve.defined) {
if (lastDefined) {
drawAreaBlock(path, topList, bottomList, params);
topList.length = 0;
bottomList.length = 0;
} else {
// 找到合法的点
const { originP1, originP2 } = curve;
let validTopCurve: ICurve<IPoint>;
let validBottomCurve: ICurve<IPoint>;
if (originP1 && originP1.defined !== false) {
validTopCurve = currentTopCurve;
validBottomCurve = currentBottomCurve;
} else if (originP1 && originP2.defined !== false) {
validTopCurve = curve;
validBottomCurve = bototmCurve;
}
// 合法/(初始)变非法,moveTo
if (defined0) {
defined0 = !defined0;
topList.push(validTopCurve || curve);
bottomList.push(validBottomCurve || bototmCurve);
} else {
// 非法变非法/合法,看情况要不要lineTo
if (validTopCurve) {
// 非法变合法,需要lineTo
defined0 = !defined0;
topList.push(validTopCurve || curve);
bottomList.push(validBottomCurve || bototmCurve);
drawAreaConnectBlock(path, topList, bottomList, params);
topList.length = 0;
bottomList.length = 0;
}
}
topList.push(topCurve);
bottomList.push(bottom.curves[n - i - 1]);
}
lastCurve = curve;
});
drawAreaConnectBlock(path, topList, bottomList, params);
} else {
for (let i = 0, n = top.curves.length; i < n; i++) {
const topCurve = top.curves[i];
if (lastDefined !== topCurve.defined) {
if (lastDefined) {
drawAreaBlock(path, topList, bottomList, params);
topList.length = 0;
bottomList.length = 0;
} else {
topList.push(topCurve);
bottomList.push(bottom.curves[n - i - 1]);
}
lastDefined = !lastDefined;
} else {
if (lastDefined) {
topList.push(topCurve);
bottomList.push(bottom.curves[n - i - 1]);
}
lastDefined = !lastDefined;
} else {
if (lastDefined) {
topList.push(topCurve);
bottomList.push(bottom.curves[n - i - 1]);
}
}
drawAreaBlock(path, topList, bottomList, params);
}
drawAreaBlock(path, topList, bottomList, params);

return;
}
Expand Down Expand Up @@ -159,7 +87,7 @@ export function drawAreaSegments(
let lastDefined: boolean = true;
const topList: ICurve<IPoint>[] = [];
const bottomList: ICurve<IPoint>[] = [];
let defined0 = true;
const defined0 = true;
let lastTopCurve: ICurve<IPoint>;
let lastBottomCurve: ICurve<IPoint>;
for (let i = 0, n = top.curves.length; i < n; i++) {
Expand All @@ -171,112 +99,50 @@ export function drawAreaSegments(
}
drawedLengthUntilLast += curCurveLength;

if (drawConnect) {
// step的逻辑
const bototmCurve = bottom.curves[n - i - 1];
let currentTopCurve = topCurve;
let currentBottomCurve = bototmCurve;
if (topCurve.originP1 === topCurve.originP2) {
lastTopCurve = topCurve;
lastBottomCurve = bototmCurve;
continue;
}
if (lastTopCurve && lastTopCurve.originP1 === lastTopCurve.originP2) {
currentTopCurve = lastTopCurve;
currentBottomCurve = lastBottomCurve;
}
if (topCurve.defined) {
// 非法变合法需要lineTo,合法变非法需要moveTo,初始非法需要moveTo
if (!defined0) {
topList.push(currentTopCurve);
bottomList.push(currentBottomCurve);
drawAreaConnectBlock(path, topList, bottomList, params);
topList.length = 0;
bottomList.length = 0;
defined0 = !defined0;
}
let tc: ICurve<IPoint> | null = null;
let bc: ICurve<IPoint> | null = null;
if (lastDefined !== topCurve.defined) {
if (lastDefined) {
drawAreaBlock(path, topList, bottomList, params);
topList.length = 0;
bottomList.length = 0;
} else {
// 找到合法的点
const { originP1, originP2 } = topCurve;
let validTopCurve: ICurve<IPoint>;
let validBottomCurve: ICurve<IPoint>;
if (originP1 && originP1.defined !== false) {
validTopCurve = currentTopCurve;
validBottomCurve = currentBottomCurve;
} else if (originP1 && originP2.defined !== false) {
validTopCurve = topCurve;
validBottomCurve = bototmCurve;
}
// 合法/(初始)变非法,moveTo
if (defined0) {
defined0 = !defined0;
topList.push(validTopCurve || topCurve);
bottomList.push(validBottomCurve || bototmCurve);
} else {
// 非法变非法/合法,看情况要不要lineTo
if (validTopCurve) {
// 非法变合法,需要lineTo
defined0 = !defined0;
topList.push(validTopCurve || topCurve);
bottomList.push(validBottomCurve || bototmCurve);
drawAreaConnectBlock(path, topList, bottomList, params);
topList.length = 0;
bottomList.length = 0;
}
}
tc = topCurve;
bc = bottom.curves[n - i - 1];
}
lastTopCurve = topCurve;
// drawAreaBlock(path, topList, bottomList, params);
lastDefined = !lastDefined;
} else {
let tc: ICurve<IPoint> | null = null;
let bc: ICurve<IPoint> | null = null;
if (lastDefined !== topCurve.defined) {
if (lastDefined) {
drawAreaBlock(path, topList, bottomList, params);
topList.length = 0;
bottomList.length = 0;
} else {
tc = topCurve;
bc = bottom.curves[n - i - 1];
}
lastDefined = !lastDefined;
} else {
if (lastDefined) {
tc = topCurve;
bc = bottom.curves[n - i - 1];
}
if (lastDefined) {
tc = topCurve;
bc = bottom.curves[n - i - 1];
}
}

if (tc && bc) {
if (percent < 1) {
if (tc.p2 && tc.p3) {
tc = divideCubic(tc as ICubicBezierCurve, percent)[0];
} else {
tc = divideLinear(tc as ILineCurve, percent)[0];
}
if (bc.p2 && bc.p3) {
bc = divideCubic(bc as ICubicBezierCurve, 1 - percent)[1];
} else {
bc = divideLinear(bc as ILineCurve, 1 - percent)[1];
}
if (tc && bc) {
if (percent < 1) {
if (tc.p2 && tc.p3) {
tc = divideCubic(tc as ICubicBezierCurve, percent)[0];
} else {
tc = divideLinear(tc as ILineCurve, percent)[0];
}
if (bc.p2 && bc.p3) {
bc = divideCubic(bc as ICubicBezierCurve, 1 - percent)[1];
} else {
bc = divideLinear(bc as ILineCurve, 1 - percent)[1];
}
tc.defined = lastDefined;
bc.defined = lastDefined;
topList.push(tc);
bottomList.push(bc);
}

tc = null;
bc = null;
tc.defined = lastDefined;
bc.defined = lastDefined;
topList.push(tc);
bottomList.push(bc);
}
}

if (drawConnect) {
drawAreaConnectBlock(path, topList, bottomList, params);
} else {
drawAreaBlock(path, topList, bottomList, params);
tc = null;
bc = null;
}

drawAreaBlock(path, topList, bottomList, params);

// const totalLength = segPath.tryUpdateLength();

// // 直到上次绘制的长度
Expand All @@ -303,39 +169,6 @@ export function drawAreaSegments(
// }
}

function drawAreaConnectBlock(
path: IPath2D,
topList: ICurve<IPoint>[],
bottomList: ICurve<IPoint>[],
params?: {
offsetX?: number;
offsetY?: number;
offsetZ?: number;
mode?: 'none' | 'connect' | 'zero';
zeroX?: number;
zeroY?: number;
}
) {
if (topList.length < 2) {
return;
}
const { offsetX = 0, offsetY = 0, offsetZ = 0, mode } = params || {};
let curve = topList[0];
// mode不支持zero
path.moveTo(curve.p0.x + offsetX, curve.p0.y + offsetY, offsetZ);
curve = topList[topList.length - 1];
let end = curve.p3 || curve.p1;
path.lineTo(end.x + offsetX, end.y + offsetY, offsetZ);

curve = bottomList[bottomList.length - 1];
path.lineTo(curve.p0.x + offsetX, curve.p0.y + offsetY, offsetZ);
curve = bottomList[0];
end = curve.p3 || curve.p1;
path.lineTo(end.x + offsetX, end.y + offsetY, offsetZ);

path.closePath();
}

function drawAreaBlock(
path: IPath2D,
topList: ICurve<IPoint>[],
Expand Down
2 changes: 1 addition & 1 deletion packages/vrender-core/src/common/render-curve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export function drawSegments(
offsetY?: number;
offsetZ?: number;
drawConnect?: boolean; // 是否是绘制connect区域的效果
mode?: 'none' | 'connect' | 'zero';
mode?: 'none' | 'connect';
zeroX?: number;
zeroY?: number;
}
Expand Down
6 changes: 5 additions & 1 deletion packages/vrender-core/src/common/segment/step.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ export class Step implements ICurvedSegment {
this.context.lineTo(x, y, this._lastDefined !== false && p.defined !== false, p);
} else {
const x1 = this._x * (1 - this._t) + x * this._t;
this.context.lineTo(x1, this._y, this._lastDefined !== false && p.defined !== false, this.lastPoint);
if (this._t === 0.5) {
this.context.lineTo(x1, this._y, this._lastDefined !== false, this.lastPoint);
} else {
this.context.lineTo(x1, this._y, this._lastDefined !== false && p.defined !== false, this.lastPoint);
}
this.context.lineTo(x1, y, this._lastDefined !== false && p.defined !== false, p);
}
break;
Expand Down
4 changes: 2 additions & 2 deletions packages/vrender-core/src/graphic/line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export class Line extends Graphic<ILineGraphicAttribute> implements ILine {
const { points = lineTheme.points, connectedType } = attribute;
const b = aabbBounds;
points.forEach(p => {
if (p.defined !== false || connectedType === 'zero' || connectedType === 'connect') {
if (p.defined !== false || connectedType === 'connect') {
b.add(p.x, p.y);
}
});
Expand All @@ -98,7 +98,7 @@ export class Line extends Graphic<ILineGraphicAttribute> implements ILine {
const b = aabbBounds;
segments.forEach(s => {
s.points.forEach(p => {
if (p.defined !== false || connectedType === 'zero' || connectedType === 'connect') {
if (p.defined !== false || connectedType === 'connect') {
b.add(p.x, p.y);
}
});
Expand Down
2 changes: 1 addition & 1 deletion packages/vrender-core/src/interface/graphic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ export type IConnectedStyle = {
/**
* 连接,取零或者断开
*/
connectedType: 'connect' | 'zero' | 'none';
connectedType: 'connect' | 'none';
/**
* 连接线的样式配置
*/
Expand Down
Loading
Loading