Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Change stroke color of existing HanziWriter #87

Closed
nahanil opened this issue Sep 30, 2018 · 4 comments
Closed

Question: Change stroke color of existing HanziWriter #87

nahanil opened this issue Sep 30, 2018 · 4 comments

Comments

@nahanil
Copy link

nahanil commented Sep 30, 2018

Is there any way to change the color of a HanziWriter that already exists in the DOM without messing with its internal variables?

Looks like I could make it work by hacking on hw._canvas.svg / hw._renderState.state.character.main.strokeColor, but that doesn't seem like a great idea 😈

Alternatively, looks like I might be able to use CSS to colour the paths instead of relying on a JS way..

svg g g:nth-child(2) path {
  stroke: hotpink;
}

Not sure if there's a better way :)

@chanind
Copy link
Owner

chanind commented Sep 30, 2018

There's currently not a method to do this, but it should be relatively easy to add. Just need to add a mutation in characterActions.js which changes some of the color variables and then add a method to HanziWriter.js to initiate it. I'll see if I can get a PR together implementing this.

@nahanil
Copy link
Author

nahanil commented Oct 1, 2018

After taking a look at the options and given my use case I'm thinking that colouring the characters with CSS would actually be for the best - assuming it works in enough browsers (probably 'sorry IE users' kinda moment).

Basically, I'm trying to have characters coloured according to tone, with users able to select their choice of tone colours from a set list of common colouring schemes/define their own and have them updated without a page refresh as part of a Vue-based 'single-page' app deely - similar to the Hanping dictionary.
Turns out using CSS I can just swap out class names on the <body /> or some other wrapper, ie tone-coloring-<scheme> and it'll take care of things across the board - plain text & the SVGs from HanziWriter...

For anyone else who finds this, I think I've settled on something like the following using, the tone colors mentioned by Albert Wolfe.

.tone-colors.tone-colors-dummit.color-char .char.t1 svg g g:nth-child(2) path {
  stroke: #f41d2f;
}

@chanind chanind mentioned this issue Oct 1, 2018
@chanind
Copy link
Owner

chanind commented Oct 1, 2018

Using CSS like you described should work. I also went ahead and adding a PR to do this via a method on the HanziWriter instance in #88, since this seems like a reasonable feature to expect should exist. It turned out to be slightly trickier than I thought originally since tweening colors requires parsing color strings and turning them into corresponding color values, but it wasn't too bad. I'll get this merged soon.

@chanind
Copy link
Owner

chanind commented Oct 1, 2018

writer.updateColor(colorName, colorVal) is now live in v1.1.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants