<!DOCTYPE html>
<html lang="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<head>
     <title>函数x^2-4/x^2-2x-3曲线勾画</title>
    </head>

     <body onload="draw()">
        <canvas id="myCanvus" width="1300px" height="640px" style="border:1px dashed black;">
            出现文字表示你的浏览器不支持HTML5
        </canvas>
     </body>
</html>
<script type="text/javascript">
<!--
    function draw(){
        var canvas=document.getElementById("myCanvus");
        var canvasWidth=1300;
        var canvasHeight=640;

        var context=canvas.getContext("2d");

        context.fillStyle = "white";
        context.fillRect(0, 0, canvasWidth, canvasHeight);

        context.strokeStyle = "black";
        context.fillStyle = "black";

        // 进行坐标变换:把原点放在左下角,东方为X轴正向,北方为Y轴正向
        var offsetY=320;// Y向偏移值,正值向上偏,用来画坐标轴
        var offsetX=650;// X向偏移值,正值向右偏,用来画坐标轴

        context.save();
        context.translate(0+offsetX,canvasHeight-offsetY);

        drawAxisXText(context);// 文字和线分开画比较好处理
        drawAxisYText(context);
        drawTitleText(context);

        context.rotate(getRad(180));
        context.scale(-1,1);        

        drawAxisX(context);
        drawAxisY(context);
        drawCurve(context);       

        context.restore();
    }

    function drawTitleText(ctx){
        ctx.lineWidth=0.5;
        ctx.strokeStyle='navy';
        ctx.fillStyle='navy';

        var x=350;
        var y=-250;

        // 写文字
        ctx.fillText("x^2-4",x,y);
        ctx.fillText("-----------",x,y+10);
        ctx.fillText("x^2-2x-3",x,y+20);
        ctx.fillText("y=",x-20,y+10);
        ctx.fillText("曲线,^2即平方. 作者:逆火狂飙",x+70,y+10);
    }

    function drawCurve(ctx){
        var SU=50;// Scale Unit

        var cds=[{}];
        var cds1=[{}];
        var cds2=[{}];

        var x,y;
        for(x=-13;x<=13;x+=0.01){

            if(x<-1){
                y=(x*x-4)/(x*x-2*x-3);// 函数式在此
                var arr={"x":x,"y":y};
                cds.push(arr);
            }

            if(x>-1 && x<3){
                y=(x*x-4)/(x*x-2*x-3);// 函数式在此
                var arr={"x":x,"y":y};
                cds1.push(arr);
            }

            if(x>3 ){
                y=(x*x-4)/(x*x-2*x-3);// 函数式在此
                var arr={"x":x,"y":y};
                cds2.push(arr);
            }
        }

        // 将数组里面的点一段段连线
        //var ymax=-6,ymin=6,xmax,xmin;

        ctx.strokeStyle = "red";
        ctx.beginPath();

        for(var i=0; i<cds.length; i++){
            ctx.lineTo(cds[i].x*SU,cds[i].y*SU);
        } 

        ctx.stroke();
        ctx.closePath();

        ctx.beginPath();
        for(var i=0; i<cds1.length; i++){
            ctx.lineTo(cds1[i].x*SU,cds1[i].y*SU);
        } 

        ctx.stroke();
        ctx.closePath();

        ctx.beginPath();
        for(var i=0; i<cds2.length; i++){
            ctx.lineTo(cds2[i].x*SU,cds2[i].y*SU);
        } 

        ctx.stroke();
        ctx.closePath();

        // 极大值
        /*ctx.beginPath();
        ctx.moveTo(xmax*SU,ymax*SU-5);
        ctx.lineTo(xmax*SU,ymax*SU+5);

        ctx.save();
        ctx.scale(1,-1);
        ctx.fillText("ymax="+cutShort(ymax.toString(),8),xmax*SU,-ymax*SU);
        ctx.restore();

        ctx.stroke();
        ctx.closePath();

        // 极小值
        ctx.beginPath();
        ctx.moveTo(xmin*SU,ymin*SU-5);
        ctx.lineTo(xmin*SU,ymin*SU+5);

        ctx.save();
        ctx.scale(1,-1);
        ctx.fillText("ymin="+ymin,xmin*SU,-ymin*SU);
        ctx.restore();

        ctx.stroke();
        ctx.closePath();*/
    }

    function drawAxisX(ctx){
        ctx.save();

        ctx.lineWidth=0.5;
        ctx.strokeStyle='navy';
        ctx.fillStyle='navy';

        var start=-650;
        var end=650;

        // 画轴
        ctx.beginPath();
        ctx.moveTo(start, 0);
        ctx.lineTo(end, 0);
        ctx.stroke();
        ctx.closePath();

        // 画箭头
        ctx.beginPath();
        ctx.moveTo(end-Math.cos(getRad(15))*10, Math.sin(getRad(15))*10);
        ctx.lineTo(end, 0);
        ctx.lineTo(end-Math.cos(getRad(15))*10, -Math.sin(getRad(15))*10);
        ctx.stroke();
        ctx.closePath();

        // 画刻度
        var x,y;
        y=5;
        for(x=start;x<end;x+=50){
            ctx.beginPath();
            ctx.moveTo(x, 0);
            ctx.lineTo(x, y);

            ctx.stroke();
            ctx.closePath();
        }

        ctx.restore();
    }

    function drawAxisXText(ctx){
        ctx.lineWidth=0.5;
        ctx.strokeStyle='navy';
        ctx.fillStyle='navy';

        var start=-650;
        var end=650;

        // 写文字
        var x,y=5;
        for(x=start;x<end;x+=50){
            ctx.fillText(x/50,x,y+10);
        }
    }

    function drawAxisY(ctx){
        ctx.save();

        ctx.lineWidth=0.5;
        ctx.strokeStyle='navy';
        ctx.fillStyle='navy';

        var start=-300;
        var end=300;

        // 画轴
        ctx.beginPath();
        ctx.moveTo(0, start);
        ctx.lineTo(0, end);
        ctx.stroke();
        ctx.closePath();

        // 画箭头
        ctx.beginPath();
        ctx.moveTo(Math.sin(getRad(15))*10, end-Math.cos(getRad(15))*10);
        ctx.lineTo(0, end);
        ctx.lineTo(-Math.sin(getRad(15))*10, end-Math.cos(getRad(15))*10);
        ctx.stroke();
        ctx.closePath();

        // 画刻度
        var x,y;
        x=5;
        for(y=start;y<end;y+=50){
            ctx.beginPath();
            ctx.moveTo(x, y);
            ctx.lineTo(0, y);

            ctx.stroke();
            ctx.closePath();
        }
    }

    function drawAxisYText(ctx){
        ctx.lineWidth=0.5;
        ctx.strokeStyle='navy';
        ctx.fillStyle='navy';

        var start=-250;
        var end=350;

        // 写文字
        var x=-19,y=5;
        for(y=start;y<end;y+=50){

            if(y!=0){
                ctx.fillText(-y/50,x,y);
            }
        }
    }

    function getRad(degree){
        return degree/180*Math.PI;
    }

    function cutShort(str,length){
        if(str.length>length){
            str=str.substr(0,length)+"...";
        }

        return str;
    }
//-->
</script>

最新文章

  1. TypeScript为Zepto编写LazyLoad插件
  2. 精通css 高级web标准解决方案——可视化格式模型-定位模型
  3. BUG等级和严重等级关系
  4. codeforces George and Job
  5. jQuery的事件click
  6. (一)问候Hibernate4
  7. jvm之内存分配与回收策略
  8. IOS_多线程_ASI_AFN_UIWebView
  9. jQuery选择器与CSS选择器
  10. python3学习笔记(2)
  11. genymotion中app打开后屏幕是倒的问题
  12. L2-007 家庭房产 (25 分) (并查集)
  13. 第十一节:Bundles压缩合并js和css及原理分析
  14. GIS 网站 参考网站
  15. C# winform 无边框 窗体的拖动
  16. 用vue写一个仿app下拉刷新的组件
  17. 【BZOJ】【4152】【AMPZZ2014】The Captain
  18. 服务端API安全解决方案
  19. VSCode编辑器编写Python代码
  20. HTML常用头部变量

热门文章

  1. Pycharm中的Django项目连接mysql数据库
  2. firfox与about:config
  3. markdown与html之间转换引发的问题
  4. 问题记载——keil中写for循环嵌套
  5. 解决JPA懒加载典型的N+1问题-注解@NamedEntityGraph
  6. 【我要学python】爬虫准备之了解基本的html标签
  7. cobol COMP-3最后1位
  8. Linux基础系列-Day3
  9. [BZOJ2216]Lightning Conductor
  10. [POJ3728]The merchant