canvas.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * 创建页面元素
  3. * @param {String} value - 页面元素类型
  4. */
  5. function createElement(value, obj, ind) {
  6. // if (value == "pictureMarker") {
  7. var pictureMarkerHtml =
  8. `
  9. <div class="pictureMarker" style="position: absolute;">
  10. <div class="popup" style="width: 190px;font-size: 16px;text-align: center;
  11. background: url('/uploads/wechat/163607/file/安防展/images/backUIAnchor.png') no-repeat;
  12. background-size: 100% 100%;color: #eee;position: absolute;bottom: 20px;left: -80px;">
  13. <div style="word-wrap: break-word;padding: 10px;">
  14. <div>设备名称 : <span>${obj.name}</span></div>
  15. <div style="${ind == 0 || ind == 1 ? 'display:block;' : 'display:none;'}">状态 : <span style="color:red;">入侵告警</span></div>
  16. <div style="${ind == 2 ? 'display:block;' : 'display:none;'}">状态 : <span style="color:red;">信号丢失</span></div>
  17. <div style="${ind == 3 ? 'display:block;' : 'display:none;'}">状态 : <span style="color:red;">火灾告警</span></div>
  18. </div>
  19. </div>
  20. <div id="box${ind}" style="display:none;position: absolute;height: 40px;width: 40px;z-index:-1;"></div>
  21. <div class="picture" style="height: 30px;width: 30px;"></div>
  22. </div>`;
  23. $('#div3d').append($(pictureMarkerHtml));
  24. // }
  25. createUIAnchor(value, obj, ind);
  26. // 如果闪烁动画/跳跃动画发光开启中,先关闭闪烁动画/跳跃动画发光,再开启图片标注发光
  27. $('.pictureMarker').removeClass('scaleAnimation');
  28. $('.pictureMarker').removeClass('moveAnimation');
  29. $('#box' + ind).css('top', '-7px');
  30. $('#box' + ind).css('left', '-25px');
  31. $('#box' + ind).css('display', 'block');
  32. createCanvas(ind); // 创建圆形扩散效果
  33. }
  34. /**
  35. * 创建UIAnchor界面
  36. * @param {String} value - 界面类型
  37. */
  38. function createUIAnchor(value, obj, ind) {
  39. var anchorCreateJson = {
  40. type: "UIAnchor",
  41. element: null,
  42. parent: obj,
  43. position: null,
  44. pivotPixel: null,
  45. }
  46. // if (value == "pictureMarker") {
  47. anchorCreateJson.element = $(".pictureMarker")[ind];
  48. anchorCreateJson.position = obj.position;
  49. anchorCreateJson.pivotPixel = [parseFloat($(".pictureMarker").css("width")) / 2, parseFloat($(".pictureMarker").css("height"))];
  50. // }
  51. tempTopCard = app.create(anchorCreateJson); // 创建顶牌
  52. tempTopCard.visible = true; // 设置初始顶牌状态
  53. ui.push(tempTopCard)
  54. }
  55. /**
  56. * 创建圆形扩散效果
  57. */
  58. function createCanvas(ind) {
  59. var canvasList = document.getElementById('box' + ind);
  60. var canvas = document.createElement('canvas');
  61. canvasList.appendChild(canvas);
  62. canvas.width = 80;
  63. canvas.height = 80;
  64. var context = canvas.getContext("2d");
  65. var width = 80,
  66. height = 80;
  67. var arr = [];
  68. arr.push({
  69. x: parseInt(canvas.width / 2),
  70. y: parseInt(canvas.height / 2)
  71. });
  72. // 创建构造函数Circle
  73. function Circle(x, y, radius) {
  74. this.xx = x; // 在画布内随机生成x值
  75. this.yy = y;
  76. this.radius = radius;
  77. };
  78. Circle.prototype.radiu = function () {
  79. radius += 0.5; // 每一帧半径增加0.5
  80. if (this.radius > 25) {
  81. radius = 0;
  82. };
  83. };
  84. // 绘制圆形的方法
  85. Circle.prototype.paint = function () {
  86. context.beginPath();
  87. context.arc(this.xx, this.yy, this.radius, 0, Math.PI * 2);
  88. context.closePath();
  89. context.lineWidth = 2; // 线条宽度
  90. context.strokeStyle = 'red'; // 颜色
  91. context.stroke();
  92. };
  93. var newfun = null;
  94. var radius = 0;
  95. function createCircles() {
  96. for (var j = 0; j < arr.length; j++) {
  97. newfun = new Circle(arr[j].x, arr[j].y, radius); //调用构造函数
  98. newfun.paint();
  99. };
  100. newfun.radiu();
  101. };
  102. // 创建临时canvas
  103. var backCanvas = document.createElement('canvas'),
  104. backCtx = backCanvas.getContext('2d');
  105. backCanvas.width = width;
  106. backCanvas.height = height;
  107. // 设置主canvas的绘制透明度
  108. context.globalAlpha = 0.95;
  109. // 显示即将绘制的图像,忽略临时canvas中已存在的图像
  110. backCtx.globalCompositeOperation = 'copy';
  111. var render = function () {
  112. // 先将主canvas的图像缓存到临时canvas中
  113. backCtx.drawImage(canvas, 0, 0, width, height);
  114. // 清除主canvas上的图像
  115. context.clearRect(0, 0, width, height);
  116. // 在主canvas上画新圆
  117. createCircles();
  118. // 等新圆画完后,再把临时canvas的图像绘制回主canvas中
  119. context.drawImage(backCanvas, 0, 0, width, height);
  120. };
  121. // 刷新计时器
  122. setInterval(function () {
  123. render();
  124. }, 20);
  125. }