-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
executable file
·191 lines (140 loc) · 3.69 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
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
#!/usr/bin/env node
(function() {
// Requires
var ansi = require('ansi')
, cursor = ansi(process.stdout)
, stdo = process.stdout
, cols = stdo.columns
, rows = stdo.rows
, defaultChar = ' '
, PI2 = Math.PI*2
, color = {
fg:{ r: 255, g: 255, b: 255 },
bg:{ r: 255, g: 255, b: 255 }
}
;
var axel = {
// Clears a block
scrub: function(x1, y1, w, h){
// Turn off the color settings while we scrub
var oldBrush = this.defaultChar;
cursor.reset();
this.defaultChar = ' ';
this.box(x1, y1, w, h);
// Put the colors back after
cursor.fg.rgb(color.fg.r, color.fg.g, color.fg.b);
cursor.bg.rgb(color.bg.r, color.bg.g, color.bg.b);
this.defaultChar = oldBrush;
},
clear:function () {
console.log('\033[2J');
},
// Changes the foreground character █ default is [space]
set brush (character){
defaultChar = character || ' ';
},
get brush (){
return defaultChar;
},
cursorInterface: {
on: function () { cursor.show(); },
off: function () { cursor.hide(); },
// Resets background & foreground colors
reset: function () { cursor.reset(); },
// Restores colors and places cursor after the graphics
// so that the drawing does not get drawn over when the
// program ends
restore: function () {
cursor.reset();
cursor.goto(axel.cols, axel.rows-1);
}
},
get cursor() {
return this.cursorInterface;
},
get rows (){
return stdo.rows;
},
get cols (){
return stdo.columns;
},
goto: function(x, y){
cursor.goto(parseInt(x), parseInt(y));
},
point: function (x, y, char){
if(!(
x < 0 || y < 0 ||
x > stdo.columns || y > stdo.rows ||
x < 0 || y < 0 ||
x > stdo.columns || y > stdo.rows
)){
cursor.goto(parseInt(x), parseInt(y)).write(char || defaultChar);
}
},
// Get in interpolation point between two points at a given magnitude
lerp: function (p1, p2, m) {
return ((p2 - p1) * m) + p1;
},
circ: function (x, y, m) {
var res = m*PI2
, i
;
for(i=0; i< res; i+=1){
var loc = PI2/res * i;
this.point(x+Math.sin(loc)*m,y+Math.cos(loc)*m/2);
}
},
box: function (x1, y1, w, h) {
var line = ''
, x
, y
;
for (x=0; x< w; x+=1) {
line+=this.brush;
}
for (y=0; y< h; y+=1) {
cursor.goto(x1,y1+y).write(line);
}
},
// Get the distance between two points
dist: function (x1, y1, x2, y2){
return Math.sqrt(Math.pow(x2-x1,2) + Math.pow(y2-y1,2));
},
// Get all the points along a line and draw them
line: function (x1, y1, x2, y2) {
var D = this.dist(x1, y1, x2, y2)+1;
// this.point(x1, y1);
for(var i=0; i< D; i++){
var m = 1 / D * i
, x = this.lerp(x1, x2, m)
, y = this.lerp(y1, y2, m)
;
this.point(x, y);
}
// this.point(x2, y2);
},
color: function (hex, g, b, a) {
// if
},
text: function(x, y, text){
cursor.goto(x,y).write(text);
},
// moveTo: function (x, y) {
// cursor.moveTo()
// },
// Changes foreground color
fg: function (r, g, b) {
cursor.fg.rgb(r,g,b);
},
// Changes background color
bg: function (r, g, b) {
cursor.bg.rgb(r,g,b);
},
draw: function(cb){
with(this){
cb();
}
}
};
module.exports = axel;
}());