WXdraw.js 7.7 KB

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