WXdraw.js 8.7 KB

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