欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

用canvas画心电图的示例代码

程序员文章站 2022-06-09 13:30:18
这篇文章主要介绍了用canvas画心电图的示例代码的相关资料,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧... 18-09-10...

本文介绍了用canvas画心电图的示例代码,分享给大家,具体如下:

效果图:

用canvas画心电图的示例代码

思路:

​1.模拟点(如果你有真实的数据,那就是把数据幻化成canvas对应的坐标点)

​ 模拟点时注意的点就是高起部分需要对称以及为了好看要随机出现上上下下

2.画线

​ 画线需要注意有一个匀速移动的过程。

​ 比如 a点到b点,不是简单的a画到b,而是a点到a1,a2....最后到b(这一块按照比例移动比较难)

3.画线的一些效果,比如加上阴影(这里就可以*发挥了)具体代码

<!doctype html>
 <html lang="en">
 <head>
     <meta charset="utf-8">
     <title>心电图</title>
     <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
     <style>
         html,body{
             width: 100%;
             height: 100%;
             margin: 0;
         }
         canvas{
             background: #000;
             width: 100%;
            height: 100%;
         }
     </style>
 </head>
 <body>
 <div id="canvas">
     <canvas id="can"></canvas>
 </div>
 <script>
     var can = document.getelementbyid('can'),
         pan,
         index = 0,
         flag = true,
         wid = document.body.clientwidth,
         hei = document.body.clientheight,
         x = 0,
         y = hei/2,
         drawx = 0, 
         drawy = hei/2,
         drawxy = [],
         cdrawx = 0,
         i = 0,
         rex = 0,
         rey = 0;
     start();
     function start(){
         can.height = hei;
         can.width  = wid;
         pan = can.getcontext("2d");
         pan.strokestyle = "white";
         pan.linejoin = "round";
         pan.linewidth = 6;
         pan.shadowcolor = "#228dff";
         pan.shadowoffsetx = 0;
         pan.shadowoffsety = 0;
         pan.shadowblur = 20;
         pan.beginpath();
         pan.moveto(x,y);
         drawxys();
         index = setinterval(move,1);
     };

     function drawxys(){
         if(drawx > wid){
         }else{
             if(drawy == hei/2){
                 if(flag){
                     flag = false;
                 }else{
                     var _y = math.ceil(math.random()*10);
                     _y = _y/2;
                     if(number.isinteger(_y)){
                         drawy += math.random()*180+30;
                     }else{
                         drawy -= math.random()*180+30;
                     }
                     flag = true;
                 }
                 cdrawx = math.random()*40+15;
             }else{
                 drawy = hei/2;
             }
             drawx += cdrawx;
             drawxy.push({
                 x : drawx,
                 y : drawy
             });
             drawxys();
         }
     }

     function move(){
         var x = drawxy[i].x,
             y = drawxy[i].y;
         if(rex >= x - 1){
             rex = x;
             rey = y;
             i++;
             cc();
             return;
         }
         if(y > hei/2){
             if(rey >= y){
                 rex = x;
                 rey = y;
                 i++;
                 cc();
                 return;
             }
         }else if(y < hei/2){
             if(rey <= y){
                 rex = x;
                 rey = y;
                 i++;
                 cc();
                 return;
             }
         }else{
             rex = x;
             rey = y;
             i++;
             cc();
             return;
         }

         rex += 1;
         if(y == hei/2){
             rey = hei/2;
         }else{
             var c = math.abs((drawxy[i].x-drawxy[i-1].x)/(drawxy[i].y-drawxy[i-1].y));
             var _yt = (rex-drawxy[i-1].x)/c;

             if(drawxy[i].y < drawxy[i-1].y){
                 rey = drawxy[i-1].y - _yt;
             }else{
                 rey = drawxy[i-1].y + _yt;
             }
         }
         cc();
     }

    function cc(){
        if(i == drawxy.length){
             pan.closepath();
             clearinterval(index);
             index = 0;
             x = 0;
             y = hei/2;
             flag = true;
             i = 0;
         }else{
             pan.lineto(rex, rey);
             pan.stroke();
         }
    }
 
</script>
</body>
</html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。