canvas初学2

news2025/7/14 11:15:52

一、碰撞检测

在这里插入图片描述

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>碰撞检测</title>
  <style>
    canvas {
      display: block;
      margin: 40px auto 0;
      border: 1px solid sienna;
    }
  </style>
</head>

<body>
  <canvas id="canvas" width="600" height="400">您的浏览器不支持 canvas</canvas>
</body>
<script>
  const canvas = document.getElementById('canvas')
  const ctx = canvas.getContext('2d')

  canvas.style.width = canvas.width + 'px'
  canvas.style.height = canvas.height + 'px'
  canvas.width = canvas.width * 1.5
  canvas.height = canvas.height * 1.5

  const drawCircle = (x, y, r) => {
    ctx.beginPath()
    ctx.fillStyle = 'orange'
    ctx.arc(x, y, r, 0, Math.PI * 2)
    ctx.fill()
    ctx.closePath()
  }

  // 配置属性
  const wd = canvas.clientWidth * 1.5
  const ht = canvas.clientHeight * 1.5
  let x = y = 100
  const r = 20
  let xSpeed = 6
  let ySpeed = 4

  drawCircle(x, y, r)

  setInterval(() => {
    ctx.clearRect(0, 0, wd, ht)  // 清空画布
    if (x - r <= 0 || x + r >= wd) {
      xSpeed = -xSpeed
    }

    if (y - r <= 0 || y + r >= ht) {
      ySpeed = -ySpeed
    }
    x += xSpeed
    y += ySpeed
    drawCircle(x, y, r)
  }, 20)

</script>

</html>

二、canvas动画

在这里插入图片描述

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>弹性球</title>
  <style>
    canvas {
      display: block;
      margin: 40px auto 0;
      border: 1px solid sienna;
    }
  </style>
</head>

<body>
  <canvas id="canvas" width="600" height="400">您的浏览器不支持 canvas</canvas>
</body>
<script>
  const canvas = document.getElementById('canvas')
  const ctx = canvas.getContext('2d')

  canvas.style.width = canvas.width + 'px'
  canvas.style.height = canvas.height + 'px'
  canvas.width = canvas.width * 1.5
  canvas.height = canvas.height * 1.5

  class Ball {
    constructor(canvas) {
      this.canvas = canvas
      this.ctx = this.canvas.getContext('2d')
      this.wd = this.canvas.clientWidth * 1.5
      this.ht = this.canvas.clientHeight * 1.5
      this.r = Math.random() * 40 + 10
      this.x = Math.random() * (this.wd - (this.r * 2)) + this.r
      this.y = Math.random() * (this.ht - (this.r * 2)) + this.r
      this.color = '#' + parseInt(Math.random() * 0xFFFFFF).toString(16)
      this.xSpeed = Math.random() * 4 + 6
      this.ySpeed = Math.random() * 6 + 4
      this.init()
    }

    init() {
      this.run()
      this.draw()
    }

    draw() {
      this.ctx.beginPath()
      this.ctx.fillStyle = this.color
      this.ctx.arc(this.x, this.y, this.r, 0, 2 * Math.PI)
      this.ctx.fill()
      this.ctx.closePath()
    }

    run() {
      if (this.x - this.r <= 0 || this.x + this.r >= this.wd) {
        this.xSpeed = -this.xSpeed
      }
      if (this.y - this.r <= 0 || this.y + this.r >= this.ht) {
        this.ySpeed = -this.ySpeed
      }
      this.x += this.xSpeed
      this.y += this.ySpeed
    }
  }

  let ballArr = []
  for (let i = 0; i < 100; i++) {
    let ball = new Ball(canvas)
    ballArr.push(ball)
  }

  // 动画
  setInterval(() => {
    ctx.clearRect(0, 0, canvas.clientWidth * 1.5, canvas.clientHeight * 1.5)
    for (let i = 0; i < ballArr.length; i++) {
      let ball = ballArr[i]
      ball.init()
    }
  }, 15)
</script>

</html>

三、canvas绘制关系图

在这里插入图片描述

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>关系图</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }
  </style>
</head>

<body>
  <canvas id="canvas">您的浏览器不支持 canvas</canvas>
</body>
<script>
  const canvas = document.getElementById('canvas')
  const ctx = canvas.getContext('2d')

  canvas.style.width = document.documentElement.clientWidth - 6 + 'px'
  canvas.style.height = document.documentElement.clientHeight - 6 + 'px'
  canvas.width = document.documentElement.clientWidth * 1.5
  canvas.height = document.documentElement.clientHeight * 1.5

  class Ball {
    constructor(options) {
      this.canvas = options.canvas
      this.text = options.title
      this.ctx = this.canvas.getContext('2d')
      this.wd = this.canvas.clientWidth * 1.5
      this.ht = this.canvas.clientHeight * 1.5
      this.r = Math.random() * 40 + 10
      this.x = Math.random() * (this.wd - (this.r * 2)) + this.r
      this.y = Math.random() * (this.ht - (this.r * 2)) + this.r
      this.color = '#' + parseInt(Math.random() * 0xFFFFFF).toString(16)
      this.xSpeed = Math.random() * 4 + 6
      this.ySpeed = Math.random() * 6 + 4
      this.init()
    }

    init() {
      this.run()
      this.draw()
    }

    draw() {
      this.drawCircle()
      this.drawText(this.text, this.x, this.y + this.r + 10)
    }

    // 绘制圆形
    drawCircle() {
      this.ctx.beginPath()
      this.ctx.fillStyle = this.color
      this.ctx.arc(this.x, this.y, this.r, 0, 2 * Math.PI)
      this.ctx.fill()
      this.ctx.closePath()
    }

    // 绘制文字
    drawText(text, x, y) {
      this.ctx.font = 'normal 20px 微软雅黑'
      this.ctx.textAlign = 'center'
      this.ctx.textBaseline = 'middle'
      this.ctx.fillText(text, x, y)
    }

    // 绘制线条
    drawLine(x1, y1, x2, y2, color) {
      this.ctx.beginPath()
      this.ctx.lineWidth = 1
      this.ctx.strokeStyle = color || '#666'
      this.ctx.moveTo(x1, y1)
      this.ctx.lineTo(x2, y2)
      this.ctx.stroke()
      this.ctx.closePath()
    }

    run() {
      if (this.x - this.r <= 0 || this.x + this.r >= this.wd) {
        this.xSpeed = -this.xSpeed
      }
      if (this.y - this.r <= 0 || this.y + this.r >= this.ht) {
        this.ySpeed = -this.ySpeed
      }
      this.x += this.xSpeed
      this.y += this.ySpeed
    }
  }

  let ballArr = []
  let titleArr = ['Vue', 'Webpack', 'React', 'Angular', 'Python', 'Nodejs', 'eCharts', 'Next', '模块化', 'Bootstrap', 'Electron', 'flutter', '小程序', '混合应用', 'Git',]

  for (let i = 0; i < 8; i++) {
    let ball = new Ball({
      canvas: canvas,
      title: titleArr[i]
    })
    ballArr.push(ball)


    // 连线
    for (let j = 0; j < i; j++) {
      let preBall = ballArr[j]
      ball.drawLine(ball.x, ball.y, preBall.x, preBall.y)
    }
  }


  // 做动画
  setInterval(() => {
    ctx.clearRect(0, 0, canvas.clientWidth * 1.5 + 10, canvas.clientHeight * 1.5 + 10)
    for (let i = 0; i < ballArr.length; i++) {
      let ball = ballArr[i]
      // 连线
      for (let j = 0; j < i; j++) {
        let preBall = ballArr[j]
        ball.drawLine(ball.x, ball.y, preBall.x, preBall.y, ball.color)
      }
    }
    for (let i = 0; i < ballArr.length; i++) {
      let ball = ballArr[i]
      ball.init()
    }
  }, 15)

</script>

</html>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/369187.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Linux下java服务占用cpu过高如何处理

Linux下java服务占用cpu过高如何处理 top命令查看进程信息 top按下shiftp,按cpu使用率排行,可见进程1932占用最高,并且是一个java服务 使用jps命令确认java服务 [rootVM-16-16-centos ~]# jps 1011 Jps 9462 yuan_back-0.0.1-SNAPSHOT.jar 1932 spigot-1.18.jar查找异常进程中…

在windows11上安装openssh服务端并修改端口号

在windows11上安装openssh服务端并修改端口号 目录1.软件下载地址2.修改端口3.启动ssh原文链接&#xff1a;https://blog.csdn.net/qq_62129885/article/details/1268467751.软件下载地址 Release v9.2.0.0p1-Beta PowerShell/Win32-OpenSSH (github.com) https://github.co…

【BBuf的CUDA笔记】八,对比学习OneFlow 和 FasterTransformer 的 Softmax Cuda实现

0x1. OneFlow/FasterTransformer SoftMax CUDA Kernel 实现学习 这篇文章主要学习了oneflow的softmax kernel实现以及Faster Transformer softmax kernel的实现&#xff0c;并以个人的角度分别解析了原理和代码实现&#xff0c;最后对性能做一个对比方便大家直观的感受到onefl…

光伏行业规模“狂飙”至87.41GW,机器视觉检测成保量提质“王牌”?

在产业政策引导和市场需求驱动的双重作用下&#xff0c;我国光伏产业已成为具备国际竞争优势的产业&#xff0c;在制造规模、技术水平和市场份额等方面均位居全球前列。近日&#xff0c;国家能源局公布了2022年光伏新增装机规模&#xff1a;87.41GW&#xff0c;增长率59.27%。行…

一文带你了解MySQL的Server层和引擎层是如何交互的?

对于很多开发小伙伴来说&#xff0c;每天写SQL是必不可少的一项工作。 那不知道大家有没有深入了解过&#xff0c;当我们的一条SQL命令被执行时&#xff0c;MySQL是如何把数据从硬盘/内存中查出来并展示到用户面前的呢&#xff1f; 其实&#xff0c;MySQL也没有大家想象的那么…

DateTimeParseException

前端请求为字符串的时间格式2023-02-16 19:19:51&#xff0c;服务端用LocalDateTime类型接收时报解析异常java.time.format.DateTimeParseException: Text 2023-02-16 19:19:51 could not be parsed at index 10方法一&#xff1a;JsonFormat(shape Shape.STRING, pattern &q…

第四章.神经网络—BP神经网络

第四章.神经网络 4.3 BP神经网络 BP神经网络(误差反向传播算法)是整个人工神经网络体系中的精华&#xff0c;广泛应用于分类识别&#xff0c;逼近&#xff0c;回归&#xff0c;压缩等领域&#xff0c;在实际应用中&#xff0c;大约80%的神经网络模型都采用BP网络或BP网络的变化…

详解Ubuntu1804安装Anaconda(附网盘极速下载链接)

2022版的anaconda下载如下 百度网盘链接链接&#xff1a;https://pan.baidu.com/s/1RZICFpR_MYVxOxHzEbStRg 提取码&#xff1a;z864 下面是具体的安装过程 1、进入下载文件存放的位置打开终端运行.sh文件&#xff1a;&#xff08;我本人的下载位置就在“下载”里面&#x…

【ROS学习笔记3】ROS的架构

【ROS学习笔记3】ROS的架构 文章目录【ROS学习笔记3】ROS的架构零、前言一、ROS系统文件二、ROS文件系统相关命令三、ROS计算图四、Reference写在前面&#xff0c;本系列笔记参考的是AutoLabor的教程&#xff0c;具体项目地址在 这里 零、前言 从系统架构方面来看&#xff0c;…

jwt,accesstoken、refresh token详解

jwt&#xff0c;accesstoken、refresh token详解 JWT(json web token) 概念 JWT定义了一种紧凑的&#xff0c;自包含的形式&#xff0c;被用作在网络中安全的传输信息 格式 例如&#xff1a;xxxx.yyyyyyy.zzz 根据.分割&#xff0c;可以得到三部分&#xff0c;header&#x…

git创建自模块submodule

一、git仓库创建并拉取主项目 1、创建project项目和submodule项目 2、拉取主项目project1和project2 二、主模块增加子模块 1、进入主项目project1并初始化子模块 A、初始化 git submodule add http://ip:port/path/submodule.git aa B、 查看状态 git status C、添加到主项…

Leetcode.1801 积压订单中的订单总数

题目链接 Leetcode.1801 积压订单中的订单总数 Rating &#xff1a; 1711 题目描述 给你一个二维整数数组 orders&#xff0c;其中每个 orders[i] [pricei, amounti, orderTypei]表示有 amounti笔类型为 orderTypei、价格为 pricei的订单。 订单类型 orderTypei 可以分为两种…

c++类对象数据成员和虚函数的内存布局

一直想搞清楚类对象的数据成员和虚函数的内存布局&#xff0c;今天刚好有时间&#xff0c;所以就写了个demo查看了一下具体的内存布局情况&#xff08;使用的编译器为微软的&#xff09;。下面是自己demo的代码&#xff1a;#include <iostream> #include <windows.h&g…

吃透spring6只用了两天!2023最新详解【完结】

正式发布Spring是一款优秀的轻量级开源框架&#xff0c;凭借强大的功能和优良的性能&#xff0c;在企业开发中被广泛应用。2022年11月&#xff0c;Spring6正式版的发布&#xff0c;标志着一个新时代的到来。新特性我们一起来看看这次6.0版本带来了哪些特性&#xff1f;需要注意…

新享科技UniPro用户中心护航数据安全

2021年数据泄露成本报告》近日发布&#xff0c;报告显示&#xff0c;数据泄露成本在新冠疫情期间创历史新高&#xff0c;全球20%企业表示&#xff0c;远程办公是导致数据泄露的重要因素&#xff0c;而此类数据泄露会给公司造成高达 496 万美元的损失&#xff0c;比平均水平高出…

软件测试面试在简历上写了“精通”后,拥有工作经验的我被面试官问到窒息...

前言 如果有真才实学&#xff0c;写个精通可以让面试官眼前一亮&#xff01; 如果是瞎写&#xff1f;基本就要被狠狠地虐一把里&#xff01; 最近在面试&#xff0c;我现在十分后悔在简历上写了“精通”二字… 先给大家看看我简历上的技能列表&#xff1a; 熟悉软件测试理…

Web前端学习:二

二一&#xff1a;文字font-size样式 font-size&#xff1a;**px 控制文字大小&#xff0c;可精准控制大小 默认样式medium&#xff0c;中等的 large&#xff0c;大一号 x-large&#xff0c;再大一号 xx-large&#xff0c;再大一号 small&#xff0c;小一号 <!DOCTYPE html…

自定义View练习题目整理

一、动态音频播放柱形图 1、效果图&#xff1a; 2、步骤 &#xff08;1&#xff09;、新建自定义View类&#xff0c;继承View &#xff08;2&#xff09;、重写onDraw()方法&#xff0c;使用画笔和画布循环画一定数量的柱形 Overrideprotected void onDraw(Canvas canvas) {s…

npm install vue2-ace-editor 安装报错

npm install vue2-ace-editor 安装报错&#xff0c;如下图 目录 npm install vue2-ace-editor 安装报错&#xff0c;如下图 &#x1f9e8;&#x1f9e8;&#x1f9e8;解决方法&#xff1a;在命令后面加上 神秘代码 npm install vue2-ace-editor --save --legacy-peer-deps &a…

该抛弃 x86 Linux,改用 64 位的了

如果你想获得安全的体验&#xff0c;你可能不会再继续使用 32 位 Linux 内核。我们有很多 为 32 位系统定制的 Linux 发行版。那么&#xff0c;为什么我想要不鼓励使用 32 位&#xff0c;而升级到 64 位 Linux 呢&#xff1f;有几个原因&#xff0c;其中一个最大的原因&#xf…