Alexander Deplov

Bouncing ball

Bouncing ball

Code

var bg = new Path.Rectangle({
    size: view.bounds,
    fillColor: 'black',
    position: view.center
})

var x = 100
var y = 100
var xspeed = 1
var yspeed = 3.3
var ballRadius = 50
var ball = new Path.Circle(view.center, ballRadius)
ball.fillColor = 'blue'
ball.applyMatrix = false

function onFrame(event) {
    x += xspeed * 1
    y += yspeed * 1
    if (x > view.size.width - ballRadius / 2 || x < ballRadius / 2) {
        xspeed = xspeed * -1
        tweenOnBounce(ball)
    }
    if (y > view.size.height - ballRadius / 2 || y < ballRadius / 2) {
        yspeed = yspeed * -1
        tweenOnBounce(ball)
    }
    ball.position = [x, y]
}

function tweenOnBounce(path) {
    var tween = path.tweenTo({
        scaling: 0.8,
        fillColor: 'red'
    }, 20)
    tween.then(function() {
        path.tweenTo({
            scaling: 1,
            fillColor: 'blue'
        }, 200)
    })
}