Skip to content

Instantly share code, notes, and snippets.

@heolin
Created November 3, 2017 14:10
Show Gist options
  • Select an option

  • Save heolin/037ba154ad249add67da700cfef3d710 to your computer and use it in GitHub Desktop.

Select an option

Save heolin/037ba154ad249add67da700cfef3d710 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="js/paper.js"></script>
<script type="text/javascript">
//plane flying behind the mouse pointer
paper.install(window);
function lerp(v0, v1, t) {
return (1 - t) * v0 + t * v1;
}
function atan2(x1, y1, x2, y2) {
var m = (y2-y1) / (x2-x1);
var angleDeg = Math.floor(Math.atan2(y2-y1, x2 -x1) * 180 / Math.PI);
return angleDeg;
}
window.onload = function() {
var tool = new Tool();
var position = new Point();
var target = new Point();
var canvas = document.getElementById('myCanvas');
var rotation = 270;
paper.setup(canvas);
position.x = canvas.width / 2;
position.y = canvas.height / 2;
target = position;
var path = new Raster('plane');
path.strokeColor = 'black';
view.onFrame = function(event) {
var oldX = path.position.x;
var oldY = path.position.y;
path.position.x = lerp(path.position.x, target.x, event.delta * 5);
path.position.y = lerp(path.position.y, target.y, event.delta * 5);
lastRot = rotation;
var newRot = atan2(path.position.x, path.position.y, target.x, target.y);
console.log(lastRot + " "+newRot);
angle = newRot - lastRot;
rotation += angle;
path.rotate(angle);
}
tool.onMouseDown = function(event) {
target = event.point;
}
tool.onMouseDrag = function(event) {
target = event.point;
}
}
</script>
</head>
<body>
<canvas id="myCanvas" resize></canvas>
<img id="plane" src="images/plane.png" style="display:none;"/>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment