I am using html5 canvas to create graph function software. There is a critical error, but I do not know what is happening.
That is, when I call:
yOFxGraph(plotFUNCinputYofX,-4,5,-4,4,"rgb(66,44,255)",1);
everything works well on Firefox26 / Chrome32 / Safari7 on Mac.
However, when I change only one tiny parameter, for example:
yOFxGraph(plotFUNCinputYofX,-5,5,-4,4,"rgb(66,44,255)",1);
the graph only disappears in Chrome / Safari , but works well in Firefox .
All the code below, could you tell me how to solve it? Many thanks. How can this error happen with major browsers !? no matter which code is so simple.
<!doctype html>
<html>
<body onload="draw()">
<script>
function plotFUNCinputYofX(x) {return 1/Math.sin(x);}
function draw() {
yOFxGraph(plotFUNCinputYofX,-4,5,-4,4,"rgb(66,44,255)",1);
}
function yOFxGraph (func,xFrom,xTo,yFrom,yTo,color,thick) {
var canvas = document.getElementById("canvas");
var ctx=canvas.getContext("2d");
ctx.lineWidth = thick;
ctx.strokeStyle = color;
var Xunit = canvas.width/(xTo-xFrom);
var Yunit = canvas.height/(yTo-yFrom);
var samplingX=(xTo-xFrom)/400;
var xx = xFrom;
ctx.beginPath();
ctx.moveTo(0 , canvas.height-(func(xx)-yFrom)*Yunit);
function pivoting(){
xx+=samplingX;
ctx.lineTo((xx-xFrom)*Xunit , canvas.height-(func(xx)-yFrom)*Yunit);
}
if(xFrom<xTo){while(xx<xTo){pivoting()}}
else if(xFrom>xTo){while(xx>xTo){pivoting()}}
ctx.stroke();
}
</script>
<canvas id="canvas" width="500" height="500"></canvas>
</body>
</html>