diff --git a/packages/render-vue/src/use.ts b/packages/render-vue/src/use.ts index 46afa0d..1f90831 100644 --- a/packages/render-vue/src/use.ts +++ b/packages/render-vue/src/use.ts @@ -1,4 +1,5 @@ import { + ElementLocator, IRenderItemBaseHooks, IRenderTreeRoot, MotaRenderer, @@ -28,7 +29,11 @@ import { ExcitationCurve, IAnimatable } from '@motajs/animate'; -import { ICycledTransitionController, ITransitionedController } from './types'; +import { + DefaultProps, + ICycledTransitionController, + ITransitionedController +} from './types'; //#region 函数功能接口 @@ -335,9 +340,119 @@ export function cycledTransition( ); } +/** + * 根据传入组件的参数获取组件的横纵坐标 + * @param props 传递给组件的原始 `props`,要求必须声明了 `x` `y` `loc` 三个参数 + */ +export function xy( + props: DefaultProps +): [x: ComputedRef, y: ComputedRef] { + return [ + computed(() => props.loc?.[0] ?? props.x ?? 0), + computed(() => props.loc?.[1] ?? props.y ?? 0) + ]; +} + +/** + * 根据传入组件的参数获取组件的宽度和高度 + * @param props 传递给组件的原始 `props`,要求必须声明了 `width` `height` `loc` 三个参数 + */ +export function wh( + props: DefaultProps +): [w: ComputedRef, h: ComputedRef] { + return [ + computed(() => props.loc?.[2] ?? props.width ?? 200), + computed(() => props.loc?.[3] ?? props.height ?? 200) + ]; +} + +/** + * 根据传入组件的参数获取组件的横纵坐标、宽度和高度 + * @param props 传递给组件的原始 `props`,要求必须声明了 `x` `y` `width` `height` `loc` 五个参数 + */ +export function xywh( + props: DefaultProps +): [ + x: ComputedRef, + y: ComputedRef, + w: ComputedRef, + h: ComputedRef +] { + return [ + computed(() => props.loc?.[0] ?? props.x ?? 0), + computed(() => props.loc?.[1] ?? props.y ?? 0), + computed(() => props.loc?.[2] ?? props.width ?? 200), + computed(() => props.loc?.[3] ?? props.height ?? 200) + ]; +} + +/** + * 根据传入组件的参数获取组件的可透传坐标、宽度、高度,相当于: + * + * ```ts + * const [loc, _x, _y, w, h] = lxywh(props); + * ``` + * @param props 传递给组件的原始 `props`,要求必须声明了 `x` `y` `width` `height` `loc` 五个参数 + */ +export function lwh( + props: DefaultProps +): [ + loc: ComputedRef, + w: ComputedRef, + h: ComputedRef +] { + const x = computed(() => props.loc?.[0] ?? props.x ?? 0); + const y = computed(() => props.loc?.[1] ?? props.y ?? 0); + const w = computed(() => props.loc?.[2] ?? props.width ?? 200); + const h = computed(() => props.loc?.[3] ?? props.height ?? 200); + const loc = computed(() => [ + x.value, + y.value, + w.value, + h.value + ]); + return [loc, w, h]; +} + +/** + * 根据传入组件的参数获取组件的可透传坐标、横纵坐标、宽度、高度 + * @param props 传递给组件的原始 `props`,要求必须声明了 `x` `y` `width` `height` `loc` 五个参数 + */ +export function lxywh( + props: DefaultProps +): [ + loc: ComputedRef, + x: ComputedRef, + y: ComputedRef, + w: ComputedRef, + h: ComputedRef +] { + const x = computed(() => props.loc?.[0] ?? props.x ?? 0); + const y = computed(() => props.loc?.[1] ?? props.y ?? 0); + const w = computed(() => props.loc?.[2] ?? props.width ?? 200); + const h = computed(() => props.loc?.[3] ?? props.height ?? 200); + const loc = computed(() => [ + x.value, + y.value, + w.value, + h.value + ]); + return [loc, x, y, w, h]; +} + +/** + * 挤压传入的定位数组指定像素值,相当于将容器上下左右各减少 `pad` 像素 + * @param loc 原始定位数组 + * @param pad 要挤压的区域 + */ +export function squeeze(loc: ElementLocator, pad: number): ElementLocator { + const [x = 0, y = 0, w = 200, h = 200] = loc; + return [x + pad, y + pad, w - 2 * pad, h - 2 * pad]; +} + //#endregion -//#region 渐变 +//#region 渐变类 class RenderTransition implements ITransitionedController { readonly ref: Ref; diff --git a/packages/render/src/core/graphics.ts b/packages/render/src/core/graphics.ts index 7c325a2..03cc32a 100644 --- a/packages/render/src/core/graphics.ts +++ b/packages/render/src/core/graphics.ts @@ -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) { - 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; } diff --git a/packages/render/src/core/item.ts b/packages/render/src/core/item.ts index 4ce859a..93f1c3e 100644 --- a/packages/render/src/core/item.ts +++ b/packages/render/src/core/item.ts @@ -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': { diff --git a/packages/render/src/core/types.ts b/packages/render/src/core/types.ts index 363c24a..d81de8d 100644 --- a/packages/render/src/core/types.ts +++ b/packages/render/src/core/types.ts @@ -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];