WXdraw.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 cardToken = parameters["cardToken"];
  20. cardToken = cardToken && cardToken.replace(/@/g,"=");
  21. form.cardIp = parameters["cardIp"];
  22. form.port = parameters["port"];
  23. form.username = parameters["username"];
  24. form.userCardId = parameters["userCardId"];
  25. form.ip = parameters["ip"];
  26. form.domainName = parameters["domainName"];
  27. var isWSS = true;
  28. var cUrl = isWSS ? "wss://" + form.domainName + "?cardIp=" + form.ip + "&token=" + cardToken : "ws://" + form.domainName + "?cardIp=" + form.ip + "&token=" + cardToken;
  29. // var cUrl = "wss://192.168.31.198/businessChannel?cardIp=192.168.20.53&token=LDAz32tutqS9wQqi0WlmvJ68/Is83cX+kavJQij1IMRvewgZ1dLvviFZqd4b8gcaR1eLztR/ViRLyyAVvZO7GA==";
  30. var wsss,errorTime = 0;
  31. doConnect();
  32. function throttle(fn, delay) {
  33. var flag = true;
  34. errorTime += delay;
  35. return () => {
  36. if (!flag) return;
  37. flag = false;
  38. timer = setTimeout(() => {
  39. fn();
  40. flag = true;
  41. }, delay);
  42. };
  43. }
  44. function doConnect() {
  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 screenPic = {
  60. type: 'getScreenPic',
  61. data: {
  62. taskUid: '346789',
  63. intervalTime: 0
  64. }
  65. }
  66. wsss.send(JSON.stringify(screenPic));
  67. };
  68. wsss.onerror = function (evt) {
  69. wsss.close();
  70. throttle(doConnect,100);
  71. if(errorTime > 1000){
  72. wx.miniProgram.switchTab({
  73. url: '/pages/home/home'
  74. })
  75. }
  76. };
  77. wsss.onmessage = function(res) {
  78. var result = typeof res.data === 'string' ? JSON.parse(res.data) : res.data
  79. if (result.type === 'cutting') {
  80. if (result.data.status === 0) {
  81. $.toast('复制成功', "text");
  82. } else {
  83. $.toast(result.msg, "text");
  84. }
  85. return
  86. }
  87. var unit8Arr = new Uint8Array(result);
  88. if (unit8Arr[0] == 0x68 && unit8Arr[1] == 0x09) {
  89. unit8Arr = unit8Arr.slice(12,unit8Arr.length-2);
  90. var base64 = 'data:image/jpg;base64,' + window.btoa(String.fromCharCode(...new Uint8Array(unit8Arr)));
  91. $(".loading_sceen_pic").attr('src', base64)
  92. }
  93. }
  94. }
  95. $('body').on("click", function () {
  96. draw_graph('pencil');
  97. })
  98. //剪切板
  99. $(".upload").on("click", function () {
  100. var texts = $(this).attr("data-text")
  101. if (texts == "uploads") {
  102. $(".mainbox").css({
  103. "display": "block"
  104. })
  105. $(".sbox").css({
  106. "display": "none"
  107. })
  108. }
  109. })
  110. //home 控制home
  111. $(".botmat1img").on("click", function () {
  112. var codes = $(this).attr("data-text")
  113. if (codes == "home") {
  114. wsss.send(ExexuteKeyBoard(3));
  115. } else if (codes == "return") {
  116. wsss.send(ExexuteKeyBoard(4));
  117. } else if (codes == "gengduo") {
  118. wsss.send(ExexuteKeyBoard(187));
  119. }
  120. })
  121. //高清控制
  122. $(".PictureQuality").on("click", function () {
  123. $(this).addClass("avit").siblings().removeClass('avit')
  124. var id = $(this).attr("data-id")
  125. var bitRate = {
  126. "data": {
  127. "bitRate": id
  128. },
  129. "type": "bitRate"
  130. }
  131. wsss.send(JSON.stringify(bitRate))
  132. })
  133. var ongoingTouches = [];
  134. //画图形
  135. var draw_graph = function (graphType, obj) {
  136. //把蒙版放于画板上面
  137. $("#container").css("z-index", 30);
  138. $("#dedit").css("z-index", 20);
  139. var canDraw = false;
  140. //鼠标按下获取 开始xy开始画图
  141. var touchstart = function (e) {
  142. $('.control-right-img').attr({
  143. "data-id": "2"
  144. })
  145. $(".leftmains").css({
  146. "right": "-4rem"
  147. })
  148. var touchfor = e.originalEvent.changedTouches[0]; //for 的手指数组
  149. ongoingTouches.push(touchfor);
  150. var acrossWidthX = touchfor.pageY * (1280 / voheight);
  151. var acrossHeightY = 720 - touchfor.pageX * (720 / vowidth);
  152. var verticalWidthX = touchfor.pageX * (720 / vowidth);
  153. var verticalHeightY = touchfor.pageY * (1280 / voheight);
  154. //是否横屏
  155. var ping = resolving == 0 ?
  156. {"data":{"action": 0, "count": ongoingTouches.length, "id": touchfor.identifier, "x": acrossWidthX.toFixed(2), "y": acrossHeightY.toFixed(2)},"type": "event"} :
  157. {"data":{"action": 0, "count": ongoingTouches.length, "id": touchfor.identifier, "x": verticalWidthX.toFixed(2), "y": verticalHeightY.toFixed(2)},"type": "event"};
  158. wsss.send(JSON.stringify(ping));
  159. canDraw = true;
  160. };
  161. //鼠标离开 把蒙版canvas的图片生成到canvas中
  162. var touchend = function (e) {
  163. var touchfor = e.originalEvent.changedTouches[0]; //for 的手指数组
  164. var acrossWidthX = touchfor.pageY * (1280 / voheight);
  165. var acrossHeightY = 720 - touchfor.pageX * (720 / vowidth);
  166. var verticalWidthX = touchfor.pageX * (720 / vowidth);
  167. var verticalHeightY = touchfor.pageY * (1280 / voheight);
  168. //是否横屏
  169. var ping = resolving == 0 ?
  170. {"data":{"action": 1, "count": ongoingTouches.length, "id": touchfor.identifier, "x": acrossWidthX.toFixed(2), "y": acrossHeightY.toFixed(2)},"type": "event"} :
  171. {"data":{"action": 1, "count": ongoingTouches.length, "id": touchfor.identifier, "x": verticalWidthX.toFixed(2), "y": verticalHeightY.toFixed(2)},"type": "event"};
  172. wsss.send(JSON.stringify(ping));
  173. ongoingTouches.splice(touchfor.identifier, 1);
  174. canDraw = false;
  175. };
  176. //清空层 云手机超出屏幕的开关
  177. var clearContext = function () {
  178. canDraw = false;
  179. }
  180. // 鼠标移动
  181. var touchmove = function (e) {
  182. e = e || window.event;
  183. var touchfor = e.originalEvent.changedTouches[0]; //for 的手指数组
  184. var acrossWidthX = touchfor.pageY * (1280 / voheight);
  185. var acrossHeightY = 720 - touchfor.pageX * (720 / vowidth);
  186. var verticalWidthX = touchfor.pageX * (720 / vowidth);
  187. var verticalHeightY = touchfor.pageY * (1280 / voheight);
  188. var ping = resolving == 0 ?
  189. {"data":{"action": 2, "count": ongoingTouches.length, "id": touchfor.identifier, "x": acrossWidthX.toFixed(2), "y": acrossHeightY.toFixed(2)},"type": "event"} :
  190. {"data":{"action": 2, "count": ongoingTouches.length, "id": touchfor.identifier, "x": verticalWidthX.toFixed(2), "y": verticalHeightY.toFixed(2)},"type": "event"};
  191. wsss.send(JSON.stringify(ping));
  192. };
  193. //鼠标离开区域以外 除了涂鸦 都清空
  194. var mouseout = function () {
  195. if (graphType != 'handwriting') {
  196. clearContext();
  197. }
  198. }
  199. $(canvas_bak).unbind();
  200. $(canvas_bak).bind('touchstart', touchstart);
  201. $(canvas_bak).bind('touchmove', touchmove);
  202. $(canvas_bak).bind('touchend', touchend);
  203. $(canvas_bak).bind('mouseout', mouseout);
  204. }
  205. // 获取url中"?"符后的字串
  206. function GetRequest() {
  207. var url = location.search;
  208. var obj = new Object();
  209. if (url.indexOf("?") != -1) {
  210. var str = url.substr(1);
  211. strs = str.split("&");
  212. for (var i = 0; i < strs.length; i++) {
  213. obj[strs[i].split("=")[0]] = (strs[i].split("=")[1]);
  214. }
  215. }
  216. return obj;
  217. }