Skip to content

Commit 89ed33b

Browse files
authored
Merge pull request #78 from Ubugeeei/46-draw-stroke-orders
feat: #46 💪 Tecack.drawStrokeOrder and DrawStrokeOrderOptions
2 parents 0b66e30 + e7fd4cc commit 89ed33b

File tree

2 files changed

+109
-6
lines changed

2 files changed

+109
-6
lines changed

docs/reference/apis.md

+62
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,68 @@ Restores canvas strokes and internal data from existing strokes data.
221221
tecack.restoreFromStrokes(strokes);
222222
```
223223

224+
### Tecack.drawStrokeOrder()
225+
226+
It draws the stroke order on the strokes entered into the Canvas.
227+
228+
- #### Type
229+
230+
```ts
231+
function drawStrokeOrder(options?: DrawStrokeOrderOptions): void | CanvasCtxNotFoundError;
232+
```
233+
234+
- #### Example
235+
236+
```ts
237+
tecack.drawStrokeOrder();
238+
```
239+
240+
### DrawStrokeOrderOptions
241+
242+
Options for `Tecack.drawStrokeOrder()`
243+
244+
- #### Type
245+
246+
```ts
247+
export interface DrawStrokeOrderOptions {
248+
withColor?: boolean;
249+
colorSet?: Array<string>;
250+
}
251+
```
252+
253+
#### DrawStrokeOrderOptions.withColor
254+
255+
When `withColor` is enabled, it draws each stroke in a different color.
256+
This is to make it easier to understand which stroke order corresponds to which stroke.
257+
Please refer to the `colorSet` to see which colors will be used for the drawing.
258+
259+
default: `false`
260+
261+
#### DrawStrokeOrderOptions.colorSet
262+
263+
This is the set of colors used when `withColor` is enabled.
264+
The index of the color set (array) corresponds to the stroke order, and if the stroke order exceeds the length of the color set, it will cycle through.
265+
266+
::: warning
267+
Please note that the colors (strings) registered as `colorSet` **must be six-digit hexadecimal numbers starting with `#`**.
268+
(Shortened three-digit notation, specific color names, and rgb notation cannot be used.)
269+
:::
270+
271+
default:
272+
273+
```ts
274+
// prettier-ignore
275+
/** based on https://kanjivg.tagaini.net/viewer.html */
276+
[
277+
"#bf0000", "#bf5600", "#bfac00", "#7cbf00", "#26bf00",
278+
"#00bf2f", "#00bf85", "#00a2bf", "#004cbf", "#0900bf",
279+
"#5f00bf", "#b500bf", "#bf0072", "#bf001c", "#bf2626",
280+
"#bf6b26", "#bfaf26", "#89bf26", "#44bf26", "#26bf4c",
281+
"#26bf91", "#26a8bf", "#2663bf", "#2d26bf", "#7226bf",
282+
"#b726bf", "#bf2682", "#bf263d", "#bf4c4c", "#bf804c",
283+
]
284+
```
285+
224286
## @tecack/backend
225287

226288
### recognize()

packages/frontend/src/index.ts

+47-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,31 @@ export interface TecackOptions {
77
backgroundPainter?: (el: HTMLCanvasElement) => void;
88
}
99

10+
export interface DrawStrokeOrderOptions {
11+
/** default: `false` */
12+
withColor?: boolean;
13+
14+
/**
15+
* default:
16+
* ```ts
17+
* [
18+
* "#bf0000", "#bf5600", "#bfac00", "#7cbf00", "#26bf00",
19+
* "#00bf2f", "#00bf85", "#00a2bf", "#004cbf", "#0900bf",
20+
* "#5f00bf", "#b500bf", "#bf0072", "#bf001c", "#bf2626",
21+
* "#bf6b26", "#bfaf26", "#89bf26", "#44bf26", "#26bf4c",
22+
* "#26bf91", "#26a8bf", "#2663bf", "#2d26bf", "#7226bf",
23+
* "#b726bf", "#bf2682", "#bf263d", "#bf4c4c", "#bf804c",
24+
* ]
25+
* ```
26+
* color coded stroke colors (for 30 strokes)
27+
*
28+
* based on https://kanjivg.tagaini.net/viewer.html
29+
*
30+
* NOTE: must be 6-digit hex color codes (e.g. `#ff0000`)
31+
*/
32+
colorSet?: Array<string>;
33+
}
34+
1035
export interface Tecack {
1136
$el: HTMLCanvasElement | null;
1237

@@ -35,7 +60,7 @@ export interface Tecack {
3560
draw: (color?: string) => void | CanvasCtxNotFoundError;
3661
deleteLast: () => void | CanvasCtxNotFoundError;
3762
erase: () => void | CanvasCtxNotFoundError;
38-
redraw: () => void | CanvasCtxNotFoundError;
63+
drawStrokeOrder: (options?: DrawStrokeOrderOptions) => void | CanvasCtxNotFoundError;
3964
restoreFromStrokes(
4065
strokesMut: Readonly<Array<TecackStroke>>,
4166
options?: {
@@ -177,7 +202,14 @@ export function createTecack(options?: TecackOptions): Tecack {
177202
_canvas && options?.backgroundPainter?.(_canvas);
178203
},
179204

180-
redraw: () => {
205+
drawStrokeOrder: opt => {
206+
const defaultOptions: Required<DrawStrokeOrderOptions> = {
207+
withColor: false,
208+
colorSet: STROKE_COLORS,
209+
};
210+
const mergedOptions = { ...defaultOptions, ...opt };
211+
const BASE_COLOR = "#333333";
212+
181213
if (!_ctx) {
182214
return createCanvasError();
183215
}
@@ -194,7 +226,7 @@ export function createTecack(options?: TecackOptions): Tecack {
194226

195227
_currX = stroke_i[j + 1][0];
196228
_currY = stroke_i[j + 1][1];
197-
tecack.draw(STROKE_COLORS[i]);
229+
tecack.draw(mergedOptions.withColor ? mergedOptions.colorSet[i % mergedOptions.colorSet.length] : BASE_COLOR);
198230
}
199231
}
200232

@@ -209,11 +241,20 @@ export function createTecack(options?: TecackOptions): Tecack {
209241

210242
// outline
211243
_ctx.lineWidth = 3;
212-
_ctx.strokeStyle = _alterHex(STROKE_COLORS[i] ? STROKE_COLORS[i] : "#333333", 60, "dec");
244+
_ctx.strokeStyle = _alterHex(
245+
mergedOptions.withColor && mergedOptions.colorSet[i % mergedOptions.colorSet.length]
246+
? mergedOptions.colorSet[i % mergedOptions.colorSet.length]
247+
: BASE_COLOR,
248+
60,
249+
"dec",
250+
);
213251
_ctx.strokeText((i + 1).toString(), x, y);
214252

215253
// fill
216-
_ctx.fillStyle = STROKE_COLORS[i] ? STROKE_COLORS[i] : "#333";
254+
_ctx.fillStyle =
255+
mergedOptions.withColor && mergedOptions.colorSet[i % mergedOptions.colorSet.length]
256+
? mergedOptions.colorSet[i % mergedOptions.colorSet.length]
257+
: BASE_COLOR;
217258
_ctx.fillText((i + 1).toString(), x, y);
218259
}
219260
}
@@ -283,7 +324,7 @@ export function createTecack(options?: TecackOptions): Tecack {
283324
normalizedPattern.push(normalized_stroke_i);
284325
}
285326
_recordedPattern = normalizedPattern;
286-
tecack.redraw();
327+
// tecack.redraw(); // FIXME:
287328
},
288329

289330
getStrokes: () => {

0 commit comments

Comments
 (0)