WXdraw.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. // 蒙版
  2. var canvas_bak = document.getElementById('box');
  3. var winHeight = window.screen.height - window.innerHeight;
  4. var vowidth = window.screen.width;
  5. var topwinHeightDraw = window.screen.height - window.innerHeight + 30; //计算title top 头部
  6. var numse = window.screen.height; // -winHeight
  7. //计算title top 头部
  8. if (numse <= 70) {
  9. var voheight = window.screen.height - winHeight - 34 - 20;
  10. } else {
  11. var voheight = window.screen.height - topwinHeightDraw - 20;
  12. }
  13. //画笔大小
  14. var resolving = 1; // 1: 竖屏;2:横屏;
  15. var url = window.location.href;
  16. url = url.split('/');
  17. var parameters = GetRequest();
  18. var videoWidth, videoHeight;
  19. var isControl = true; // 是否是观看模式
  20. changIsControl(true);
  21. var isAuth = parameters['authPhone']; // 是否是获取的云手机
  22. var wsss;
  23. var errorTime = 0;
  24. var first = true;
  25. function changIsControl(value) {
  26. isControl = value;
  27. // if(){}
  28. $('#open-set-phone-size-dialog-btn').attr('hidden', !value);
  29. }
  30. function throttle(fn, delay) {
  31. var flag = true;
  32. errorTime += delay;
  33. return () => {
  34. if (!flag) return;
  35. flag = false;
  36. timer = setTimeout(() => {
  37. fn();
  38. flag = true;
  39. }, delay);
  40. };
  41. }
  42. function doConnectDirectives() {
  43. videoWidth = Number(resolvingPower) ? Number(resolvingPower) : 720;
  44. videoHeight = videoWidth === 720 ? 1280 : 1920;
  45. wsss = new WebSocket(cUrl);
  46. wsss.binaryType = 'arraybuffer';
  47. wsss.onopen = function () {
  48. // 获取虚拟场景状态
  49. var pings = { type: 'getVsStatus' };
  50. wsss.send(JSON.stringify(pings));
  51. var bitRate = {
  52. data: {
  53. bitRate: 1243000,
  54. },
  55. type: 'bitRate',
  56. };
  57. wsss.send(JSON.stringify(bitRate));
  58. // 进入发起询问
  59. var pings2 = {
  60. type: 'forwardMsg',
  61. data: {
  62. code: '3000',
  63. desc: '询问是否有在控制', // 可选
  64. },
  65. };
  66. wsss.send(JSON.stringify(pings2));
  67. wsss.send(
  68. JSON.stringify({
  69. type: 'getPhoneSize',
  70. }),
  71. );
  72. };
  73. wsss.onerror = function () {
  74. wsss.close();
  75. throttle(doConnectDirectives, 100);
  76. if (errorTime > 1000) {
  77. quit();
  78. }
  79. };
  80. wsss.onmessage = function (res) {
  81. var result = typeof res.data === 'string' ? JSON.parse(res.data) : res.data;
  82. console.log(
  83. '🚀 ~ file: WXdraw.js ~ line 78 ~ doConnectDirectives ~ result',
  84. result,
  85. );
  86. if (result.type === 'cutting') {
  87. if (result.data.status === 0) {
  88. $.toast('复制成功', 'text');
  89. } else {
  90. $.toast(result.msg, 'text');
  91. }
  92. return;
  93. }
  94. if (result.type === 'forwardMsg') {
  95. switch (String(result.data.code)) {
  96. case '3000': {
  97. }
  98. case '4000': {
  99. if (isAuth === 'huo') {
  100. $.confirm(
  101. '授权方已收回控制权,您进入观看屏幕模式',
  102. function () {
  103. //点击确认后的回调函数
  104. changIsControl(false);
  105. },
  106. function () {
  107. changIsControl(false);
  108. //点击取消后的回调函数
  109. quit();
  110. },
  111. );
  112. return;
  113. }
  114. $.confirm(
  115. '当前云手机正在授控,是否请求获取云手机控制权?',
  116. function () {
  117. //点击确认后的回调函数
  118. var ping = {
  119. type: 'forwardMsg',
  120. data: {
  121. code: '5000',
  122. desc: '控制权限收回', // 可选
  123. },
  124. };
  125. wsss.send(JSON.stringify(ping));
  126. changIsControl(true);
  127. },
  128. function () {
  129. //点击取消后的回调函数
  130. changIsControl(false);
  131. },
  132. );
  133. return;
  134. }
  135. case '5000': {
  136. console.log(result);
  137. $.confirm(
  138. '授权方已收回控制权,您进入观看屏幕模式',
  139. function () {
  140. //点击确认后的回调函数
  141. changIsControl(false);
  142. },
  143. function () {
  144. changIsControl(false);
  145. //点击取消后的回调函数
  146. quit();
  147. },
  148. );
  149. return;
  150. }
  151. case 'phoneSizeChange': {
  152. window.currentPhoneSize = {
  153. id: result.data.id,
  154. width: result.data.width,
  155. height: result.data.height,
  156. dpi: result.data.dpi,
  157. };
  158. return;
  159. }
  160. default: {
  161. return;
  162. }
  163. }
  164. }
  165. if (result.type === 'payInitiateEvent') {
  166. var url = window.location.href;
  167. url = url.split('/');
  168. var baseUrl = url[0] + '//' + url[2];
  169. $.ajax({
  170. url: baseUrl + '/api/pay/third/order',
  171. data: JSON.stringify({
  172. orderNum: result.data.orderNum,
  173. orderAmount: result.data.orderAmount,
  174. appKey: result.data.appKey,
  175. }),
  176. type: 'POST',
  177. dataType: 'json',
  178. contentType: 'application/json;charset=UTF-8',
  179. success: function (res) {
  180. if (result.data.payType === 1) {
  181. // 微信
  182. if (window.__wxjs_environment === 'miniprogram') {
  183. // 小程序
  184. // copyUrl(result.data.payUrl);
  185. } else {
  186. window.location.href = result.data.payUrl;
  187. }
  188. } else {
  189. window.location.href = result.data.payUrl;
  190. }
  191. },
  192. });
  193. return;
  194. }
  195. if (result.type === 'getPhoneSize') {
  196. // console.log(result);
  197. const data = window.phoneSizeList.find(function (v) {
  198. return (
  199. v.width === Math.min(result.data.width, result.data.height) &&
  200. v.height === Math.max(result.data.width, result.data.height) &&
  201. v.dpi === result.data.dpi
  202. );
  203. });
  204. window.currentPhoneSize = data || {
  205. width: Math.min(result.data.width, result.data.height),
  206. height: Math.max(result.data.width, result.data.height),
  207. dpi: result.data.dpi,
  208. };
  209. return;
  210. }
  211. };
  212. }
  213. $('body').on('click', function () {
  214. draw_graph('pencil', this);
  215. });
  216. //剪切板
  217. $('.upload').on('click', function () {
  218. var texts = $(this).attr('data-text');
  219. if (texts == 'uploads') {
  220. $('.mainbox').css({
  221. display: 'block',
  222. });
  223. $('.sbox').css({
  224. display: 'none',
  225. });
  226. }
  227. });
  228. //home 控制home
  229. $('.botmat1img').on('click', function () {
  230. var codes = $(this).attr('data-text');
  231. if (codes == 'home' && isControl) {
  232. wsss.send(ExexuteKeyBoard(3));
  233. } else if (codes == 'return' && isControl) {
  234. wsss.send(ExexuteKeyBoard(4));
  235. } else if (codes == 'gengduo' && isControl) {
  236. wsss.send(ExexuteKeyBoard(187));
  237. }
  238. });
  239. // 高清控制
  240. $('.PictureQuality').on('click', function () {
  241. if (!isControl) {
  242. return;
  243. }
  244. $(this).addClass('avit').siblings().removeClass('avit');
  245. var id = $(this).attr('data-id');
  246. var cmd = {
  247. type: 'switchSharpness',
  248. };
  249. decodeWoker.postMessage(cmd); //通知解码器worker切换分辨率
  250. var buffer = makeSharpness(Number(id));
  251. webSocketWorker.postMessage(buffer);
  252. });
  253. var canDraw = false;
  254. //画图形
  255. var draw_graph = function (graphType) {
  256. //把蒙版放于画板上面
  257. $('#container').css('z-index', 30);
  258. $('#dedit').css('z-index', 20);
  259. // 先画在蒙版上 再复制到画布上
  260. //鼠标按下获取 开始xy开始画图
  261. // var ongoingTouches = [];
  262. var touchstart = function (e) {
  263. // console.log('🚀 ~ file: WXdraw.js ~ line 244 ~ touchstart ~ e', e);
  264. if (!isControl) {
  265. return;
  266. }
  267. const action = 0;
  268. Array.from(e.originalEvent.changedTouches).forEach(function (item, index) {
  269. const x = item.clientX - item.target.getBoundingClientRect().x;
  270. const y = item.clientY - item.target.getBoundingClientRect().y;
  271. return wsss.send(
  272. JSON.stringify({
  273. type: 'event',
  274. data: {
  275. action,
  276. count: e.originalEvent.touches.length,
  277. pointerId: item.identifier,
  278. x: (function () {
  279. return (
  280. resolving
  281. ? x * (window.currentPhoneSize.width / vowidth)
  282. : y * (window.currentPhoneSize.height / voheight)
  283. ).toFixed(2);
  284. })(),
  285. y: (function () {
  286. return (
  287. resolving
  288. ? y * (window.currentPhoneSize.height / voheight)
  289. : (vowidth - x) * (window.currentPhoneSize.width / vowidth)
  290. ).toFixed(2);
  291. })(),
  292. },
  293. }),
  294. );
  295. });
  296. canDraw = true;
  297. };
  298. //鼠标离开 把蒙版canvas的图片生成到canvas中
  299. var touchend = function (e) {
  300. if (!isControl) {
  301. return;
  302. }
  303. const action = 1;
  304. Array.from(e.originalEvent.changedTouches).forEach(function (item, index) {
  305. const x = item.clientX - item.target.getBoundingClientRect().x;
  306. const y = item.clientY - item.target.getBoundingClientRect().y;
  307. return wsss.send(
  308. JSON.stringify({
  309. type: 'event',
  310. data: {
  311. action,
  312. count: e.originalEvent.touches.length,
  313. pointerId: item.identifier,
  314. x: (function () {
  315. return (
  316. resolving
  317. ? x * (window.currentPhoneSize.width / vowidth)
  318. : y * (window.currentPhoneSize.height / voheight)
  319. ).toFixed(2);
  320. })(),
  321. y: (function () {
  322. return (
  323. resolving
  324. ? y * (window.currentPhoneSize.height / voheight)
  325. : (vowidth - x) * (window.currentPhoneSize.width / vowidth)
  326. ).toFixed(2);
  327. })(),
  328. },
  329. }),
  330. );
  331. });
  332. // var touchfor = e.originalEvent.changedTouches; //for 的手指数组
  333. // //是否横屏
  334. // for (var i = 0; i < touchfor.length; i++) {
  335. // // var acrossWidthX = touchfor[i].pageY * (videoHeight / voheight);
  336. // // var acrossHeightY =
  337. // // videoWidth - touchfor[i].pageX * (videoWidth / vowidth);
  338. // // if (resolving) {
  339. // // var verticalWidthX =
  340. // // touchfor[i].pageX * (window.currentPhoneSize.width / vowidth);
  341. // // var verticalHeightY =
  342. // // touchfor[i].pageY * (window.currentPhoneSize.height / voheight);
  343. // // } else {
  344. // // var verticalWidthX =
  345. // // touchfor[i].pageX * (window.currentPhoneSize.width / vowidth);
  346. // // var verticalHeightY =
  347. // // touchfor[i].pageY * (window.currentPhoneSize.height / voheight);
  348. // // }
  349. // var ping =
  350. // // resolving == 0 ?
  351. // // { "data": { "action": 1, "count": ongoingTouches.length, "pointerId": touchfor[i].identifier, "x": acrossWidthX.toFixed(2), "y": acrossHeightY.toFixed(2) }, "type": "event" } :
  352. // {
  353. // data: {
  354. // action: 1,
  355. // count: ongoingTouches.length,
  356. // pointerId: touchfor[i].identifier,
  357. // x: (() =>
  358. // (resolving
  359. // ? touchfor[i].pageX * (window.currentPhoneSize.width / vowidth)
  360. // : touchfor[i].pageY * (window.currentPhoneSize.width / voheight)
  361. // ).toFixed(2))(),
  362. // y: (() =>
  363. // (resolving
  364. // ? touchfor[i].pageY *
  365. // (window.currentPhoneSize.height / voheight)
  366. // : (vowidth - touchfor[i].pageX) *
  367. // (window.currentPhoneSize.height / vowidth)
  368. // ).toFixed(2))(),
  369. // },
  370. // type: 'event',
  371. // };
  372. // wsss.send(JSON.stringify(ping));
  373. // ongoingTouches.forEach(function (item, index) {
  374. // if (item.identifier === touchfor[i].identifier) {
  375. // ongoingTouches.splice(index, 1);
  376. // }
  377. // });
  378. // }
  379. canDraw = false;
  380. };
  381. //清空层 云手机超出屏幕的开关
  382. var clearContext = function () {
  383. canDraw = false;
  384. };
  385. // 鼠标移动
  386. var touchmove = function (e) {
  387. e.preventDefault();
  388. if (!isControl) {
  389. return;
  390. }
  391. const action = 2;
  392. Array.from(e.originalEvent.changedTouches).forEach(function (item, index) {
  393. const x = item.clientX - item.target.getBoundingClientRect().x;
  394. const y = item.clientY - item.target.getBoundingClientRect().y;
  395. return wsss.send(
  396. JSON.stringify({
  397. type: 'event',
  398. data: {
  399. action,
  400. count: e.originalEvent.touches.length,
  401. pointerId: item.identifier,
  402. x: (function () {
  403. return (
  404. resolving
  405. ? x * (window.currentPhoneSize.width / vowidth)
  406. : y * (window.currentPhoneSize.height / voheight)
  407. ).toFixed(2);
  408. })(),
  409. y: (function () {
  410. return (
  411. resolving
  412. ? y * (window.currentPhoneSize.height / voheight)
  413. : (vowidth - x) * (window.currentPhoneSize.width / vowidth)
  414. ).toFixed(2);
  415. })(),
  416. },
  417. }),
  418. );
  419. });
  420. // var touchfor = e.originalEvent.targetTouches; //for 的手指数组
  421. // for (var i = 0; i < touchfor.length; i++) {
  422. // // var acrossWidthX = touchfor[i].pageY * (videoHeight / voheight);
  423. // // var acrossHeightY =
  424. // // videoWidth - touchfor[i].pageX * (videoWidth / vowidth);
  425. // // let verticalWidthX = 0;
  426. // // let verticalHeightY = 0;
  427. // // if (resolving) {
  428. // // verticalWidthX =
  429. // // touchfor[i].pageX * (window.currentPhoneSize.width / vowidth);
  430. // // verticalHeightY =
  431. // // touchfor[i].pageY * (window.currentPhoneSize.height / voheight);
  432. // // } else {
  433. // // verticalWidthX =
  434. // // touchfor[i].pageX * (window.currentPhoneSize.width / vowidth);
  435. // // verticalHeightY =
  436. // // touchfor[i].pageY * (window.currentPhoneSize.height / voheight);
  437. // // }
  438. // var ping =
  439. // // resolving == 0 ?
  440. // // { "data": { "action": 2, "count": touchfor.length, "pointerId": touchfor[i].identifier, "x": acrossWidthX.toFixed(2), "y": acrossHeightY.toFixed(2) }, "type": "event" } :
  441. // {
  442. // data: {
  443. // action: 2,
  444. // count: touchfor.length,
  445. // pointerId: touchfor[i].identifier,
  446. // x: (() =>
  447. // (resolving
  448. // ? touchfor[i].pageX * (window.currentPhoneSize.width / vowidth)
  449. // : touchfor[i].pageY * (window.currentPhoneSize.width / voheight)
  450. // ).toFixed(2))(),
  451. // y: (() =>
  452. // (resolving
  453. // ? touchfor[i].pageY *
  454. // (window.currentPhoneSize.height / voheight)
  455. // : (vowidth - touchfor[i].pageX) *
  456. // (window.currentPhoneSize.height / vowidth)
  457. // ).toFixed(2))(),
  458. // },
  459. // type: 'event',
  460. // };
  461. // wsss.send(JSON.stringify(ping));
  462. // }
  463. };
  464. //鼠标离开区域以外 除了涂鸦 都清空
  465. var mouseout = function () {
  466. if (graphType != 'handwriting') {
  467. clearContext();
  468. }
  469. };
  470. $(canvas_bak).off();
  471. $(canvas_bak).on('touchstart', touchstart);
  472. $(canvas_bak).on('touchmove', touchmove);
  473. $(canvas_bak).on('touchend', touchend);
  474. $(canvas_bak).on('mouseout', mouseout);
  475. };
  476. function GetRequest() {
  477. var url = location.search; // 获取url中"?"符后的字串
  478. var obj = new Object();
  479. if (url.indexOf('?') != -1) {
  480. var str = url.substr(1);
  481. strs = str.split('&');
  482. for (var i = 0; i < strs.length; i++) {
  483. obj[strs[i].split('=')[0]] = strs[i].split('=')[1];
  484. }
  485. }
  486. return obj;
  487. }