首页 > 其他分享 >Cesium 使用canvas 自定义 label 样式

Cesium 使用canvas 自定义 label 样式

时间:2023-02-24 11:45:17浏览次数:51  
标签:canvas 自定义 settings height width radius var label

https://blog.csdn.net/tutouxiaoyaonie/article/details/127306197

项目需求:车辆按照轨迹移动,模型上方要有文本框显示车辆信息,但是cesium自带的label设置样式有限,不美观,所以采取canvas生成图片,用billboard加载的方法实现。

cesium官网例子,label样式:

处理后样式,可修改字体,边距等数据

代码如下:

(1)封装的js

let showLabelFun = (dataMcInfo) => {
    var c = document.createElement("canvas");
    var settings = {
		canvas:c,
		canvasWidth:150, //canvas的宽度
        canvasHeight:65, //canvas的高度
		drawStartX:15, //绘制字符串起始x坐标 距离左侧
		drawStartY:28, //绘制字符串起始y坐标 距离顶部
		font:"12px 'Microsoft Yahei'", //文字样式
		text:"请修改掉默认的配置", //需要绘制的文本
		color:"#ffffff",//"#000000", //文字的颜色
		backgroundColor:"rgba(38, 38, 38, 0.75)"//"#ffffff"//背景颜色
	};

    //绘制带有圆角的背景
    CanvasRenderingContext2D.prototype.roundRect =
      function (x, y, width, height, radius, fill, stroke) {
        if (typeof stroke == "undefined") {
            stroke = true;
        }
        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('终端名称:自动驾驶车01',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); // 触发超链接的点击事件
}

(2)引用

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

本文转自 https://blog.csdn.net/tutouxiaoyaonie/article/details/127306197,如有侵权,请联系删除。

标签:canvas,自定义,settings,height,width,radius,var,label
From: https://www.cnblogs.com/hustshu/p/17150740.html

相关文章

  • 【网关开发】9.Openresty 自定义流量分流策略支持灰度(金丝雀)等发布业务场景
    目录背景发布模式蓝绿发布金丝雀发布滚动更新实现原理架构图流程说明核心代码数据结构流量标记代理策略测试总结与思考优先级扩展背景随着云技术和基础架构的成熟,发布过......
  • Cesium label 换行
    字符串中加换行符"\n"label:{//文字标签text:newCesium.CallbackProperty((result)=>{lettextTipsArr=textTips......
  • el-select change自定义参数
    只有带value参数的默认方法 <el-table-column            label="eleName"            width="180"><templa......
  • 自定义注解+反射机制
    @Target用来表示注解作用范围,超过这个作用范围,编译的时候就会报错。@Target:注解的作用目标@Target(ElementType.TYPE)——接口、类、枚举、注解@Target(ElementType.FIE......
  • 用es6的class类单例模式封装canvas环形进度条
    项目中需要一个请求进度效果,尝试了下自己用canvas来绘制一个环形进度条,动效直接用的休眠函数加随机数来模拟。用到了es6里的class类,用单例模式的懒汉模式来实例化对象......
  • 直播软件源码,在vue中使用html2canvas在前端生成图片
    直播软件源码,在vue中使用html2canvas在前端生成图片1、安装 npminstallhtml2canvas​2、用法 importhtml2canvasfrom'html2canvas'; html2canvas(document.......
  • Nginx生成自定义证书
    1、创建存放证书的目录,此目录可自定义cd/usr/local/nginx/conf/cretmkdirkeycdkey先安装依赖包yum-yinstallporc-develzlib-develpopt-developenssl-developen......
  • 自定义属性访问
    什么是属性?下面的例子a和b是属性吗?不是,他们是全局变量,属性(attribute)是类中的成员变量,也可以理解为属性就是类变量。pythona=11234b='python'PYTHON复制全屏类中......
  • Vue3自定义指令
    Vue3自定义指令自定义指令的定义:自定义指令是用来操作DOM的,尽管vue推崇数据驱动视图的理念,但并非所有情况都适合数据驱动。自定义指令就是一种有效的补充和拓展,不仅仅......
  • fusion app自定义事件源码介绍(下)
    安装apk安装APK(filePath)lua源码function安装APK(filePath)localintent=Intent(Intent.ACTION_VIEW);intent.addCategory("android.intent.category.DEFAU......