WXdraw.js 8.3 KB

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