// Load this file in the DisplayScript editor to view it. state x = @width / 2 state y = @height / 2 state vx = 0 state vy = 0 var radius = 25 state mousePos = #none draw at(x - radius, y - radius, 2 * radius, 2 * radius, ellipse(black)) draw dragHandler(function (x, y) { next mousePos = #position{ x: x, y: y } }, function (dx, dy) { switch mousePos { case #position(pt): next mousePos = #position{ x: pt.x + dx, y: pt.y + dy } } }, function () { next mousePos = #none }) if abs(vx) > 0 || abs(vy) > 0 || mousePos != #none { tick(function (dt) { next x = x + vx * dt next y = y + vy * dt next vx = vx next vy = vy if next x + radius > @width { next vx = -vx next x = @width - radius } if next x - radius < 0 { next vx = -vx next x = radius } if next y + radius > @height { next vy = -vy next y = @height - radius } if next y - radius < 0 { next vy = -vy next y = radius } var c = 1.0 switch mousePos { case #position(pt): var k = 800 c = 25 next vx = next vx - k * (next x - pt.x) * dt next vy = next vy - k * (next y - pt.y) * dt } next vx = next vx - c * dt * next vx next vy = next vy - c * dt * next vy if abs(next vx) < 1 { next vx = 0 } if abs(next vy) < 1 { next vy = 0 } }) }