WXdraw.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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;
  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;
  15. var url = window.location.href;
  16. url = url.split('/');
  17. var parameters = GetRequest();
  18. var form = {};
  19. form.clientType = parameters["clientType"];
  20. form.cardIp = parameters["cardIp"];
  21. form.port = parameters["port"];
  22. form.sn = parameters["sn"];
  23. form.username = parameters["username"];
  24. form.userCardId = parameters["userCardId"];
  25. form.ip = parameters["ip"];
  26. form.domainName = parameters["domainName"];
  27. var isWSS = false;
  28. var cUrl = isWSS ? "wss://" + form.domainName + "/card?cardIp=" + form.ip : "ws://" + form.domainName + "/card?cardIp=" + form.ip;
  29. var wsss,errorTime = 0;
  30. doConnect();
  31. function throttle(fn, delay) {
  32. let flag = true;
  33. errorTime += delay;
  34. return () => {
  35. if (!flag) return;
  36. flag = false;
  37. timer = setTimeout(() => {
  38. fn();
  39. flag = true;
  40. }, delay);
  41. };
  42. }
  43. function doConnect() {
  44. wsss = new WebSocket(cUrl);
  45. wsss.binaryType = 'arraybuffer';
  46. wsss.onopen = function () {
  47. var pings = {
  48. "event": "getScreenStatus"
  49. }
  50. wsss.send(JSON.stringify(pings));
  51. var bitRate = {
  52. "data": {
  53. "bitRate": 2243000
  54. },
  55. "event": "bitRate"
  56. }
  57. wsss.send(ExexuteMove(JSON.stringify(bitRate), form.sn))
  58. };
  59. wsss.onclose = function (evt) {
  60. console.log(evt);
  61. ws.close();
  62. throttle(doConnect,100);
  63. if(errorTime > 1000){
  64. wx.miniProgram.switchTab({
  65. url: '/pages/home/home'
  66. })
  67. }
  68. };
  69. wsss.onerror = function (evt) {
  70. console.log(evt);
  71. ws.close();
  72. throttle(doConnect,100);
  73. if(errorTime > 1000){
  74. wx.miniProgram.switchTab({
  75. url: '/pages/home/home'
  76. })
  77. }
  78. // $.confirm({
  79. // title: '提示',
  80. // text: '连接断开,是否准备重连?',
  81. // onOK: function () {
  82. // doConnect();
  83. // },
  84. // onCancel: function () {
  85. // wx.miniProgram.switchTab({
  86. // url: '/pages/home/home'
  87. // })
  88. // }
  89. // });
  90. };
  91. }
  92. $('body').on("click", function () {
  93. draw_graph('pencil');
  94. })
  95. //剪切板
  96. $(".upload").on("click", function () {
  97. var texts = $(this).attr("data-text")
  98. if (texts == "uploads") {
  99. $(".mainbox").css({
  100. "display": "block"
  101. })
  102. $(".sbox").css({
  103. "display": "none"
  104. })
  105. }
  106. })
  107. //home 控制home
  108. $(".botmat1img").on("click", function () {
  109. var codes = $(this).attr("data-text")
  110. if (codes == "home") {
  111. wsss.send(ExexuteKeyBoard(3), form.sn);
  112. } else if (codes == "return") {
  113. wsss.send(ExexuteKeyBoard(4), form.sn);
  114. } else if (codes == "gengduo") {
  115. wsss.send(ExexuteKeyBoard(187), form.sn);
  116. }
  117. })
  118. //高清控制
  119. $(".PictureQuality").on("click", function () {
  120. $(this).addClass("avit").siblings().removeClass('avit')
  121. var id = $(this).attr("data-id")
  122. var bitRate = {
  123. "data": {
  124. "bitRate": id
  125. },
  126. "event": "bitRate"
  127. }
  128. wsss.send(ExexuteMove(JSON.stringify(bitRate), form.sn))
  129. })
  130. //画图形
  131. var draw_graph = function (graphType, obj) {
  132. //把蒙版放于画板上面
  133. $("#container").css("z-index", 30);
  134. $("#dedit").css("z-index", 20);
  135. var canDraw = false;
  136. //鼠标按下获取 开始xy开始画图
  137. var touchstart = function (e) {
  138. $('.control-right-img').attr({
  139. "data-id": "2"
  140. })
  141. $(".leftmains").css({
  142. "right": "-4rem"
  143. })
  144. var touchfor = e.originalEvent.targetTouches; //for 的手指数组
  145. //是否横屏
  146. if (resolving == 0) {
  147. var ping;
  148. for (var i = 0; i < touchfor.length; i++) {
  149. var cawidthXs = touchfor[i].pageY * (1280 / voheight)
  150. var caheightYs = 720 - touchfor[i].pageX * (720 / vowidth)
  151. ping = {
  152. "data": {
  153. "action": 0,
  154. "count": touchfor.length,
  155. "pointerId": i,
  156. "x": cawidthXs.toFixed(2),
  157. "y": caheightYs.toFixed(2)
  158. },
  159. "event": "0"
  160. }
  161. }
  162. wsss.send(ExexuteMove(JSON.stringify(ping), form.sn))
  163. } else {
  164. var ping;
  165. for (var i = 0; i < touchfor.length; i++) {
  166. var cawidthXs = touchfor[i].pageX * (720 / vowidth)
  167. var caheightYs = touchfor[i].pageY * (1280 / voheight)
  168. ping = {
  169. "data": {
  170. "action": 0,
  171. "count": touchfor.length,
  172. "pointerId": i,
  173. "x": cawidthXs.toFixed(2),
  174. "y": caheightYs.toFixed(2)
  175. },
  176. "event": "0"
  177. }
  178. }
  179. wsss.send(ExexuteMove(JSON.stringify(ping), form.sn));
  180. }
  181. canDraw = true;
  182. };
  183. //鼠标离开 把蒙版canvas的图片生成到canvas中
  184. var touchend = function (e) {
  185. var touchfor = e.originalEvent.changedTouches; //for 的手指数组
  186. //是否横屏 morePortionUp
  187. if (resolving == 0) {
  188. var ping;
  189. for (var i = 0; i < touchfor.length; i++) {
  190. var cawidthXs = touchfor[i].pageY * (1280 / voheight);
  191. var caheightYs = 720 - touchfor[i].pageX * (720 / vowidth);
  192. ping = {
  193. "data": {
  194. "action": 1,
  195. "count": touchfor.length,
  196. "pointerId": i,
  197. "x": cawidthXs.toFixed(2),
  198. "y": caheightYs.toFixed(2)
  199. },
  200. "event": "1"
  201. }
  202. }
  203. wsss.send(ExexuteMove(JSON.stringify(ping), form.sn));
  204. } else {
  205. var ping;
  206. for (var i = 0; i < touchfor.length; i++) {
  207. var cawidthXs = touchfor[i].pageX * (720 / vowidth);
  208. var caheightYs = touchfor[i].pageY * (1280 / voheight);
  209. ping = {
  210. "data": {
  211. "action": 1,
  212. "count": touchfor.length,
  213. "pointerId": i,
  214. "x": cawidthXs.toFixed(2),
  215. "y": caheightYs.toFixed(2)
  216. },
  217. "event": "1"
  218. }
  219. }
  220. wsss.send(ExexuteMove(JSON.stringify(ping), form.sn))
  221. }
  222. canDraw = false;
  223. };
  224. //清空层 云手机超出屏幕的开关
  225. var clearContext = function () {
  226. canDraw = false;
  227. }
  228. // 鼠标移动
  229. var touchmove = function (e) {
  230. e = e || window.event;
  231. var touchfor = e.originalEvent.targetTouches; //for 的手指数组
  232. var ping;
  233. //是否横屏 morePortionMove
  234. if (resolving == 0) {
  235. for (var i = 0; i < touchfor.length; i++) {
  236. var cawidthXs = touchfor[i].pageY * (1280 / voheight)
  237. var caheightYs = 720 - touchfor[i].pageX * (720 / vowidth)
  238. ping = {
  239. "data": {
  240. "action": 2,
  241. "count": touchfor.length,
  242. "pointerId": i,
  243. "x": cawidthXs.toFixed(2),
  244. "y": caheightYs.toFixed(2)
  245. },
  246. "event": "2"
  247. }
  248. }
  249. wsss.send(ExexuteMove(JSON.stringify(ping), form.sn))
  250. } else {
  251. var ping;
  252. for (var i = 0; i < touchfor.length; i++) {
  253. var cawidthXs = touchfor[i].pageX * (720 / vowidth);
  254. var caheightYs = touchfor[i].pageY * (1280 / voheight);
  255. ping = {
  256. "data": {
  257. "action": 2,
  258. "count": touchfor.length,
  259. "pointerId": i,
  260. "x": cawidthXs.toFixed(2),
  261. "y": caheightYs.toFixed(2)
  262. },
  263. "event": "2"
  264. }
  265. }
  266. wsss.send(ExexuteMove(JSON.stringify(ping), form.sn));
  267. };
  268. };
  269. //鼠标离开区域以外 除了涂鸦 都清空
  270. var mouseout = function () {
  271. if (graphType != 'handwriting') {
  272. clearContext();
  273. }
  274. }
  275. $(canvas_bak).unbind();
  276. $(canvas_bak).bind('touchstart', touchstart);
  277. $(canvas_bak).bind('touchmove', touchmove);
  278. $(canvas_bak).bind('touchend', touchend);
  279. $(canvas_bak).bind('mouseout', mouseout);
  280. }
  281. // 获取url中"?"符后的字串
  282. function GetRequest() {
  283. var url = location.search;
  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. }