WXdraw.js 7.9 KB

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