WXdraw.js 7.3 KB

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