WXdraw.js 8.6 KB

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