WXdraw.js 9.9 KB

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