html:

 <canvas id="canvas"></canvas>

css:

 canvas{display: block;width: 100%;height: 100%;position:absolute;z-index: -1;}

js:
//动态背景

class Circle {//创建对象//以一个圆为对象//设置随机的 x,y坐标,r半径,_mx,_my移动的距离//this.r是创建圆的半径,参数越大半径越大//this._mx,this._my是移动的距离,参数越大移动constructor(x, y) {this.x = x;this.y = y;this.r = Math.random() * 10 ;this._mx = Math.random() ;this._my = Math.random() ;}//canvas 画圆和画直线//画圆就是正常的用canvas画一个圆//画直线是两个圆连线,为了避免直线过多,给圆圈距离设置了一个值,距离很远的圆圈,就不做连线处理drawCircle(ctx) {ctx.beginPath();//arc() 方法使用一个中心点和半径,为一个画布的当前子路径添加一条弧。ctx.arc(this.x, this.y, this.r, 0, 360)ctx.closePath();ctx.fillStyle = 'rgba(204, 204, 204, 0.3)';ctx.fill();}drawLine(ctx, _circle) {let dx = this.x - _circle.x;let dy = this.y - _circle.y;let d = Math.sqrt(dx * dx + dy * dy)if (d < 180) {ctx.beginPath();//开始一条路径,移动到位置 this.x,this.y。创建到达位置 _circle.x,_circle.y 的一条线:ctx.moveTo(this.x, this.y);   //起始点ctx.lineTo(_circle.x, _circle.y);   //终点ctx.closePath();ctx.strokeStyle = 'rgba(204, 204, 204, 0.3)';ctx.stroke();}}// 圆圈移动// 圆圈移动的距离必须在屏幕范围内move(w, h) {this._mx = (this.x < w && this.x > 0) ? this._mx : (-this._mx);this._my = (this.y < h / 4 && this.y > 0) ? this._my : (-this._my);this.x += this._mx / 2;this.y += this._my / 2;}
}
//鼠标点画圆闪烁变动
class currentCirle extends Circle {constructor(x, y) {super(x, y)}drawCircle(ctx) {ctx.beginPath();//注释内容为鼠标焦点的地方圆圈半径变化
//        this.r = (this.r < 14 && this.r > 1) ? this.r + (Math.random() * 2 - 1) : 2;this.r = 8;ctx.arc(this.x, this.y, this.r, 0, 360);ctx.closePath();
//      ctx.fillStyle = 'rgba(0,0,0,' + (parseInt(Math.random() * 100) / 100) + ')'ctx.fillStyle = 'rgba(0, 0, 0, 0.3)'ctx.fill();}
}
//更新页面用requestAnimationFrame替代setTimeout
window.requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;let canvas = document.getElementById('canvas');
let ctx = canvas.getContext('2d');
let w = canvas.width = canvas.offsetWidth;
let h = canvas.height = canvas.offsetHeight;
let circles = [];
let current_circle = new currentCirle(0, 0)let draw = function () {ctx.clearRect(0, 0, w, h);for (let i = 0; i < circles.length; i++) {circles[i].move(w, h);circles[i].drawCircle(ctx);for (j = i + 1; j < circles.length; j++) {circles[i].drawLine(ctx, circles[j])}}if (current_circle.x) {current_circle.drawCircle(ctx);for (var k = 1; k < circles.length; k++) {current_circle.drawLine(ctx, circles[k])}}requestAnimationFrame(draw)
}let init = function (num) {for (var i = 0; i < num; i++) {circles.push(new Circle(Math.random() * w, Math.random() * h));}draw();
}
window.addEventListener('load', init(60));
window.onmousemove = function (e) {e = e || window.event;current_circle.x = e.clientX;current_circle.y = e.clientY;
}
window.onmouseout = function () {current_circle.x = null;current_circle.y = null;};

canvas实现动态点线背景,鼠标画点连线。相关推荐

  1. python鼠标画点连线_使用Matplotlib在Python中使用鼠标在图像上绘制点

    我试图用鼠标在图像上绘制点. 问题是:图像出现了,但是当我点击鼠标时,什么也没有绘制出来(即使我点击了几次).在 我的Python版本是python2.7,带有Anaconda和IPython控制台. ...

  2. python画k线图_python画k线图

    广告关闭 腾讯云11.11云上盛惠 ,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元! import datetime import pandas_datareader ...

  3. python 画k线图_matplotlib画k线图

    画k线需要的包和环境: python36 import tushare as ts import re import matplotlib.pyplot as plt import mpl_finan ...

  4. python画点连线_python matplotlib 在指定的两个点之间连线方法

    python matplotlib 在指定的两个点之间连线方法 为了找到matplotlib在两个点之间连线的方法真是费了好大功夫,最后还是决定用简单的 plt.plot 来解决.如果有好多对点,则可 ...

  5. php证券k线图,php画K线图的一个工具

    支持目前流行语言. require_once("../lib/FinanceChart.php"); # Create a finance chart demo containin ...

  6. canvas擦除画的线但不擦除背景

    canvas擦除画的线但不擦除背景 首先html文件中如下写,图片的话,自己随便找一张. <!DOCTYPE html> <html lang="en"> ...

  7. canvas画图--流畅没有齿痕的线,图像画线

    画图,首先要获取鼠标位置,当鼠标在画图板上移动时,随之画线. 1.画图板canvas,监听鼠标事件 2.获取鼠标事件,得到鼠标位置. var mouse = {x: 0, y: 0}; //起始鼠标位 ...

  8. 使用canvas画迁徙线并加上动态效果与小飞机图标

    首先在页面中放上地图图片,并建立三个canvas标签,分别用于点.迁徙线.动态效果 <div class="mapBox"><div class="ma ...

  9. 【Java AWT 图形界面编程】使用鼠标滚轮缩放 Canvas 画布中绘制的背景图像 ( 绘制超大图像 + 鼠标拖动 + 鼠标滚轮缩放 + 以当前鼠标指针位置为缩放中心 示例 )

    文章目录 一.鼠标滚轮缩放的中心点设置为当前鼠标中心点 - 要点分析 1.保存当前鼠标指针指向的位置 2.根据鼠标指针指向的位置以及比例重新计算图片位置 二.绘制超大图像 + 鼠标拖动 + 鼠标滚轮缩 ...

最新文章

  1. js常见问题之为什么点击弹出的i总是最后一个
  2. PS菜鸟入门 -- 添加滤镜
  3. boost::isomorphism用法的测试程序
  4. Linux下实现脚本监测特定进程占用内存情况
  5. Jmeter操作mysql数据库测试
  6. idea断点_IDEA Debug 无法进入断点的解决方法
  7. html5 学习_5分钟内学习HTML
  8. 饿了么监控系统 EMonitor 与美团点评 CAT 的对比
  9. supervisor进程守护
  10. 云原生时代,消息中间件的演进路线 | 凌云时刻
  11. windows下yafu的下载及其安装
  12. fpgrowth算法实战 mlib_sparkmllib关联规则算法(FPGrowth,Apriori)
  13. 站在2018看单片机和嵌入式芯片方案选型和发展趋势
  14. 美国enom域名的优势
  15. python列表输出序号_Python中打印列表的序号和内容
  16. 《葬花吟》笛箫简谱-红楼梦主题曲
  17. 花洒水龙头加州节水认证CEC
  18. java mysql geometry,扩展mybatis和通用mapper,支持mysql的geometry类型字段,mybatis用mapper...
  19. 琴伤+园游会+迷迭香+美人鱼+上海一九四三+威廉古堡+最后的战役+她的睫毛+麦芽糖
  20. 输出一个区间内的质数(素数)

热门文章

  1. golang 从windows 剪切板 (剪贴板)中读取bmp图片数据的方法
  2. 本地时间与Web标准时间校对
  3. java调用命令行校对系统时间
  4. 计算机科研 感悟1
  5. Public Key Infrastructure
  6. Knight On the Chessboard
  7. 计算机褐色,脂肪也有“好坏”之分?看颜(yán)值(sè),看分布
  8. 产品经理 - 产品设计方法论业务落地部分_包括流程产品文档方法论需求设计方法论
  9. 汽车电子功能安全标准ISO26262解析(十)——HSI
  10. 局部静态变量和全局静态变量