Lettering.js
Lettering.js copied to clipboard
Added custom data attributes, character escape function, and character class for kerning pairs
Changes to LetteringJS
- Copies the text to custom data attributes of the span.
- Uses the method to create data attribute i.e.
data-char
,data-word
anddata-line
- Uses the method to create data attribute i.e.
- Escapes the text before copying it using an 'escape' function. Makes sure that common HTML attributes are properly escaped: quotation marks, ampersands, less than or greater than symbols.
- Only if the 'char' method is being used it add a addition class with the letter value. Adds a class
char - item value
i.e. If the letter isa
it's adds a class ofchar-a
.- you can escape non-alphanumeric characters in css using
\
i.eclass="char-&"
can be styled.char-\&
see CSS character escape sequences for more details. - This allows you create kerning pairs. You can create kerning pair rules for all the character combos. You won't need to know the content beforehand. Use case could be on a CMS where you won't know the headings beforehand but want strict kerning rules.
- you can escape non-alphanumeric characters in css using
Use cases (codepens)
Collection of examples LetteringJS Collection
- Text effects using Custom Data Attributes on characters.
- Layered Type using Custom Data Attributes on words.
- Kerning Pairs using extra class.
Incorporates Ideas/Work from Other Pull Requests
#41 Add a character-specific class to letters
#26 Add data-content='item' attribute to the generated span tags
#6 Added additional classes
I am sure I have missed something and this approach may be overly simplistic.
Thanks, Mark
Thanks for this commit. Really pulling things together. Let's figure this out b/c there's quite a few requests that do the same thing but they've all become a little too complex.
char-* classes
The .char-
class seems a bit redundant. It seems you could style kerning pairs with a simple:
/* Edit: Updated example. It's too early. Need more coffee */
[data-char="a"] + [data-char="b"] {
/* do something */
}
Escaped characters
EDIT: Re-re-read and looked at your char-\&
example. Suppose that's fine but explaining escaped chars might be sorta difficult to designer'y types.
- Do we need quotes?
Non-latin languages
I think you've solved it the best with a limited escape set, but are we sure this problem is solved fairly well?
Next steps
What might make sense is schedule this for a v0.7
branch. We can even go so far as to write some tests to make sure features, etc are preserved. Sound good?
Hi Dave, Thanks for the quick feedback I've made some quick changes.
Removed the .char-
classes and the if
statement.
Removed the unnecessary .char-
classes and the if
statement. I also updated the Kerning Pairs demo using the [data-char="a"] + [data-char="b"]
. Great idea and it's so elegant.
Escape Characters
The escape function in the JS just escapes the copied text so that it doesn't break the html when copied to the data attribute.
Without the .char-
classes you no longer need to do the hacky css escapes .char-\&
you can use [data-char="&"]
the quotes around escape the &
already (without the quotes you need to [data-char=\&]
) Here's a codepen demoing the escape characters.
- You DO need to escape the " quote
[data-char="\""]
. - Need to come up with a solution for space character
[data-char=" "]
or[data-char="\ "]
doesn't work.
Non-latin languages and Next Steps
Needs a lot more testing for non-latin characters!
v0.7
branch makes sense. Let me know how I can help!
Need to come up with a solution for space character
[data-char=" "]
or[data-char="\ "]
doesn't work.
It might be a v1
thing since it breaks current functionality, but maybe not-wrapping spaces is a better solution. I know we've run into it not producing the correct results: http://fishslapsababy.com/2013/01/09/stuff-with-friends/ (it's a blog for kids, go easy :smile:) might make sense to deprecate wrapping spaces.
EDIT: On second thought, removing space-wrapping may actually break kerning pairs since two words would be next to each other. :frowning:
I was breaking the space character!
In the escape function I was breaking the space character!
.replace(/ /g, ' ')
was removed from line 20
I was changing space to
(which isn't the right character anyway). Now [data-char="\ "]
works. I updated the codepen.
Opened up example/index.html
<div id="demo3" class="demo">
<p>
<span class="line1" data-line="This is an amazing">This is an amazing</span>
<span class="line2" data-line=" Revolution in Typography. "> Revolution in Typography. </span>
<span class="line3" data-line=" The possibilities are endless: "> The possibilities are endless: </span>
<span class="line4" data-line=" Coloring, Vertical spacing, and Kerning."> Coloring, Vertical spacing, and Kerning.</span>
</p>
</div>
Looks like we might have to $.trim()
some whitespace from in the lettering('lines')
method. This actually came up in #42 as well. So let's add that to the milestone. Get that wrapped up in a separate commit.
Bookmarking: Escape function from #7 might be useful here for a complete escaping setup (if we still need it in the data-char
attribute)
https://github.com/davatron5000/Lettering.js/blob/80d53da7ea753af9836923dc09a23171e35161fe/jquery.lettering.js#L14-L30