- <script src="easeljs/utils/UID.js"></script>
- <script src="easeljs/events/MouseEvent.js"></script>
- <script src="easeljs/geom/Point.js"></script>
- <script src="easeljs/geom/Matrix2D.js"></script>
- <script src="easeljs/display/DisplayObject.js"></script>
- <script src="easeljs/display/Container.js"></script>
- <script src="easeljs/display/Stage.js"></script>
- <script src="easeljs/display/Bitmap.js"></script>
- <script src="easeljs/utils/Ticker.js"></script>
- <script src="preloadjs/AbstractLoader.js"></script>
- <script src="preloadjs/PreloadJS.js"></script>
- <script src="preloadjs/TagLoader.js"></script>
- <script src="preloadjs/XHRLoader.js"></script>
- <script type="text/javascript">
- var stage;
- var origin;
- var lastMouse;
- var DECELERATION = 0.8;
- var RATIO = 0.01;
- var angularVelocity = 0;
- var instance;
- var velocity = new Point(0, 0);
- var update;
- function xInitialize() {
- var canvasObject = document.getElementById("myCanvas");
- var position = new Point(canvasObject.width / 2, canvasObject.height / 2);
- var file = {src:"images/card.png", id:"image", data:position}
- var loader = new PreloadJS(false);
- stage = new Stage(canvasObject);
- loader.onFileLoad = xDraw;
- loader.loadFile(file);
- }
- function xDraw(eventObject) {
- var myImage = eventObject.result;
- var position = eventObject.data
- var myBitmap = new Bitmap(myImage);
- myBitmap.x = position.x;
- myBitmap.y = position.y;
- myBitmap.regX = myImage.width / 2;
- myBitmap.regY = myImage.height / 2;
- myBitmap.onPress = clickHandler;
- stage.addChild(myBitmap);
- stage.update();
- }
- function clickHandler(eventObject) {
- var mouseX = eventObject.stageX;
- var mouseY = eventObject.stageY;
- lastMouse = new Point(mouseX, mouseY);
- origin = this.globalToLocal(mouseX, mouseY);
- eventObject.onMouseMove = mouseMoveHandler;
- eventObject.onMouseUp = mouseUpHandler;
- instance = this;
- update = updateDrag;
- Ticker.addListener(window);
- }
- function mouseMoveHandler(eventObject) {
- var mouseX = eventObject.stageX;
- var mouseY = eventObject.stageY;
- var radius = new Point(lastMouse.x - instance.x, lastMouse.y - instance.y);
- var force = new Point(mouseX - lastMouse.x, mouseY - lastMouse.y);
- var moment = crossProduct2D(radius, force);
- angularVelocity += moment * RATIO;
- lastMouse = new Point(mouseX, mouseY);
- velocity = force;
- }
- function mouseUpHandler(eventObject) {
- update = updateInertia;
- this.onMouseMove = this.onMouseUp = null;
- }
- function tick() {
- rotate();
- stage.update();
- }
- function rotate() {
- var mouseX = lastMouse.x;
- var mouseY = lastMouse.y;
- instance.rotation += angularVelocity;
- var offsetPoint = instance.localToGlobal(origin.x, origin.y);
- instance.x += mouseX - offsetPoint.x;
- instance.y += mouseY - offsetPoint.y;
- angularVelocity *= DECELERATION;
- update(mouseX, mouseY);
- }
- function updateDrag(mouseX, mouseY) {
- angularVelocity *= DECELERATION;
- }
- function updateInertia(mouseX, mouseY) {
- var velocityX = velocity.x;
- var velocityY = velocity.y;
- if (Math.abs(velocityX) + Math.abs(velocityY) + Math.abs(angularVelocity) < 1) {
- Ticker.removeListener(window);
- } else {
- lastMouse = new Point(mouseX + velocityX, mouseY + velocityY);
- velocity = new Point(velocityX * DECELERATION, velocityY * DECELERATION);
- angularVelocity *= DECELERATION;
- }
- }
- function crossProduct2D(point0, point1) {
- return point0.x * point1.y - point0.y * point1.x;
- }
- </script>
|