WXdraw.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 && decodeURIComponent(cardToken);
  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 + "&type=directives" : "ws://" + form.domainName + "?cardIp=" + form.ip + "&token=" + cardToken + "&type=directives";
  29. var videoWidth = Number(parameters['resolvingPower']) ? Number(parameters['resolvingPower']) : 720
  30. var videoHeight = videoWidth === 720 ? 1280 : 1920
  31. var isControl = true; // 是否是观看模式
  32. var isAuth = parameters['authPhone']; // 是否是获取的云手机
  33. var wsss, 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. var pings2 = {
  64. "type": "forwardMsg",
  65. "data": {
  66. "code": "3000",
  67. "desc": "询问是否有在控制" // 可选
  68. }
  69. }
  70. wsss.send(JSON.stringify(pings2));
  71. };
  72. wsss.onerror = function (evt) {
  73. wsss.close();
  74. throttle(doConnect, 100);
  75. if (errorTime > 1000) {
  76. if (navigator.userAgent.toLowerCase().includes('toutiaomicroapp')) {
  77. tt.miniProgram.switchTab({
  78. url: '/pages/home/home'
  79. })
  80. } else {
  81. wx.miniProgram.switchTab({
  82. url: '/pages/home/home'
  83. })
  84. }
  85. }
  86. };
  87. wsss.onmessage = function (res) {
  88. var result = typeof res.data === 'string' ? JSON.parse(res.data) : res.data
  89. if (result.type === 'cutting') {
  90. if (result.data.status === 0) {
  91. $.toast('复制成功', "text");
  92. } else {
  93. $.toast(result.msg, "text");
  94. }
  95. return
  96. }
  97. if (result.type === 'forwardMsg' && isAuth !== 'none') {
  98. if (result.data.code === 4000 || result.data.code === 3000) {
  99. if (isAuth === 'huo') {
  100. $.confirm("授权方已收回控制权,您进入观看屏幕模式", function () {
  101. //点击确认后的回调函数
  102. isControl = false;
  103. }, function () {
  104. isControl = false;
  105. //点击取消后的回调函数
  106. if (navigator.userAgent.toLowerCase().includes('toutiaomicroapp')) {
  107. tt.miniProgram.switchTab({
  108. url: '/pages/home/home'
  109. })
  110. } else {
  111. wx.miniProgram.switchTab({
  112. url: '/pages/home/home'
  113. })
  114. }
  115. });
  116. } else {
  117. $.confirm("当前云手机正在授控,是否请求获取云手机控制权?", function () {
  118. //点击确认后的回调函数
  119. var ping = {
  120. "type": "forwardMsg",
  121. "data": {
  122. "code": "5000",
  123. "desc": "控制权限收回" // 可选
  124. }
  125. }
  126. wsss.send(JSON.stringify(ping));
  127. isControl = true;
  128. }, function () {
  129. //点击取消后的回调函数
  130. isControl = false;
  131. });
  132. }
  133. }
  134. return
  135. }
  136. }
  137. }
  138. $('body').on("click", function () {
  139. draw_graph('pencil');
  140. })
  141. //剪切板
  142. $(".upload").on("click", function () {
  143. var texts = $(this).attr("data-text")
  144. if (texts == "uploads") {
  145. $(".mainbox").css({
  146. "display": "block"
  147. })
  148. $(".sbox").css({
  149. "display": "none"
  150. })
  151. }
  152. })
  153. //home 控制home
  154. $(".botmat1img").on("click", function () {
  155. var codes = $(this).attr("data-text")
  156. if (codes == "home" && isControl) {
  157. wsss.send(ExexuteKeyBoard(3));
  158. } else if (codes == "return" && isControl) {
  159. wsss.send(ExexuteKeyBoard(4));
  160. } else if (codes == "gengduo" && isControl) {
  161. wsss.send(ExexuteKeyBoard(187));
  162. }
  163. })
  164. // 高清控制
  165. $(".PictureQuality").on("click", function () {
  166. if (!isControl) {
  167. return
  168. }
  169. $(this).addClass("avit").siblings().removeClass('avit')
  170. var id = $(this).attr("data-id")
  171. var buffer = makeSharpness(Number(id));
  172. ws.send(buffer);
  173. isFeed = false;
  174. isAudioPlay = false;
  175. $('.weui-mask_transparent').show();
  176. $('.weui-toast').show();
  177. if (jmuxer) {
  178. jmuxer.destroy();
  179. }
  180. jmuxer = new JMuxer({
  181. node: 'playerVideo',
  182. flushingTime: 33,
  183. fps: 30,
  184. mode: 'video',
  185. debug: false
  186. });
  187. myVideo.play();
  188. isWaitSps = true;
  189. })
  190. //画图形
  191. var draw_graph = function (graphType, obj) {
  192. //把蒙版放于画板上面
  193. $("#container").css("z-index", 30);
  194. $("#dedit").css("z-index", 20);
  195. var canDraw = false;
  196. //鼠标按下获取 开始xy开始画图
  197. var ongoingTouches = [];
  198. var touchstart = function (e) {
  199. if (!isControl) {
  200. return
  201. }
  202. $('.control-right-img').attr({
  203. "data-id": "2"
  204. })
  205. $(".leftmains").css({
  206. "right": "-4rem"
  207. })
  208. var touchfor = e.originalEvent.changedTouches; //for 的手指数组
  209. //是否横屏
  210. for (var i = 0; i < touchfor.length; i++) {
  211. var acrossWidthX = touchfor[i].pageY * (videoHeight / voheight);
  212. var acrossHeightY = videoWidth - touchfor[i].pageX * (videoWidth / vowidth);
  213. var verticalWidthX = touchfor[i].pageX * (videoWidth / vowidth);
  214. var verticalHeightY = touchfor[i].pageY * (videoHeight / voheight);
  215. var idx = ongoingTouches.findIndex(function (ele) {
  216. return ele.identifier === touchfor[i].identifier
  217. })
  218. if (idx < 0) {
  219. ongoingTouches.push(touchfor[i]);
  220. }
  221. var ping = resolving == 0 ?
  222. { "data": { "action": 0, "count": ongoingTouches.length, "pointerId": touchfor[i].identifier, "x": acrossWidthX.toFixed(2), "y": acrossHeightY.toFixed(2) }, "type": "event" } :
  223. { "data": { "action": 0, "count": ongoingTouches.length, "pointerId": touchfor[i].identifier, "x": verticalWidthX.toFixed(2), "y": verticalHeightY.toFixed(2) }, "type": "event" };
  224. wsss.send(JSON.stringify(ping));
  225. }
  226. canDraw = true;
  227. };
  228. //鼠标离开 把蒙版canvas的图片生成到canvas中
  229. var touchend = function (e) {
  230. if (!isControl) {
  231. return
  232. }
  233. var touchfor = e.originalEvent.changedTouches; //for 的手指数组
  234. //是否横屏
  235. for (var i = 0; i < touchfor.length; i++) {
  236. var acrossWidthX = touchfor[i].pageY * (videoHeight / voheight);
  237. var acrossHeightY = videoWidth - touchfor[i].pageX * (videoWidth / vowidth);
  238. var verticalWidthX = touchfor[i].pageX * (videoWidth / vowidth);
  239. var verticalHeightY = touchfor[i].pageY * (videoHeight / voheight);
  240. var ping = resolving == 0 ?
  241. { "data": { "action": 1, "count": ongoingTouches.length, "pointerId": touchfor[i].identifier, "x": acrossWidthX.toFixed(2), "y": acrossHeightY.toFixed(2) }, "type": "event" } :
  242. { "data": { "action": 1, "count": ongoingTouches.length, "pointerId": touchfor[i].identifier, "x": verticalWidthX.toFixed(2), "y": verticalHeightY.toFixed(2) }, "type": "event" };
  243. wsss.send(JSON.stringify(ping));
  244. ongoingTouches.forEach(function (item, index) {
  245. if (item.identifier === touchfor[i].identifier) {
  246. ongoingTouches.splice(index, 1)
  247. }
  248. })
  249. }
  250. canDraw = false;
  251. };
  252. //清空层 云手机超出屏幕的开关
  253. var clearContext = function () {
  254. canDraw = false;
  255. }
  256. // 鼠标移动
  257. var touchmove = function (e) {
  258. if (!isControl) {
  259. return
  260. }
  261. var touchfor = e.originalEvent.targetTouches; //for 的手指数组
  262. for (var i = 0; i < touchfor.length; i++) {
  263. var acrossWidthX = touchfor[i].pageY * (videoHeight / voheight);
  264. var acrossHeightY = videoWidth - touchfor[i].pageX * (videoWidth / vowidth);
  265. var verticalWidthX = touchfor[i].pageX * (videoWidth / vowidth);
  266. var verticalHeightY = touchfor[i].pageY * (videoHeight / voheight);
  267. var ping = resolving == 0 ?
  268. { "data": { "action": 2, "count": touchfor.length, "pointerId": touchfor[i].identifier, "x": acrossWidthX.toFixed(2), "y": acrossHeightY.toFixed(2) }, "type": "event" } :
  269. { "data": { "action": 2, "count": touchfor.length, "pointerId": touchfor[i].identifier, "x": verticalWidthX.toFixed(2), "y": verticalHeightY.toFixed(2) }, "type": "event" };
  270. wsss.send(JSON.stringify(ping));
  271. }
  272. };
  273. //鼠标离开区域以外 除了涂鸦 都清空
  274. var mouseout = function () {
  275. if (graphType != 'handwriting') {
  276. clearContext();
  277. }
  278. }
  279. $(canvas_bak).unbind();
  280. $(canvas_bak).bind('touchstart', touchstart);
  281. $(canvas_bak).bind('touchmove', touchmove);
  282. $(canvas_bak).bind('touchend', touchend);
  283. $(canvas_bak).bind('mouseout', mouseout);
  284. }
  285. // 获取url中"?"符后的字串
  286. function GetRequest() {
  287. var url = location.search;
  288. var obj = new Object();
  289. if (url.indexOf("?") != -1) {
  290. var str = url.substr(1);
  291. strs = str.split("&");
  292. for (var i = 0; i < strs.length; i++) {
  293. obj[strs[i].split("=")[0]] = (strs[i].split("=")[1]);
  294. }
  295. }
  296. return obj;
  297. }