Skip to content

Commit f08dab3

Browse files
committed
feat(DotImageCanvas, DotTextCanvas): enhance constructors to accept options objects and add new properties for better configuration
1 parent 05939c7 commit f08dab3

File tree

3 files changed

+248
-17
lines changed

3 files changed

+248
-17
lines changed

src/canvas/DotImageCanvas.ts

Lines changed: 109 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,103 @@ import { useRic } from '../perf/useRic'
33
import { createElement } from '../event/createElement'
44
import { insertElement } from '../event/insertElement'
55
import { removeElement } from '../event/removeElement'
6-
import type { Direction, MaybeElement } from '../types'
6+
import type { Direction, DotImageCanvasOptions, MaybeElement } from '../types'
77
import { useRaf } from '../perf'
88

9+
/**
10+
* DotImageCanvas 将图片转换为点阵图形式展示,并提供动画绘制效果
11+
*
12+
* 支持多种绘制方向,颜色控制,背景设置,以及动画效果控制
13+
* @class
14+
*/
915
export class DotImageCanvas {
16+
/** 用于绘制的 Canvas 元素 */
1017
canvas = document.createElement('canvas')
18+
19+
/** Canvas 的绘制上下文 */
1120
ctx = this.canvas.getContext('2d')!
21+
22+
/** 缓存已处理图像的点阵数据 */
1223
points: Map<string, Record<string, any>> = new Map()
24+
25+
/** 原始图片源地址 */
1326
originSrc = ''
27+
28+
/** 点阵图颜色,设置后将覆盖原图颜色 */
1429
color = ''
30+
31+
/** 点阵粗细,影响绘制的圆点大小 */
1532
fontWeight = 1
33+
34+
/** 绘制状态:pending-绘制中,success-完成,fail-失败,reverted-已撤销 */
1635
status = 'pending'
36+
37+
/** 背景颜色 */
1738
bgColor?: string = '#fff'
39+
40+
/** 停止当前动画的函数 */
1841
stop: () => void = () => {}
42+
43+
/** 绘制方向 */
1944
direction: Direction = 'horizontal'
2045

21-
// Enhanced state tracking for better revert
46+
/** 所有绘制任务 */
2247
allTasks: Function[] = []
48+
49+
/** 已完成的任务索引 */
2350
completedTaskIndex = 0
51+
52+
/** 是否正在撤销绘制 */
2453
isReverting = false
25-
drawnPoints: Array<{ x: number, y: number, color: any }[]> = [] // Store points per task
2654

27-
// Array to store clear functions
55+
/** 每个绘制任务中的点坐标记录 */
56+
drawnPoints: Array<{ x: number, y: number, color: any }[]> = []
57+
58+
/** 清除任务列表 */
2859
clearTasks: Function[] = []
60+
61+
/** 是否使用优先渲染模式 (RAF) */
2962
isPreferred = false
63+
64+
/** 是否已挂载到DOM */
3065
mounted = false
3166

67+
/**
68+
* 创建 DotImageCanvas 实例
69+
*
70+
* @param {string | DotImageCanvasOptions} srcOrOptions - 图片URL或选项对象
71+
* @param {string} [color] - 绘制颜色,为空时保留原图颜色
72+
* @param {number} [fontWeight] - 点阵粗细
73+
* @param {string} [bgColor] - 背景颜色
74+
* @param {Direction} [direction] - 绘制方向
75+
*/
3276
constructor(
33-
src: string,
34-
color: string,
35-
fontWeight: number,
36-
bgColor = '#fff',
77+
srcOrOptions: string | DotImageCanvasOptions,
78+
color?: string,
79+
fontWeight?: number,
80+
bgColor: string = '#fff',
3781
direction?: Direction,
3882
) {
39-
this.initOptions(src, color, fontWeight, bgColor, direction)
83+
if (typeof srcOrOptions === 'string') {
84+
// 原始参数列表模式
85+
this.initOptions(srcOrOptions, color!, fontWeight!, bgColor, direction)
86+
}
87+
else {
88+
// 选项对象模式
89+
const options = srcOrOptions
90+
this.initOptions(
91+
options.src,
92+
options.color,
93+
options.fontWeight,
94+
options.bgColor || '#fff',
95+
options.direction,
96+
)
97+
98+
// 设置其他选项
99+
if (options.isPreferred !== undefined) {
100+
this.isPreferred = options.isPreferred
101+
}
102+
}
40103
this.executor()
41104
}
42105

@@ -522,22 +585,54 @@ export class DotImageCanvas {
522585
this.direction = direction
523586
}
524587

588+
/**
589+
* 重新绘制图像,可以更新配置
590+
*
591+
* @param {string | Partial<DotImageCanvasOptions>} srcOrOptions - 图片URL或选项对象
592+
* @param {string} [color] - 点阵颜色
593+
* @param {number} [fontWeight] - 点阵粗细
594+
* @param {string} [bgColor] - 背景颜色
595+
* @param {Direction} [direction] - 绘制方向
596+
* @returns {Promise<DotImageCanvas>} 更新后的实例
597+
*/
525598
async repaint(
526-
src: string,
527-
color: string,
528-
fontWeight: number,
529-
bgColor = '#fff',
599+
srcOrOptions: string | Partial<DotImageCanvasOptions>,
600+
color?: string,
601+
fontWeight?: number,
602+
bgColor: string = '#fff',
530603
direction?: Direction,
531604
) {
532605
this.stop()
533606
const p = removeElement(this.canvas)
534607
this.status = 'pending'
535-
this.initOptions(src, color, fontWeight, bgColor, direction)
608+
609+
if (typeof srcOrOptions === 'string') {
610+
// 原始参数列表模式
611+
this.initOptions(srcOrOptions, color!, fontWeight!, bgColor, direction)
612+
}
613+
else {
614+
// 选项对象模式
615+
const options = srcOrOptions
616+
this.initOptions(
617+
options.src || this.originSrc,
618+
options.color || this.color,
619+
options.fontWeight !== undefined ? options.fontWeight : this.fontWeight,
620+
options.bgColor || this.bgColor || '#fff',
621+
options.direction || this.direction,
622+
)
623+
624+
// 更新其他选项
625+
if (options.isPreferred !== undefined) {
626+
this.isPreferred = options.isPreferred
627+
}
628+
}
629+
536630
if (!p) {
537631
throw new Error(
538632
'repaint error not found canvas container or has been removed',
539633
)
540634
}
635+
541636
return Object.assign(
542637
this,
543638
(await this.executor()) as DotImageCanvas,

0 commit comments

Comments
 (0)