Hanzi Writer
repository·master·Indexed 26 days ago
https://github.com/chanind/hanzi-writerA free and open-source JavaScript library for animating simplified and traditional Chinese character stroke orders and implementing stroke order practice quizzes. Version 3.0.0 provides tools for animating specific strokes, managing user input during quizzes, and customizing rendering via HanziWriterOptions.
What's inside hanzi-writer
- Hanzi Writer is a free and open-source JavaScript library designed for Chinese character stroke order animations and stroke order practice quizzes. It supports both simplified and traditional Chinese characters.
Access Hanzi Writer documentation and demo
masterDetailed documentation, getting started instructions, and a live demo are available at the official Hanzi Writer website.
- Documentation: https://chanind.github.io/hanzi-writer
- Live Demo: https://chanind.github.io/hanzi-writer/demo.html
Understand the Hanzi Writer data source
masterThe SVG and stroke order data used by the library is derived from the Make me a Hanzi project. The raw data is maintained in the Hanzi Writer Data repository, and a visualizer for this data is available here.Implement a custom character data loader
masterIf you are not using the default data source, you can provide a customcharDataLoaderviaLoadingManagerOptions. The function must follow theCharDataLoaderFnsignature to fetch and provideCharacterJsondata.Animate a single specific stroke
masterUse
animateSingleStroketo animate the drawing of one specific stroke within a character. This function handles the necessary state transitions to ensure the stroke appears correctly relative to the character's current opacity.Parameters:
charName: The name of the character.character: The character model instance.strokeNum: The index of the stroke to animate.speed: Controls the speed of the stroke animation.
Animate character writing in a loop
masterUse
animateCharacterLoopto create a repeating animation sequence. It performs the standardanimateCharactersequence and then adds a delay before the next loop begins.Parameters:
charName: The name of the character.character: The character model instance.fadeDuration: Duration to fade out the character.speed: Controls the speed of the stroke animation.delayBetweenStrokes: Delay in milliseconds between starting each stroke.delayBetweenLoops: Delay in milliseconds before the entire animation sequence repeats.
Manage user stroke input during a quiz
masterThese functions generate mutations to track and update the strokes a user is currently drawing during a quiz session.
startUserStroke(id, point): Initializes a new user stroke with a specific ID and the first point.updateUserStroke(userStrokeId, points): Updates the list of points for an existing user stroke.hideUserStroke(userStrokeId, duration): Fades out a user stroke by setting its opacity to 0 over a specified duration. Note that the stroke is not removed from the DOM to prevent touch event issues.removeAllUserStrokes(userStrokeIds): Removes all specified user strokes by setting them tonull.
Show or hide character strokes and visibility
masterThese functions return arrays of
GenericMutationobjects to manipulate the visual state of a character:showStrokes(charName, character, duration): Makes all strokes of a character visible with the specified duration.showCharacter(charName, character, duration): Makes the entire character (including all strokes) visible.hideCharacter(charName, character, duration?): Fades out the character's opacity and hides its strokes.showStroke(charName, strokeNum, duration): Makes a specific stroke index visible.
Start a stroke order practice quiz
masterUsestartQuizto prepare a character for a quiz session. It returns an array ofGenericMutationobjects that should be applied to the Hanzi Writer instance to hide the main character and set up the initial stroke visibility based on the providedstartStrokeNum.Animate character stroke writing sequences
masterUse
animateCharacterto create a sequence of mutations that hides the character and then animates each stroke one by one. This is useful for teaching the correct stroke order and speed.Parameters:
charName: The name of the character.character: The character model instance.fadeDuration: Duration to fade out the character.speed: Controls the speed of the stroke animation.delayBetweenStrokes: Delay in milliseconds between starting each stroke.
Define a custom RenderTarget
masterYou can override the default renderer by providing acreateRenderTargetfunction within therendererOverrideobject. This function must match theRenderTargetInitFunctionsignature.Highlight a completed character
masterUsehighlightCompleteCharto provide visual feedback when a user successfully completes a character. It updates theoptions.highlightColorand triggers a temporary visibility animation on the 'highlight' character layer.