chanind / hanzi-writer

Chinese character stroke order animations and practice quizzes
https://hanziwriter.org
MIT License
3.47k stars 539 forks source link

How do I know the previous stroke and the next stroke? #222

Open lixue128 opened 3 years ago

lixue128 commented 3 years ago

How do I know the previous stroke and the next stroke? for example: Click the previous stroke to display the previous stroke,Not all of the strokes are shown

yxxhero commented 3 years ago

You can do it same as below:

  animationWriter = HanziWriter.create('animation-target', "我", {
    width: 300,
    height: 300,
    showOutline: true,
    showCharacter: false
  });

totalStroke = animationWriter._character.strokes.length

animationWriter.animateStroke(0)
animationWriter.animateStroke(1)
animationWriter.animateStroke(2)
...
animationWriter.animateStroke(totalStroke  -1 )

@chanind is right? Thanks very much.

yxxhero commented 3 years ago

I think it is a good idea that add preStroke and nextStroke for HanziWriter instance. @chanind

chanind commented 3 years ago

What you wrote should work @yxxhero, but it's awkward because animationWriter._character might be null until the character data can load successfully. I think it makes sense to add a new method to get the current character as a promise, like animationWriter.getCharacter(). Adding a animateNextStroke() method or something similar is interesting too, I can see it being useful to make it easier to animate characters by clicking a button. animatePreviousStroke() is tricky though - would that remove the current stroke that's drawn first before animating the previous stroke? I think for the previous stroke case it might be better to use animateStoke(strokeNum) explicitly to make things clear. I'll work on a PR for this!

yxxhero commented 3 years ago

Thanks your answer. In the chrome console, animationWriter._character is ok, but in the nodejs, it is not ok. why??? @chanind thanks very much.

yxxhero commented 3 years ago

animatePreviousStroke() is tricky though - would that remove the current stroke that's drawn first before animating the previous stroke?
Yes

chanind commented 3 years ago

It might be that _character is null at first because the writer hasn't finished loading the character data yet. I'm hoping adding a proper method to get the character will solve that problem

yxxhero commented 3 years ago

Thanks very much! 太谢谢您了,感谢您百忙之中能回复。@chanind

chanind commented 3 years ago

v3.1.0 adds a writer.getCharacterData() method that you can use to get the contents of writer._character after the data has loaded. The method returns a promise which will contain this data. For instance, you could do:

const character = await writer.getCharacterData();
const numStrokes = character.strokes.length;

Hopefully this should make it easier to work with character data in both node and browser!

yxxhero commented 3 years ago

Thanks very much!