feat: Rect元素

This commit is contained in:
unanmed 2024-12-04 19:35:30 +08:00
parent feecb3a380
commit 8233cb094b
2 changed files with 53 additions and 3 deletions

View File

@ -247,6 +247,13 @@ export abstract class RenderItem<E extends ERenderItemEvent = ERenderItemEvent>
/** 该渲染元素的子元素 */ /** 该渲染元素的子元素 */
children: Set<RenderItem<ERenderItemEvent>> = new Set(); children: Set<RenderItem<ERenderItemEvent>> = new Set();
get x() {
return this._transform.x;
}
get y() {
return this._transform.y;
}
/** 渲染缓存信息 */ /** 渲染缓存信息 */
protected cache: MotaOffscreenCanvas2D = new MotaOffscreenCanvas2D(); protected cache: MotaOffscreenCanvas2D = new MotaOffscreenCanvas2D();
/** 是否需要更新缓存 */ /** 是否需要更新缓存 */

View File

@ -74,10 +74,22 @@ export abstract class GraphicItemBase
setLineOption(options: Partial<ILineProperty>) {} setLineOption(options: Partial<ILineProperty>) {}
/** /**
* *
* @param style * @param style
*/ */
setStyle(style: CanvasStyle) {} setFillStyle(style: CanvasStyle) {}
/**
*
* @param style
*/
setStrokeStyle(style: CanvasStyle) {}
/**
*
* @param rule
*/
setFillRule(rule: CanvasFillRule) {}
/** /**
* *
@ -85,6 +97,15 @@ export abstract class GraphicItemBase
*/ */
setMode(mode: GraphicMode) {} setMode(mode: GraphicMode) {}
/**
*
* @param canvas
*/
protected setCanvasState(canvas: MotaOffscreenCanvas2D) {
const ctx = canvas.ctx;
ctx.fillStyle = this.fill; // 示例后面的都按这个写不需要save restore写完把这个注释删了
}
patchProp( patchProp(
key: string, key: string,
prevValue: any, prevValue: any,
@ -102,7 +123,29 @@ export class Rect extends GraphicItemBase {
protected render( protected render(
canvas: MotaOffscreenCanvas2D, canvas: MotaOffscreenCanvas2D,
transform: Transform transform: Transform
): void {} ): void {
const ctx = canvas.ctx;
this.setCanvasState(canvas);
ctx.beginPath();
ctx.rect(this.x, this.y, this.width, this.height);
switch (this.mode) {
case GraphicMode.Fill:
ctx.fill(this.fillRule);
break;
case GraphicMode.Stroke:
ctx.stroke();
break;
case GraphicMode.FillAndStroke:
ctx.fill(this.fillRule);
ctx.stroke();
break;
case GraphicMode.StrokeAndFill:
ctx.stroke();
ctx.fill(this.fillRule);
break;
}
}
patchProp( patchProp(
key: string, key: string,