Я пытаюсь создать 2 d3.js & svg глаза, которые следуют курсору мыши со зрачками, и когда мышь наступает над ними, они перемещаются в случайном положении. Однако после случайного числа успешных движений они замерзают, и я не могу понять, почему.d3.js глаза, следующие за блоками мыши
Это код:
var width = 1200,
height = 600;
var ptdata = [];
var pray = 10,
eray = 50;
var cx0 = 0, cx1 = 100
cy = 0;
var svg = d3.select("body")
.append("svg")
.attr("width", width)
.attr("height", height)
.append("g");
var eye0 = svg.append("circle")
.attr("id", "eye0")
.attr("class", "eye")
.attr("cx", cx0)
.attr("cy", cy)
.attr("r", eray)
.attr("stroke", "black")
.attr("fill", "white")
.data([ptdata]);
var eye1 = svg.append("circle")
.attr("id", "eye1")
.attr("class", "eye")
.attr("cx", cx1)
.attr("cy", cy)
.attr("r", eray)
.attr("stroke", "black")
.attr("fill", "white")
.data([ptdata]);
var pupil0 = svg.append("circle")
.attr("id", "pupil0")
.attr("class", "pupil")
.attr("cx", cx0)
.attr("cy", cy)
.attr("r", pray)
.attr("fill", "black")
.data([ptdata]);
var pupil1 = svg.append("circle")
.attr("id", "pupil1")
.attr("class", "pupil")
.attr("cx", cx1)
.attr("cy", cy)
.attr("r", pray)
.attr("fill", "black")
.data([ptdata]);
var eyes = svg.selectAll(".eye");
var pupils = svg.selectAll(".pupil");
function init() {
var tx0 = 0, ty0 = 0;
while (tx0<eray || (tx0+cx1)>(width-eray)) {
var randx = Math.round(Math.random() * width, 2);
tx0 = randx;
}
while (ty0<eray || (ty0+cy)>(height-eray)) {
var randy = Math.round(Math.random() * height, 2);
ty0 = randy;
}
cx0 += tx0; cx1 += tx0; cy += ty0;
eye0.attr("cx", cx0);
eye1.attr("cx", cx1);
eyes.attr("cy", cy);
pupil0.attr("cx", cx0);
pupil1.attr("cx", cx1);
pupils.attr("cy", cy);
}
init();
var distanceThreshold = eray - pray;
var mouseX = cx0, mouseY = cy;
var svgagain = d3.select("body").select("svg")
.on("mousemove", function() {
var pt = d3.mouse(this);
follow(pt);
});
function follow(pt) {
ptdata.push(pt);
var x = pt[0];
var y = pt[1];
var d0 = {
dx: x - cx0,
dy: y - cy
};
var distance0 = Math.sqrt(d0.dx * d0.dx + d0.dy * d0.dy);
var d1 = {
dx: x - cx1,
dy: y - cy
};
var distance1 = Math.sqrt(d1.dx * d1.dx + d1.dy * d1.dy);
mouseX0 = d0.dx/distance0 * distanceThreshold + cx0;
mouseY0 = d0.dy/distance0 * distanceThreshold + cy;
var xp0 = cx0, yp0 = cy;
var xp1 = cx1, yp1 = cy;
pupil0.attr("cx",
function(d) {
xp0 += (mouseX0 - xp0)/1;
return xp0;
})
.attr("cy",
function(d) {
yp0 += (mouseY0 - yp0)/1;
return yp0;
});
mouseX1 = d1.dx/distance1 * distanceThreshold + cx1;
mouseY1 = d1.dy/distance1 * distanceThreshold + cy;
pupil1.attr("cx",
function(d) {
xp1 += (mouseX1 - xp1)/1;
return xp1;
})
.attr("cy",
function(d) {
yp1 += (mouseY1 - yp1)/1;
return yp1;
});
}
function move(pt) {
ptdata.push(pt);
var x = pt[0];
var y = pt[1];
var tx = 0, ty = 0;
var newCX0 = cx0, newCX1 = cx1, newCY = cy;
while (x<(newCX1+eray) && x>(newCX0-eray) && y<(newCY+eray) && y>(newCY-eray) ||
(newCX0<eray || newCX1>(width-eray) || newCY<eray || newCY>(height-eray))){
var randx = Math.round(Math.random() * width, 2);
tx = Math.random() < 0.5 ? (-randx) : randx;
newCX0 += tx; newCX1 += tx;
var randy = Math.round(Math.random() * height, 2);
ty = Math.random() < 0.5 ? (-randy) : randy;
newCY += ty;
}
cx0 = newCX0; cx1 = newCX1; cy = newCY;
eye0.attr("cx", cx0);
eye1.attr("cx", cx1);
eyes.attr("cy", cy);
pupil0.attr("cx", cx0);
pupil1.attr("cx", cx1);
pupils.attr("cy", cy);
}
var eye0again = eye0.on("mouseover", function() {
var pt = d3.mouse(this);
move(pt);
});
в вашей демонстрации они не двигаются. –
Они делают для меня ... какой браузер вы используете? –
Google Chrome ... –