WXdraw.js 7.9 KB

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