当前位置: 首页 > article >正文

Cesium笔记 canvas定制billboard

Cesium 中label时,想要设置text的背景图片时,label没有backgroundimage属性。

如果使用label+billboard 的方式设置,会存在文本长短不一时,图片不能自适应等问题。

const showLabelFun = (dataMcInfo, backgroundcolor) => {
  var c = document.createElement('canvas')

  var settings = {
    canvas: c,
    canvasWidth: getTextWidth(dataMcInfo, "12px 'Microsoft Yahei'") + 20, //canvas的宽度
    canvasHeight: 36, //canvas的高度
    drawStartX: 10, //绘制字符串起始x坐标 距离左侧
    drawStartY: 22, //绘制字符串起始y坐标 距离顶部
    font: "12px 'Microsoft Yahei'", //文字样式
    text: dataMcInfo, //需要绘制的文本
    color: '#ffffff', //"#000000", //文字的颜色
    backgroundColor: backgroundcolor, //"#ffffff"//背景颜色
    innerGlowColor: '#00ff96'
  }
  function getTextWidth(text, font) {
    var canvas = document.createElement('canvas')
    var context = canvas.getContext('2d')
    context.font = font
    var metrics = context.measureText(text)
    return metrics.width
  }
  //绘制带有圆角的背景
  CanvasRenderingContext2D.prototype.roundRect = function (x, y, width, height, radius, fill, stroke) {
    if (typeof stroke == 'undefined') {
      stroke = false
    }
    if (typeof radius === 'undefined') {
      radius = 5
    }

    this.beginPath()
    this.moveTo(x + radius, y)
    this.lineTo(x + width - radius, y)
    this.quadraticCurveTo(x + width, y, x + width, y + radius)
    this.lineTo(x + width, y + height - radius)
    this.quadraticCurveTo(x + width, y + height, x + width - radius, y + height)
    this.lineTo(x + radius, y + height)
    this.quadraticCurveTo(x, y + height, x, y + height - radius)
    this.lineTo(x, y + radius)
    this.quadraticCurveTo(x, y, x + radius, y)

    this.fillStyle = settings.backgroundColor

    this.closePath()
    if (stroke) {
      this.stroke()
    }
    if (fill) {
      this.fill()
    }
  }

  //获取2d的上线文开始设置相关属性
  var canvas = settings.canvas
  canvas.width = settings.canvasWidth
  canvas.height = settings.canvasHeight
  var ctx = canvas.getContext('2d')

  //绘制带有圆角的背景
  ctx.roundRect(0, 0, canvas.width, canvas.height, 15, true)

  //填充文字
  ctx.font = settings.font
  ctx.fillStyle = settings.color
  ctx.fillText(settings.text, settings.drawStartX, settings.drawStartY)
  //ctx.fillText('速度:30 km/h', settings.drawStartX, settings.drawStartY + 20)

  return canvas.toDataURL()

  //下载图片测试查看
  //var src = canvas.toDataURL();
  // var a = document.createElement('a');
  // var event = new MouseEvent('click');
  // a.download = (new Date()).getTime() + ".jpg"; // 指定下载图片的名称
  // a.href = src;
  // a.dispatchEvent(event); // 触发超链接的点击事件
}

使用

 viewer.entities.add({
    position: Cesium.Cartesian3.fromDegrees(lng, lat, alt), //坐标

    billboard: {
      image: showLabelFun(projectName, color),
      pixelOffset: new Cesium.Cartesian2(0, -50),
      disableDepthTestDistance: Number.POSITIVE_INFINITY //被建筑物遮挡问题
    }
  })

http://www.kler.cn/news/323575.html

相关文章:

  • 音频以及麦克风
  • 【生物服务器】DAP-seq与H3K4me3 ChIP-seq服务,推动表观遗传学研究的创新工具
  • BERT训练之数据集处理(代码实现)
  • 又一条地铁无人线开通!霞智科技智能清洁机器人正式“上岗”
  • 多线程事务管理:Spring Boot 实现全局事务回滚
  • MySQL篇(leetcode刷题100(排序和分组、函数))(一)(持续更新迭代)
  • 华为OD机试真题----BOSS的收入
  • 通过深度学习识别情绪
  • vue3 通过 axios + jsonp 实现根据公网 ip, 查询天气信息
  • Spring Gateway学习
  • 影响上证50股指期货价格的因素有哪些?
  • robomimic基础教程(四)——开源数据集
  • Hive优化高频面试题
  • C++远端开发环境手动编译安装(centos7)
  • SpringMVC源码-SpringMVC框架中Spring父容器和SpringMVC子容器加载的流程以及SpringMVC九大内置组件的初始
  • Unity 资源 之 PoseAI 基于肌肉的姿势创作工具
  • 【C++】内存管理:内存分布、new/delete
  • 基于CentOS7上安装MicroK8s(最小生产的 Kubernetes)
  • unix中的vfork函数
  • 2025秋招内推--招联金融
  • Webpack教程-初次体验
  • 重塑未来:组织文化建设助你应对时代挑战
  • 事后被动处置向事前主动预警转变的智慧工业开源了
  • 嵌入式Linux系统TF卡热插拔检测问题
  • 递归,搜索与回溯40道算法题
  • Android 保存本地图片
  • 深度学习(入门)03:监督学习
  • 9.24 C++ 常成员,运算符重载
  • 人工智能-机器学习-深度学习-分类与算法梳理
  • qt 模仿简易的软狗实现