-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvas.js
167 lines (145 loc) · 5.21 KB
/
canvas.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
/**
* Created by benoit on 19/04/17.
*/
/* Angular */
var app = angular.module('canvasApp', []);
var drawing = new Drawing();
drawing.init({fn: "sin(7x)/7x"});
drawing.draw();
app.controller('canvasController', [
'$scope',
function($scope){
$scope.showSettings = false;
$scope.fn = drawing.fn;
$scope.lineWidth = 1;
$scope.update = function() {
// first check if function is valid. if yes updateUI
$scope.fnInvalid = fnIsInvalid($scope.fn);
if (!$scope.fnInvalid) {
$scope.updateUI();
}
};
$scope.updateUI = function() {
drawing.init({
fn: $scope.fn,
minx: $scope.minx,
maxx: $scope.maxx,
miny: $scope.miny,
maxy: $scope.maxy,
lineWidth: $scope.lineWidth
});
drawing.draw();
};
window.addEventListener('resize', function(event){
$scope.updateUI()
});
$scope.toggleSettings = function() {
$scope.showSettings = $scope.showSettings? false : true;
}
$scope.setFn = function(expr) {
console.log('setFn');
$scope.fn = expr;
}
}
]);
/* Canvas part */
function Drawing() {
this.init = function(spec) {
this.fn = spec.fn || "x*x";
this.zone_dessin = document.getElementById("schema");
this.zone_dessin.height=this.zone_dessin.parentNode.offsetHeight;
this.zone_dessin.width=this.zone_dessin.parentNode.offsetWidth;
this.minx = spec.minx || -8;
this.maxx = spec.maxx || 8;
this.pas = 1.01*(this.maxx-this.minx)/this.zone_dessin.width; // 1 peu moins d'un point per pixel
this.miny = spec.miny || -2;
this.maxy = spec.maxy || 2;
this.echelleX = this.zone_dessin.width / (this.maxx - this.minx);
this.echelleY = this.zone_dessin.height / (this.maxy - this.miny);
this.graphe= this.zone_dessin.getContext("2d");
this.x =this.minx;
this.y = 0;
this.strokeStyle = "#d82140";
this.lineWidth= spec.lineWidth;
this.cmp = math.compile(this.fn);
this.f = function(x) {
var y = 0;
try {
y = this.cmp.eval({x:x})
}
catch(e) {
console.log('math eval error: '+ e.message);
y = NaN;
}
return y;
}
}
this.draw = function() {
this.drawAxis();
this.graphe.strokeStyle = this.strokeStyle;
this.graphe.lineWidth= this.lineWidth;
//console.log(x);
this.graphe.beginPath();
var move = true; // if move make a move else draw a line
while(this.x<this.maxx) {
//console.log((x + " " + f(x)))
this.y = this.f(this.x);
if (this.y > this.maxy+1 || this.y < this.miny-1) { // on lève le crayon
move = true;
this.graphe.stroke();
}
else if (move) {
this.graphe.moveTo((this.x-this.minx)*this.echelleX,this.maxy*this.echelleY-this.f(this.x)*this.echelleY);
move = false;
} else {
this.graphe.lineTo((this.x-this.minx)*this.echelleX,this.maxy*this.echelleY-this.f(this.x)*this.echelleY);
}
this.x=(this.x+this.pas);
}
this.graphe.stroke();
};
this.drawAxis = function() {
this.graphe.beginPath();
this.graphe.lineWidth="1";
this.graphe.strokeStyle="grey";
// Axe des X
this.graphe.moveTo(0,this.zone_dessin.height/2);
this.graphe.lineTo(this.zone_dessin.width,this.zone_dessin.height/2);
this.graphe.lineTo(this.zone_dessin.width-5,(this.zone_dessin.height/2)-5);
this.graphe.moveTo(this.zone_dessin.width,this.zone_dessin.height/2);
this.graphe.lineTo(this.zone_dessin.width-5,(this.zone_dessin.height/2)+5);
// Axe Y
this.graphe.moveTo(-this.minx*this.echelleX,this.zone_dessin.height); // origine
this.graphe.lineTo(-this.minx*this.echelleX,0);
this.graphe.lineTo((-this.minx*this.echelleX)+5,5);
this.graphe.moveTo(-this.minx*this.echelleX,0);
this.graphe.lineTo(-this.minx*this.echelleX-5,5);
this.graphe.stroke();
this.graphe.fillText(this.minx,0,-5+this.zone_dessin.height/2);
this.graphe.fillText(this.maxx,this.zone_dessin.width-20,-5+this.zone_dessin.height/2);
this.graphe.fillText(this.miny,5-this.minx*this.echelleX,-8+this.zone_dessin.height);
this.graphe.fillText(this.maxy,5-this.minx*this.echelleX,8);
if (this.minx < 0 && this.maxx > 0) {
this.graphe.fillText("0", -this.minx*this.echelleX+5, this.maxy*this.echelleY-5 );
}
};
}
/**
* Check the expression of the function, not secure =>
* must be replaced by Math.js or Javascript evaluation
* Uses Math.js
* @param expr
* @returns {*}
*/
var fnIsInvalid = function(expr) {
var scope = {x:0};
var fnInvalid = false;
try {
var y = math.eval(expr,scope);
}
catch(e){
console.log(e.message);
fnInvalid = e.message; // is invalid
}
return fnInvalid;
}