WXdraw.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 = Number(parameters['resolvingPower']) ? Number(parameters['resolvingPower']) : 720
  30. var videoHeight = videoWidth === 720 ? 1280 : 1920
  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 buffer = makeSharpness(Number(id));
  119. ws.send(buffer);
  120. isFeed = false;
  121. isAudioPlay = false;
  122. $('.weui-mask_transparent').show();
  123. $('.weui-toast').show();
  124. if (jmuxer) {
  125. jmuxer.destroy();
  126. }
  127. jmuxer = new JMuxer({
  128. node: 'playerVideo',
  129. flushingTime: 33,
  130. fps: 30,
  131. mode: 'video',
  132. debug: false
  133. });
  134. myVideo.play();
  135. isWaitSps = true;
  136. })
  137. //画图形
  138. var draw_graph = function (graphType, obj) {
  139. //把蒙版放于画板上面
  140. $("#container").css("z-index", 30);
  141. $("#dedit").css("z-index", 20);
  142. var canDraw = false;
  143. //鼠标按下获取 开始xy开始画图
  144. var ongoingTouches = [];
  145. var touchstart = function (e) {
  146. $('.control-right-img').attr({
  147. "data-id": "2"
  148. })
  149. $(".leftmains").css({
  150. "right": "-4rem"
  151. })
  152. var touchfor = e.originalEvent.changedTouches; //for 的手指数组
  153. //是否横屏
  154. for (var i = 0; i < touchfor.length; i++) {
  155. var acrossWidthX = touchfor[i].pageY * (videoHeight / voheight);
  156. var acrossHeightY = videoWidth - touchfor[i].pageX * (videoWidth / vowidth);
  157. var verticalWidthX = touchfor[i].pageX * (videoWidth / vowidth);
  158. var verticalHeightY = touchfor[i].pageY * (videoHeight / voheight);
  159. var idx = ongoingTouches.findIndex(function (ele) {
  160. return ele.identifier === touchfor[i].identifier
  161. })
  162. if (idx < 0) {
  163. ongoingTouches.push(touchfor[i]);
  164. }
  165. var ping = resolving == 0 ?
  166. { "data": { "action": 0, "count": ongoingTouches.length, "pointerId": touchfor[i].identifier, "x": acrossWidthX.toFixed(2), "y": acrossHeightY.toFixed(2) }, "type": "event" } :
  167. { "data": { "action": 0, "count": ongoingTouches.length, "pointerId": touchfor[i].identifier, "x": verticalWidthX.toFixed(2), "y": verticalHeightY.toFixed(2) }, "type": "event" };
  168. wsss.send(JSON.stringify(ping));
  169. }
  170. canDraw = true;
  171. };
  172. //鼠标离开 把蒙版canvas的图片生成到canvas中
  173. var touchend = function (e) {
  174. var touchfor = e.originalEvent.changedTouches; //for 的手指数组
  175. //是否横屏
  176. for (var i = 0; i < touchfor.length; i++) {
  177. var acrossWidthX = touchfor[i].pageY * (videoHeight / voheight);
  178. var acrossHeightY = videoWidth - touchfor[i].pageX * (videoWidth / vowidth);
  179. var verticalWidthX = touchfor[i].pageX * (videoWidth / vowidth);
  180. var verticalHeightY = touchfor[i].pageY * (videoHeight / voheight);
  181. var ping = resolving == 0 ?
  182. { "data": { "action": 1, "count": ongoingTouches.length, "pointerId": touchfor[i].identifier, "x": acrossWidthX.toFixed(2), "y": acrossHeightY.toFixed(2) }, "type": "event" } :
  183. { "data": { "action": 1, "count": ongoingTouches.length, "pointerId": touchfor[i].identifier, "x": verticalWidthX.toFixed(2), "y": verticalHeightY.toFixed(2) }, "type": "event" };
  184. wsss.send(JSON.stringify(ping));
  185. ongoingTouches.forEach(function (item, index) {
  186. if (item.identifier === touchfor[i].identifier) {
  187. ongoingTouches.splice(index, 1)
  188. }
  189. })
  190. }
  191. canDraw = false;
  192. };
  193. //清空层 云手机超出屏幕的开关
  194. var clearContext = function () {
  195. canDraw = false;
  196. }
  197. // 鼠标移动
  198. var touchmove = function (e) {
  199. var touchfor = e.originalEvent.targetTouches; //for 的手指数组
  200. for (var i = 0; i < touchfor.length; i++) {
  201. var acrossWidthX = touchfor[i].pageY * (videoHeight / voheight);
  202. var acrossHeightY = videoWidth - touchfor[i].pageX * (videoWidth / vowidth);
  203. var verticalWidthX = touchfor[i].pageX * (videoWidth / vowidth);
  204. var verticalHeightY = touchfor[i].pageY * (videoHeight / voheight);
  205. var ping = resolving == 0 ?
  206. { "data": { "action": 2, "count": touchfor.length, "pointerId": touchfor[i].identifier, "x": acrossWidthX.toFixed(2), "y": acrossHeightY.toFixed(2) }, "type": "event" } :
  207. { "data": { "action": 2, "count": touchfor.length, "pointerId": touchfor[i].identifier, "x": verticalWidthX.toFixed(2), "y": verticalHeightY.toFixed(2) }, "type": "event" };
  208. wsss.send(JSON.stringify(ping));
  209. }
  210. };
  211. //鼠标离开区域以外 除了涂鸦 都清空
  212. var mouseout = function () {
  213. if (graphType != 'handwriting') {
  214. clearContext();
  215. }
  216. }
  217. $(canvas_bak).unbind();
  218. $(canvas_bak).bind('touchstart', touchstart);
  219. $(canvas_bak).bind('touchmove', touchmove);
  220. $(canvas_bak).bind('touchend', touchend);
  221. $(canvas_bak).bind('mouseout', mouseout);
  222. }
  223. // 获取url中"?"符后的字串
  224. function GetRequest() {
  225. var url = location.search;
  226. var obj = new Object();
  227. if (url.indexOf("?") != -1) {
  228. var str = url.substr(1);
  229. strs = str.split("&");
  230. for (var i = 0; i < strs.length; i++) {
  231. obj[strs[i].split("=")[0]] = (strs[i].split("=")[1]);
  232. }
  233. }
  234. return obj;
  235. }