WXdraw.js 7.9 KB

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