-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrollText.js
72 lines (63 loc) · 2.67 KB
/
scrollText.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
const font = require('./font');
const materix = require('./materixSimulation'); //placeholder
var tickTimeout;
setText({
text: "Hello World!",
endX: 9
});
function setText(conf) {
var c = {
text: conf.text || "",
scrollSpeed: conf.scrollSpeed || 100,
font: conf.font || "5x7",
charSpacing: conf.charSpacing || 1, //spacing between characters
textColor: conf.textColor || [255, 255, 255],
backColor: conf.backColor || [0, 0, 0],
startX: conf.startX || 0,
startY: conf.startY || 0,
endX: conf.endX || 4,
iterationCounter: 0,
};
tickTimeout = setTimeout(() => { //start the scrollText | TODO: maybe provide an additional start function
scrollTick(c);
}, c.scrollSpeed);
}
function stop() {
clearTimeout(tickTimeout);
}
function scrollTick(conf) {
var charWidth = font[conf.font].width + conf.charSpacing;
var offset = conf.endX - conf.startX + 1; //offset for the spacing before (and after) the actual text
for (var x = 0; x < offset; x++) {
if (conf.iterationCounter + x < conf.endX - conf.startX || conf.iterationCounter + x >= offset + (conf.text.length * charWidth)) {
var curChar = 32; //space
var curCharColumn = 0; //always print back Color
} else {
var curCharIdx = (conf.iterationCounter - offset + x) / charWidth;
var curCharColumn = (conf.iterationCounter - offset + x) % charWidth; //get column count of the current character (which column of the font to draw)
curChar = conf.text.charCodeAt(curCharIdx);
}
var fontColumn = font[conf.font].data[curChar - 32][curCharColumn]; //get the correct font data column
for (var y = 0; y < font[conf.font].height; y++) {
if (curCharColumn < font[conf.font].width) { //if column to draw is not a spacing column (between characters)
var fontBit = (128 >> y) & fontColumn; //get the right bit from the font column data
materix.setPixel(x + conf.startX, y + conf.startY, fontBit ? conf.textColor : conf.backColor);
} else {
materix.setPixel(x + conf.startX, y + conf.startY, conf.backColor);
}
}
}
conf.iterationCounter++;
if (conf.iterationCounter <= offset + conf.text.length * charWidth) //if the text is not over
{
tickTimeout = setTimeout(() => { //schedule the next tick
scrollTick(conf);
}, conf.scrollSpeed);
} else {
materix.stop(); //only needed for clean stopping of the simulation (not needed for real MATErix) | TODO: Remove before prod
}
}
module.exports = {
setText,
stop
};