feat: 图形元素考虑描边粗细

This commit is contained in:
AncTe committed 2026-11-20 20:56:42 +08:00
1 parent 99fff4a4ad
commit b253feebd0
4 files changed
+180 -55

No files matched your search

+62 -44
View File
@@ -21,7 +21,9 @@ import {
IRenderItemBaseHooks
} from './types';
import { MotaOffscreenCanvas2D } from './canvas2d';
import { Transform } from './transform';
// 所有的图形元素都考虑了描边粗细的影响,为防止因为描边导致内容溢出,在有描边存在时,会将图形路径向内收缩以避免溢出,
// 图形路径会向内收缩描边宽度的一半,不能保证在 `miter` 线接方式下,连接处不会溢出,不能保证线头不会溢出
const enum GraphicModeProp {
Fill,
@@ -67,7 +69,7 @@ export abstract class GraphicItemBase
/**
* 获取这个元素的绘制路径
*/
abstract getPath(): Path2D;
abstract getPath(stroke: boolean, lineWidth: number): Path2D;
protected getDefaultCache(): boolean {
return false;
@@ -77,16 +79,17 @@ export abstract class GraphicItemBase
return new RenderItemDefaultHooks(this.events);
}
protected render(
canvas: MotaOffscreenCanvas2D,
_transform: Transform
): void {
private refreshCache() {
if (!this.pathDirty) return;
const stroke = this.mode !== GraphicMode.Fill;
this.cachePath = this.getPath(stroke, this.lineWidth);
this.pathDirty = false;
}
protected render(canvas: MotaOffscreenCanvas2D): void {
const ctx = canvas.ctx;
this.setCanvasState(canvas);
if (this.pathDirty) {
this.cachePath = this.getPath();
this.pathDirty = false;
}
this.refreshCache();
const path = this.cachePath;
if (!path) return;
@@ -110,10 +113,7 @@ export abstract class GraphicItemBase
protected isActionInElement(x: number, y: number): boolean {
const ctx = testCanvas.ctx;
if (this.pathDirty) {
this.cachePath = this.getPath();
this.pathDirty = false;
}
this.refreshCache();
const path = this.cachePath;
if (!path) return false;
const fixX = x * devicePixelRatio;
@@ -148,13 +148,25 @@ export abstract class GraphicItemBase
* @param options 线的信息
*/
setLineOption(options: Partial<ILineProperty>) {
if (!isNil(options.lineWidth)) this.lineWidth = options.lineWidth;
if (!isNil(options.lineDash)) this.lineDash = options.lineDash;
if (!isNil(options.lineDashOffset))
if (!isNil(options.lineWidth)) {
this.lineWidth = options.lineWidth;
this.pathDirty = true;
}
if (!isNil(options.lineDash)) {
this.lineDash = options.lineDash;
}
if (!isNil(options.lineDashOffset)) {
this.lineDashOffset = options.lineDashOffset;
if (!isNil(options.lineJoin)) this.lineJoin = options.lineJoin;
if (!isNil(options.lineCap)) this.lineCap = options.lineCap;
if (!isNil(options.miterLimit)) this.miterLimit = options.miterLimit;
}
if (!isNil(options.lineJoin)) {
this.lineJoin = options.lineJoin;
}
if (!isNil(options.lineCap)) {
this.lineCap = options.lineCap;
}
if (!isNil(options.miterLimit)) {
this.miterLimit = options.miterLimit;
}
this.update();
}
@@ -190,7 +202,13 @@ export abstract class GraphicItemBase
* @param mode 绘制模式
*/
setMode(mode: GraphicMode) {
const beforeStroke = this.mode !== GraphicMode.Fill;
const nowStroke = mode !== GraphicMode.Fill;
this.mode = mode;
if (nowStroke !== beforeStroke) {
// 当描边状态改变时,需要把路径标记为脏
this.pathDirty = true;
}
this.update();
}
@@ -328,9 +346,10 @@ export class Rect extends GraphicItemBase {
this.pathDirty = true;
}
getPath(): Path2D {
getPath(stroke: boolean, lineWidth: number): Path2D {
const pad = stroke ? lineWidth / 2 : 0;
const path = new Path2D();
path.rect(0, 0, this.width, this.height);
path.rect(pad, pad, this.width - pad * 2, this.height - pad * 2);
return path;
}
}
@@ -342,9 +361,11 @@ export class Circle extends GraphicItemBase implements IGraphicCircle {
anchorX: number = 0.5;
anchorY: number = 0.5;
getPath(): Path2D {
getPath(stroke: boolean, lineWidth: number): Path2D {
const pad = stroke ? lineWidth / 2 : 0;
const path = new Path2D();
path.arc(this.radius, this.radius, this.radius, this.start, this.end);
const radius = this.radius;
path.arc(radius, radius, radius - pad, this.start, this.end);
return path;
}
@@ -418,17 +439,12 @@ export class Ellipse extends GraphicItemBase implements IGraphicEllipse {
anchorX: number = 0.5;
anchorY: number = 0.5;
getPath(): Path2D {
getPath(stroke: boolean, lineWidth: number): Path2D {
const pad = stroke ? lineWidth / 2 : 0;
const path = new Path2D();
path.ellipse(
this.radiusX,
this.radiusY,
this.radiusX,
this.radiusY,
0,
this.start,
this.end
);
const rx = this.radiusX;
const ry = this.radiusY;
path.ellipse(rx, ry, rx - pad, ry - pad, 0, this.start, this.end);
return path;
}
@@ -879,28 +895,30 @@ export class RectR extends GraphicItemBase implements IGraphicRectR {
[0, 0]
];
getPath(): Path2D {
getPath(stroke: boolean, lineWidth: number): Path2D {
const pad = stroke ? lineWidth / 2 : 0;
const path = new Path2D();
const { width: w, height: h } = this;
const [[xtl, ytl], [xtr, ytr], [xbr, ybr], [xbl, ybl]] = this.corner;
const pi = Math.PI;
// 左上圆角终点
path.moveTo(xtl, 0);
path.moveTo(xtl + pad, pad);
// 右上圆角起点
path.lineTo(w - xtr, 0);
path.lineTo(w - xtr - pad, pad);
// 右上圆角终点
path.ellipse(w - xtr, ytr, xtr, ytr, 0, -Math.PI / 2, 0);
path.ellipse(w - xtr - pad, ytr + pad, xtr, ytr, 0, -pi / 2, 0);
// 右下圆角起点
path.lineTo(w, h - ybr);
path.lineTo(w - pad, h - ybr - pad);
// 右下圆角终点
path.ellipse(w - xbr, h - ybr, xbr, ybr, 0, 0, Math.PI / 2);
path.ellipse(w - xbr - pad, h - ybr - pad, xbr, ybr, 0, 0, pi / 2);
// 左下圆角起点
path.lineTo(xbl, h);
path.lineTo(xbl + pad, h - pad);
// 左下圆角终点
path.ellipse(xbl, h - ybl, xbl, ybl, 0, Math.PI / 2, Math.PI);
path.ellipse(xbl + pad, h - ybl - pad, xbl, ybl, 0, pi / 2, pi);
// 左上圆角起点
path.lineTo(0, ytl);
path.lineTo(pad, ytl + pad);
// 左上圆角终点
path.ellipse(xtl, ytl, xtl, ytl, 0, Math.PI, -Math.PI / 2);
path.ellipse(xtl + pad, ytl + pad, xtl, ytl, 0, pi, -pi / 2);
path.closePath();
return path;
}
-6
View File
@@ -1106,12 +1106,6 @@ export abstract class RenderItem<
if (!isNil(nextValue[2]) && !isNil(nextValue[3])) {
this.size(nextValue[2] as number, nextValue[3] as number);
}
if (!isNil(nextValue[4]) && !isNil(nextValue[5])) {
this.setAnchor(
nextValue[4] as number,
nextValue[5] as number
);
}
return;
}
case 'anc': {
+1 -3
View File
@@ -41,9 +41,7 @@ export type ElementLocator = [
x?: number,
y?: number,
width?: number,
height?: number,
anchorX?: number,
anchorY?: number
height?: number
];
export type ElementAnchor = [x: number, y: number];