WXdraw.js 9.6 KB

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