WXdraw.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. if (result.type === 'payInitiateEvent') {
  110. var url = window.location.href;
  111. url = url.split('/');
  112. var baseUrl = url[0] + '//' + url[2];
  113. $.ajax({
  114. url: baseUrl + '/api/pay/third/order',
  115. data: JSON.stringify({
  116. orderNum: result.data.orderNum,
  117. orderAmount: result.data.orderAmount,
  118. appKey: result.data.appKey,
  119. }),
  120. type: 'POST',
  121. dataType: 'json',
  122. contentType: 'application/json;charset=UTF-8',
  123. success: function (res) {
  124. if(result.data.payType === 1) { // 微信
  125. if (window.__wxjs_environment === 'miniprogram') { // 小程序
  126. // copyUrl(result.data.payUrl);
  127. } else {
  128. window.location.href = result.data.payUrl
  129. }
  130. } else {
  131. window.location.href = result.data.payUrl
  132. }
  133. },
  134. });
  135. return
  136. }
  137. }
  138. }
  139. $('body').on("click", function () {
  140. draw_graph('pencil', this)
  141. })
  142. //剪切板
  143. $(".upload").on("click", function () {
  144. var texts = $(this).attr("data-text")
  145. if (texts == "uploads") {
  146. $(".mainbox").css({
  147. "display": "block"
  148. })
  149. $(".sbox").css({
  150. "display": "none"
  151. })
  152. }
  153. })
  154. //home 控制home
  155. $(".botmat1img").on("click", function () {
  156. var codes = $(this).attr("data-text")
  157. if (codes == "home" && isControl) {
  158. wsss.send(ExexuteKeyBoard(3));
  159. } else if (codes == "return" && isControl) {
  160. wsss.send(ExexuteKeyBoard(4));
  161. } else if (codes == "gengduo" && isControl) {
  162. wsss.send(ExexuteKeyBoard(187));
  163. }
  164. })
  165. // 高清控制
  166. $(".PictureQuality").on("click", function () {
  167. if (!isControl) {
  168. return
  169. }
  170. $(this).addClass("avit").siblings().removeClass('avit')
  171. var id = $(this).attr("data-id")
  172. var cmd = {
  173. type: "switchSharpness",
  174. };
  175. decodeWoker.postMessage(cmd);//通知解码器worker切换分辨率
  176. var buffer = makeSharpness(Number(id));
  177. webSocketWorker.postMessage(buffer);
  178. })
  179. var canDraw = false;
  180. //画图形
  181. var draw_graph = function (graphType) {
  182. //把蒙版放于画板上面
  183. $("#container").css("z-index", 30);
  184. $("#dedit").css("z-index", 20);
  185. // 先画在蒙版上 再复制到画布上
  186. //鼠标按下获取 开始xy开始画图
  187. var ongoingTouches = [];
  188. var touchstart = function (e) {
  189. if (!isControl) {
  190. return
  191. }
  192. $('.control-right-img').attr({
  193. "data-id": "2"
  194. })
  195. $(".leftmains").css({
  196. "right": "-4rem"
  197. })
  198. var touchfor = e.originalEvent.changedTouches; //for 的手指数组
  199. //是否横屏
  200. for (var i = 0; i < touchfor.length; i++) {
  201. var acrossWidthX = touchfor[i].pageY * (videoHeight / voheight);
  202. var acrossHeightY = videoWidth - touchfor[i].pageX * (videoWidth / vowidth);
  203. var verticalWidthX = touchfor[i].pageX * (videoWidth / vowidth);
  204. var verticalHeightY = touchfor[i].pageY * (videoHeight / voheight);
  205. var idx = ongoingTouches.findIndex(function (ele) {
  206. return ele.identifier === touchfor[i].identifier
  207. })
  208. if (idx < 0) {
  209. ongoingTouches.push(touchfor[i]);
  210. }
  211. var ping = resolving == 0 ?
  212. { "data": { "action": 0, "count": ongoingTouches.length, "pointerId": touchfor[i].identifier, "x": acrossWidthX.toFixed(2), "y": acrossHeightY.toFixed(2) }, "type": "event" } :
  213. { "data": { "action": 0, "count": ongoingTouches.length, "pointerId": touchfor[i].identifier, "x": verticalWidthX.toFixed(2), "y": verticalHeightY.toFixed(2) }, "type": "event" };
  214. wsss.send(JSON.stringify(ping));
  215. }
  216. canDraw = true;
  217. };
  218. //鼠标离开 把蒙版canvas的图片生成到canvas中
  219. var touchend = function (e) {
  220. if (!isControl) {
  221. return
  222. }
  223. var touchfor = e.originalEvent.changedTouches; //for 的手指数组
  224. //是否横屏
  225. for (var i = 0; i < touchfor.length; i++) {
  226. var acrossWidthX = touchfor[i].pageY * (videoHeight / voheight);
  227. var acrossHeightY = videoWidth - touchfor[i].pageX * (videoWidth / vowidth);
  228. var verticalWidthX = touchfor[i].pageX * (videoWidth / vowidth);
  229. var verticalHeightY = touchfor[i].pageY * (videoHeight / voheight);
  230. var ping = resolving == 0 ?
  231. { "data": { "action": 1, "count": ongoingTouches.length, "pointerId": touchfor[i].identifier, "x": acrossWidthX.toFixed(2), "y": acrossHeightY.toFixed(2) }, "type": "event" } :
  232. { "data": { "action": 1, "count": ongoingTouches.length, "pointerId": touchfor[i].identifier, "x": verticalWidthX.toFixed(2), "y": verticalHeightY.toFixed(2) }, "type": "event" };
  233. wsss.send(JSON.stringify(ping));
  234. ongoingTouches.forEach(function (item, index) {
  235. if (item.identifier === touchfor[i].identifier) {
  236. ongoingTouches.splice(index, 1)
  237. }
  238. })
  239. }
  240. canDraw = false;
  241. };
  242. //清空层 云手机超出屏幕的开关
  243. var clearContext = function () {
  244. canDraw = false;
  245. }
  246. // 鼠标移动
  247. var touchmove = function (e) {
  248. if (!isControl) {
  249. return
  250. }
  251. var touchfor = e.originalEvent.targetTouches; //for 的手指数组
  252. for (var i = 0; i < touchfor.length; i++) {
  253. var acrossWidthX = touchfor[i].pageY * (videoHeight / voheight);
  254. var acrossHeightY = videoWidth - touchfor[i].pageX * (videoWidth / vowidth);
  255. var verticalWidthX = touchfor[i].pageX * (videoWidth / vowidth);
  256. var verticalHeightY = touchfor[i].pageY * (videoHeight / voheight);
  257. var ping = resolving == 0 ?
  258. { "data": { "action": 2, "count": touchfor.length, "pointerId": touchfor[i].identifier, "x": acrossWidthX.toFixed(2), "y": acrossHeightY.toFixed(2) }, "type": "event" } :
  259. { "data": { "action": 2, "count": touchfor.length, "pointerId": touchfor[i].identifier, "x": verticalWidthX.toFixed(2), "y": verticalHeightY.toFixed(2) }, "type": "event" };
  260. wsss.send(JSON.stringify(ping));
  261. }
  262. };
  263. //鼠标离开区域以外 除了涂鸦 都清空
  264. var mouseout = function () {
  265. if (graphType != 'handwriting') {
  266. clearContext();
  267. }
  268. }
  269. $(canvas_bak).unbind();
  270. $(canvas_bak).bind('touchstart', touchstart);
  271. $(canvas_bak).bind('touchmove', touchmove);
  272. $(canvas_bak).bind('touchend', touchend);
  273. $(canvas_bak).bind('mouseout', mouseout);
  274. }
  275. function GetRequest() {
  276. var url = location.search; // 获取url中"?"符后的字串
  277. var obj = new Object();
  278. if (url.indexOf("?") != -1) {
  279. var str = url.substr(1);
  280. strs = str.split("&");
  281. for (var i = 0; i < strs.length; i++) {
  282. obj[strs[i].split("=")[0]] = (strs[i].split("=")[1]);
  283. }
  284. }
  285. return obj;
  286. }