简体   繁体   English

定制 Cursor - 快速移动时更改比例

[英]Custom Cursor - change scale while moving fast

I've created a custom cursor and I want to change its scale dynamically on fast movements.我创建了一个自定义 cursor,我想在快速移动时动态更改它的比例。 It's a cursor like this one: Cuberto.com这是一个像这样的 cursor: Cuberto.com

My cursor: CodeSandBox我的cursor :代码沙盒

When the cursor moves quickly to the right or left, scaleX should grow and get smaller again.当 cursor 快速向右或向左移动时,scaleX 应该会增长并再次变小。 When the cursor moves up or down, scaleY should grow and so on...当 cursor 向上或向下移动时,scaleY 应该增长等等......

How can I do this?我怎样才能做到这一点?

My cursor我的 cursor

mounted() {
    this.customCursor()
  },

  methods: {
    customCursor() {
      const self = this
      const cursor = self.$refs.cursor
      document.addEventListener('mousemove', (e) => {
        cursor.setAttribute(
          'style',
          'top: ' + (e.pageY - scrollY) + 'px; left: ' + e.pageX + 'px;'
        )
      })
    },
  },

In order to create this effect, here are some steps to achieve that, starting from the end:为了创造这种效果,这里有一些步骤来实现,从最后开始:

  1. You will want to use scaleX() and scaleY() in CSS transforms您将希望在 CSS 转换中使用scaleX()scaleY()
  2. You will therefore need to somehow get the velocity of the cursor movement in the x and y axis因此,您需要以某种方式获得 cursor 在 x 和 y 轴上的运动速度
  3. To get the velocity you will need the following information:要获得速度,您将需要以下信息:
    • The current position of the cursor (easily accessed via the MouseEvent object) cursor 的当前 position(通过MouseEvent对象轻松访问)
    • The previous position of the cursor (you will need to cache this) cursor 的前 position(您需要缓存这个)
    • The time elapsed between the previous mouse event and current mouse event上一个鼠标事件和当前鼠标事件之间经过的时间

With this strategy in mind, here is how it could be done:考虑到这种策略,可以这样做:

Step 1: Allow your data object to cache the previous XY coordinates and timestamp第 1 步:允许您的data object 缓存之前的 XY 坐标时间戳

This can be done by simply updating the data object:这可以通过简单地更新data object 来完成:

data() {
  return {
    hideCursor: false,
    prevX: 0,
    prevY: 0,
    prevTimeStamp: 0,
  };
},

Step 2: Ensure that you cache these values in your mousemove callback:第 2 步:确保在mousemove回调中缓存这些值:

We need to store these values at the very end of the mousemove callback, and place the rest of our logic before that (we will get to that later).我们需要在mousemove回调的最后存储这些值,并在此之前放置我们逻辑的 rest(我们稍后会谈到)。

document.addEventListener("mousemove", (e) => {
  // SOME LOGIC HERE

  // Update cached values
  this.prevX = e.pageX;
  this.prevY = e.pageY;
  this.prevTimeStamp = e.timeStamp;
});

Step 3: Calculate velocity by comparing current and previous value第 3 步:通过比较当前值和先前值来计算速度

To calculate velocity in the X and Y axes, you need to calculate the absolute difference between previous and current X/Y values, and divide them by the time elapsed:要计算 X 和 Y 轴上的速度,您需要计算先前和当前 X/Y 值之间的绝对差,然后将它们除以经过的时间:

const deltaX = Math.abs(e.pageX - this.prevX);
const deltaY = Math.abs(e.pageY - this.prevY);
const deltaTime = e.timeStamp - this.prevTimeStamp;
const velocityX = deltaX / deltaTime;
const velocityY = deltaY / deltaTime;

Step 4: Determine how to translate velocities into CSS scale transform第 4 步:确定如何将速度转换为 CSS 比例变换

This is completely arbitrarily determined and it's up to you .这完全是任意决定的,由您决定 For me, dividing the calculated velocity by 10 gives an effect that I find not too jarring/abrupt and still perceptible to the user.对我来说,将计算出的速度除以 10 会产生一种效果,我发现它不会太刺耳/突然,并且用户仍然可以感知。 This value might need to be changed based on the absolute pixel size of the cursor.此值可能需要根据 cursor 的绝对像素大小进行更改。

const scaleX = 1 + velocityX / 10;
const scaleY = 1 + velocityY / 10;

cursor.style.top = `${e.pageY}px`;
cursor.style.left = `${e.pageX}px`;
cursor.style.transform = `translate(-50%, -50%) scaleX(${scaleX}) scaleY(${scaleY})`;

(Optional) Step 5: Ensure that you handle mouseout event on the viewport (可选)第 5 步:确保在视口上处理mouseout事件

This is so that the cursor is not stuck in a strange state when mouse leaves the viewport:这样当鼠标离开视口时,cursor 就不会卡在奇怪的 state 中:

document.addEventListener('mouseout', () => {
  cursor.style.transform = 'none';
});

See an updated example based on your CodeSandbox:查看基于您的 CodeSandbox 的更新示例:

编辑充满活力的 easley-7odl4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM