Nền tảng của tôi trong Javascript không phải là mạnh nhất và tôi tò mò về cách những người khác sẽ đi về thử thách hiện tại mà tôi đã tạo cho bản thân mình.Tạo các biến độc đáo
Tôi đang chơi xung quanh với paper.js
Đoạn mã sau tạo này
Con mắt phản ứng với các sự kiện chuột trong cùng một cách như con mắt ở đây (rút ra từ mã mà) - www. arc.id.au/XEyes.html
Dưới đây là những gì tôi có:
// Eye position center
eCntrX = 100
eCntrY = 100
var topLid = new Path()
topLid.add(new Point(eCntrX - 60, eCntrY))
topLid.add(new Point(eCntrX, eCntrY - 28))
topLid.add(new Point(eCntrX + 60, eCntrY))
topLid.add(new Point(eCntrX, eCntrY + 28))
topLid.strokeWidth = '6'
topLid.strokeColor = '#000'
topLid.closed = true
topLid.smooth()
var iris = new Path.Circle(eCntrX, eCntrY, 24)
iris.fillColor = '#6CE0FF'
iris.strokeWidth = '6'
iris.strokeColor = '#000'
var pupil = new Path.Circle(eCntrX, eCntrY, 15)
pupil.fillColor = '#000'
var glint = new Path.Circle(eCntrX, eCntrY, 5)
glint.fillColor = '#fff'
glint.position = new Point(eCntrX + 6, eCntrY - 6)
var ball = new Group([iris, pupil, glint])
function onMouseMove(event) {
// Cursor position
var csrX = event.point.x
var csrY = event.point.y
// Ball position
var ballX = ball.position.x
var ballY = ball.position.y
// Displacement
var dx = csrX - ballX
var dy = csrY - ballY
//Radius
var r = 5
//Pythagerous thereom calcs. R
var R = Math.sqrt(dx*dx+dy*dy)
x = dx*r/R
y = dy*r/R
ball.position = new Point(eCntrX + x, eCntrY + y)
// console.log('x:' + x + 'y:' + y)
}
tôi đang tìm cách để lấp đầy toàn bộ trang với đôi mắt. mục tiêu cuối cùng của tôi là tạo ra một cái gì đó như thế này:
Câu hỏi của tôi là, cách tốt nhất để đi về việc tạo ra nhiều mắt có tương tác là gì.
Tôi đã chơi xung quanh với 'for', nhưng hàm onMouseMove chỉ áp dụng cho Eye mới nhất được tạo.
cũng đã được nhìn paperjs item.clone - paperjs.org/reference/item#clone
Hoặc là nó là vấn đề của tôi tạo ra các biến độc đáo cho mỗi mắt?
Dưới đây là đoạn code với cho theo yêu cầu:
for(var i = 0; i < 10; i++){
// Eye position center
// 100, 300, 500, 600
eCntrX = 100 * i + 100
eCntrY = 100
var topLid = new Path()
topLid.add(new Point(eCntrX - 60, eCntrY))
topLid.add(new Point(eCntrX, eCntrY - 28))
topLid.add(new Point(eCntrX + 60, eCntrY))
topLid.add(new Point(eCntrX, eCntrY + 28))
topLid.strokeWidth = '6'
topLid.strokeColor = '#000'
topLid.closed = true
topLid.smooth()
var iris = new Path.Circle(eCntrX, eCntrY, 24)
iris.fillColor = '#6CE0FF'
iris.strokeWidth = '6'
iris.strokeColor = '#000'
var pupil = new Path.Circle(eCntrX, eCntrY, 15)
pupil.fillColor = '#000'
var glint = new Path.Circle(eCntrX, eCntrY, 5)
glint.fillColor = '#fff'
glint.position = new Point(eCntrX + 6, eCntrY - 6)
var ball = new Group([iris, pupil, glint])
}
function onMouseMove(event) {
// Cursor position
var csrX = event.point.x
var csrY = event.point.y
// Ball position
var ballX = ball.position.x
var ballY = ball.position.y
// Displacement
var dx = csrX - ballX
var dy = csrY - ballY
//Radius
var r = 5
//Pythagerous thereom calcs. R
var R = Math.sqrt(dx*dx+dy*dy)
x = dx*r/R
y = dy*r/R
ball.position = new Point(eCntrX + x, eCntrY + y)
console.log('x:' + x + 'y:' + y)
}
phần quan trọng nhất bị thiếu .. đăng vòng lặp 'for' và tạo mắt. –
Có thể bạn sẽ cần một vòng lặp 'while' để tạo mắt và vòng lặp' for' 'onMouseMove' để cập nhật tất cả các con mắt. – MiniGod
@KarolyHorvath - Tôi đã thêm vòng lặp 'for' mà tôi đã thử. Chỉ là thử nghiệm nó khi tạo ra một vài. Vấn đề là chỉ có mắt cuối cùng được tạo ra là tương tác. – cupcakekid