-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdemo.js
More file actions
193 lines (180 loc) · 4.96 KB
/
demo.js
File metadata and controls
193 lines (180 loc) · 4.96 KB
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
/*
demo.js
Only used for the demo, not a dependency
*/
var target, strokeText, textSource, resizeTimeout,
tagline = 'Simple, pixel-perfect text stroking for the web.',
code = '',
demoOptions = {},
fontSize = 2.3,
fontSizeIncrement = 0.5;
window.onload = function() {
target = document.getElementById('targetId');
setFontSizeLabel();
textSource = document.getElementById('textSource');
textSource.value = tagline;
document.getElementById('tagline').innerHTML = tagline;
initStrokeText();
executeStroke();
}
function objectSize(obj) {
var size = 0, key;
for (key in obj) {
if (obj.hasOwnProperty(key)) size++;
}
return size;
};
function updateCodeVisualizer(code) {
var visualizer = document.getElementById('codeVisualizer');
visualizer.innerHTML = code;
}
function initStrokeText() {
if (strokeText) strokeText.reset();
strokeText = new StrokeText(target, demoOptions);
document.getElementById('version').innerHTML = 'v '+strokeText.version;
if (objectSize(demoOptions) > 0) {
code = 'var options = {<br>';
for (var key in demoOptions) {
if (key === 'lineDashArray') {
var lineDashArray = demoOptions[key];
code += '  '+key+': ['+lineDashArray[0]+', '+lineDashArray[1]+'],<br>'
}
else if (key === 'miterLimit' || key === 'debug') {
code += '  '+key+': '+demoOptions[key]+',<br>'
}
else {
code += '  '+key+': \''+demoOptions[key]+'\',<br>'
}
}
code += '};<br>';
code += 'var strokeText = new StrokeText(\'targetId\', options);<br>';
} else {
code = 'var strokeText = new StrokeText(\'targetId\');<br>';
}
}
function assignText() {
strokeText.reset();
var txt = textSource.value;
target.innerHTML = txt;
}
function executeStroke() {
assignText();
var strokeWidth = parseFloat(document.getElementById('strokeWidth').value);
var strokeColor = document.getElementById('strokeColor').value;
strokeText.stroke(strokeWidth, strokeColor);
code += 'strokeText.stroke('+strokeWidth+', \''+strokeColor+'\');<br>';
updateCodeVisualizer(code);
}
function resetStrokeText() {
strokeText.reset();
code += 'strokeText.reset();<br>';
updateCodeVisualizer(code);
}
function initAndExecuteStrokeText() {
// need delay to allow browser to render text style changes
setTimeout(function() {
initStrokeText();
executeStroke();
}, 100);
}
// responsive!
function handleViewportChange() {
clearTimeout(resizeTimeout);
resizeTimeout = setTimeout(function() {
resetStrokeText();
initAndExecuteStrokeText();
}, 100);
}
window.onresize = handleViewportChange;
window.onorientationchange = handleViewportChange;
/*
OPTIONS
*/
function changeLineJoin() {
var lineJoin = document.getElementById('lineJoin').value;
demoOptions.lineJoin = lineJoin;
initAndExecuteStrokeText();
}
function changeLineCap() {
var lineCap = document.getElementById('lineCap').value;
demoOptions.lineCap = lineCap;
initAndExecuteStrokeText();
}
function changeLineDashArray() {
resetStrokeText();
var line = document.getElementById('lineDashArrayLine').value;
var gap = document.getElementById('lineDashArrayGap').value;
demoOptions.lineDashArray = [line, gap];
initAndExecuteStrokeText();
}
function changeMiterLimit() {
var miterLimit = document.getElementById('miterLimit').value;
demoOptions.miterLimit = parseFloat(miterLimit);
initAndExecuteStrokeText();
}
function toggleDebug(checkbox) {
demoOptions.debug = checkbox.checked;
initAndExecuteStrokeText();
}
/*
TEXT STYLES
*/
function changeAlignment() {
resetStrokeText();
var align = document.getElementById('alignment').value;
target.style.textAlign = align;
executeStroke();
}
function changeFontStyle() {
resetStrokeText();
var fontStyle = document.getElementById('fontStyle').value;
target.style.fontStyle = fontStyle;
initAndExecuteStrokeText();
}
function changeFontWeight() {
resetStrokeText();
var weight = document.getElementById('fontWeight').value;
target.style.fontWeight = parseInt(weight);
initAndExecuteStrokeText();
}
function changeFont() {
resetStrokeText();
var font = document.getElementById('fontFamily').value;
if (font === 'SF') {
font = '-apple-system, BlinkMacSystemFont';
}
target.style.fontFamily = font;
initAndExecuteStrokeText();
}
function setFontSizeLabel() {
var fontSizeString = fontSize + 'em';
document.getElementById('fontSize').innerHTML = fontSizeString;
return fontSizeString;
}
function setFontSize() {
fontSize = Math.round(fontSize * 100) / 100;
var fontSizeString = setFontSizeLabel();
target.style.fontSize = fontSizeString;
initAndExecuteStrokeText();
}
function increaseFontSize() {
if (fontSize > 5) {
return;
}
resetStrokeText();
fontSize += fontSizeIncrement;
setFontSize();
}
function decreaseFontSize() {
var newSize = fontSize -= fontSizeIncrement;
if (newSize > 0) {
fontSize = newSize
}
resetStrokeText();
setFontSize();
}
// TODO: support text decoration
function changeDecoration() {
var decoration = document.getElementById('textDecoration').value;
target.style.textDecoration = decoration;
}