WXdraw.js 15 KB

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