首页 > 其他分享 >前端可视化echarts和three

前端可视化echarts和three

时间:2024-03-31 17:35:39浏览次数:21  
标签:1.5 canvas const ctx three 可视化 let 100 echarts

canvas画一条直线

  const canvas = document.getElementById('canvas')
  const ctx = canvas.getContext('2d')

  ctx.beginPath()//绘制都用beginPath和closePath包裹
  ctx.lineWidth = 4
  ctx.strokeStyle = 'orange'
  // 起点  终点  中间点
  ctx.moveTo(100, 100)
  ctx.lineTo(300, 300)
  ctx.lineTo(500, 200)
  ctx.stroke()//添上才能显示出来
  ctx.closePath()
 

canvas绘制实心空心文字

  // 实心文字 描边文字
  ctx.fillStyle = 'orange'
  ctx.strokeStyle = "hotpink"
  ctx.font = 'bold 60px 微软雅黑'
  ctx.fillText('拉勾教育', 100, 100, 100)
  ctx.strokeText('前端', 100, 240)

  // 对齐属性设置
  ctx.textAlign = 'center' // left right start end center 
  ctx.textBaseline = "middle"  // top bottom middle
  ctx.fillText('拉勾教育', 450, 300)

canvas实现动画及碰撞检测

实现动画的原理时不停地改变物体的x,y值,记得每次都要有清空画布的操作,碰撞检测原理就是物体的坐标减去物体一半的宽度小于等于0或者物体的坐标加上物体一半的宽度大于等于画布的宽度时,让坐标的变化速度值取反

  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 += xSpeedjiang
    y += ySpeed
    drawCircle(x, y, r)
  }, 20)

将一个会变颜色,不同大小,不同速率走的球写成一个类,然后遍历添加

  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)

canva实现每个圆连线并且能够添加文字的思路
将绘制文字和绘制线的方法添加到球的类中,
然后在定时器中循环遍历ball数组,先画线,然后再画球

动态设置rem全局实现

在public中的index下添加

  <title>可视化插件封装</title>
  <script>
    document.documentElement.style.fontSize = document.documentElement.clientWidth / 10 + 'px'
    window.addEventListener('resize', () => {
      document.documentElement.style.fontSize = document.documentElement.clientWidth / 10 + 'px'
    })
  </script>

动画函数从0到指定值方法的实现

export default function myAnimation(param) {
  let current = 0
  let looped
  const ctx = this.ctx
  const _canvas = this._canvas
  const callback = param.render
  const successCb = param.success;
  (function looping() {
    looped = requestAnimationFrame(looping)
    if (current < param.percent) {
      ctx.clearRect(0, 0, _canvas.width, _canvas.height)
      current = current + 4 > param.percent ? param.percent : current + 4
      callback(current)
    } else {
      window.cancelAnimationFrame(looping)
      looped = null
      successCb && successCb()
    }
  })()
}

函数调用

myAnimation.call(this, {
          percent: 100,
          render: (current) => {
            console.log(current)
          }
        })

标签:1.5,canvas,const,ctx,three,可视化,let,100,echarts
From: https://www.cnblogs.com/zhixy/p/18106743

相关文章

  • 5-4Tensorboard可视化
    在我们的炼丹过程中,如果能够使用丰富的图像来展示模型的结构,指标的变化,参数的分布,输入的形态等信息,无疑会提升我们对问题的洞察力,并增加许多炼丹的乐趣。TensorBoard正是这样一个神奇的炼丹可视化辅助工具。它原是TensorFlow的小弟,但它也能够很好地和Pytorch进行配合。甚至在Pyt......
  • TransformControls 是 Three.js 中的一个类,用于在网页中进行 3D 场景中物体的交互式操
    demo案例TransformControls是Three.js中的一个类,用于在网页中进行3D场景中物体的交互式操作。让我们来详细讲解它的输入参数、输出、属性和方法:输入参数:TransformControls构造函数通常接受两个参数:camera(THREE.Camera):用于渲染场景的摄像机。这个参数是必需的。......
  • STLExporter 是用于将 Three.js 场景中的几何体数据导出为 STL 格式的类。
    demo案例STLExporter是用于将Three.js场景中的几何体数据导出为STL格式的类。下面是关于STLExporter的入参、出参、方法和属性的讲解:入参(Parameters):scene:THREE.Scene类型的参数,表示要导出为STL格式的Three.js场景对象。出参(ReturnValue):string:......
  • .NET开源、免费、跨平台的Git可视化管理工具
    前言俗话说得好“工欲善其事,必先利其器”,合理的选择和使用可视化的管理工具可以降低技术入门和使用的门槛。今天大姚给大家分享一款.NETAvalonia开源、免费、跨平台、快速的Git可视化管理工具:SourceGit。Avalonia介绍Avalonia是一个强大的框架,使开发人员能够使用.NET创建跨平......
  • 初学可视化PyQt5系列--hello my four rotor drone
    【初学可视化PyQt5系列】第1章PyQt5简介第2章PyQt5新增功能第3章Hellomyfourrotordrone第4章PyQt5主要类第5章PyQt5使用Qt设计器第6章PyQt5信号与插槽第7章PyQt5布局与管理第8章PyQt5基本小部件第9章PyQt5QDialog类第10章PyQt5QMessageBox......
  • 数据剑舞,图表如潮!Matplotlib傲视数据可视化江湖
    在代码的世界中,隐藏着一座神秘而神奇的画图殿堂,它就是Matplotlib。这座殿堂矗立在数据的海洋中,每一行代码都是一笔神奇的咒语,让数据在图像之间舞动,展现出无限可能。Matplotlib的大门上镶嵌着闪烁的彩虹宝石,每当有开发者走近,便散发出五彩斑斓的光芒,仿佛在诉说着这里的神秘。而在宫......
  • 毕业设计:基于python的药品销售数据分析可视化系统 大数据
    目录前言课题背景和意义实现技术思路一、算法理论基础1.1 ETL技术1.2OLAP技术1.3数据可视化二、 数据集三、实验及结果分析3.1 实验环境搭建3.2 模型训练最后前言  ......
  • 如何在vue中使用echarts,与jquery中有啥不同。
    一、vue中使用echarts的步骤在Vue中使用ECharts可以按照以下步骤进行:安装ECharts:使用npm或yarn安装ECharts:npminstallecharts在Vue组件中引入ECharts:importechartsfrom'echarts'在Vue组件的mounted钩子函数中初始化ECharts实例,并绑定到某个......
  • vue3+threejs新手从零开发卡牌游戏(二十二):添加己方游戏流程(先后手、抽牌、主要阶段、战
    首先在utils/common.ts里定义一些流程相关的变量:constflow=ref([//游戏流程{name:"抽卡阶段"},{name:"主要阶段"},{name:"战斗阶段"},{name:"结束阶段"}])constflowIndex=ref(......
  • C#的笔记~Three
    1.数据类型转换★为什么需要数据类型转换(1)隐式类型转换:在某种条件下,系统自动完成类型转换(即隐式类型转换)❉两种类型兼容例如:double兼容int类型❉目标类型精度大于源类型例如:double类型大于int类型(2)显示类型转换※※其中第二种是在程序开发中最常用的一种显性类型转换方......