[英]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:为了创造这种效果,这里有一些步骤来实现,从最后开始:
scaleX()
and scaleY()
in CSS transforms您将希望在 CSS 转换中使用scaleX()
和scaleY()
MouseEvent
object) cursor 的当前 position(通过MouseEvent
对象轻松访问)With this strategy in mind, here is how it could be done:考虑到这种策略,可以这样做:
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,
};
},
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;
});
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;
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})`;
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 的更新示例:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.