WXdraw.js 8.2 KB

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