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