1 Star 0 Fork 0

ZhengQiang/mouse-follow

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
index.html 6.99 KB
一键复制 编辑 原始数据 按行查看 历史
ZhengQiang 提交于 2020-11-24 16:13 . first commit
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
html,
body {
background-color: transparent;
}
.element {
position: fixed;
top: 0;
left: 0;
display: block;
background-color: transparent;
background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAG0klEQVRYR62XCVBV5xXHf49NAj6UJ4IoqwZFUDBYF2wVAVEgRONWJ9JIrYqpS7WNbbTTqpAa0yQ2McYN4xjp0I6NGLQEUCJqTd0iKqgsmoAsyiqLLAFZXudcfcwDH9tMz8wdHt/9zjn/e/ajov80DZgHjAUmPmdPB7KBk8Cl/ohU9eOyE/AJMF/H46jWtKOFovoqIz05XwEbgcK+yO4PgETg1THWw5jnOoEgR09GDRqq6PihtoLUoruczL9FbnWpHH0NhP0/AfiKaYOdxrftmxlu3JPgtefj2pILb8sdcdXl3kB0Z4FgQB5v4ApQDvztM79wQp3H9ygzqeA26y7EyZ3fAbbAVCADSHn+dOLvCmAUsBVYZkjLlomhrPKc0SOAQ3f/w870pO7uxALR4jXdha4AJIrnOquHsMLjZ4S5ePOg7jHLUj9vq29pNp4+3I2js1b0CCDim8NcfHSfgaYD2mKDVhq7qIeQ+CCDw1nfUlD3WHhPPc8iRY4+gPXAp1PtRrLHL5wh5pYdiq6W5vHGmRjl/9Nzf4vbYDuDIIrrq5lx4q/Ku3/OjmTKsJEd9x43NbD+QhxXyvLk7DfAHn0AZkYqVZ65san9v4J/beShsX9Bwa3KIqKvnWo4EbrW8usHmVwry+e78nxKGmoRy/gM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p8Z2UNNZ2KkS9lmKRJIOGrg+MctDg5TYM1xEabmQ/JD37EXWNzViZvcSRwOW8MlRGyBepu1IstjreWzMScWnFOey5fbY9o6JIfxDFztKqPcjBw0gypyfSa0aLgHhdDJgaqVT3BhibOMXNjjQy1ErPFmezNzNNaceioOLHOq6XF1DWWMtkO1c8NMMVvQuS9jas9QqwlCLUlSSjws/EtD9taytq07a7AS36deBt4CPp6wf9l3UaSO7XlDHn1MeKvLT5v0emHEOkK9vyruvgIgPJ6nOx3KgokNebgF3yo+tIdgYI0o1kIc5eFNRVsiottrW6ucFkkp0rx+as7tHES04f5LuyfKwHWLYeClhm4qy2IbkgU38kSwVm64R0BSBm+VN3Q+kG71nI0xPtzvgGebohGUr/AtzvDoDuXMYiWb/cxZpAFXBQGosh3+ork1hZlXZUjsRUGmAOkPN8bZPRvBP1dTOS6fKHfi4mMuIrE2hP1FcASlyJ73Srmb+DO2MGP2u/uTWlnCvO0V/NJJbky3ul/gBwke1Ifzkdbjm4XTQ8aqjpupzKVvSgV+0GsqAvPLLzyYYsXUZWNyFZvTIB2Yz7tZ7/D9umpT+/eF4eAAAAAElFTkSuQmCC");
background-size: 100%;
}
</style>
<script type="text/javascript">
function Point(x, y) {
this.x = x;
this.y = y;
}
function ElementPoint(x, y) {
this.x = x;
this.y = y;
this.el = null;
this.render = function () {
if (!this.el) {
this.el = document.createElement("span");
this.el.setAttribute("style", "width: " + elementSize + "px; height: " + elementSize + "px;");
this.el.setAttribute("class", "element");
document.body.appendChild(this.el);
}
this.el.style.top = this.y + "px";
this.el.style.left = this.x + "px";
};
this.reset = function (x, y) {
this.x = x;
this.y = y;
};
}
var elementSize = 24;
var elmentArr = new Array(20);
var historyArr = new Array(elmentArr.length);
function loadFunc() {
for (var i = 0; i < elmentArr.length; i++) {
elmentArr[i] = new ElementPoint(0, 0);
elmentArr[i].render();
}
resetHistory(0, 0);
setInterval(actionLoop, 30);
}
function resetHistory(x, y) {
for (let i = 0; i < historyArr.length; i++) {
historyArr[i] = new Point(x, y);
}
}
function actionLoop() {
for (var i = historyArr.length - 1; i > 0 ; i--) {
historyArr[i].x = historyArr[i - 1].x;
historyArr[i].y = historyArr[i - 1].y;
elmentArr[i].reset(historyArr[i - 1].x, historyArr[i - 1].y);
elmentArr[i].render();
}
if (historyArr[0].x == historyArr[historyArr.length -1].x && historyArr[0].y == historyArr[historyArr.length -1].y) {
resetHistory(elmentArr[0].x, elmentArr[0].y);
}
}
function mouseMoveAction(e) {
var x = e.clientX + 10;
var y = e.clientY + 10;
elmentArr[0].reset(x, y);
elmentArr[0].render();
var dis = calculateDistance(x, y, historyArr[0].x, historyArr[0].y);
if (dis > elementSize) {
var mDis = dis % elementSize || elementSize;
var middlePoint = calculatePosition(x, y, historyArr[0].x, historyArr[0].y, mDis, dis);
var currentPoint = historyArr[0];
historyArr[0] = middlePoint;
dis = dis - mDis;
var count = parseInt(dis / elementSize);
if (count > historyArr.length - 1) {
count = historyArr.length - 1;
}
for (var i = elmentArr.length - 1; i > count; i--) {
historyArr[i] = historyArr[i - count];
}
for (var i = 1; i <= count; i++) {
historyArr[i] = middlePoint = calculatePosition(middlePoint.x, middlePoint.y, currentPoint.x, currentPoint.y, elementSize, dis - ((i - 1) * elementSize));
}
}
for (var i = 1; i < historyArr.length; i++) {
elmentArr[i].reset(historyArr[i - 1].x, historyArr[i - 1].y);
elmentArr[i].render();
}
}
//求两点距离
function calculateDistance(x, y, x1, y1) {
return Math.sqrt(Math.pow(x - x1, 2) + Math.pow(y - y1, 2));
}
//已知距离和两点坐标,求另一点坐标
function calculatePosition(x, y, x1, y1, d, r) {
var cy = (d * (y1 - y)) / r + y;
var cx = (d * (x1 - x)) / r + x;
return new Point(cx, cy);
}
window.addEventListener("load", loadFunc, false);
window.addEventListener("mousemove", mouseMoveAction, false);
</script>
</head>
<body>
</body>
</html>
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/4mylove/mouse-follow.git
git@gitee.com:4mylove/mouse-follow.git
4mylove
mouse-follow
mouse-follow
master

搜索帮助