123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- var sceneReadyBehavior = require('../../behavior-scene/scene-ready');
- let currentTouchX = 0;
- let currentTouchY = 0;
- let rotateIdx = -1;
- let moveIdx = -1;
- let centerX = 50;
- let centerY = 50;
- let hWidth = 40;
- let hHeight = 40;
- let radius = 50;
- Page({
- behaviors: [sceneReadyBehavior],
- data: {
- height: 600,
- heightScale: 0.8,
-
- reset: 0,
-
- hTop: 30,
- hLeft: 30,
- transferData:{
-
- biasX: 0,
- biasY: 0,
-
- initRotX: 0,
- initRotY: 0,
-
- biasRotX: 0,
- biasRotY: 0,
- }
- },
- reset: function () {
- this.setData({
- reset: this.data.reset + 1
- })
- },
- touchStart(e) {},
- touchMove: function (e) {
-
- if (rotateIdx == -1) {
- moveIdx = 0;
- } else {
-
- moveIdx = ~rotateIdx & 0x01;
- }
- var touchX = e.touches[moveIdx].clientX - 30;
- var touchY = e.touches[moveIdx].clientY - (this.data.height - 100);
- var posInfo = this.limitPosition(touchX, touchY);
- this.setData({
- hLeft: centerX - hWidth / 2 + posInfo.posX,
- hTop: centerY - hHeight / 2 + posInfo.posY,
- transferData:{
- biasX: posInfo.posX,
- biasY: posInfo.posY,
- }
- })
- },
- touchEnd: function (e) {
- moveIdx = -1;
-
- if (rotateIdx == 1)
- rotateIdx = 0;
- this.setData({
- hLeft: centerX - hWidth / 2,
- hTop: centerY - hHeight / 2,
- transferData:{
- biasX: 0,
- biasY: 0,
- }
- });
- },
- touchRotateStart: function (e) {
- if (moveIdx == -1) {
- rotateIdx = 0;
- } else {
- rotateIdx = ~moveIdx & 0x01;
- }
- currentTouchX = e.touches[rotateIdx].clientX;
- currentTouchY = e.touches[rotateIdx].clientY;
- this.setData({
- transferData:{
- initRotX: currentTouchX,
- initRotY: currentTouchY,
- }
- })
- },
- touchRotateMove: function (e) {
- if (moveIdx == -1) {
- rotateIdx = 0;
- } else {
- rotateIdx = ~moveIdx & 0x01;
- }
- currentTouchX = e.touches[rotateIdx].clientX;
- currentTouchY = e.touches[rotateIdx].clientY;
- this.setData({
- transferData:{
- biasRotX: currentTouchX,
- biasRotY: currentTouchY,
- }
- })
- },
- touchRotateEnd: function (e) {
- rotateIdx = -1;
-
- if (moveIdx == 1)
- moveIdx = 0;
- this.setData({
- transferData:{
- biasRotX: 0,
- biasRotY: 0,
- }
- })
- },
-
- limitPosition: function (touchX, touchY) {
- var x = touchX - centerX;
- var y = touchY - centerY;
- var z = Math.sqrt(x * x + y * y);
-
- if (z <= radius) {
- x = Math.round(x);
- y = Math.round(y);
- return {
- posX: x,
- posY: y
- };
- } else {
-
- var ratio = radius / z;
- x = x * ratio;
- y = y * ratio;
- x = Math.round(x);
- y = Math.round(y);
- return {
- posX: x,
- posY: y
- };
- }
- }
- });
|