forked from danmarshall/google-font-to-svg-path
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
79 lines (79 loc) · 3.51 KB
/
index.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
var makerjs = require('makerjs');
var App = /** @class */ (function () {
function App() {
var _this = this;
this.renderCurrent = function () {
var size = _this.sizeInput.valueAsNumber;
if (!size)
size = parseFloat(_this.sizeInput.value);
if (!size)
size = 100;
_this.render(_this.selectFamily.selectedIndex, _this.selectVariant.selectedIndex, _this.textInput.value, size, _this.unionCheckbox.checked, _this.separateCheckbox.checked, parseFloat(_this.bezierAccuracy.value) || undefined);
};
this.loadVariants = function () {
_this.selectVariant.options.length = 0;
var f = _this.fontList.items[_this.selectFamily.selectedIndex];
var v = f.variants.forEach(function (v) { return _this.addOption(_this.selectVariant, v); });
_this.renderCurrent();
};
}
App.prototype.init = function () {
this.selectFamily = this.$('#font-select');
this.selectVariant = this.$('#font-variant');
this.unionCheckbox = this.$('#input-union');
this.separateCheckbox = this.$('#input-separate');
this.textInput = this.$('#input-text');
this.bezierAccuracy = this.$('#input-bezier-accuracy');
this.sizeInput = this.$('#input-size');
this.renderDiv = this.$('#svg-render');
this.outputTextarea = this.$('#output-svg');
};
App.prototype.handleEvents = function () {
this.selectFamily.onchange = this.loadVariants;
this.selectVariant.onchange = this.textInput.onchange = this.textInput.onkeyup = this.sizeInput.onchange = this.unionCheckbox.onchange = this.separateCheckbox.onchange = this.bezierAccuracy.onchange = this.renderCurrent;
};
App.prototype.$ = function (selector) {
return document.querySelector(selector);
};
App.prototype.addOption = function (select, optionText) {
var option = document.createElement('option');
option.text = optionText;
select.options.add(option);
};
App.prototype.getGoogleFonts = function (apiKey) {
var _this = this;
var xhr = new XMLHttpRequest();
xhr.open('get', 'https://www.googleapis.com/webfonts/v1/webfonts?key=' + apiKey, true);
xhr.onloadend = function () {
_this.fontList = JSON.parse(xhr.responseText);
_this.fontList.items.forEach(function (font) { return _this.addOption(_this.selectFamily, font.family); });
_this.loadVariants();
_this.handleEvents();
};
xhr.send();
};
App.prototype.render = function (fontIndex, variantIndex, text, size, union, separate, bezierAccuracy) {
var _this = this;
var f = this.fontList.items[fontIndex];
var v = f.variants[variantIndex];
var url = f.files[v].substring(5); //remove http:
opentype.load(url, function (err, font) {
//generate the text using a font
var textModel = new makerjs.models.Text(font, text, size, union, false, bezierAccuracy);
if (separate) {
for (var i in textModel.models) {
textModel.models[i].layer = i;
}
}
var svg = makerjs.exporter.toSVG(textModel);
_this.renderDiv.innerHTML = svg;
_this.outputTextarea.value = svg;
});
};
return App;
}());
var app = new App();
window.onload = function () {
app.init();
app.getGoogleFonts('AIzaSyAOES8EmKhuJEnsn9kS1XKBpxxp-TgN8Jc');
};