WXdraw.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. wsss.onerror = function () {
  61. wsss.close();
  62. throttle(doConnect, 100);
  63. if (errorTime > 1000) {
  64. wx.miniProgram.switchTab({
  65. url: '/pages/home/home'
  66. })
  67. }
  68. };
  69. }
  70. $('body').on("click", function () {
  71. draw_graph('pencil', this)
  72. })
  73. //剪切板
  74. $(".upload").on("click", function () {
  75. var texts = $(this).attr("data-text")
  76. if (texts == "uploads") {
  77. $(".mainbox").css({
  78. "display": "block"
  79. })
  80. $(".sbox").css({
  81. "display": "none"
  82. })
  83. }
  84. })
  85. //home 控制home
  86. $(".botmat1img").on("click", function () {
  87. var codes = $(this).attr("data-text")
  88. if (codes == "home") {
  89. wsss.send(ExexuteKeyBoard(3));
  90. } else if (codes == "return") {
  91. wsss.send(ExexuteKeyBoard(4));
  92. } else if (codes == "gengduo") {
  93. wsss.send(ExexuteKeyBoard(187));
  94. }
  95. })
  96. //高清控制
  97. $(".PictureQuality").on("click", function () {
  98. $(this).addClass("avit").siblings().removeClass('avit')
  99. var id = $(this).attr("data-id")
  100. var bitRate = {
  101. "data": {
  102. "bitRate": id
  103. },
  104. "type": "bitRate"
  105. }
  106. wsss.send(JSON.stringify(bitRate))
  107. })
  108. var canDraw = false;
  109. var ongoingTouches = [];
  110. //画图形
  111. var draw_graph = function (graphType) {
  112. //把蒙版放于画板上面
  113. $("#container").css("z-index", 30);
  114. $("#dedit").css("z-index", 20);
  115. // 先画在蒙版上 再复制到画布上
  116. //鼠标按下获取 开始xy开始画图
  117. var touchstart = function (e) {
  118. $('.control-right-img').attr({
  119. "data-id": "2"
  120. })
  121. $(".leftmains").css({
  122. "right": "-4rem"
  123. })
  124. var touchfor = e.originalEvent.changedTouches[0]; //for 的手指数组
  125. ongoingTouches.push(touchfor);
  126. var acrossWidthX = touchfor.pageY * (1280 / voheight);
  127. var acrossHeightY = 720 - touchfor.pageX * (720 / vowidth);
  128. var verticalWidthX = touchfor.pageX * (720 / vowidth);
  129. var verticalHeightY = touchfor.pageY * (1280 / voheight);
  130. //是否横屏
  131. var ping = resolving == 0 ?
  132. {"data":{"action": 0, "count": ongoingTouches.length, "id": touchfor.identifier, "x": acrossWidthX.toFixed(2), "y": acrossHeightY.toFixed(2)},"type": "event"} :
  133. {"data":{"action": 0, "count": ongoingTouches.length, "id": touchfor.identifier, "x": verticalWidthX.toFixed(2), "y": verticalHeightY.toFixed(2)},"type": "event"};
  134. wsss.send(JSON.stringify(ping));
  135. canDraw = true;
  136. };
  137. //鼠标离开 把蒙版canvas的图片生成到canvas中
  138. var touchend = function (e) {
  139. var touchfor = e.originalEvent.changedTouches[0]; //for 的手指数组
  140. var acrossWidthX = touchfor.pageY * (1280 / voheight);
  141. var acrossHeightY = 720 - touchfor.pageX * (720 / vowidth);
  142. var verticalWidthX = touchfor.pageX * (720 / vowidth);
  143. var verticalHeightY = touchfor.pageY * (1280 / voheight);
  144. //是否横屏
  145. var ping = resolving == 0 ?
  146. {"data":{"action": 1, "count": ongoingTouches.length, "id": touchfor.identifier, "x": acrossWidthX.toFixed(2), "y": acrossHeightY.toFixed(2)},"type": "event"} :
  147. {"data":{"action": 1, "count": ongoingTouches.length, "id": touchfor.identifier, "x": verticalWidthX.toFixed(2), "y": verticalHeightY.toFixed(2)},"type": "event"};
  148. wsss.send(JSON.stringify(ping));
  149. ongoingTouches.splice(touchfor.identifier, 1);
  150. canDraw = false;
  151. };
  152. //清空层 云手机超出屏幕的开关
  153. var clearContext = function () {
  154. canDraw = false;
  155. }
  156. // 鼠标移动
  157. var touchmove = function (e) {
  158. e = e || window.event;
  159. var touchfor = e.originalEvent.changedTouches[0]; //for 的手指数组
  160. var acrossWidthX = touchfor.pageY * (1280 / voheight);
  161. var acrossHeightY = 720 - touchfor.pageX * (720 / vowidth);
  162. var verticalWidthX = touchfor.pageX * (720 / vowidth);
  163. var verticalHeightY = touchfor.pageY * (1280 / voheight);
  164. var ping = resolving == 0 ?
  165. {"data":{"action": 2, "count": ongoingTouches.length, "id": touchfor.identifier, "x": acrossWidthX.toFixed(2), "y": acrossHeightY.toFixed(2)},"type": "event"} :
  166. {"data":{"action": 2, "count": ongoingTouches.length, "id": touchfor.identifier, "x": verticalWidthX.toFixed(2), "y": verticalHeightY.toFixed(2)},"type": "event"};
  167. wsss.send(JSON.stringify(ping));
  168. };
  169. //鼠标离开区域以外 除了涂鸦 都清空
  170. var mouseout = function () {
  171. if (graphType != 'handwriting') {
  172. clearContext();
  173. }
  174. }
  175. $(canvas_bak).unbind();
  176. $(canvas_bak).bind('touchstart', touchstart);
  177. $(canvas_bak).bind('touchmove', touchmove);
  178. $(canvas_bak).bind('touchend', touchend);
  179. $(canvas_bak).bind('mouseout', mouseout);
  180. }
  181. function GetRequest() {
  182. var url = location.search; // 获取url中"?"符后的字串
  183. var obj = new Object();
  184. if (url.indexOf("?") != -1) {
  185. var str = url.substr(1);
  186. strs = str.split("&");
  187. for (var i = 0; i < strs.length; i++) {
  188. obj[strs[i].split("=")[0]] = (strs[i].split("=")[1]);
  189. }
  190. }
  191. return obj;
  192. }