在VueJS中正确实现自定义类?

如何解决在VueJS中正确实现自定义类?

我想知道在Vue方面有经验的人能否指出我正确的方向。

我正在尝试在VueJS中导入一个自定义类(我正在使用Nuxt),但我不确定我是否正确地执行了此操作,此刻我的主要问题是mouseOver Effects arent与Vanilla JS和我无法改变类的半径(可以在Vanilla JS中使用它)。

下面是我的Vue文件:

<template id="breath">
  <div id="page__home">
    <v-row dense>
      <v-col cols="12" align="center" justify="center">
        <h1 class="card__header">
          <v-icon large color="black">
            mdi-comment-question-outline
          </v-icon>
          Mindful Breath
        </h1>
        <div class="canv_wrap">
          <canvas id="canvas" width="550" height="450" @mouseover="mouseMove">
          </canvas>
          <h1 class="instructions">{{ breathText[0] }}</h1>
        </div>
        <v-container fluid class="container">
          <v-row align="center" justify="center">
            <v-spacer></v-spacer>
            <v-btn fab medium color="secondary">
              <v-icon color="black">
                mdi-volume-high
              </v-icon>
            </v-btn>
            <v-spacer></v-spacer>
            <v-btn fab x-large color="primary" @click="start">
              <v-icon large color="white">
                mdi-play
              </v-icon>
            </v-btn>
            <v-spacer></v-spacer>
            <v-btn fab medium color="secondary">
              <v-icon color="black">
                mdi-tune-vertical
              </v-icon>
            </v-btn>
            <v-spacer></v-spacer>
          </v-row>
        </v-container>
      </v-col>
    </v-row>
  </div>
</template>

<script>
import Blob from "../assets/content/Blob_Point"
import { Point } from "../assets/content/Blob_Point"
let oldMousePoint = { x: 0,y: 0 }
let blob = new Blob()
let hover = false
let canvas = document.getElementById("canvas")
export default {
  data() {
    return {
      canvas: null,x: 0,y: 0,ticker: undefined,timer: undefined,timerState: "stopped",breathText: ["Inhale","Hold","Exhale","Hold"],}
  },mounted() {
    let canvas = document.getElementById("canvas")
    this.canvas = canvas.getContext("2d")
    canvas.setAttribute("touch-action","none")
    blob = new Blob(this.color,this.canvas,this.radius)
    blob.color = "#8CE5EA"
    blob.canvas = canvas
    blob.radius = 50
    blob.init()
    blob.render()
  },created() {
    window.addEventListener("pointermove",this.mouseMove)
    window.addEventListener("mousemove",this.mouseMove)
    window.addEventListener("resize",this.resize)
  },methods: {
    start() {
      if (this.timerState !== "running") {
        this.tick()
        this.timerState = "running"
      } else {
        window.clearInterval(this.timer)
        this.timerState = "stopped"
      }
    },// ------ Text Change methods + Timer ---------//
    tick() {
      this.timer = window.setInterval(() => {
        this.textChange()
        this.f_varySize()
      },5)
    },textChange() {
      const first = this.breathText.shift()
      this.breathText = this.breathText.concat(first)
    },// -------- Radius Change (Not Working) ---- //
    f_varySize() {
      console.log(this.radius)
      let dir = 0.25
      if (blob.radius > 300) {
        dir = -0.25
        //console.log(g_radius);
      }
      if (blob.radius < 200) {
        dir = 0.25
        //console.log(g_radius);
      } else {
        blob.radius += dir
      }
    },resize() {
      console.log("resized")
      canvas.width = window.innerWidth
      canvas.height = window.innerHeight
    },// -------- spring/wave animation on mouseOver of blob ----//
    mouseMove(e) {
      let pos = blob.center
      let diff = { x: e.clientX - pos.x,y: e.clientY - pos.y }
      let distx = diff.x * diff.x
      let disty = diff.y * diff.y
      let dist = Math.sqrt(distx + disty)
      let angle = null
      blob.mousePos = {
        x: pos.x - e.clientX,y: pos.y - e.clientY,}
      if (dist < blob.radius && hover === false) {
        let vector = {
          x: e.clientX - pos.x,y: e.clientY - pos.y,}
        angle = Math.atan2(vector.y,vector.x)
        hover = true
      } else if (dist > blob.radius && hover === true) {
        let vector = {
          x: e.clientX - pos.x,}

        angle = Math.atan2(vector.y,vector.x)
        hover = false
        blob.color = null
      }
      if (typeof angle == "number") {
        let nearestPoint = null
        let distanceFromPoint = 100
        blob.points.forEach((point) => {
          if (Math.abs(angle - point.azimuth) < distanceFromPoint) {
            nearestPoint = point
            distanceFromPoint = Math.abs(angle - point.azimuth)
          }
        })
        if (nearestPoint) {
          let strength = {
            x: oldMousePoint.x - e.clientX,y: oldMousePoint.y - e.clientY,}
          let strX = strength.x * strength.x
          let strY = strength.y * strength.y
          strength = Math.sqrt(strX + strY) * 1
          if (strength > 100) strength = 100
          let strDiv = strength / 100
          nearestPoint.acceleration = strDiv * (hover ? -1 : 1)
        }
      }
      oldMousePoint.x = e.clientX
      oldMousePoint.y = e.clientY
    },// ------ Blob initialisation ------//
    init() {
      for (let i = 0; i < this.numPoints; i++) {
        let point = new Point(this.divisional * (i + 1),this)
        //point.acceleration = -1 + Math.random() * 2
        this.push(point)
      }
    },//---------//
  },}
</script>

<style scoped>
#canvas {
  width: 100%;
  height: auto;
  position: relative;
  border: 1px solid grey;
  z-index: 0;
}
.instructions {
  color: #fff;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%,-50%);
  z-index: 100;
}
.container {
  z-index: 100;
  position: fixed;
  bottom: 5%;
  left: 50%;
  transform: translateX(-50%);
}
.canv_wrap {
  position: relative;
}
</style>

下面是带有Blob类的JS文件:

/* eslint-disable */

// Blob Class
export default class Blob {
  // setup function
  constructor(color,canvas) {
    //the objects setup
    // 'this' is a reference to the current class
    this.points = []
    this._color = color
    this.canvas = canvas
  }
  init() {
    for (let i = 0; i < this.numPoints; i++) {
      let point = new Point(this.divisional * (i + 1),this)
      //point.acceleration = -1 + Math.random() * 2;
      this.push(point)
    }
  }
  render() {
    let canvas = this.canvas
    let ctx = this.ctx
    let position = this.position
    let pointsArray = this.points
    let radius = this.radius
    let points = this.numPoints
    let divisional = this.divisional
    let center = this.center
    ctx.clearRect(0,canvas.width,canvas.height)
    pointsArray[0].solveWith(pointsArray[points - 1],pointsArray[1])
    let p0 = pointsArray[points - 1].position
    let p1 = pointsArray[0].position
    let _p2 = p1
    // this is the draw
    ctx.beginPath()
    ctx.moveTo(center.x,center.y)
    ctx.moveTo((p0.x + p1.x) / 2,(p0.y + p1.y) / 2)
    for (let i = 1; i < points; i++) {
      pointsArray[i].solveWith(
        pointsArray[i - 1],pointsArray[i + 1] || pointsArray[0]
      )
      let p2 = pointsArray[i].position
      var xc = (p1.x + p2.x) / 2
      var yc = (p1.y + p2.y) / 2
      ctx.quadraticCurveTo(p1.x,p1.y,xc,yc)
      // ctx.lineTo(p2.x,p2.y);
      //ctx.fillStyle = this.color;
      // ctx.fillRect(p1.x-2.5,p1.y-2.5,5,5);
      p1 = p2
    }

    var xc = (p1.x + _p2.x) / 2
    var yc = (p1.y + _p2.y) / 2
    ctx.quadraticCurveTo(p1.x,yc)
    ctx.lineTo(_p2.x,_p2.y)

    ctx.closePath()
    ctx.fillStyle = this.color
    ctx.fill()
    ctx.strokeStyle = this.color
    // ctx.stroke();

    /*
    ctx.fillStyle = '#000000';
    if(this.mousePos) {
    let angle = Math.atan2(this.mousePos.y,this.mousePos.x) + Math.PI;
    ctx.fillRect(center.x + Math.cos(angle) * this.radius,center.y + Math.sin(angle) * this.radius,5);
    }*/
    requestAnimationFrame(this.render.bind(this))
  }

  push(item) {
    if (item instanceof Point) {
      this.points.push(item)
    }
  }
  set color(value) {
    this._color = value
  }
  get color() {
    return this._color
  }
  set canvas(value) {
    if (
      value instanceof HTMLElement &&
      value.tagName.toLowerCase() === "canvas"
    ) {
      this._canvas = canvas
      this.ctx = this._canvas.getContext("2d")
    }
  }
  get canvas() {
    return this._canvas
  }
  set numPoints(value) {
    if (value > 10) {
      this._points = value
    }
  }
  get numPoints() {
    return this._points || 32
  }
  set radius(value) {
    if (value > 0) {
      this._radius = value
    }
  }
  get radius() {
    return this._radius || 50
  }
  set position(value) {
    if (typeof value == "object" && value.x && value.y) {
      this._position = value
    }
  }
  get position() {
    return this._position || { x: 0.5,y: 0.5 }
  }
  get divisional() {
    return Math.PI * 2 / this.numPoints
  }
  get center() {
    return {
      x: this.canvas.width * this.position.x,y: this.canvas.height * this.position.y,}
  }
  set running(value) {
    this._running = value === true
  }
  get running() {
    return this.running !== false
  }
  
  f_varySize() {
    if (this.radius > 300) {
    dir = -.25;
    console.log(this.radius);
  }
  if (this.radius < 200) {
    dir = .25;
    console.log(this.radius);
  }
  this.radius += dir;
  
  } 
}
// Point Class
export class Point {
  constructor(azimuth,parent) {
    this.parent = parent
    this.azimuth = Math.PI - azimuth
    this._components = {
      x: Math.cos(this.azimuth),y: Math.sin(this.azimuth),}
    this.acceleration = -0.3 + Math.random() * 0.6
  }
  solveWith(leftPoint,rightPoint) {
    this.acceleration =
      (-0.3 * this.radialEffect +
        (leftPoint.radialEffect - this.radialEffect) +
        (rightPoint.radialEffect - this.radialEffect)) *
        this.elasticity -
      this.speed * this.friction
  }
  set acceleration(value) {
    if (typeof value == "number") {
      this._acceleration = value
      this.speed += this._acceleration * 2
    }
  }
  get acceleration() {
    return this._acceleration || 0
  }
  set speed(value) {
    if (typeof value == "number") {
      this._speed = value
      this.radialEffect += this._speed * 3
    }
  }
  get speed() {
    return this._speed || 0
  }
  set radialEffect(value) {
    if (typeof value == "number") {
      this._radialEffect = value
    }
  }
  get radialEffect() {
    return this._radialEffect || 0
  }
  get position() {
    return {
      x:
        this.parent.center.x +
        this.components.x * (this.parent.radius + this.radialEffect),y:
        this.parent.center.y +
        this.components.y * (this.parent.radius + this.radialEffect),}
  }
  get components() {
    return this._components
  }
  set elasticity(value) {
    if (typeof value === "number") {
      this._elasticity = value
    }
  }
  get elasticity() {
    return this._elasticity || 0.001
  }
  set friction(value) {
    if (typeof value === "number") {
      this._friction = value
    }
  }
  get friction() {
    return this._friction || 0.0085
  }
}

这个想法是基于Liam Egan的草图: https://codepen.io/shubniggurath/pen/EmMzpp

这是单击“播放”按钮时控制台的屏幕截图,该按钮触发了start()方法,该方法中有一个功能可以更改文本(有效)并更改半径(无效) 。 Console Log

感谢您的帮助/指导/建议

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res