WXdraw.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. quit();
  74. }
  75. };
  76. wsss.onmessage = function (res) {
  77. var result = typeof res.data === 'string' ? JSON.parse(res.data) : res.data
  78. if (result.type === 'cutting') {
  79. if (result.data.status === 0) {
  80. $.toast('复制成功', "text");
  81. } else {
  82. $.toast(result.msg, "text");
  83. }
  84. return
  85. }
  86. if (result.type === 'forwardMsg' && isAuth !== 'none') {
  87. if (result.data.code === 4000 || result.data.code === 3000) {
  88. if (isAuth === 'huo') {
  89. $.confirm("授权方已收回控制权,您进入观看屏幕模式", function () {
  90. //点击确认后的回调函数
  91. isControl = false;
  92. }, function () {
  93. isControl = false;
  94. //点击取消后的回调函数
  95. quit();
  96. });
  97. } else {
  98. $.confirm("当前云手机正在授控,是否请求获取云手机控制权?", function () {
  99. //点击确认后的回调函数
  100. var ping = {
  101. "type": "forwardMsg",
  102. "data": {
  103. "code": "5000",
  104. "desc": "控制权限收回" // 可选
  105. }
  106. }
  107. wsss.send(JSON.stringify(ping));
  108. isControl = true;
  109. }, function () {
  110. //点击取消后的回调函数
  111. isControl = false;
  112. });
  113. }
  114. }
  115. return
  116. }
  117. }
  118. }
  119. $('body').on("click", function () {
  120. draw_graph('pencil', this)
  121. })
  122. //剪切板
  123. $(".upload").on("click", function () {
  124. var texts = $(this).attr("data-text")
  125. if (texts == "uploads") {
  126. $(".mainbox").css({
  127. "display": "block"
  128. })
  129. $(".sbox").css({
  130. "display": "none"
  131. })
  132. }
  133. })
  134. //home 控制home
  135. $(".botmat1img").on("click", function () {
  136. var codes = $(this).attr("data-text")
  137. if (codes == "home" && isControl) {
  138. wsss.send(ExexuteKeyBoard(3));
  139. } else if (codes == "return" && isControl) {
  140. wsss.send(ExexuteKeyBoard(4));
  141. } else if (codes == "gengduo" && isControl) {
  142. wsss.send(ExexuteKeyBoard(187));
  143. }
  144. })
  145. // 高清控制
  146. $(".PictureQuality").on("click", function () {
  147. if (!isControl) {
  148. return
  149. }
  150. $(this).addClass("avit").siblings().removeClass('avit')
  151. var id = $(this).attr("data-id")
  152. var cmd = {
  153. type: "switchSharpness",
  154. };
  155. decodeWoker.postMessage(cmd);//通知解码器worker切换分辨率
  156. var buffer = makeSharpness(Number(id));
  157. webSocketWorker.postMessage(buffer);
  158. })
  159. var canDraw = false;
  160. //画图形
  161. var draw_graph = function (graphType) {
  162. //把蒙版放于画板上面
  163. $("#container").css("z-index", 30);
  164. $("#dedit").css("z-index", 20);
  165. // 先画在蒙版上 再复制到画布上
  166. //鼠标按下获取 开始xy开始画图
  167. var ongoingTouches = [];
  168. var touchstart = function (e) {
  169. if (!isControl) {
  170. return
  171. }
  172. $('.control-right-img').attr({
  173. "data-id": "2"
  174. })
  175. $(".leftmains").css({
  176. "right": "-4rem"
  177. })
  178. var touchfor = e.originalEvent.changedTouches; //for 的手指数组
  179. //是否横屏
  180. for (var i = 0; i < touchfor.length; i++) {
  181. var acrossWidthX = touchfor[i].pageY * (videoHeight / voheight);
  182. var acrossHeightY = videoWidth - touchfor[i].pageX * (videoWidth / vowidth);
  183. var verticalWidthX = touchfor[i].pageX * (videoWidth / vowidth);
  184. var verticalHeightY = touchfor[i].pageY * (videoHeight / voheight);
  185. var idx = ongoingTouches.findIndex(function (ele) {
  186. return ele.identifier === touchfor[i].identifier
  187. })
  188. if (idx < 0) {
  189. ongoingTouches.push(touchfor[i]);
  190. }
  191. var ping = resolving == 0 ?
  192. { "data": { "action": 0, "count": ongoingTouches.length, "pointerId": touchfor[i].identifier, "x": acrossWidthX.toFixed(2), "y": acrossHeightY.toFixed(2) }, "type": "event" } :
  193. { "data": { "action": 0, "count": ongoingTouches.length, "pointerId": touchfor[i].identifier, "x": verticalWidthX.toFixed(2), "y": verticalHeightY.toFixed(2) }, "type": "event" };
  194. wsss.send(JSON.stringify(ping));
  195. }
  196. canDraw = true;
  197. };
  198. //鼠标离开 把蒙版canvas的图片生成到canvas中
  199. var touchend = function (e) {
  200. if (!isControl) {
  201. return
  202. }
  203. var touchfor = e.originalEvent.changedTouches; //for 的手指数组
  204. //是否横屏
  205. for (var i = 0; i < touchfor.length; i++) {
  206. var acrossWidthX = touchfor[i].pageY * (videoHeight / voheight);
  207. var acrossHeightY = videoWidth - touchfor[i].pageX * (videoWidth / vowidth);
  208. var verticalWidthX = touchfor[i].pageX * (videoWidth / vowidth);
  209. var verticalHeightY = touchfor[i].pageY * (videoHeight / voheight);
  210. var ping = resolving == 0 ?
  211. { "data": { "action": 1, "count": ongoingTouches.length, "pointerId": touchfor[i].identifier, "x": acrossWidthX.toFixed(2), "y": acrossHeightY.toFixed(2) }, "type": "event" } :
  212. { "data": { "action": 1, "count": ongoingTouches.length, "pointerId": touchfor[i].identifier, "x": verticalWidthX.toFixed(2), "y": verticalHeightY.toFixed(2) }, "type": "event" };
  213. wsss.send(JSON.stringify(ping));
  214. ongoingTouches.forEach(function (item, index) {
  215. if (item.identifier === touchfor[i].identifier) {
  216. ongoingTouches.splice(index, 1)
  217. }
  218. })
  219. }
  220. canDraw = false;
  221. };
  222. //清空层 云手机超出屏幕的开关
  223. var clearContext = function () {
  224. canDraw = false;
  225. }
  226. // 鼠标移动
  227. var touchmove = function (e) {
  228. if (!isControl) {
  229. return
  230. }
  231. var touchfor = e.originalEvent.targetTouches; //for 的手指数组
  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": 2, "count": touchfor.length, "pointerId": touchfor[i].identifier, "x": acrossWidthX.toFixed(2), "y": acrossHeightY.toFixed(2) }, "type": "event" } :
  239. { "data": { "action": 2, "count": touchfor.length, "pointerId": touchfor[i].identifier, "x": verticalWidthX.toFixed(2), "y": verticalHeightY.toFixed(2) }, "type": "event" };
  240. wsss.send(JSON.stringify(ping));
  241. }
  242. };
  243. //鼠标离开区域以外 除了涂鸦 都清空
  244. var mouseout = function () {
  245. if (graphType != 'handwriting') {
  246. clearContext();
  247. }
  248. }
  249. $(canvas_bak).unbind();
  250. $(canvas_bak).bind('touchstart', touchstart);
  251. $(canvas_bak).bind('touchmove', touchmove);
  252. $(canvas_bak).bind('touchend', touchend);
  253. $(canvas_bak).bind('mouseout', mouseout);
  254. }
  255. function GetRequest() {
  256. var url = location.search; // 获取url中"?"符后的字串
  257. var obj = new Object();
  258. if (url.indexOf("?") != -1) {
  259. var str = url.substr(1);
  260. strs = str.split("&");
  261. for (var i = 0; i < strs.length; i++) {
  262. obj[strs[i].split("=")[0]] = (strs[i].split("=")[1]);
  263. }
  264. }
  265. return obj;
  266. }