materialDynamicImage.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. (function () {
  2. function createDynamicImage(opt) {
  3. let that = this;
  4. // opt = {
  5. // imageWidth: 1000,
  6. // imageHeight: 500,
  7. // canvasWidth: 1000,
  8. // canvasHeight: 500,
  9. // period: 4,
  10. // text:"请18号前往14号服务台"
  11. // }
  12. for (const key in opt) {
  13. if (Object.hasOwnProperty.call(opt, key)) {
  14. this[key] = opt[key];
  15. }
  16. }
  17. this.beforeTime = new Date().getTime();
  18. this.time = new Date().getTime();
  19. this.period = this.period * 1000 // 循环周期,单位:秒
  20. // this.interval = this.interval*1000 // 时间间隔,单位:秒
  21. // this.text = "请18号"
  22. this.textImage = this.createTextImage();
  23. // this.canvas = this.createCanvas();
  24. return new Promise((resolve, reject) => {
  25. that.textImage.onload = function () {
  26. let t = new Cesium.ImageMaterialProperty({
  27. // image: new Cesium.CallbackProperty(that.drawCanvas.bind(that), false),
  28. image: that.textImage,
  29. transparent: true
  30. })
  31. resolve(t)
  32. }
  33. });
  34. }
  35. createDynamicImage.prototype.createTextImage = function () {
  36. var canvas = document.createElement('canvas');
  37. canvas.width = this.imageWidth || 1000;
  38. canvas.height = this.imageHeight || 500;
  39. var context = canvas.getContext('2d');
  40. context.clearRect(0, 0, canvas.width, canvas.height);
  41. context.font = this.textStyle;
  42. context.fillStyle = "red";
  43. context.fillText(this.text, 0, 100);
  44. let img = new Image();
  45. img.src = canvas.toDataURL("image/png");
  46. // img.src = "/static/image/skyscenery/cloud.png"
  47. // let image = canvas.toDataURL("image/png").replace("image/png", "image/octet-stream");
  48. // let link = document.createElement("a");
  49. // let blob = dataURLtoBlob(image);
  50. // let objurl = URL.createObjectURL(blob);
  51. // link.download = "scene.png";
  52. // link.href = objurl;
  53. // link.click();
  54. // function dataURLtoBlob(dataurl) {
  55. // let arr = dataurl.split(','),
  56. // mime = arr[0].match(/:(.*?);/)[1],
  57. // bstr = atob(arr[1]),
  58. // n = bstr.length,
  59. // u8arr = new Uint8Array(n);
  60. // while (n--) {
  61. // u8arr[n] = bstr.charCodeAt(n);
  62. // }
  63. // return new Blob([u8arr], {
  64. // type: mime
  65. // });
  66. // };
  67. // canvas.style.position = "absolute"
  68. // canvas.style.top = "0px"
  69. // canvas.style.left = "0px"
  70. // canvas.style.zIndex = "1"
  71. // document.body.appendChild(canvas)
  72. return img
  73. }
  74. createDynamicImage.prototype.createCanvas = function () {
  75. var canvas = document.createElement('canvas');
  76. canvas.width = this.canvasWidth || 1000;
  77. canvas.height = this.canvasHeight || 500;
  78. canvas.style.position = "absolute"
  79. canvas.style.top = "0px"
  80. canvas.style.left = "0px"
  81. canvas.style.zIndex = "1"
  82. document.body.appendChild(canvas)
  83. return canvas
  84. }
  85. createDynamicImage.prototype.beforeTime = null
  86. createDynamicImage.prototype.drawCanvas = function () {
  87. let that = this;
  88. let goTime = new Date().getTime() - that.time;
  89. // if (new Date().getTime() - this.beforeTime < 400) {
  90. // return this.canvas;
  91. // } else {
  92. // this.beforeTime = new Date().getTime()
  93. // }
  94. // let ctx = that.canvas.getContext("2d");
  95. var canvas = document.getElementById("canvas-b");
  96. var ctx = canvas.getContext('2d');
  97. ctx.clearRect(0, 0, that.canvasWidth, that.canvasHeight);
  98. ctx.rect(0, 0, canvas.width, canvas.height);
  99. ctx.fillStyle = "#000000";
  100. ctx.fill();
  101. // img 规定要使用的图像、画布或视频。
  102. // sx 开始剪切的 x 坐标位置。
  103. // sy 开始剪切的 y 坐标位置。
  104. // swidth 被剪切图像的宽度。
  105. // sheight 被剪切图像的高度。
  106. // x 在画布上放置图像的 x 坐标位置。
  107. // y 在画布上放置图像的 y 坐标位置。
  108. // width 要使用的图像的宽度(伸展或缩小图像)。
  109. // height 要使用的图像的高度(伸展或缩小图像)。
  110. /* context.drawImage(
  111. img,
  112. sx, sy, // 图片裁剪起始位置
  113. swidth, sheight, // 被剪切图像的宽高
  114. x, y, // 图像放置在画布
  115. width, height) */
  116. let xhCount = parseInt(goTime / that.period); // 循环次数
  117. let syTime = goTime - xhCount * that.period; // 当前已进行时间
  118. let nowRW = that.canvasWidth * syTime / that.period // 本次循环图片的已展示的宽度
  119. let beforeRW = that.canvasWidth * (1 - syTime / that.period) // 上次循环图片的剩余宽度
  120. // ctx.clearRect(0, 0, canvas.width, canvas.height);
  121. // ctx.font = 'italic 40pt Calibri';
  122. // ctx.fillStyle = "red";
  123. // ctx.fillText(Cesium.JulianDate.toDate(Cesium.JulianDate.now()).getTime(), 20, 100);
  124. // this.textImage.onload = function () {
  125. // 绘制上次循环
  126. if (xhCount > 0) {
  127. ctx.drawImage(this.textImage,
  128. nowRW, 0,
  129. beforeRW, that.imageHeight,
  130. 0, 0,
  131. beforeRW, that.imageHeight);
  132. }
  133. // 绘制本次循环图片
  134. ctx.drawImage(this.textImage,
  135. 0, 0,
  136. nowRW, that.imageHeight,
  137. beforeRW, 0,
  138. nowRW, that.imageHeight);
  139. // }
  140. return canvas.toDataURL("image/png");;
  141. }
  142. Cesium.createDynamicImage = createDynamicImage
  143. })();