main.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. document.body.addEventListener('touchmove', bodyScroll, { passive: false });
  2. function bodyScroll(event) {
  3. event.preventDefault();
  4. }
  5. var canvas = document.getElementById("canvas");
  6. var stage = new createjs.Stage(canvas);
  7. function init() {
  8. var comp = AdobeAn.getComposition("5529E26743974F469545223C44E61D22");
  9. var lib = comp.getLibrary();
  10. var loader = new createjs.LoadQueue(false);
  11. loader.addEventListener("fileload", function(evt) { handleFileLoad(evt, comp) });
  12. loader.addEventListener("complete", function(evt) { handleComplete(evt, comp) });
  13. loader.addEventListener("progress", loadProgressHandler);
  14. var lib = comp.getLibrary();
  15. loader.loadManifest(lib.properties.manifest);
  16. createjs.MotionGuidePlugin.install();
  17. createjs.Touch.enable(stage);
  18. }
  19. function handleFileLoad(evt, comp) {
  20. var loadingimages = comp.getImages();
  21. if (evt && (evt.item.type == "image")) {
  22. loadingimages[evt.item.id] = evt.result;
  23. }
  24. }
  25. function loadProgressHandler(event) {
  26. var num = Math.floor(event.progress * 100);
  27. $("#loadingTxt").text(num);
  28. $("#loadingBar").css("width", num + '%');
  29. }
  30. function handleComplete(evt, comp) {
  31. //This function is always called, irrespective of the content. You can use the variable "stage" after it is created in token create_stage.
  32. var lib = comp.getLibrary();
  33. var ss = comp.getSpriteSheet();
  34. var queue = evt.target;
  35. var ssMetadata = lib.ssMetadata;
  36. for (i = 0; i < ssMetadata.length; i++) {
  37. ss[ssMetadata[i].name] = new createjs.SpriteSheet({ "images": [queue.getResult(ssMetadata[i].name)], "frames": ssMetadata[i].frames })
  38. }
  39. var exportRoot = new lib.assets();
  40. $(".loading").fadeOut(function(){
  41. $(".select-dish-box").find(".animated").removeClass("js-an");
  42. });
  43. var dishIndex = 0;
  44. $(".select-dish-box .item").on("click", function() {
  45. $(this).addClass("active").siblings().removeClass("active");
  46. $(".select-dish-box .confirm").show();
  47. })
  48. $(".select-dish-box .confirm").on("click", function() {
  49. if (!$(".select-dish-box .item").hasClass("active")) {
  50. alert("请选择你要做的菜")
  51. return false;
  52. }
  53. dishIndex = $(".select-dish-box .item.active").index();
  54. $(".select-dish-box").hide();
  55. $(".game-box").show();
  56. // 判断是哪个菜
  57. if(dishIndex == 0){
  58. exportRoot.plate.hsr_icon1.alpha = 1;
  59. exportRoot.plate.hsr_icon2.alpha = 1;
  60. exportRoot.plate.hsr_icon3.alpha = 1;
  61. exportRoot.plate.hsr_icon4.alpha = 1;
  62. cutDishArr = hsr_iconArr;
  63. }else if(dishIndex == 1){
  64. exportRoot.plate.qcdf_icon1.alpha = 1;
  65. exportRoot.plate.qcdf_icon2.alpha = 1;
  66. exportRoot.plate.qcdf_icon3.alpha = 1;
  67. exportRoot.plate.qcdf_icon4.alpha = 1;
  68. exportRoot.plate.qcdf_icon5.alpha = 1;
  69. cutDishArr = qcdf_iconArr;
  70. }else if(dishIndex == 2){
  71. exportRoot.plate.swy_icon1.alpha = 1;
  72. exportRoot.plate.swy_icon2.alpha = 1;
  73. cutDishArr = swy_iconArr;
  74. }else{
  75. exportRoot.plate.ymc_icon1.alpha = 1;
  76. exportRoot.plate.ymc_icon2.alpha = 1;
  77. exportRoot.plate.ymc_icon3.alpha = 1;
  78. exportRoot.plate.ymc_icon4.alpha = 1;
  79. cutDishArr = ymc_iconArr;
  80. }
  81. })
  82. $(".replay-btn").on("click",function(){
  83. location.reload()
  84. })
  85. $(".secret-btn").on("click",function(){
  86. $(".secret-box").fadeIn();
  87. })
  88. $(".secret-box").on("click",function(){
  89. $(".secret-box").fadeOut();
  90. })
  91. function ballAnim(dom) {
  92. var anim = new TimelineMax({ repeat: -1 });
  93. anim.to(dom, 0.15, { rotation: "-=15", ease: Power0.easeIn })
  94. .to(dom, 0.3, { rotation: "+=30", ease: Power0.easeIn })
  95. .to(dom, 0.3, { rotation: "-=30", ease: Power0.easeIn })
  96. .to(dom, 0.3, { rotation: "+=30", ease: Power0.easeIn })
  97. .to(dom, 0.15, { rotation: "-=15", ease: Power0.easeIn });
  98. return anim;
  99. }
  100. function materialAnim(dom, x, y, callback) {
  101. var anim = new TimelineMax({ onComplete: callback });
  102. anim.to(dom, 0.5, { x: "+=" + x, y: "+=" + y, ease: Power0.easeIn })
  103. .to(dom, 0.3, { rotation: "-=25", ease: Power0.easeIn })
  104. .to(dom, 0.3, { rotation: "+=25", ease: Power0.easeIn })
  105. .to(dom, 0.3, { rotation: "-=25", ease: Power0.easeIn })
  106. .to(dom, 0.3, { rotation: "+=25", ease: Power0.easeIn });
  107. return anim;
  108. }
  109. function oilAnim(){
  110. if(dishIndex != 2){
  111. var dom = exportRoot.pot.oil;
  112. }else{
  113. var dom = exportRoot.bowl.oil;
  114. }
  115. var anim = new TimelineMax({ delay:0.8});
  116. anim.to(dom, 0.4, { alpha: 1, ease: Power0.easeIn })
  117. .to(dom, 1, { scale: 1, ease: Power0.easeIn })
  118. return anim;
  119. }
  120. function bowlAnim(callback) {
  121. var anim = new TimelineMax({ onComplete: callback, delay: 0.3 });
  122. anim.to(exportRoot.plate, 0.5, { y: "-=370", ease: Power0.easeIn })
  123. .to(exportRoot.bowl, 0.5, { alpha: 1, ease: Power0.easeIn });
  124. return anim;
  125. }
  126. function proAnim(callback) {
  127. var anim = new TimelineMax({ onComplete: callback, delay: 0.5 });
  128. anim.to(exportRoot.pro.proImg, 0.5, { rotation: "-=25", ease: Power0.easeIn })
  129. .to(exportRoot.pro.oil_icon1, 0.3, {alpha:1})
  130. .to(exportRoot.pro.oil_icon2, 0.2, {alpha:1})
  131. .to(exportRoot.pro.proImg, 1, {})
  132. .to(exportRoot.pro, 0.3, { alpha: 0, ease: Power0.easeIn })
  133. .to(exportRoot.pro, 1, {})
  134. return anim;
  135. }
  136. function oilIconAnim() {
  137. oilAnim();
  138. var anim = new TimelineMax({ repeat:10,delay:1});
  139. anim.to(exportRoot.pro.oil_icon2, 0.3, {y:"+=100",alpha:0})
  140. return anim;
  141. }
  142. function btnAnim(dom){
  143. var anim = new TimelineMax({ repeat:-1 });
  144. anim.to(dom, 0.3, { scale: "+=0.1", ease: Power0.easeIn })
  145. .to(dom, 0.3, { scale: "-=0.1", ease: Power0.easeIn })
  146. return anim;
  147. }
  148. function fadeInAnim(dom){
  149. var anim = new TimelineMax({ });
  150. anim.to(dom, 0.4, { alpha: 1, ease: Power0.easeIn })
  151. return anim;
  152. }
  153. function materialEnterAnim(dom,delay){
  154. dom.scale = 0.1;
  155. delay = delay || 0.1;
  156. var anim = new TimelineMax({ delay:delay});
  157. anim.to(dom,1.2, { scale: 1, ease: Elastic.easeOut.config(1, 0.3) })
  158. return anim;
  159. }
  160. var cutDishArr = [];
  161. // 油麦菜
  162. var ymc_iconArr = [
  163. {
  164. x: exportRoot.plate.x + exportRoot.plate.ymc_icon1.x,
  165. y: exportRoot.plate.y + exportRoot.plate.ymc_icon1.y,
  166. width: 266,
  167. height: 187,
  168. isClear: false
  169. },
  170. {
  171. x: exportRoot.plate.x + exportRoot.plate.ymc_icon2.x,
  172. y: exportRoot.plate.y + exportRoot.plate.ymc_icon2.y,
  173. width: 103,
  174. height: 118,
  175. isClear: false
  176. },
  177. {
  178. x: exportRoot.plate.x + exportRoot.plate.ymc_icon3.x,
  179. y: exportRoot.plate.y + exportRoot.plate.ymc_icon3.y,
  180. width: 371,
  181. height: 144,
  182. isClear: false
  183. },
  184. {
  185. x: exportRoot.plate.x + exportRoot.plate.ymc_icon4.x,
  186. y: exportRoot.plate.y + exportRoot.plate.ymc_icon4.y,
  187. width: 86,
  188. height: 75,
  189. isClear: false
  190. }
  191. ]
  192. // 三文鱼
  193. var swy_iconArr = [
  194. {
  195. x: exportRoot.plate.x + exportRoot.plate.hsr_icon1.x,
  196. y: exportRoot.plate.y + exportRoot.plate.hsr_icon1.y,
  197. width: 262,
  198. height: 195,
  199. isClear: false
  200. }
  201. ]
  202. // 红烧肉的位置
  203. var hsr_iconArr = [{
  204. x: exportRoot.plate.x + exportRoot.plate.hsr_icon1.x,
  205. y: exportRoot.plate.y + exportRoot.plate.hsr_icon1.y,
  206. width: 202,
  207. height: 129,
  208. isClear: false
  209. },
  210. {
  211. x: exportRoot.plate.x + exportRoot.plate.hsr_icon2.x,
  212. y: exportRoot.plate.y + exportRoot.plate.hsr_icon2.y,
  213. width: 86,
  214. height: 75,
  215. isClear: false
  216. },
  217. {
  218. x: exportRoot.plate.x + exportRoot.plate.hsr_icon3.x,
  219. y: exportRoot.plate.y + exportRoot.plate.hsr_icon3.y,
  220. width: 371,
  221. height: 144,
  222. isClear: false
  223. },
  224. {
  225. x: exportRoot.plate.x + exportRoot.plate.hsr_icon4.x,
  226. y: exportRoot.plate.y + exportRoot.plate.hsr_icon4.y,
  227. width: 103,
  228. height: 118,
  229. isClear: false
  230. }
  231. ];
  232. // 青菜豆腐的位置
  233. var qcdf_iconArr = [{
  234. x: exportRoot.plate.x + exportRoot.plate.qcdf_icon1.x,
  235. y: exportRoot.plate.y + exportRoot.plate.qcdf_icon1.y,
  236. width: 169,
  237. height: 320,
  238. isClear: false
  239. },
  240. {
  241. x: exportRoot.plate.x + exportRoot.plate.qcdf_icon2.x,
  242. y: exportRoot.plate.y + exportRoot.plate.qcdf_icon2.y,
  243. width: 207,
  244. height: 144,
  245. isClear: false
  246. },
  247. {
  248. x: exportRoot.plate.x + exportRoot.plate.qcdf_icon3.x,
  249. y: exportRoot.plate.y + exportRoot.plate.qcdf_icon3.y,
  250. width: 103,
  251. height: 118,
  252. isClear: false
  253. },
  254. {
  255. x: exportRoot.plate.x + exportRoot.plate.qcdf_icon4.x,
  256. y: exportRoot.plate.y + exportRoot.plate.qcdf_icon4.y,
  257. width: 371,
  258. height: 144,
  259. isClear: false
  260. },
  261. {
  262. x: exportRoot.plate.x + exportRoot.plate.qcdf_icon5.x,
  263. y: exportRoot.plate.y + exportRoot.plate.qcdf_icon5.y,
  264. width: 86,
  265. height: 75,
  266. isClear: false
  267. }
  268. ]
  269. // 画线
  270. var lineArr = [];
  271. var lineTimr = null;
  272. var lineShape = new createjs.Shape();
  273. function drawLine() {
  274. var value = 40;
  275. lineTimr = setInterval(function() {
  276. lineShape.graphics.clear();
  277. lineShape.graphics.setStrokeStyle(8).beginStroke("#787878");
  278. if (lineArr.length) {
  279. lineShape.graphics.moveTo(lineArr[0].x, lineArr[0].y);
  280. }
  281. for (var i = 0; i < lineArr.length; i++) {
  282. var x = lineArr[i].x;
  283. var y = lineArr[i].y;
  284. lineShape.graphics.lineTo(x, y);
  285. for (var p = 0; p < cutDishArr.length; p++) {
  286. if (x-cutDishArr[p].width/3 > cutDishArr[p].x && x < (cutDishArr[p].x + cutDishArr[p].width)) {
  287. if (y-cutDishArr[p].height/3 > cutDishArr[p].y && y < (cutDishArr[p].y + cutDishArr[p].height)) {
  288. if (cutDishArr[p].isClear == false) {
  289. cutDishArr[p].isClear = true;
  290. if(dishIndex == 0){
  291. exportRoot.plate['hsr_icon' + (p + 1)].gotoAndStop(1);
  292. }else if(dishIndex == 1){
  293. exportRoot.plate['qcdf_icon' + (p + 1)].gotoAndStop(1);
  294. }else if(dishIndex == 2){
  295. exportRoot.plate.swy_icon1.gotoAndStop(1);
  296. }else{
  297. exportRoot.plate['ymc_icon' + (p + 1)].gotoAndStop(1);
  298. }
  299. }
  300. }
  301. }
  302. }
  303. }
  304. lineArr.splice(0, 1);
  305. }, 30)
  306. }
  307. // 画线事件
  308. drawLine();
  309. exportRoot.addEventListener("mousedown", function(event) {
  310. lineArr = [];
  311. var obj = {
  312. x: event.stageX,
  313. y: event.stageY,
  314. };
  315. lineArr.push(obj);
  316. })
  317. exportRoot.addEventListener("pressmove", function(event) {
  318. if (lineArr.length >= 15) {
  319. lineArr.splice(0, 1);
  320. }
  321. var obj = {
  322. x: event.stageX,
  323. y: event.stageY,
  324. };
  325. lineArr.push(obj);
  326. })
  327. exportRoot.addEventListener("pressup", function(event) {
  328. playbowlAnim();
  329. })
  330. var dishMoveIndex = 0;
  331. // 移动菜品
  332. function dishMove(dom,x,y) {
  333. var startX = 0;
  334. var startY = 0;
  335. var domFirst = $.extend(true, {}, dom);
  336. dom.addEventListener("mousedown", function(event) {
  337. startX = event.stageX;
  338. startY = event.stageY;
  339. })
  340. dom.addEventListener("pressmove", function(event) {
  341. dom.x -= (startX - event.stageX);
  342. dom.y -= (startY - event.stageY);
  343. startX = event.stageX;
  344. startY = event.stageY;
  345. })
  346. dom.addEventListener("pressup", function(event) {
  347. if (dom.x + exportRoot.plate.x > 80 && dom.y + exportRoot.plate.y > 518) {
  348. dom.x = 190 - exportRoot.plate.x + x;
  349. dom.y = 630 - exportRoot.plate.y + y;
  350. dom.removeAllEventListeners();
  351. if(dishIndex == 0){
  352. ++dishMoveIndex;
  353. if (dishMoveIndex >= 4) {
  354. exportRoot.btn1.alpha = 1;
  355. btnAnim(exportRoot.btn1);
  356. isAllInplate();
  357. }
  358. }else if(dishIndex == 1){
  359. ++dishMoveIndex;
  360. if (dishMoveIndex >= 5) {
  361. exportRoot.btn1.alpha = 1;
  362. btnAnim(exportRoot.btn1);
  363. isAllInplate();
  364. }
  365. }else if(dishIndex == 2){
  366. exportRoot.btn1.alpha = 1;
  367. btnAnim(exportRoot.btn1);
  368. isAllInplate();
  369. }else{
  370. ++dishMoveIndex;
  371. if (dishMoveIndex >= 4) {
  372. exportRoot.btn1.alpha = 1;
  373. btnAnim(exportRoot.btn1);
  374. isAllInplate();
  375. }
  376. }
  377. } else {
  378. dom.x = domFirst.x;
  379. dom.y = domFirst.y;
  380. }
  381. })
  382. }
  383. // 菜全部到盘子里
  384. function isAllInplate() {
  385. exportRoot.btn1.addEventListener("mousedown", function() {
  386. exportRoot.btn1.removeAllEventListeners();
  387. exportRoot.btn1.alpha = 0;
  388. exportRoot.pro.alpha = 1;
  389. if(dishIndex != 2){
  390. exportRoot.bowl.alpha = 0;
  391. exportRoot.plate.alpha = 0;
  392. exportRoot.pot.alpha = 1;
  393. }else{
  394. exportRoot.plate.instance.alpha = 0;
  395. exportRoot.plate.swy_icon2.alpha = 0;
  396. }
  397. oilIconAnim();
  398. proAnim(function() {
  399. exportRoot.btn2.alpha = 1;
  400. btnAnim(exportRoot.btn2);
  401. if(dishIndex == 0){
  402. fadeInAnim(exportRoot.pot.hsr);
  403. }else if(dishIndex == 1){
  404. fadeInAnim(exportRoot.pot.qcdf);
  405. }else if(dishIndex == 3){
  406. fadeInAnim(exportRoot.pot.ymc);
  407. }
  408. });
  409. })
  410. exportRoot.btn2.addEventListener("mousedown", function() {
  411. exportRoot.btn2.removeAllEventListeners();
  412. exportRoot.btn2.alpha = 0;
  413. fadeInAnim(exportRoot.blackboard);
  414. materialEnterAnim(exportRoot.blackboard.material1,0.2);
  415. materialEnterAnim(exportRoot.blackboard.material2,0.4);
  416. materialEnterAnim(exportRoot.blackboard.material3,0.6);
  417. exportRoot.ball1.alpha = 1;
  418. ballAnim(exportRoot.ball1);
  419. })
  420. exportRoot.blackboard.material1.addEventListener("mousedown", function() {
  421. exportRoot.blackboard.material1.removeAllEventListeners();
  422. exportRoot.blackboard.material2.removeAllEventListeners();
  423. exportRoot.blackboard.material3.removeAllEventListeners();
  424. $(".poster-mode").addClass("poster-bg3");
  425. materialAnim(exportRoot.blackboard.material1, 300, 180, function() {
  426. exportRoot.blackboard.material1.x -=300;
  427. exportRoot.blackboard.material1.y -=180;
  428. $(".shake-mode").show();
  429. shakeInit();
  430. });
  431. })
  432. exportRoot.blackboard.material2.addEventListener("mousedown", function() {
  433. exportRoot.blackboard.material1.removeAllEventListeners();
  434. exportRoot.blackboard.material2.removeAllEventListeners();
  435. exportRoot.blackboard.material3.removeAllEventListeners();
  436. $(".poster-mode").addClass("poster-bg2");
  437. materialAnim(exportRoot.blackboard.material2, 120, 180, function() {
  438. exportRoot.blackboard.material2.x -=120;
  439. exportRoot.blackboard.material2.y -=180;
  440. $(".shake-mode").show();
  441. shakeInit();
  442. });
  443. })
  444. exportRoot.blackboard.material3.addEventListener("mousedown", function() {
  445. exportRoot.blackboard.material1.removeAllEventListeners();
  446. exportRoot.blackboard.material2.removeAllEventListeners();
  447. exportRoot.blackboard.material3.removeAllEventListeners();
  448. $(".poster-mode").addClass("poster-bg1");
  449. materialAnim(exportRoot.blackboard.material3, -30, 180, function() {
  450. exportRoot.blackboard.material3.x +=30;
  451. exportRoot.blackboard.material3.y -=180;
  452. $(".shake-mode").show();
  453. shakeInit();
  454. });
  455. })
  456. }
  457. // 菜切完盘子上移动
  458. function playbowlAnim() {
  459. for (var i = 0; i < cutDishArr.length; i++) {
  460. if (cutDishArr[i].isClear == false) {
  461. return false;
  462. }
  463. }
  464. stage.removeChild(lineShape);
  465. clearInterval(lineTimr);
  466. exportRoot.removeAllEventListeners();
  467. bowlAnim(function() {
  468. if(dishIndex == 0){
  469. dishMove(exportRoot.plate.hsr_icon1,0,0);
  470. dishMove(exportRoot.plate.hsr_icon2,0,0);
  471. dishMove(exportRoot.plate.hsr_icon3,0,0);
  472. dishMove(exportRoot.plate.hsr_icon4,0,0);
  473. }else if(dishIndex == 1){
  474. dishMove(exportRoot.plate.qcdf_icon1,0,-80);
  475. dishMove(exportRoot.plate.qcdf_icon2,0,0);
  476. dishMove(exportRoot.plate.qcdf_icon3,0,0);
  477. dishMove(exportRoot.plate.qcdf_icon4,0,0);
  478. dishMove(exportRoot.plate.qcdf_icon5,0,0);
  479. }else if(dishIndex == 2){
  480. dishMove(exportRoot.plate.swy_icon1,120,100);
  481. }else{
  482. dishMove(exportRoot.plate.ymc_icon1,0,0);
  483. dishMove(exportRoot.plate.ymc_icon2,0,0);
  484. dishMove(exportRoot.plate.ymc_icon3,0,0);
  485. dishMove(exportRoot.plate.ymc_icon4,0,0);
  486. }
  487. });
  488. }
  489. function shakeInit() {
  490. if (window.DeviceMotionEvent) {
  491. window.addEventListener('devicemotion', deviceMotionHandler, false);
  492. } else {
  493. alert('您的手机不支持摇一摇功能!');
  494. }
  495. setTimeout(function(){
  496. window.removeEventListener('devicemotion', deviceMotionHandler, false);
  497. exportRoot.pot.hsr.play();
  498. exportRoot.btn3.alpha = 1;
  499. btnAnim(exportRoot.btn3);
  500. exportRoot.btn3.addEventListener("mousedown",function(){
  501. exportRoot.btn3.removeAllEventListeners();
  502. $(".poster-mode").fadeIn();
  503. })
  504. if(dishIndex == 0){
  505. exportRoot.pot.hsr.gotoAndStop(1);
  506. }else if(dishIndex == 1){
  507. exportRoot.pot.qcdf.gotoAndStop(1);
  508. }else if(dishIndex == 2){
  509. exportRoot.bowl.oil.alpha = 0;
  510. exportRoot.plate['swy_icon1'].gotoAndStop(2);
  511. }else{
  512. exportRoot.pot.ymc.gotoAndStop(1);
  513. }
  514. $(".shake-mode").fadeOut();
  515. },3000)
  516. //获取加速度信息
  517. //通过监听上一步获取到的x, y, z 值在一定时间范围内的变化率,进行设备是否有进行晃动的判断。
  518. //而为了防止正常移动的误判,需要给该变化率设置一个合适的临界值。
  519. var SHAKE_THRESHOLD = 8000; //阈值越大,触发摇晃事件时手机摇晃的程度越剧烈
  520. var last_update = 0;
  521. var x, y, z, last_x = 0,
  522. last_y = 0,
  523. last_z = 0;
  524. function deviceMotionHandler(eventData) {
  525. var acceleration = eventData.accelerationIncludingGravity;
  526. var curTime = new Date().getTime();
  527. if ((curTime - last_update) > 10) {
  528. var diffTime = curTime - last_update;
  529. last_update = curTime;
  530. x = acceleration.x;
  531. y = acceleration.y;
  532. z = acceleration.z;
  533. var speed = Math.abs(x + y + z - last_x - last_y - last_z) / diffTime * 10000;
  534. if (speed > SHAKE_THRESHOLD) {
  535. //dosomething
  536. $("#fire").addClass("anim-time");
  537. }else{
  538. $("#fire").removeClass("anim-time");
  539. }
  540. last_x = x;
  541. last_y = y;
  542. last_z = z;
  543. }
  544. }
  545. }
  546. //Registers the "tick" event listener.
  547. function fnStartAnimation() {
  548. stage.addChild(exportRoot, lineShape);
  549. createjs.Ticker.timingMode = createjs.Ticker.RAF_SYNCHED;
  550. createjs.Ticker.addEventListener("tick", stage);
  551. }
  552. fnStartAnimation();
  553. stageBreakHandler();
  554. }
  555. // 自适应
  556. var stageWidth, stageHeight, stageScale;
  557. function stageBreakHandler(event) {
  558. if (stageWidth != $(".warp").width() || stageHeight != $(".warp").height()) {
  559. stageWidth = $(".warp").width();
  560. stageHeight = $(".warp").height();
  561. stageScale = stageWidth / 640;
  562. canvas.style.width = 640 * stageScale + 'px';
  563. canvas.style.height = 1040 * stageScale + 'px';
  564. }
  565. }