WXdraw.js 9.8 KB

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