xxxxxxxxxx
1
<canvas id="mdbs2" width="700" height="700"></canvas>
2
<span>y=</span>
3
<input type="text" name="function" id="finput" value="sin(x)-(x)">
4
<button onclick="draw()">Submit</button>
xxxxxxxxxx
1
canvas{
2
background-color: #eee;
3
}
xxxxxxxxxx
1
const canvas = document.getElementById("mdbs2");
2
const ctx = canvas.getContext("2d");
3
const w = canvas.width;
4
const h = canvas.height;
5
const pi = Math.PI;
6
7
const axisWidth = 2;
8
let scale = 20;
9
let fun = getf()
10
ctx.translate(w/2, h/2)
11
12
function tan(x){
13
return Math.tan(x);
14
}
15
function sin(x){
16
return Math.sin(x);
17
}
18
function cos(x){
19
return Math.cos(x);
20
}
21
function log(x){
22
return Math.log(x);
23
}
24
function sqrt(x){
25
return Math.sqrt(x);
26
}
27
function rect(rx, ry, rw, rh, rc) {
28
ctx.beginPath()
29
ctx.rect(rx, ry, rw, rh);
30
ctx.fillStyle = rc;
31
ctx.fill();
32
ctx.closePath();
33
}
34
function txt(text, x, y, size="7px", font="Arial"){
35
ctx.font = size+" "+font;
36
ctx.fillText(text, x, y)
37
}
38
function arc(ax, ay, ar=axisWidth, ac="#2bbbad"){
39
ctx.beginPath()
40
ctx.arc(ax, ay, ar, 0, 2*Math.PI)
41
ctx.fillStyle=ac;
42
ctx.fill();
43
ctx.closePath();
44
}
45
function line(lx1, ly1, lx2, ly2, lc="#2bbbad", lw=axisWidth) {
46
ctx.beginPath()
47
ctx.moveTo(lx1, ly1)
48
ctx.lineTo(lx2, ly2);
49
ctx.strokeStyle = lc;
50
ctx.lineWidth = lw;
51
ctx.stroke();
52
ctx.closePath();
53
}
54
function drawAxis() {
55
let color = "#2bbbad"
56
line(-w/2, 0, w/2, 0, color, axisWidth);
57
line(-10, -h/2 + 12, 0, -h/2 + 1, color, axisWidth);
58
line(0, -h/2 + 1, 10, -h/2 + 12, color, axisWidth);
59
60
line(0, -h/2, 0, h/2, color, axisWidth);
61
line(w/2 - 12, -10, w/2 - 1, 0, color, axisWidth);
62
line(w/2 - 1, 0, w/2 - 12, 10, color, axisWidth);
63
}
64
function drawScale(s){
65
let r=2;
66
let c1="#ddd"
67
let wl=1;
68
for(let i = 0; i<w/2; i+=s){
69
line(i, -w/2, i, w/2, c1, wl);
70
line(-i, -w/2, -i, w/2, c1, wl);
71
line(-h/2, i, h/2, i, c1, wl);
72
line(-h/2, -i, h/2, -i, c1, wl);
73
arc(i, 0, r);
74
arc(-i, 0, r);
75
arc(0, i, r);
76
arc(0, -i, r);
77
if(i!=0){
78
txt(i/s, i-2, 10)
79
txt(-i/s, -i+2, 10)
80
txt(i/s, 10, i)
81
txt(-i/s, 10, -i)
82
}
83
}
84
}
85
function multiplication(t){
86
87
88
for(let i = 1; i<t.length; i++){
89
if(t[i]=="x"&&!isNaN(t[i-1])){
90
let p1 = t.substring(0, i);
91
let p2 = t.substring(i);
92
t=p1.concat("*", p2);
93
}
94
95
}
96
for(let i = 1; i<t.length; i++){
97
if(t[i]=="("&&t[i-1]==")"){
98
let p1 = t.substring(0, i);
99
let p2 = t.substring(i);
100
t=p1.concat("*", p2);
101
console.log(i);
102
}
103
104
}
105
return t;
106
}
107
function powers(t){
108
109
110
let pom1="";
111
let pom2="";
112
let j=0;
113
let k=1;
114
let x=1;
115
116
117
for(let i = 0; i<t.length; i++){
118
if(t[i]=="^"){
119
if(t[i-1]=="x"){
120
pom1="x";
121
}else if(t[i-1]==")"){
122
123
while(t[i-k]!="("){
124
k++;
125
}
126
for(let j=k-1; j>1; j--){
127
pom1+=t[i-j]
128
}
129
}else if(!isNaN(t[i-1])){
130
131
while(!isNaN(t[i-k-1])){
132
k++;
133
}
134
console.log(k)
135
for(let j=k; j>0; j--){
136
pom1+=t[i-j]
137
}
138
}
139
140
if(t[i+1]=="x"){
141
pom2="x";
142
}else if(t[i+1]=="("){
143
144
while(t[i+x]!=")"){
145
x++;
146
}
147
for(let j=2; j<x; j++){
148
pom2+=t[i+j]
149
console.log(pom2)
150
}
151
}else if(!isNaN(t[i+1])){
152
153
while(!isNaN(t[i+x+1])){
154
x++;
155
}
156
console.log(x)
157
for(let j=1; j<=x; j++){
158
pom2+=t[i+j]
159
}
160
}
161
let p1 = t.substring(0, i-k);
162
let p2 = t.substring(i+x+1);
163
t=p1+"Math.pow("+pom1+","+pom2+")"+p2;
164
}
165
166
}
167
console.log(t, x);
168
return t
169
}
170
function getf(){
171
fstring = document.getElementById("finput").value;
172
if(fstring=="")fstring="x^(3)";
173
fstring=multiplication(fstring);
174
fstring=powers(fstring);
175
return fstring;
176
}
177
178
179
function f(x){
180
181
182
183
let res = eval(fun)
184
//res = Math.tan(x);
185
return res;
186
187
}
188
function drawValues(scale){
189
let r=2;
190
let s=0.05;
191
let px1;
192
let px2;
193
let py1;
194
let py2;
195
for(let i = 0; i<w/2; i+=s){
196
let x1=i;
197
let x2=-i;
198
let y1=-f(i/scale)*scale;
199
let y2=-f(-i/scale)*scale;
200
if(i!=0){
201
if(py1<500 && py1>-500)
202
line(px1, py1, x1, y1, "#E88");
203
if(py2<1000 && py2>-1000)
204
line(px2, py2, x2, y2, "#E88");
205
206
}
207
px1=x1;
208
py1=y1;
209
px2=x2;
210
py2=y2;
211
// arc(x1, y1, r, "#E88");
212
// arc(x2, y2, r, "#E88");
213
}
214
}
215
draw();
216
function draw() {
217
ctx.clearRect(-w/2, -h/2, w, h);
218
fun = getf();
219
drawAxis(scale);
220
drawScale(scale);
221
drawValues(scale);
222
}
223
//setInterval(draw, 15);
Console errors: 0