mirror of
https://github.com/motajs/template.git
synced 2026-11-23 14:04:30 +08:00
feat: 可动画贴图
This commit is contained in:
1 parent
ed693feb40
commit
6c81872040
6 files changed
+198
-10
No files matched your search
@@ -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