在地图上使用canvas绘制面板

html页面中引入:

<div id="result">
	<canvas id="canvas" width="300" height="150"></canvas>
</div>

css样式:

#canvas {
    filter:alpha(opacity=60);/*IE*/
    opacity:0.6;/*Mozilla*/
    /*background: #fff;*/
    padding: 7px;
    position: absolute;
    z-index: 100;
    bottom: 5px;
    right: auto;
    top: auto;
    left: 5px;/*old:225*/
}

js方法:

function showOriginPanel(splitList){
    var canvas = document.getElementById('canvas');
    if(1 == splitList.length && splitList[0].color == "#d9d9d9" ){
        canvas.style.display = 'none';
    }
    if(5<splitList.length){
        canvas.width = 300;//130
        canvas.height = 35;//130+(splitList.length-5)*26
        canvas.style.width = '300px';//130px
        canvas.style.height = '35px';//'130px'+(splitList.length-5)*26
    }else{
        canvas.width = splitList.length * 100;
        canvas.height = 35;
        canvas.style.width = (splitList.length * 100) + 'px';
        canvas.style.height = '35px';
    }

    canvas.style.display = 'block';
    canvas.style.background = '#115FB1';//#fff
    canvas.style.boxShadow = 'rgba(0,0,0,0.2) 0 0 4px 2px';
    canvas.style.border = '1px solid #0C345C';
    canvas.style.borderRadius = '4px';
    // 添加DOM元素到地图中
    //map.getContainer().appendChild(canvas);

    var ctx=canvas.getContext("2d");
    ctx.fillStyle="#0000ff";
    //ctx.globalAlpha = 0.8;
    //ctx.fillRect(20,20,80,20);
    for (var i = 0; i < splitList.length; i++) {
        ctx.beginPath();
        //ctx.arc(15, i * 25 + 15, 5, 0, Math.PI * 2, false);
        ctx.arc(15 + i * 100, 15, 5, 0, Math.PI * 2, false);
        var text = '';
        text = splitList[i].alarmName;
        ctx.closePath();
        ctx.fillStyle = splitList[i].color;
        ctx.fill();
        ctx.font='12px 微软雅黑';
        ctx.fillStyle = '#fff';//#d2691e
        //ctx.fillText(text, 25, i * 25 + 20);
        ctx.fillText(text, 25 + i*100, 20);
    };

}

调用:

var panelColorList = new Array();
panelColorList.push({color: "#700000", alarmName: "小偷"});
panelColorList.push({color: "#F0F8FF", alarmName: "强盗"});
showOriginPanel(panelColorList);

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值