WXdraw.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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;
  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 = Number(resolvingPower) ? Number(resolvingPower) : 720
  19. var videoHeight = videoWidth === 720 ? 1280 : 1920
  20. var isControl = true; // 是否是观看模式
  21. var isAuth = parameters['authPhone']; // 是否是获取的云手机
  22. var wsss, 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. wsss = new WebSocket(cUrl);
  38. wsss.binaryType = 'arraybuffer';
  39. wsss.onopen = function () {
  40. // 进入发起询问
  41. var pings2 = {
  42. "type": "forwardMsg",
  43. "data": {
  44. "code": "3000",
  45. "desc": "询问是否有在控制" // 可选
  46. }
  47. }
  48. wsss.send(JSON.stringify(pings2));
  49. };
  50. wsss.onerror = function (evt) {
  51. wsss.close();
  52. throttle(doConnectDirectives, 100);
  53. if (errorTime > 1000) {
  54. quit();
  55. }
  56. };
  57. wsss.onmessage = function (res) {
  58. var result = typeof res.data === 'string' ? JSON.parse(res.data) : res.data
  59. if (result.type === 'cutting') {
  60. if (result.data.status === 0) {
  61. $.toast('复制成功', "text");
  62. } else {
  63. $.toast(result.msg, "text");
  64. }
  65. return
  66. }
  67. if (result.type === 'forwardMsg' && isAuth !== 'none') {
  68. if (result.data.code === 4000 || result.data.code === 3000) {
  69. if (isAuth === 'huo') {
  70. $.confirm("授权方已收回控制权,您进入观看屏幕模式", function () {
  71. //点击确认后的回调函数
  72. isControl = false;
  73. }, function () {
  74. isControl = false;
  75. //点击取消后的回调函数
  76. quit();
  77. });
  78. } else {
  79. $.confirm("当前云手机正在授控,是否请求获取云手机控制权?", function () {
  80. //点击确认后的回调函数
  81. var ping = {
  82. "type": "forwardMsg",
  83. "data": {
  84. "code": "5000",
  85. "desc": "控制权限收回" // 可选
  86. }
  87. }
  88. wsss.send(JSON.stringify(ping));
  89. isControl = true;
  90. }, function () {
  91. //点击取消后的回调函数
  92. isControl = false;
  93. });
  94. }
  95. }
  96. return
  97. }
  98. }
  99. }
  100. $('body').on("click", function () {
  101. draw_graph('pencil');
  102. })
  103. //剪切板
  104. $(".upload").on("click", function () {
  105. var texts = $(this).attr("data-text")
  106. if (texts == "uploads") {
  107. $(".mainbox").css({
  108. "display": "block"
  109. })
  110. $(".sbox").css({
  111. "display": "none"
  112. })
  113. }
  114. })
  115. //home 控制home
  116. $(".botmat1img").on("click", function () {
  117. var codes = $(this).attr("data-text")
  118. if (codes == "home" && isControl) {
  119. wsss.send(ExexuteKeyBoard(3));
  120. } else if (codes == "return" && isControl) {
  121. wsss.send(ExexuteKeyBoard(4));
  122. } else if (codes == "gengduo" && isControl) {
  123. wsss.send(ExexuteKeyBoard(187));
  124. }
  125. })
  126. // 高清控制
  127. $(".PictureQuality").on("click", function () {
  128. if (!isControl) {
  129. return
  130. }
  131. $(this).addClass("avit").siblings().removeClass('avit')
  132. var id = $(this).attr("data-id")
  133. var buffer = makeSharpness(Number(id));
  134. ws.send(buffer);
  135. })
  136. //画图形
  137. var draw_graph = function (graphType, obj) {
  138. //把蒙版放于画板上面
  139. $("#container").css("z-index", 30);
  140. $("#dedit").css("z-index", 20);
  141. var canDraw = false;
  142. //鼠标按下获取 开始xy开始画图
  143. var ongoingTouches = [];
  144. var touchstart = function (e) {
  145. if (!isControl) {
  146. return
  147. }
  148. $('.control-right-img').attr({
  149. "data-id": "2"
  150. })
  151. $(".leftmains").css({
  152. "right": "-4rem"
  153. })
  154. var touchfor = e.originalEvent.changedTouches; //for 的手指数组
  155. //是否横屏
  156. for (var i = 0; i < touchfor.length; i++) {
  157. var acrossWidthX = touchfor[i].pageY * (videoHeight / voheight);
  158. var acrossHeightY = videoWidth - touchfor[i].pageX * (videoWidth / vowidth);
  159. var verticalWidthX = touchfor[i].pageX * (videoWidth / vowidth);
  160. var verticalHeightY = touchfor[i].pageY * (videoHeight / voheight);
  161. var idx = ongoingTouches.findIndex(function (ele) {
  162. return ele.identifier === touchfor[i].identifier
  163. })
  164. if (idx < 0) {
  165. ongoingTouches.push(touchfor[i]);
  166. }
  167. var ping = resolving == 0 ?
  168. { "data": { "action": 0, "count": ongoingTouches.length, "pointerId": touchfor[i].identifier, "x": acrossWidthX.toFixed(2), "y": acrossHeightY.toFixed(2) }, "type": "event" } :
  169. { "data": { "action": 0, "count": ongoingTouches.length, "pointerId": touchfor[i].identifier, "x": verticalWidthX.toFixed(2), "y": verticalHeightY.toFixed(2) }, "type": "event" };
  170. wsss.send(JSON.stringify(ping));
  171. }
  172. canDraw = true;
  173. };
  174. //鼠标离开 把蒙版canvas的图片生成到canvas中
  175. var touchend = function (e) {
  176. if (!isControl) {
  177. return
  178. }
  179. var touchfor = e.originalEvent.changedTouches; //for 的手指数组
  180. //是否横屏
  181. for (var i = 0; i < touchfor.length; i++) {
  182. var acrossWidthX = touchfor[i].pageY * (videoHeight / voheight);
  183. var acrossHeightY = videoWidth - touchfor[i].pageX * (videoWidth / vowidth);
  184. var verticalWidthX = touchfor[i].pageX * (videoWidth / vowidth);
  185. var verticalHeightY = touchfor[i].pageY * (videoHeight / voheight);
  186. var ping = resolving == 0 ?
  187. { "data": { "action": 1, "count": ongoingTouches.length, "pointerId": touchfor[i].identifier, "x": acrossWidthX.toFixed(2), "y": acrossHeightY.toFixed(2) }, "type": "event" } :
  188. { "data": { "action": 1, "count": ongoingTouches.length, "pointerId": touchfor[i].identifier, "x": verticalWidthX.toFixed(2), "y": verticalHeightY.toFixed(2) }, "type": "event" };
  189. wsss.send(JSON.stringify(ping));
  190. ongoingTouches.forEach(function (item, index) {
  191. if (item.identifier === touchfor[i].identifier) {
  192. ongoingTouches.splice(index, 1)
  193. }
  194. })
  195. }
  196. canDraw = false;
  197. };
  198. //清空层 云手机超出屏幕的开关
  199. var clearContext = function () {
  200. canDraw = false;
  201. }
  202. // 鼠标移动
  203. var touchmove = function (e) {
  204. if (!isControl) {
  205. return
  206. }
  207. var touchfor = e.originalEvent.targetTouches; //for 的手指数组
  208. for (var i = 0; i < touchfor.length; i++) {
  209. var acrossWidthX = touchfor[i].pageY * (videoHeight / voheight);
  210. var acrossHeightY = videoWidth - touchfor[i].pageX * (videoWidth / vowidth);
  211. var verticalWidthX = touchfor[i].pageX * (videoWidth / vowidth);
  212. var verticalHeightY = touchfor[i].pageY * (videoHeight / voheight);
  213. var ping = resolving == 0 ?
  214. { "data": { "action": 2, "count": touchfor.length, "pointerId": touchfor[i].identifier, "x": acrossWidthX.toFixed(2), "y": acrossHeightY.toFixed(2) }, "type": "event" } :
  215. { "data": { "action": 2, "count": touchfor.length, "pointerId": touchfor[i].identifier, "x": verticalWidthX.toFixed(2), "y": verticalHeightY.toFixed(2) }, "type": "event" };
  216. wsss.send(JSON.stringify(ping));
  217. }
  218. };
  219. //鼠标离开区域以外 除了涂鸦 都清空
  220. var mouseout = function () {
  221. if (graphType != 'handwriting') {
  222. clearContext();
  223. }
  224. }
  225. $(canvas_bak).unbind();
  226. $(canvas_bak).bind('touchstart', touchstart);
  227. $(canvas_bak).bind('touchmove', touchmove);
  228. $(canvas_bak).bind('touchend', touchend);
  229. $(canvas_bak).bind('mouseout', mouseout);
  230. }
  231. // 获取url中"?"符后的字串
  232. function GetRequest() {
  233. var url = location.search;
  234. var obj = new Object();
  235. if (url.indexOf("?") != -1) {
  236. var str = url.substr(1);
  237. strs = str.split("&");
  238. for (var i = 0; i < strs.length; i++) {
  239. obj[strs[i].split("=")[0]] = (strs[i].split("=")[1]);
  240. }
  241. }
  242. return obj;
  243. }