WXdraw.js 8.4 KB

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