Files
doodle-mini/miniprogram/chinesePages/shared/draw/drawServiceFactory.ts
T
2026-05-09 09:56:16 +08:00

61 lines
1.5 KiB
TypeScript

/**
* 识字绘制服务工厂
* 根据模板类型创建对应的绘制服务实例
*
* 支持的模板类型:
* - grid:网格模板
* - find:找字模板
*
* 支持的绘制服务:
* - TextDrawService:文字涂色服务
* - FindWordDrawService:找字涂色服务
*/
import TextDrawService from './textDrawService';
import FindWordDrawService from './findWordDrawService';
/**
* 绘制服务接口
*/
export interface IDrawService {
draw(list: Array<{ color: string; word: string }>): Promise<void>;
clear(): void;
setPaper(): void;
}
/**
* 模板类型
*/
export type TemplateType = 'grid' | 'find';
/**
* 绘制服务工厂类
* 根据模板类型创建对应的绘制服务实例
*/
export class DrawServiceFactory {
/**
* 创建绘制服务实例
* @param templateType 模板类型
* @param canvas Canvas 对象
* @param ctx 渲染上下文
* @param options 可选配置
* @returns 绘制服务实例
*/
static create(
templateType: TemplateType,
canvas: Canvas,
ctx: RenderingContext,
options?: Record<string, any>,
): IDrawService {
switch (templateType) {
case 'grid':
return new TextDrawService(canvas, ctx, options);
case 'find':
return new FindWordDrawService(canvas, ctx, options);
default:
// 默认使用网格模板
return new TextDrawService(canvas, ctx, options);
}
}
}