-
Notifications
You must be signed in to change notification settings - Fork 0
/
element-functions-set_text_gradient.js
210 lines (168 loc) · 5.31 KB
/
element-functions-set_text_gradient.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
* setTextGradient
*
* create text gradients on a elements TextNodes, including all children
*
* @author David Clews
* @version 1.0.0
* @authorUrl http://davidclews.com
* @repoUrl http://github.com/webciter/setTextGradient
* @licence MIT
*
*/
/*
Copyright 2019 David Clews
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
Element.prototype.setTextGradient = function(){
let self = this;
let wrappers = []; // save references to the wrappers
let init = function(){
createCSSRules();
createGradient();
self.hasTextGradientBranch = true; /* meaning this element controls a branch of textGradient wrappers */
}
/* createCSSRules
* create the common CSS used in all text gradient classes
* @return {undefined}
*/
let createCSSRules = function(){
// create the common CSS rule applied to all setGradientText
if(!document.getElementById("setGradientText_common_rules")){
let setGradientTextCommon = document.createElement("style");
setGradientTextCommon.id = "setGradientText_common_rules";
setGradientTextCommon.setAttribute("type", "text/css");
setGradientTextCommon.textContent =
`
.textGradient{
background-clip: text;
-webkit-background-clip: text;
color: transparent;
}
`;
document.getElementsByTagName("head")[0].appendChild(setGradientTextCommon)
}
}
/*
* textNodeWalker
*
* get all the TextNode's in the children
*
* @param {HTMLElement} el
* @return {array} An array of TextNode
*/
let textNodeWalker = function(el){
let n, a=[], walk=document.createTreeWalker(el,NodeFilter.SHOW_TEXT,null,false);
while(n=walk.nextNode()) a.push(n);
return a;
}
/* characterCount
*
* @param {string} str
* @return {integer}
*/
let characterCount = function(str) {
return str.trim().length;
}
/*
* textNodeWrapper
*
* Wrap all TextNode with a span, which we use to extract the background for the effect
* @param {TextNode} textNode
* @return {undefined}
*/
let textNodeWrapper = function(textNode){
if(characterCount(textNode.wholeText) >= 1){
if(textNode.parentNode.classList.contains("textGradient") === true){
textNodeUnwrapper(textNode.parentNode);
}
// create wrapper container
var wrapper = document.createElement('span');
wrapper.classList.add("textGradient");
wrappers.push(wrapper);
// insert wrapper before el in the DOM tree
textNode.parentNode.insertBefore(wrapper, textNode);
// move el into wrapper
wrapper.appendChild(textNode);
}
}
/*
* textNodeUnwrapper
*
* Unwrap a TextNode
*
* @param {HTMLElement} el
* @return {undefined}
*/
let textNodeUnwrapper = function(el){
var parent = el.parentNode; // get the element's parent node
while (el.firstChild){
parent.insertBefore(el.firstChild, el); // move all children out of the element
}
parent.removeChild(el); // remove the empty element
}
/*
* createGradient
*
* Creates a Linear gradient on the Text within the Element
* @return {undefined}
*/
let createGradient = function(){
let textNodes = textNodeWalker(self);
for(let = i =0;i<textNodes.length;i++){
textNodeWrapper(textNodes[i]);
}
}
/*
* setTextGradientClass
* sets the actual class used to apply the text gradient
* @return {undefined}
*/
self.setTextGradientClass = function(className){
for(
let i = 0;
i<wrappers.length;
i++){
// remove old class for textGradient Styles
let allClasses = Array.from(wrappers[i].classList)
for(let a = 0;a<allClasses.length; a++){
if(allClasses[a] !== "textGradient"){
// check to see if the DOM still contains the Element
if(self.contains(wrappers[i])){
wrappers[i].classList.remove(allClasses[a]);
}else{
//remove from wrappers array, no longer needed
wrappers.splice(i, 1);
i--; // conpensate for this loop
}
}
}
wrappers[i].classList.add(className);
}
}
/*
* destoryTextGradient
* remove the applied text gradient from the element
* @return {undefined}
*/
self.destroyTextGradient = function(){
// remove the wrappers from the dom
for(let i = 0;i<wrappers.length;i++){
//unwrap textnodes
if(self.contains(wrappers[i])){
textNodeUnwrapper(wrappers[i]);
}
}
wrappers = [];
self.hasTextGradientBranch = void 0;
}
init();
}