WXdraw.js 7.8 KB

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