WXdraw.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. document.addEventListener('UniAppJSBridgeReady', function () {
  83. uni.navigateTo({
  84. url: '/pages/index/index'
  85. });
  86. })
  87. }
  88. }
  89. };
  90. wsss.onmessage = function (res) {
  91. var result = typeof res.data === 'string' ? JSON.parse(res.data) : res.data
  92. if (result.type === 'cutting') {
  93. if (result.data.status === 0) {
  94. $.toast('复制成功', "text");
  95. } else {
  96. $.toast(result.msg, "text");
  97. }
  98. return
  99. }
  100. if (result.type === 'forwardMsg' && isAuth !== 'none') {
  101. if (result.data.code === 4000 || result.data.code === 3000) {
  102. if (isAuth === 'huo') {
  103. $.confirm("授权方已收回控制权,您进入观看屏幕模式", function () {
  104. //点击确认后的回调函数
  105. isControl = false;
  106. }, function () {
  107. isControl = false;
  108. //点击取消后的回调函数
  109. if (navigator.userAgent.toLowerCase().includes('toutiaomicroapp')) {
  110. tt.miniProgram.switchTab({
  111. url: '/pages/home/home'
  112. })
  113. } else {
  114. wx.miniProgram.switchTab({
  115. url: '/pages/home/home'
  116. })
  117. }
  118. });
  119. } else {
  120. $.confirm("当前云手机正在授控,是否请求获取云手机控制权?", function () {
  121. //点击确认后的回调函数
  122. var ping = {
  123. "type": "forwardMsg",
  124. "data": {
  125. "code": "5000",
  126. "desc": "控制权限收回" // 可选
  127. }
  128. }
  129. wsss.send(JSON.stringify(ping));
  130. isControl = true;
  131. }, function () {
  132. //点击取消后的回调函数
  133. isControl = false;
  134. });
  135. }
  136. }
  137. return
  138. }
  139. }
  140. }
  141. $('body').on("click", function () {
  142. draw_graph('pencil', this)
  143. })
  144. //剪切板
  145. $(".upload").on("click", function () {
  146. var texts = $(this).attr("data-text")
  147. if (texts == "uploads") {
  148. $(".mainbox").css({
  149. "display": "block"
  150. })
  151. $(".sbox").css({
  152. "display": "none"
  153. })
  154. }
  155. })
  156. //home 控制home
  157. $(".botmat1img").on("click", function () {
  158. var codes = $(this).attr("data-text")
  159. if (codes == "home" && isControl) {
  160. wsss.send(ExexuteKeyBoard(3));
  161. } else if (codes == "return" && isControl) {
  162. wsss.send(ExexuteKeyBoard(4));
  163. } else if (codes == "gengduo" && isControl) {
  164. wsss.send(ExexuteKeyBoard(187));
  165. }
  166. })
  167. // 高清控制
  168. $(".PictureQuality").on("click", function () {
  169. if (!isControl) {
  170. return
  171. }
  172. $(this).addClass("avit").siblings().removeClass('avit')
  173. var id = $(this).attr("data-id")
  174. var cmd = {
  175. type: "switchSharpness",
  176. };
  177. decodeWoker.postMessage(cmd);//通知解码器worker切换分辨率
  178. var buffer = makeSharpness(Number(id));
  179. webSocketWorker.postMessage(buffer);
  180. })
  181. var canDraw = false;
  182. //画图形
  183. var draw_graph = function (graphType) {
  184. //把蒙版放于画板上面
  185. $("#container").css("z-index", 30);
  186. $("#dedit").css("z-index", 20);
  187. // 先画在蒙版上 再复制到画布上
  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. function GetRequest() {
  278. var url = location.search; // 获取url中"?"符后的字串
  279. var obj = new Object();
  280. if (url.indexOf("?") != -1) {
  281. var str = url.substr(1);
  282. strs = str.split("&");
  283. for (var i = 0; i < strs.length; i++) {
  284. obj[strs[i].split("=")[0]] = (strs[i].split("=")[1]);
  285. }
  286. }
  287. return obj;
  288. }