WXdraw.js 8.4 KB

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