我在构造函数(draw
)函数内编写了一个名为Circle()
的函数。但是,当我调用此函数时,它将返回错误。谁能帮我解决这个问题?
这是我的代码:
var canvas = document.getElementById('canvas'),
c = canvas.getContext('2d')
canvas.width = innerWidth
canvas.height = innerHeight
canvas.style.background = 'black'
function Circle(x, y, dx, dy, radius) {
this.x = x
this.y = y
this.dx = dx
this.dy = dy
this.radius = radius
this.circleColor = 'red'
this.draw = function () {
c.beginPath()
c.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false)
c.fillStyle = this.circleColor
c.fill()
c.closePath()
}
}
var radius = Math.floor(Math.random() * 30),
x = Math.random() * (innerWidth - radius * 2) + radius,
y = Math.random() * (innerHeight - radius * 2) + radius,
dx = (Math.random() - .5 ) * 10,
dy = (Math.random() - .5 ) * 10
var circle1 = Circle(x, y, dx, dy, radius)
function animate() {
requestAnimationFrame(animate)
c.clearRect(0, 0, innerWidth, innerHeight)
circle1.draw()
}
animate()
<canvas id="canvas"></canvas>