WXdraw.js 7.2 KB

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