WXdraw.js 7.9 KB

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