<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<canvas id="canvas" style="border:1px solid #000000;">
</canvas>
<script type="text/javascript">
function Coordinate(width, height, scaleToPixel){
var pixel = scaleToPixel || 1;
width = width || 500;
height = height || 500;
var canvas = document.getElementById("canvas");
canvas.width = width;
canvas.height = height;
var context = canvas.getContext("2d");
init();
function init(){
line(-width/2, 0, width/2, 0);
line(0, -height/2/pixel, 0, height/2/pixel);
text(-width/2/pixel, -10/pixel, parseInt(-width/2/pixel) + ", 0");
text((width-55)/2/pixel, -10/pixel, parseInt(width/2/pixel) + ", 0");
text(5/pixel, (-height+5)/2/pixel, "0, " + parseInt(-height/2/pixel));
text(5/pixel, (height-20)/2/pixel, "0, " + parseInt(height/2/pixel));
text(2.5/pixel, -10/pixel, "0");
}
this.allReset = function reset(){
canvas.width = canvas.width;
}
this.drawLine = line;
function line(startX, startY, endX, endY){
//console.log(startX, startY);
context.beginPath();
context.moveTo(dx(startX), dy(startY));
context.lineTo(dx(endX), dy(endY));
context.closePath();
context.stroke();
}
this.drawText = text;
function text(x, y, text, fontSize){
context.beginPath();
context.font = (fontSize || 10)+"px Arial";
context.fillText(text, dx(x), dy(y));
}
function dx(x){
return (x * pixel + width/2);
}
function dy(y){
return (-y * pixel + height/2);
}
}
window.onload = function(){
var maxX = 501, maxY = 501;
var pixel = 100;
var c = new Coordinate(maxX, maxY, pixel);
var i = -maxX / 2 / pixel;
f = setInterval(function(){
var u = 0.1;
c.drawLine(i, Math.sin(i), i+u, Math.sin(i+u));
i += u;
//console.log(i+", "+Math.sin(i) +" : "+ (i+u)+", "+ Math.sin(i+u));
if(i * pixel >= maxX/2){
clearInterval(f);
}
}, 20);
}
</script>
</body>
</html> |