WXdraw.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. if (navigator.userAgent.toLowerCase().includes('toutiaomicroapp')) {
  80. tt.miniProgram.switchTab({
  81. url: '/pages/home/home'
  82. })
  83. } else {
  84. wx.miniProgram.switchTab({
  85. url: '/pages/home/home'
  86. })
  87. }
  88. }
  89. };
  90. wsss.onmessage = function (res) {
  91. var result = typeof res.data === 'string' ? JSON.parse(res.data) : res.data
  92. if (result.type === 'cutting') {
  93. if (result.data.status === 0) {
  94. $.toast('复制成功', "text");
  95. } else {
  96. $.toast(result.msg, "text");
  97. }
  98. return
  99. }
  100. if (result.type === 'getPhoneSize') {
  101. if (result.data.status === 0) {
  102. videoWidth = result.data.width
  103. videoHeight = result.data.height
  104. }
  105. return
  106. }
  107. var unit8Arr = new Uint8Array(result);
  108. if (unit8Arr[0] == 0x68 && unit8Arr[1] == 0x09 && first) {
  109. first = false;
  110. unit8Arr = unit8Arr.slice(12, unit8Arr.length - 2);
  111. var base64 = 'data:image/jpg;base64,' + window.btoa(String.fromCharCode(...new Uint8Array(unit8Arr)));
  112. $(".loading_sceen_pic").attr('src', base64);
  113. }
  114. }
  115. }
  116. $('body').on("click", function () {
  117. draw_graph('pencil');
  118. })
  119. //剪切板
  120. $(".upload").on("click", function () {
  121. var texts = $(this).attr("data-text")
  122. if (texts == "uploads") {
  123. $(".mainbox").css({
  124. "display": "block"
  125. })
  126. $(".sbox").css({
  127. "display": "none"
  128. })
  129. }
  130. })
  131. //home 控制home
  132. $(".botmat1img").on("click", function () {
  133. var codes = $(this).attr("data-text")
  134. if (codes == "home") {
  135. wsss.send(ExexuteKeyBoard(3));
  136. } else if (codes == "return") {
  137. wsss.send(ExexuteKeyBoard(4));
  138. } else if (codes == "gengduo") {
  139. wsss.send(ExexuteKeyBoard(187));
  140. }
  141. })
  142. //高清控制
  143. $(".PictureQuality").on("click", function () {
  144. $(this).addClass("avit").siblings().removeClass('avit')
  145. var id = $(this).attr("data-id")
  146. var bitRate = {
  147. "data": {
  148. "bitRate": id
  149. },
  150. "type": "bitRate"
  151. }
  152. wsss.send(JSON.stringify(bitRate))
  153. })
  154. //画图形
  155. var draw_graph = function (graphType, obj) {
  156. //把蒙版放于画板上面
  157. $("#container").css("z-index", 30);
  158. $("#dedit").css("z-index", 20);
  159. var canDraw = false;
  160. //鼠标按下获取 开始xy开始画图
  161. var touchstart = function (e) {
  162. $('.control-right-img').attr({
  163. "data-id": "2"
  164. })
  165. $(".leftmains").css({
  166. "right": "-4rem"
  167. })
  168. var touchfor = e.originalEvent.touches; //for 的手指数组
  169. console.log(touchfor);
  170. //是否横屏
  171. for (var i = 0; i < touchfor.length; i++) {
  172. var acrossWidthX = touchfor[i].pageY * (videoHeight / voheight);
  173. var acrossHeightY = videoWidth - touchfor[i].pageX * (videoWidth / vowidth);
  174. var verticalWidthX = touchfor[i].pageX * (videoWidth / vowidth);
  175. var verticalHeightY = touchfor[i].pageY * (videoHeight / voheight);
  176. var ping = resolving == 0 ?
  177. { "data": { "action": 0, "count": touchfor.length, "id": touchfor[i].identifier, "x": acrossWidthX.toFixed(2), "y": acrossHeightY.toFixed(2) }, "type": "event" } :
  178. { "data": { "action": 0, "count": touchfor.length, "id": touchfor[i].identifier, "x": verticalWidthX.toFixed(2), "y": verticalHeightY.toFixed(2) }, "type": "event" };
  179. console.log(ping);
  180. wsss.send(JSON.stringify(ping));
  181. }
  182. canDraw = true;
  183. };
  184. //鼠标离开 把蒙版canvas的图片生成到canvas中
  185. var touchend = function (e) {
  186. var touchfor = e.originalEvent.changedTouches; //for 的手指数组
  187. console.log(touchfor);
  188. //是否横屏
  189. for (var i = 0; i < touchfor.length; i++) {
  190. var acrossWidthX = touchfor[i].pageY * (videoHeight / voheight);
  191. var acrossHeightY = videoWidth - touchfor[i].pageX * (videoWidth / vowidth);
  192. var verticalWidthX = touchfor[i].pageX * (videoWidth / vowidth);
  193. var verticalHeightY = touchfor[i].pageY * (videoHeight / voheight);
  194. var ping = resolving == 0 ?
  195. { "data": { "action": 1, "count": touchfor.length, "id": touchfor[i].identifier, "x": acrossWidthX.toFixed(2), "y": acrossHeightY.toFixed(2) }, "type": "event" } :
  196. { "data": { "action": 1, "count": touchfor.length, "id": touchfor[i].identifier, "x": verticalWidthX.toFixed(2), "y": verticalHeightY.toFixed(2) }, "type": "event" };
  197. console.log(ping);
  198. wsss.send(JSON.stringify(ping));
  199. }
  200. canDraw = false;
  201. };
  202. //清空层 云手机超出屏幕的开关
  203. var clearContext = function () {
  204. canDraw = false;
  205. }
  206. // 鼠标移动
  207. var touchmove = function (e) {
  208. var touchfor = e.originalEvent.touches; //for 的手指数组
  209. console.log(touchfor);
  210. for (var i = 0; i < touchfor.length; i++) {
  211. var acrossWidthX = touchfor[i].pageY * (videoHeight / voheight);
  212. var acrossHeightY = videoWidth - touchfor[i].pageX * (videoWidth / vowidth);
  213. var verticalWidthX = touchfor[i].pageX * (videoWidth / vowidth);
  214. var verticalHeightY = touchfor[i].pageY * (videoHeight / voheight);
  215. var ping = resolving == 0 ?
  216. { "data": { "action": 2, "count": touchfor.length, "id": touchfor[i].identifier, "x": acrossWidthX.toFixed(2), "y": acrossHeightY.toFixed(2) }, "type": "event" } :
  217. { "data": { "action": 2, "count": touchfor.length, "id": touchfor[i].identifier, "x": verticalWidthX.toFixed(2), "y": verticalHeightY.toFixed(2) }, "type": "event" };
  218. console.log(ping);
  219. wsss.send(JSON.stringify(ping));
  220. }
  221. };
  222. //鼠标离开区域以外 除了涂鸦 都清空
  223. var mouseout = function () {
  224. if (graphType != 'handwriting') {
  225. clearContext();
  226. }
  227. }
  228. $(canvas_bak).unbind();
  229. $(canvas_bak).bind('touchstart', touchstart);
  230. $(canvas_bak).bind('touchmove', touchmove);
  231. $(canvas_bak).bind('touchend', touchend);
  232. $(canvas_bak).bind('mouseout', mouseout);
  233. }
  234. // 获取url中"?"符后的字串
  235. function GetRequest() {
  236. var url = location.search;
  237. var obj = new Object();
  238. if (url.indexOf("?") != -1) {
  239. var str = url.substr(1);
  240. strs = str.split("&");
  241. for (var i = 0; i < strs.length; i++) {
  242. obj[strs[i].split("=")[0]] = (strs[i].split("=")[1]);
  243. }
  244. }
  245. return obj;
  246. }