- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 Javascript 创建一个跟随鼠标在页面上移动的东西。我希望它是米老鼠,我希望他的眼睛跟随鼠标移动他的眼球...这是我到目前为止的代码(从网络上的各个地方收集,因此归功于编写该部分的人):
<script>
var jseyesimg="http://oi67.tinypic.com/frnys.jpg";
var jseyeimg="http://oi63.tinypic.com/nxwa5u.jpg";
var jseyeslink="http://www.javascriptkit.com";
var jseyeso=null, jseye1=null, jseye2=null;
var standardbody=(document.compatMode=="CSS1Compat")? document.documentElement : document.body //create reference to common "body" across doctypes
function jseyesobj(id) {
var i, x;
x= document[id];
if (!x && document.getElementById) x= document.getElementById(id);
for (i=0; !x && i<document.forms.length; i++) x= document.forms[i][id];
return(x);
}
function jseyesmove(x, y) {
var ex, ey, dx, dy;
if (jseyeso && jseye1 && jseye2 && jseyeso.style) {
ex=jseyeso.offsetLeft+46; ey=jseyeso.offsetTop+58;
dx=x-ex; dy=y-ey;
r=(dx*dx/49+dy*dy/289<1) ? 1 : Math.sqrt(49*289/(dx*dx*289+dy*dy*49));
jseye1.style.left= r*dx+36.5+'px'; jseye1.style.top= r*dy+44+'px';
ex+=56; dx-=56;
r=(dx*dx/49+dy*dy/289<1) ? 1 : Math.sqrt(49*289/(dx*dx*289+dy*dy*49));
jseye2.style.left= r*dx+92.5+'px'; jseye2.style.top= r*dy+44+'px';
}
}
function jseyes() {
var img;
var x, y, a=false;
if (arguments.length==2) {
x= arguments[0];
y= arguments[1];
a= true;
}
img= "<div id='jseyeslayer' style='position:"+
(a ? "absolute; left:"+x+"; top:"+y : "relative")+
"; z-index:5; width:150; height:150 overflow:hidden'>"+
"<div id='jseye1' style='position:absolute; left:130; top:44; z-index:6; width:21; height:29'>"+
"<img src='"+jseyeimg+"' width=21 height=29 onClick=\"location.href='"+jseyeslink+"'\">"+
"</div>"+
"<div id='jseye2' style='position:absolute; left:160; top:44; z-index:6; width:21; height:29'>"+
"<img src='"+jseyeimg+"' width=21 height=29 onClick=\"location.href='"+jseyeslink+"'\">"+
"</div>"+
"<img src='"+jseyesimg+"' width=300 height=300 onClick=\"location.href='"+jseyeslink+"'\">"+
"</div>";
document.write(img);
jseyeso=jseyesobj('jseyeslayer');
jseye1=jseyesobj('jseye1');
jseye2=jseyesobj('jseye2');
document.onmousemove=jseyesmousemove;
}
function jseyesmousemove(e) {
var mousex=(e)? e.pageX : event.clientX+standardbody.scrollLeft
var mousey=(e)? e.pageY : event.clientY+standardbody.scrollTop
jseyesmove(mousex, mousey);
//return(false);
}
</script>
我不知道如何将眼睛放在眼球内。如果有人能告诉我什么值意味着什么,我想尝试一些数字来看看什么看起来最好。 :)
谢谢:)
最佳答案
我个人认为使用 Canvas 来做这样的事情更有意义。
我在下面创建了一个带注释的示例。该示例不使用图像,因此您必须自己解决该部分,这是有关 Canvas API 的信息的良好资源(包括如何绘制图像)can be found on MDN .
数学可以分解为:
注意:按“运行代码片段”即可查看示例的工作情况。
const canvas = document.querySelector('canvas');
const context = canvas.getContext('2d');
const TWOPI = 2 * Math.PI;
// size the canvas to the full width/height available
// Note: this solution is not responsive
canvas.width = innerWidth;
canvas.height = innerHeight;
// eye objects will be in charge of holding state and rendering themselves
class Eye {
constructor(x, y) {
this.x = x;
this.y = y;
this.width = 40;
this.height = 80;
this.pupil = { x: 0, y: 0, width: 10, height: 20 };
}
draw() {
const {x, y} = this;
context.save();
context.translate(x, y);
this.drawOutline();
this.drawPupil()
context.restore();
}
drawOutline() {
const {width, height} = this;
context.beginPath();
context.ellipse(0, 0, width, height, 0, 0, TWOPI);
context.stroke();
}
drawPupil() {
const {x, y, width, height} = this.pupil;
context.beginPath();
context.ellipse(x, y, width, height, 0, 0, TWOPI);
context.fill();
}
track(object) {
const {x, y, width, height, pupil} = this;
// find the angle between the mouse and the center of the eye
const xDiff = (x - object.x);
const yDiff = (y - object.y);
const angle = Math.atan2(yDiff, xDiff) - Math.PI;
if (!isNaN(angle)) {
// calculate the point on the circumference of the eye
const cX = (width * Math.cos(angle));
const cY = (height * Math.sin(angle));
// calculate the point on the circumference of the pupil
const pX = pupil.width * Math.cos(angle);
const pY = pupil.height * Math.sin(angle);
// position the pupil at the edge of the eye, minus the radius of the pupil
pupil.x = cX - pX;
pupil.y = cY - pY;
}
}
}
// on mousemove update the mouse position
window.addEventListener('mousemove', e => {
mouse.x = e.clientX;
mouse.y = e.clientY;
});
const mouse = { x: 0, y: 0 };
const leftEye = new Eye(100, 100);
const rightEye = new Eye(200, 100);
// on every tick redraw the eyes with a new position
function tick() {
// clear canvas
context.clearRect(0, 0, canvas.width, canvas.height);
// track the mouse and draw the left eye
leftEye.track(mouse);
leftEye.draw();
// copy the state of the left eyes pupil
rightEye.pupil.x = leftEye.pupil.x;
rightEye.pupil.y = leftEye.pupil.y;
//rightEye.track(mouse); // or use this for independent eye tracking
rightEye.draw();
requestAnimationFrame(tick);
}
// kick off animation loop
tick();
body { margin: 0; }
<canvas />
如果您对可视化编程感兴趣,我想您可以通过研究 p5.js 学到很多东西。和 work of Daniel Shiffman ;他有大量关于如何开始使用此类内容的视频。我希望这对您有所帮助,祝您好运!
关于javascript - 眼睛跟随光标 - 定位?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43622413/
我有一个包含透明区域的 png,并将其设置为图像标签。 当光标位于图像的不透明部分上时,如何将光标设置为手? 谢谢 最佳答案 为此,您需要查看位图本身。 WPF 的 HitTest 机制认为任何使用“
我想隐藏圆形仪表的手(那就是中间的东西,对吧?)。到目前为止,我尝试过: myCircularGauge.getHand().setVisible(false); 但是,绘制图表时这似乎会产生崩溃。如
我有两张图片:一张是张开的手,一张是抓着的手。我希望一个简单的“onmousedown”和“onmouseup”函数有助于制作出著名的抓手,您可以在类似谷歌地图的东西中看到它。但...抱歉,从头开始:
是否可以在sequelize迁移中使用光标?我正在尝试创建 DML 脚本,其想法是循环表中的值,即。使用游标输入日期,然后将值插入到其他表中,即。光标内的膳食日。 table : day dayId
我正在尝试使用格式加载值 +02:00 - mysql> select SUBSTR('2016-01-12T14:29:31.000+02:00',24,6); +02:00
我一直在尝试构建一个基于网络的文本编辑器。作为该过程的一部分,我正在尝试动态创建和修改基于元素的事件和用于字体编辑的击键事件。在这个特别的jsfiddle示例 我试图在按下 CTRL+b 并将焦点/插
我同时使用了 supertab 和 snipmate 插件。假设我正在使用 snipmate 创建一个 if 语句结构。在 if 语句中完成添加语句后,如何快速将光标移动到 if 语句之后。例如: i
我正在为我的 BlackBerry 项目创建一个搜索框,但看起来 BlackBerry 没有用于创建单行 EditField 的 API。我通过扩展 BasicEditField 和覆盖布局和绘制等方
我想知道如何获得 not-allowed光标在我禁用的链接上。我试图将它添加到禁用事件中,但它在那里不起作用,然后我尝试使用相同的光标事件引入悬停效果。关于如何让它发挥作用的任何想法?我在这里包含了我
在 Delphi 6 中,我可以使用 Screen.Cursor 更改所有表单的鼠标光标: procedure TForm1.Button1Click(Sender: TObject); begin
这个 Meteor 服务器代码需要每 n 秒从集合中打印一次文档,我该如何让它工作?谢谢 myCol.find({abc: undefined}).forEach( fun
在这个论坛上花了相当长的时间寻找与我的问题类似的答案,但找不到符合我的情况的答案。 我有一个 HTML 表单,通过 javascript 将其提交到我的 aspx 页面。 function Submi
是否可以在网页上创建透明的 HTML 光标?我使用 div 作为光标,我想让 div 50% 透明: http://jsfiddle.net/mCgmP/16/ I want this cursor
我正在使用 Cursor 来获取存储在我的 SQLite 数据库中的一些数据。当我的光标有数据库中的一些数据时,代码可以正常工作。但是,当 Cursor 不包含任何数据时,我在 Cursor.move
我希望隐藏特定范围的 x 和 y 位置中的光标。这是一些示例代码,代表了我想要做的事情。 if(x >= xLowerBound && x = yLowerBound + 20 && y = xLow
我有一个 .jsp 页面,用户可以在其中输入信息,然后使用保存按钮保存。该应用程序可以运行,但由于按钮的单击事件正在运行 Java 代码,然后将保存的信息添加到 Oracle 数据库,因此需要一些时间
为什么 Android 中 Cursor 没有 moveBeforeFirst()? 其他风格的 Java 中也有类似的方法,如果您需要重新迭代结果集(例如,在 while(cursor.moveTo
我想使用 Tkinter 捕获相对鼠标运动。我附上一个监听器并且能够获取鼠标移动。但是,我希望能够“捕获”/“锁定”光标,使其不可见并且无法离开窗口(就像游戏一样)。我的目标是获得相对鼠标移动而不受窗
当应用程序同步时,我尝试更新数据库中每一行的“html”列。我用过这个教程Here将应用程序添加到“配置文件”列表中。这是我在 SyncAdapter 中使用的代码: private static v
我正在使用 Uploadify带有图像按钮。一切正常。除了,我需要在鼠标悬停时使用 cursor:crosshair; 而不是 cursor:default;。 我试着在 CSS 中这样设置它: ob
我是一名优秀的程序员,十分优秀!