mirror of
https://github.com/motajs/template.git
synced 2026-11-20 19:54:36 +08:00
feat: 可动画贴图
This commit is contained in:
1 parent
ed693feb40
commit
6c81872040
6 files changed
+198
-10
No files matched your search
@@ -1,6 +1,7 @@
|
||||
import { ReservedProps } from 'vue';
|
||||
import EventEmitter from 'eventemitter3';
|
||||
import {
|
||||
AnimatedProps,
|
||||
BezierProps,
|
||||
CirclesProps,
|
||||
CommentProps,
|
||||
@@ -49,6 +50,7 @@ declare module 'vue/jsx-runtime' {
|
||||
shader: TagDefine<ShaderProps, ERenderItemEvent>;
|
||||
text: TagDefine<TextProps, ERenderItemEvent>;
|
||||
image: TagDefine<ImageProps, ERenderItemEvent>;
|
||||
animated: TagDefine<AnimatedProps, ERenderItemEvent>;
|
||||
comment: TagDefine<CommentProps, ERenderItemEvent>;
|
||||
custom: TagDefine<CustomProps, ERenderItemEvent>;
|
||||
'g-rect': TagDefine<RectProps, ERenderItemEvent>;
|
||||
|
||||
@@ -17,7 +17,8 @@ import {
|
||||
QuadParams,
|
||||
RectRCircleParams,
|
||||
RectREllipseParams,
|
||||
ITexture
|
||||
ITexture,
|
||||
ITextureAnimater
|
||||
} from '@motajs/render';
|
||||
|
||||
export interface BaseProps {
|
||||
@@ -117,6 +118,21 @@ export interface ImageProps extends BaseProps {
|
||||
image?: ITexture | null;
|
||||
}
|
||||
|
||||
export interface AnimatedProps extends BaseProps {
|
||||
/** 要执行动画的纹理 */
|
||||
texture?: ITexture | null;
|
||||
/** 动画执行器 */
|
||||
animater?: ITextureAnimater<any> | null;
|
||||
/** 传递给动画执行器的参数 */
|
||||
param?: any;
|
||||
/** 两帧之间的间隔,必须大于 0,若传入小于等于 0 的数或不传,使用 50ms 的默认值 */
|
||||
interval?: number;
|
||||
/** 播放是否暂停 */
|
||||
paused?: boolean;
|
||||
/** 是否循环播放,默认为单次播放,调整后会从头开始重新播放 */
|
||||
cycled?: boolean;
|
||||
}
|
||||
|
||||
export interface CommentProps extends BaseProps {
|
||||
text?: string;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import {
|
||||
Animated,
|
||||
BezierCurve,
|
||||
Circle,
|
||||
Comment,
|
||||
@@ -9,15 +10,13 @@ import {
|
||||
Image,
|
||||
IRenderItem,
|
||||
IRenderTreeRoot,
|
||||
ITexture,
|
||||
Line,
|
||||
Path,
|
||||
QuadraticCurve,
|
||||
Rect,
|
||||
RectR,
|
||||
Shader,
|
||||
Text,
|
||||
Texture
|
||||
Text
|
||||
} from '@motajs/render';
|
||||
import { IRenderTagInfo, IRenderTagManager, TagCreateFunction } from './types';
|
||||
import { logger } from '@motajs/common';
|
||||
@@ -25,14 +24,11 @@ import { logger } from '@motajs/common';
|
||||
export class RenderTagManager implements IRenderTagManager {
|
||||
/** 标签注册映射 */
|
||||
private readonly tagRegistry: Map<string, IRenderTagInfo> = new Map();
|
||||
/** 空图片 */
|
||||
private readonly emptyImg: ITexture;
|
||||
|
||||
constructor(readonly renderer: IRenderTreeRoot) {
|
||||
const emptyImage = document.createElement('canvas');
|
||||
emptyImage.width = 1;
|
||||
emptyImage.height = 1;
|
||||
this.emptyImg = new Texture(emptyImage);
|
||||
|
||||
this.resgiterIntrinsicTags();
|
||||
}
|
||||
@@ -55,6 +51,10 @@ export class RenderTagManager implements IRenderTagManager {
|
||||
return this.renderer.createElement(Text, text, cache && !nocache);
|
||||
});
|
||||
this.registerTag('image', this.createStandardElement(false, Image));
|
||||
this.registerTag(
|
||||
'animated',
|
||||
this.createStandardElement(false, Animated)
|
||||
);
|
||||
this.registerTag('shader', this.createNoParamElement(Shader));
|
||||
this.registerTag('comment', props => {
|
||||
if (!props) return this.renderer.createElement(Comment);
|
||||
|
||||
@@ -1061,6 +1061,12 @@ export abstract class RenderItem
|
||||
this.update();
|
||||
return;
|
||||
}
|
||||
case 'selfTransform': {
|
||||
if (!this.assertType(nextValue, 'boolean', key)) return;
|
||||
this.selfTransform = nextValue;
|
||||
this.update();
|
||||
return;
|
||||
}
|
||||
case 'type': {
|
||||
if (!this.assertType(nextValue, 'number', key)) return;
|
||||
this.position = nextValue;
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { RenderItem, Transform, MotaOffscreenCanvas2D } from '.';
|
||||
import { CanvasStyle } from '../types';
|
||||
import { Font } from '../style';
|
||||
import { IRenderImage, IRenderText } from './types';
|
||||
import { ITexture } from '../assets';
|
||||
import { IRenderAnimated, IRenderImage, IRenderText } from './types';
|
||||
import { ITexture, ITextureAnimater, ITextureRenderable } from '../assets';
|
||||
|
||||
/** 文字的安全填充,会填充在文字的上侧和下侧,防止削顶和削底 */
|
||||
const SAFE_PAD = 1;
|
||||
@@ -192,6 +192,114 @@ export class Image extends RenderItem implements IRenderImage {
|
||||
}
|
||||
}
|
||||
|
||||
export class Animated<T> extends RenderItem implements IRenderAnimated<T> {
|
||||
texture: ITexture | null = null;
|
||||
animater: ITextureAnimater<T> | null = null;
|
||||
interval: number = 50;
|
||||
cycled: boolean = false;
|
||||
paused: boolean = false;
|
||||
param: T | undefined = void 0;
|
||||
|
||||
/** 当前时刻 */
|
||||
private currentTime: number = -1;
|
||||
/** 上一帧的时刻 */
|
||||
private lastFrameTime: number = -1;
|
||||
/** 当前的可渲染对象 */
|
||||
private current: ITextureRenderable | null = null;
|
||||
|
||||
/** 动画生成器 */
|
||||
private gen: Generator<ITextureRenderable, void> | null = null;
|
||||
|
||||
constructor(enableCache: boolean = false) {
|
||||
super(enableCache);
|
||||
|
||||
this.delegateExcitable(time => {
|
||||
this.currentTime = time;
|
||||
if (this.lastFrameTime === -1) {
|
||||
this.lastFrameTime = time;
|
||||
}
|
||||
if (this.paused || !this.gen) return;
|
||||
const dt = time - this.lastFrameTime;
|
||||
const nextFrames = Math.floor(dt / this.interval);
|
||||
if (!this.current || nextFrames >= 1) {
|
||||
const pass = this.current ? nextFrames : 1;
|
||||
for (let i = 0; i < pass; i++) {
|
||||
const next = this.gen.next();
|
||||
if (next.done) break;
|
||||
this.current = next.value;
|
||||
}
|
||||
this.lastFrameTime = time;
|
||||
this.update();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
protected render(canvas: MotaOffscreenCanvas2D): void {
|
||||
const ctx = canvas.ctx;
|
||||
if (!this.current) return;
|
||||
const { source, rect: r } = this.current;
|
||||
const { width, height } = this;
|
||||
ctx.drawImage(source, r.x, r.y, r.w, r.h, 0, 0, width, height);
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行动画执行器,获取动画序列生成器
|
||||
*/
|
||||
private generate() {
|
||||
if (!this.animater || !this.texture) {
|
||||
this.gen = null;
|
||||
this.current = null;
|
||||
this.update();
|
||||
return;
|
||||
}
|
||||
if (this.cycled) {
|
||||
this.gen = this.animater.cycled(this.texture, this.param as T);
|
||||
this.lastFrameTime = this.currentTime;
|
||||
} else {
|
||||
this.gen = this.animater.once(this.texture, this.param as T);
|
||||
this.lastFrameTime = this.currentTime;
|
||||
}
|
||||
this.update();
|
||||
}
|
||||
|
||||
setTexture(texture: ITexture | null): void {
|
||||
this.texture = texture;
|
||||
this.generate();
|
||||
}
|
||||
|
||||
setInterval(interval: number): void {
|
||||
if (interval <= 0) {
|
||||
this.interval = 50;
|
||||
} else {
|
||||
this.interval = interval;
|
||||
}
|
||||
}
|
||||
|
||||
setAnimater(animater: ITextureAnimater<T> | null): void {
|
||||
this.animater = animater;
|
||||
this.generate();
|
||||
}
|
||||
|
||||
setParam(param: T | undefined): void {
|
||||
this.param = param;
|
||||
this.generate();
|
||||
}
|
||||
|
||||
pause(): void {
|
||||
this.paused = true;
|
||||
}
|
||||
|
||||
resume(): void {
|
||||
this.paused = false;
|
||||
this.lastFrameTime = this.currentTime;
|
||||
}
|
||||
|
||||
setCycled(cycled: boolean): void {
|
||||
this.cycled = cycled;
|
||||
this.generate();
|
||||
}
|
||||
}
|
||||
|
||||
export class Comment extends RenderItem {
|
||||
readonly isComment: boolean = true;
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ import { DefineComponent, DefineSetupFnComponent } from 'vue';
|
||||
import { JSX } from 'vue/jsx-runtime';
|
||||
import EventEmitter from 'eventemitter3';
|
||||
import { SizedCanvasImageSource } from '../types';
|
||||
import { ITexture } from '../assets';
|
||||
import { ITexture, ITextureAnimater } from '../assets';
|
||||
|
||||
//#region 功能类型
|
||||
|
||||
@@ -590,6 +590,62 @@ export interface IRenderImage extends IRenderItem {
|
||||
setImage(image: ITexture): void;
|
||||
}
|
||||
|
||||
export interface IRenderAnimated<T> extends IRenderItem {
|
||||
/** 贴图的动画执行器 */
|
||||
readonly animater: ITextureAnimater<T> | null;
|
||||
/** 播放动画的纹理对象 */
|
||||
readonly texture: ITexture | null;
|
||||
/** 两帧之间的间隔,必须大于 0 */
|
||||
readonly interval: number;
|
||||
/** 当前是否为循环播放模式 */
|
||||
readonly cycled: boolean;
|
||||
/** 当前动画是否暂停 */
|
||||
readonly paused: boolean;
|
||||
/** 传递给动画执行器的参数 */
|
||||
readonly param: T | undefined;
|
||||
|
||||
/**
|
||||
* 设置播放动画的纹理对象
|
||||
* @param texture 要播放动画的纹理对象
|
||||
*/
|
||||
setTexture(texture: ITexture | null): void;
|
||||
|
||||
/**
|
||||
* 设置两帧之间的间隔,必须大于 0,如果小于等于 0 会默认使用 50ms 作为间隔
|
||||
* @param interval 两帧之间的间隔,必须大于 0
|
||||
*/
|
||||
setInterval(interval: number): void;
|
||||
|
||||
/**
|
||||
* 设置当前动画元素的动画执行器
|
||||
* @param animater 动画执行器
|
||||
*/
|
||||
setAnimater(animater: ITextureAnimater<T> | null): void;
|
||||
|
||||
/**
|
||||
* 设置传递给动画执行器的参数
|
||||
* @param param 传递给动画执行器的参数
|
||||
*/
|
||||
setParam(param: T | undefined): void;
|
||||
|
||||
/**
|
||||
* 暂停动画播放
|
||||
*/
|
||||
pause(): void;
|
||||
|
||||
/**
|
||||
* 继续动画播放
|
||||
*/
|
||||
resume(): void;
|
||||
|
||||
/**
|
||||
* 设置循环播放模式,单次播放模式下,播放完毕后会定格在最后一帧,否则会回到第一帧重新播放。
|
||||
* 设置后,会立刻重设播放状态,从第一帧开始重新播放。
|
||||
* @param cycled 是否为循环播放模式
|
||||
*/
|
||||
setCycled(cycled: boolean): void;
|
||||
}
|
||||
|
||||
//#endregion
|
||||
|
||||
//#region 自定义元素
|
||||
|
||||
Reference in new issue
Block a user