-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandwriting.js
211 lines (177 loc) · 5.34 KB
/
handwriting.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
211
/**
* class Handwriting
* Handles user handwriting input and displays the predicted values
*/
class Handwriting {
/**
* Declare variables and set up events
*/
constructor() {
let $ = document.getElementById.bind(document);
this.model = new Model
// set up canvas
this.drawingLineWidthEl = $("drawing-line-width")
this.clearEl = $("clear-canvas")
this.outputEl = $("output")
this.canvas = new fabric.Canvas('handwriting', {
backgroundColor: "#fff",
isDrawingMode: true
})
this.canvas.freeDrawingBrush.color = "#000"
this.resetCanvas(true)
this.resizeCanvas()
// let the user interact once the model is ready
this.model.isWarmedUp.then(this.bindEvents.bind(this))
}
/**
* Resets the canvas to a blank state and optionally removes previous predictions
* @param {bool} removeText
*/
resetCanvas(removeText = true) {
this.canvas.clear()
this.canvas.backgroundColor = "#fff"
if(removeText) {
this.outputEl.value = ""
this.model.clearInput()
}
}
/**
* Rescales the canvas according to current window dimensions
*/
resizeCanvas() {
this.canvas.setDimensions({
width: window.innerWidth,
height: window.innerHeight*0.63-28
})
this.canvas.calcOffset()
this.canvas.renderAll()
}
/**
* Captures a tight crop around the current drawing on the canvas
* and passes it to the model to make a prediction
*/
captureDrawing() {
let group = new fabric.Group(this.canvas.getObjects()),
{ left, top, width, height } = group,
scale = window.devicePixelRatio,
image = this.canvas.contextContainer.getImageData(left*scale, top*scale, width*scale, height*scale);
//this.showCapturedData(image);
//this.showRect({ left, top, width, height });
// group.animate('opacity', '0', {
// duration: 200,
// onChange: this.canvas.renderAll.bind(this.canvas),
// onComplete: () => this.resetCanvas(false)
// });
this.resetCanvas(false)
return image
}
/**
* Helper function that shows the crop around the handwriting
* @param {object} dims
*/
showRect(dims) {
let options = {
fill: 'rgba(255,127,39,.5)',
...dims
};
this.canvas.add(new fabric.Rect(options))
}
/**
* Helper function that shows the captured image data
* @param {ImageData} pixelData
*/
showCapturedData(pixelData) {
// render captured area onto a canvas for comparison
let can = document.getElementById("output"),
ctx = can.getContext("2d");
can.width = pixelData.width;
can.height = pixelData.height;
ctx.putImageData(pixelData, 0, 0);
}
/**
* Binds all necessary events for interactivity
*/
bindEvents() {
this.outputEl.placeholder = "Write below..."
this.clearEl.onclick = this.resetCanvas.bind(this)
this.drawingLineWidthEl.onchange = ({target}) => {
this.canvas.freeDrawingBrush.width = parseInt(target.value, 10) || 1
target.previousSibling.innerHTML = target.value
};
this.canvas.freeDrawingBrush.width = parseInt(this.drawingLineWidthEl.value, 10) || 1
this.drawingLineWidthEl.previousSibling.innerHTML = this.canvas.freeDrawingBrush.width
let timerId = null,
isTouchDevice = 'ontouchstart' in window,
timeOutDuration = isTouchDevice ? 400 : 800,
hasTimedOut = true;
this.canvas.on("mouse:down", (options) => {
// reset the canvas in case something was drawn previously
if(hasTimedOut) this.resetCanvas(false)
hasTimedOut = false
// clear any timer currently active
if(timerId) {
clearTimeout(timerId)
timerId = null
}
})
.on("mouse:up", () => {
// set a new timer
timerId = setTimeout(() => {
// once timer is triggered, flag it and run prediction
hasTimedOut = true
let [character, probability] = this.model.predict(this.captureDrawing())
this.outputEl.value += (true || probability > 0.5) ? character : "?"
}, timeOutDuration)
})
window.onresize = this.resizeCanvas.bind(this)
}
}
let handwriting = new Handwriting;
var TxtRotate = function(el, toRotate, period) {
this.toRotate = toRotate;
this.el = el;
this.loopNum = 0;
this.period = parseInt(period, 10) || 2000;
this.txt = '';
this.tick();
this.isDeleting = false;
};
TxtRotate.prototype.tick = function() {
var i = this.loopNum % this.toRotate.length;
var fullTxt = this.toRotate[i];
if (this.isDeleting) {
this.txt = fullTxt.substring(0, this.txt.length - 1);
} else {
this.txt = fullTxt.substring(0, this.txt.length + 1);
}
this.el.innerHTML = '<span class="wrap">'+this.txt+'</span>';
var that = this;
var delta = 300 - Math.random() * 100;
if (this.isDeleting) { delta /= 2; }
if (!this.isDeleting && this.txt === fullTxt) {
delta = this.period;
this.isDeleting = true;
} else if (this.isDeleting && this.txt === '') {
this.isDeleting = false;
this.loopNum++;
delta = 500;
}
setTimeout(function() {
that.tick();
}, delta);
};
window.onload = function() {
var elements = document.getElementsByClassName('txt-rotate');
for (var i=0; i<elements.length; i++) {
var toRotate = elements[i].getAttribute('data-rotate');
var period = elements[i].getAttribute('data-period');
if (toRotate) {
new TxtRotate(elements[i], JSON.parse(toRotate), period);
}
}
// INJECT CSS
var css = document.createElement("style");
css.type = "text/css";
css.innerHTML = ".txt-rotate > .wrap { border-right: 0.08em solid #666 }";
document.body.appendChild(css);
};