MotionTrackマウスから逃げる forked from: マウスから逃げる
http://wonderfl.net/c/7ZmRの魔法もどきパーティクル(MotionTrack Particle)
のMotionTrackクラスを使う練習
/**
* Copyright yabuchany ( http://wonderfl.net/user/yabuchany )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/rOJ7
*/
// forked from sakef's マウスから逃げる
/*
マウスから逃げます。
*/
// http://wonderfl.net/c/7ZmRの魔法もどきパーティクル(MotionTrack Particle)
// のMotionTrackクラスを使う練習
package
{
import flash.display.BlendMode;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.filters.BlurFilter;
// MotionTrack
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.media.Camera;
import flash.media.Video;
import flash.utils.Timer;
// MotionTrack
[SWF(width="465", height="465", backgroundColor="0x000000", frameRate="60")]
public class Main extends Sprite
{
//private static const num:int=500;
private static const num:int=500;
private var firstPointX:Array;
private var firstPointY:Array;
private var circles:Array;
// MotionTrack
private var _cam:Camera;
private var _tracker:Tracker;
private var _px:Number = 465 * .5
private var _py:Number = 465 * .5
private var _isMotion:Boolean;
// MotionTrack
public function Main()
{
firstPointX = [];
firstPointY = [];
circles = [];
for(var i:int=0; i < num; i++)
{
var circle:Shape=addChild(new Shape) as Shape;
circle.graphics.beginFill(Math.random() * 0xFFFFFF);
circle.graphics.drawCircle(0, 0, Math.random() * 18 + 5);
circle.graphics.endFill();
circle.blendMode=BlendMode.ADD;
circle.x=Math.round(Math.random() * stage.stageWidth);
circle.y=Math.round(Math.random() * stage.stageHeight);
circle.name="circle" + i.toString();
circle.filters=[new BlurFilter(10, 10, 1)];
circles[i] = circle;
firstPointX[i]=circle.x;
firstPointY[i]=circle.y;
}
initVideo();
//addEventListener(Event.ENTER_FRAME, onFrame);
}
public function onFrame(e:Event):void
{
for(var i:int=0; i < num; i++)
{
var circle:Shape= circles[i] as Shape;
//var theta:Number=Math.atan2(circle.y - mouseY, circle.x - mouseX);
var theta:Number=Math.atan2(circle.y - _tracker.py, circle.x - _tracker.px);
//var d:Number=1000 / Math.sqrt(Math.pow(mouseX - circle.x, 2) + Math.pow(mouseY - circle.y, 2));
var d:Number=1000 / Math.sqrt(Math.pow(_tracker.px - circle.x, 2) + Math.pow(_tracker.py - circle.y, 2));
circle.x+=d * Math.cos(theta) + (firstPointX[i] - circle.x) * 0.1;
circle.y+=d * Math.sin(theta) + (firstPointY[i] - circle.y) * 0.1;
}
}
private function initVideo():void {
var video:Video = new Video(465, 465);
_cam = Camera.getCamera();
_cam.setMode(video.width, video.height, 25);
video.attachCamera(_cam);
_tracker = new Tracker(video);
addEventListener(Event.ENTER_FRAME, update);
_tracker.addEventListener("isMotion", motion);
// Check Tracking
addChild(_tracker)
}
private function update(e:Event):void{
_tracker.track();
_px = _tracker.px;
_py = _tracker.py;
if(_px){
if(_isMotion){
trace("Motion");
trace(_px);
addEventListener(Event.ENTER_FRAME, onFrame);
}
}
}
private var _timer:Timer
private function motion(e:Event):void{
if(_timer) return;
_isMotion = true;
//
_timer = new Timer(1000, 1);
_timer.addEventListener(TimerEvent.TIMER_COMPLETE, timerComp);
_timer.start();
//
function timerComp():void{
_isMotion = false;
_timer = null;
//removeEventListener(Event.ENTER_FRAME, onFrame);
}
}
}
}
// MotionTrack
import flash.display.BitmapData;
import flash.display.BlendMode;
import flash.display.Sprite;
import flash.events.Event;
import flash.filters.BlurFilter;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.media.Video;
class Tracker extends Sprite {
public var _previous:BitmapData;
public var _current:BitmapData;
public var px:Number;
public var py:Number;
private var _blur:BlurFilter = new BlurFilter(64,64);
private var _vid:Video;
private var _mirror:Matrix;
private var _point:Point = new Point();
private var _area:Rectangle;
private var _isMotion:Boolean = false;
public function Tracker(vid:Video) {
_vid = vid;
_mirror = new Matrix();
_mirror.scale( -1, 1);
_mirror.translate(_vid.width, 0);
_current = new BitmapData(_vid.width,_vid.height,false,0x000000);
_previous = _current.clone();
}
public function track():void {
_current.draw(_vid, _mirror);
_current.draw(_previous, null, null, BlendMode.DIFFERENCE);
_current.applyFilter(_current, _current.rect, _point, _blur);
_current.threshold(_current, _current.rect, _point, ">", 0xFF333333, 0xFFFFFFFF);
_previous.draw(_vid, _mirror);
_area = _current.getColorBoundsRect(0xFFFFFFFF,0xFFFFFFFF,true);
_isMotion = ( _area.width > ( _vid.width / 100) * 10 || _area.height > (_vid.height / 100) * 10 );
if (_isMotion) {
px = _area.x + _area.width * .5;
py = _area.y + _area.height * .5;
graphics.clear();
graphics.lineStyle(1, 0xFF0000, .5);
graphics.drawRect(_area.x, _area.y, _area.width, _area.height);
dispatchEvent(new Event("isMotion"));
}
}
}