WXdraw.js 7.0 KB

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