WXdraw.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 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 deviceBoard = parameters['deviceBoard'];
  30. var videoWidth = deviceBoard.substring(0, 2) === 'MTK' ? 1080 : 720
  31. var videoHeight = deviceBoard.substring(0, 2) === 'MTK' ? 1920 : 1280
  32. var wsss;
  33. var 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 () {
  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 () {
  73. wsss.close();
  74. throttle(doConnect, 100);
  75. if (errorTime > 1000) {
  76. wx.miniProgram.switchTab({
  77. url: '/pages/home/home'
  78. })
  79. }
  80. };
  81. }
  82. $('body').on("click", function () {
  83. draw_graph('pencil', this)
  84. })
  85. //剪切板
  86. $(".upload").on("click", function () {
  87. var texts = $(this).attr("data-text")
  88. if (texts == "uploads") {
  89. $(".mainbox").css({
  90. "display": "block"
  91. })
  92. $(".sbox").css({
  93. "display": "none"
  94. })
  95. }
  96. })
  97. //home 控制home
  98. $(".botmat1img").on("click", function () {
  99. var codes = $(this).attr("data-text")
  100. if (codes == "home") {
  101. wsss.send(ExexuteKeyBoard(3, form.sn));
  102. } else if (codes == "return") {
  103. wsss.send(ExexuteKeyBoard(4, form.sn));
  104. } else if (codes == "gengduo") {
  105. wsss.send(ExexuteKeyBoard(187, form.sn));
  106. }
  107. })
  108. //高清控制
  109. $(".PictureQuality").on("click", function () {
  110. $(this).addClass("avit").siblings().removeClass('avit')
  111. var id = $(this).attr("data-id")
  112. var bitRate = {
  113. "data": {
  114. "bitRate": id
  115. },
  116. "event": "bitRate"
  117. }
  118. wsss.send(JSON.stringify(bitRate))
  119. })
  120. var canDraw = false;
  121. //画图形
  122. var draw_graph = function (graphType) {
  123. //把蒙版放于画板上面
  124. $("#container").css("z-index", 30);
  125. $("#dedit").css("z-index", 20);
  126. // 先画在蒙版上 再复制到画布上
  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. function GetRequest() {
  273. var url = location.search; // 获取url中"?"符后的字串
  274. var obj = new Object();
  275. if (url.indexOf("?") != -1) {
  276. var str = url.substr(1);
  277. strs = str.split("&");
  278. for (var i = 0; i < strs.length; i++) {
  279. obj[strs[i].split("=")[0]] = (strs[i].split("=")[1]);
  280. }
  281. }
  282. return obj;
  283. }