fuchengshen
2022-07-09 3a25c05b5ab837714f30469cdcb1e2a77d8d7f6c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// 通过Canvas绘制复杂或动态对象的图标点Graphic
class CanvasBillboard extends mars3d.graphic.BillboardEntity {
  /**
   * 文字
   * @type {string}
   */
  get text() {
    return this.style.text
  }
 
  set text(val) {
    this.style.text = val
    this._updateCanvas()
  }
 
  /**
   * 对象添加到图层前创建一些对象的钩子方法,
   * 只会调用一次
   * @return {void}  无
   * @private
   */
  _mountedHook() {
    super._mountedHook()
 
    if (this._pngImage) {
      this._updateCanvas()
    } else {
      this.style.imageUrl = "img/marker/textPnl.png" // 图片对象
      this.style.width = 300
      this.style.height = 140
 
      const img = new Image(this.style.width, this.style.height)
      img.crossOrigin = "Anonymous"
      img.src = this.style.imageUrl
      img.onload = () => {
        this._pngImage = img
        this._updateCanvas()
      }
    }
  }
 
  // 创建canvas
  _updateCanvas() {
    if (!this._pngImage) {
      return
    }
 
    const canvas = document.createElement("canvas")
    canvas.width = this.style.width
    canvas.height = this.style.height
 
    const ctx = canvas.getContext("2d")
    ctx.clearRect(0, 0, canvas.width, canvas.height)
    // 绘制图片
    ctx.drawImage(this._pngImage, 0, 0)
 
    // 绘制文字
    ctx.fillStyle = "rgb(255, 255, 255)"
    ctx.font = "55px 楷体"
    ctx.textBaseline = "middle"
    ctx.fillText("温度:", 20, canvas.height / 2)
    ctx.fillText(this.style.text, 160, canvas.height / 2)
    ctx.fillText("℃", 220, canvas.height / 2)
 
    // 将图片赋予给矢量对象进行显示,this.image是父类的属性
    this.image = canvas
  }
}
mars3d.graphic.CanvasBillboard = CanvasBillboard