search
HomeWeb Front-endJS TutorialNative js and canvas simulated electrocardiogram code sharing
Native js and canvas simulated electrocardiogram code sharingFeb 05, 2018 pm 01:33 PM
canvasjavascriptelectrocardiogram

The html page that simulates electrocardiogram is made using native js+canvas. Since it is packaged and put on github together with the project, it uses the single-page mode of vue.js. In fact, you do not need to use any additional frameworks and styles. You can also complete this demo, now let’s dismantle this project together!

1: Create a canvas on the page. To make the "line" of the electrocardiogram move on our page, canvas is essential. Because the project is relatively simple, the DOM elements on the page have been written so far. The main workload is concentrated in the js part


##

<p class="heartBeat">
   <canvas id="can">Canvas画板</canvas>
 </p>

2: Define several variables and assign values. These variables will be needed for calculation during runtime


var can = document.getElementById(&#39;can&#39;),//画布对象
    pan,//获取2D图像API接口
    index = 0,//用来接收setinerval的值
    flag = true,//用来控制心电图折线的运行方向
    wid = document.body.clientWidth,//获取浏览器宽度
    hei = document.body.clientHeight,//获取浏览器高度
    x = 0,//心电图的“点”在画布上的x轴坐标,从0开始
    y = hei/2;//心电图的“点”在画布上的y轴坐标,从页面y轴居中位置开始

3: Initialize the canvas and set various properties for the canvas


function start(){
    can.height = hei;//设置画布高度
    can.width = wid;//设置画布宽度
    pan = can.getContext("2d");//获取2D图像API接口
    pan.strokeStyle = "#08b95a";//设置画笔颜色
    pan.lineJoin = "round";//设置画笔轨迹基于圆点拼接
    pan.lineWidth = 9;//设置画笔粗细
    pan.beginPath();//开始一条画笔的路径
    pan.moveTo(x,y);//定位我们的“落笔点”
    index = setInterval(move,1);//让我们的画笔动起来
  };

As you can see, we have not yet involved the "painting" action here. We just initialized the canvas size so that the canvas fills the screen. At the same time, we defined the color, thickness, pen point and other operations of the brush, and then used the setInterval method. Let the brush keep moving along the route we calculated. If you are not very familiar with the setInterval method, I suggest you take a look at the usage of setInterVal, which will not be described here. Because we want the electrocardiogram to loop infinitely and execute automatically, we encapsulate it here as the start() function, so that when the electrocardiogram moves to the far right of the screen, we re-execute the start() function to make the electrocardiogram infinite. It’s a cycle

4. Let the electrocardiogram move! It can be said that the previous steps are not difficult. The real core code is to make our electrocardiogram move and move along the route we want. Now we will make the electrocardiogram really come alive


function move(){
    x++;//x轴是始终运动的,所以x一直自增
    if(x < 100){
      //前100px,我们不希望做垂直运动,让点只沿垂直方向运动即可,所以不做任何操作
    }else{
      if(x >= wid - 100){
      //最后的100px,同样希望心电图只做水平运动,不会上下波动,所以不做任何操作
      }else{
        //为了让心电图看起来更加逼真,我们希望心电图在运动时每次的波峰和波谷都是随机的,这样更类似于人类的心跳,所以我们给它一个随机值z
        var z = Math.random()*280;

        if(y <= z){
          //画布的坐标是从左上角开始计算的,也就是最左上角的点的坐标是(0,0),y是当前画笔所在坐标的y轴,假如y小于z,就代表y已经到达波峰位置,准备开始向波谷运动
          flag = true
        }
        if((hei - y) <= z){
          //假如当前画笔在y轴的坐标y距离浏览器底部hei的差值已经小于随机值z,代表当前的画笔已经运行到波谷位置,准备转向波峰位置运动
          flag = false
        }
        if(flag){
          //假如flag为true,代表画笔仍然向波谷位置前进,需要花点功夫理解的是,因为画布左上角的点的坐标是(0,0),所以y的值越大,画笔在y轴的位置越靠近浏览器底部,所以向波谷运动时,y的值是不断增加的,同时为了让波峰波谷更陡峭,我这里设置y += 5,
          y+=5
        }else{
          //假如flag为false,表示向波峰运动,y的值是不断减小的
          y-=5
        }
      }
    }
    if(x == wid){
      //当画笔运动到浏览器右侧边缘,停止绘图
      pan.closePath();
      //清除循环
      clearInterval(index);
      //将index置零,准备下一次循环
      index = 0;
      //重新定位画笔到屏幕左侧上下居中的位置
      x = 0;
      y = hei/2;
      flag = true;
      //重新进行下一次心电图的绘制
      start();
    }
    //lineTo和stroke函数负责描绘运动轨迹
    pan.lineTo(x,y);
    pan.stroke();
  }

5: Note, the electrocardiogram can actually be run at this point, but it should be noted that set your body height to 100%, otherwise the canvas may not be able to fill the entire page


html,body{
   width: 100%;
   height: 100%;
   margin: 0;
  }

Complete project code:


<!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;
    }
  </style>
</head>
<body>
<p id="canvas">
  <canvas id="can">Canvas画板</canvas>
</p>

<script src="js/vue.min.js"></script>
<script>
  var can = document.getElementById(&#39;can&#39;),
    pan,
    index = 0,
    flag = true,
    wid = document.body.clientWidth,
    hei = document.body.clientHeight,
    x = 0,
    y = hei/2;
  start();
  function start(){
    can.height = hei;
    can.width = wid;
    pan = can.getContext("2d");//获取2D图像API接口
    pan.strokeStyle = "#08b95a";//设置画笔颜色
    pan.lineJoin = "round";//设置画笔轨迹基于圆点拼接
    pan.lineWidth = 9;//设置画笔粗细
    pan.beginPath();
    pan.moveTo(x,y);
    index = setInterval(move,1);
  };
  function move(){
    x++;
    if(x < 100){

    }else{
      if(x >= wid - 100){

      }else{
        var z = Math.random()*280;
        if(y <= z){
          flag = true
        }
        if((hei - y) <= z){
          flag = false
        }
        if(flag){
          y+=5
        }else{
          y-=5
        }
      }
    }
    if(x == wid){
      pan.closePath();
      clearInterval(index);
      index = 0;
      x = 0;
      y = hei/2;
      flag = true;
      start();
    }
    pan.lineTo(x,y);
    pan.stroke();
  }

 /* */

</script>
</body>
</html>

Related recommendations:


Canvas achieves dazzling Particle motion effect

canvas polygon drawing example

html2 canvas implementation browser screenshot

The above is the detailed content of Native js and canvas simulated electrocardiogram code sharing. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
es6数组怎么去掉重复并且重新排序es6数组怎么去掉重复并且重新排序May 05, 2022 pm 07:08 PM

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

JavaScript的Symbol类型、隐藏属性及全局注册表详解JavaScript的Symbol类型、隐藏属性及全局注册表详解Jun 02, 2022 am 11:50 AM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

原来利用纯CSS也能实现文字轮播与图片轮播!原来利用纯CSS也能实现文字轮播与图片轮播!Jun 10, 2022 pm 01:00 PM

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

JavaScript对象的构造函数和new操作符(实例详解)JavaScript对象的构造函数和new操作符(实例详解)May 10, 2022 pm 06:16 PM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

JavaScript面向对象详细解析之属性描述符JavaScript面向对象详细解析之属性描述符May 27, 2022 pm 05:29 PM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

javascript怎么移除元素点击事件javascript怎么移除元素点击事件Apr 11, 2022 pm 04:51 PM

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

整理总结JavaScript常见的BOM操作整理总结JavaScript常见的BOM操作Jun 01, 2022 am 11:43 AM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。

foreach是es6里的吗foreach是es6里的吗May 05, 2022 pm 05:59 PM

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool