WXdraw.js 7.9 KB

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