Skip to content

Instantly share code, notes, and snippets.

@hitode909
Created August 17, 2012 07:06
Show Gist options
  • Select an option

  • Save hitode909/3376638 to your computer and use it in GitHub Desktop.

Select an option

Save hitode909/3376638 to your computer and use it in GitHub Desktop.
★がマウスを追い掛けるやつ
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script src="stalker.js"></script>
</head>
<body>
</body>
</html>
$(function() {
var requestAnimationFrame = (function() {
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function(callback, element) {
return window.setTimeout(function() {
return callback();
}, 1000 / 60);
};
})();
var queue = [];
var step = function() {
queue.forEach(function(f) {
f();
});
requestAnimationFrame(step);
};
requestAnimationFrame(step);
var target = {
x: 0,
y: 0
};
var timer;
$(document).on('mousemove', function(event) {
if (timer) return;
timer = setTimeout(function() {
timer = null;
}, 100);
target.x = event.pageX;
target.y = event.pageY;
});
function new_stalker() {
var $star = $('<div>')
.text('★')
.css({
position: 'absolute',
color: "hsl(" + (Math.random()*360) + ", 100% ,50%)"
})
.appendTo('body');
var current = {
x: Math.random() * 1000,
y: Math.random() * 1000
};
var vector = {
x: 0,
y: 0
};
var speed = 2.0;
var friction = 0.99;
var step = function() {
var tan = (target.y - current.y) / (target.x - current.x);
var theta = Math.atan(tan);
if (target.x - current.x < 0) theta += Math.PI;
vector.x += Math.cos(theta) * speed;
vector.y += Math.sin(theta) * speed;
vector.x *= friction;
vector.y *= friction;
current.x += vector.x;
current.y += vector.y;
$star.css({
left: Math.floor(current.x) + 'px',
top: Math.floor(current.y) + 'px'
});
};
queue.push(step);
};
var t;
$(document).on('mousedown', function () {
t = setInterval(new_stalker, 10);
});
$(document).on('mouseup', function () {
clearInterval(t);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment