;(function($){var PLUGIN_NAME = "kzwMouseChaser"; var ImplementationClass = function(target, config) { var settings = $.extend({}, $[PLUGIN_NAME].defaults, config); var myself = this; $(target).data(PLUGIN_NAME, myself); var ctx, circleX = 0, circleY = 0, mouseX = 0, mouseY = 0, timer = null, chaser = new Image(); function _constructor_() { if(!target.getContext) { return; } ctx = target.getContext('2d');circleX = settings.initialX;circleY = settings.initialY;chaser.src = settings.src; if(chaser.complete) { startHover() } else { chaser.onload = function(){ startHover(); }; } } function startHover() {$(target).parent().hover(hover, mouseOut);} $.extend( myself, { havingnopoint : "" }); _constructor_(); function hover(event) { onMouseMove(event); circleX = mouseX; circleY = mouseY; timer = setInterval(chase, settings.frameDuration); $(target).parent().on('mousemove', onMouseMove); } function mouseOut(event) { $(target).parent().off('mousemove', onMouseMove); if(timer) { clearInterval(timer); } timer = null; ctx.clearRect(0, 0, target.width, target.height); } function onMouseMove(event) {var offset = $(target).offset();mouseX = event.pageX - Math.round(offset.left);mouseY = event.pageY - Math.round(offset.top);} function chase() { circleX += (mouseX - circleX) * settings.speed; circleY += (mouseY - circleY) * settings.speed; ctx.clearRect(0, 0, target.width, target.height); ctx.save(); ctx.translate(circleX, circleY); ctx.rotate(Math.atan2(mouseY - circleY, mouseX - circleX) + 90 * Math.PI / 180); ctx.translate(-1*(circleX), -1*(circleY)); ctx.drawImage(chaser, circleX - chaser.width/2, circleY - chaser.height/2); ctx.restore(); } }; $[PLUGIN_NAME] = {defaults : {speed: 0.05, initialX: 0, initialY: 0, frameDuration: 25, src: null} }; $.fn[PLUGIN_NAME] = function(config){return this.each(function(i){new ImplementationClass(this, config);});}; })(jQuery);