Canvas制作天气预报走势图

要实现的效果如下图:

HTML代码如下:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <link rel="stylesheet" type="text/css" href="common.css"/>
</head>
<body>
    <div class="weather-container">
        <canvas id="myCanvas" width="640" height="150"></canvas>
        <a class="oneDayWeather pastDay" href="http://www.sinohuaxia.cn" target="_blank">
            <div class="weekDay">周二</div>
            <div class="curWeather">阵雨</div>
            <div class="curWeatherPic"></div>
            <div class="highTempNum" id="highTempNum0">32°</div>
            <div class="highTemp" id="highTemp0"></div>
            <div class="lowTemp" id="lowTemp0"></div>
            <div class="lowTempNum" id="lowTempNum0">20°</div>
            <div class="dayWeatherPic"></div>
            <div class="dayWeather">雷阵雨</div>
            <div class="dayDate">06/09</div>
        </a>
        <a class="oneDayWeather pastDay" href="http://www.sinohuaxia.cn" target="_blank">
            <div class="weekDay">周二</div>
            <div class="curWeather">阵雨</div>
            <div class="curWeatherPic"></div>
            <div class="highTempNum" id="highTempNum1">29°</div>
            <div class="highTemp" id="highTemp1"></div>
            <div class="lowTemp" id="lowTemp1"></div>
            <div class="lowTempNum" id="lowTempNum1">24°</div>
            <div class="dayWeatherPic"></div>
            <div class="dayWeather">雷阵雨</div>
            <div class="dayDate">06/09</div>
        </a>
        <a class="oneDayWeather" href="http://www.sinohuaxia.cn" target="_blank">
            <div class="weekDay">周二</div>
            <div class="curWeather">阵雨</div>
            <div class="curWeatherPic"></div>
            <div class="highTempNum" id="highTempNum2">32°</div>
            <div class="highTemp" id="highTemp2"></div>
            <div class="lowTemp" id="lowTemp2"></div>
            <div class="lowTempNum" id="lowTempNum2">22°</div>
            <div class="dayWeatherPic"></div>
            <div class="dayWeather">雷阵雨</div>
            <div class="dayDate">06/09</div>
        </a>
        <a class="oneDayWeather" href="http://www.sinohuaxia.cn" target="_blank">
            <div class="weekDay">周二</div>
            <div class="curWeather">阵雨</div>
            <div class="curWeatherPic"></div>
            <div class="highTempNum" id="highTempNum3">28°</div>
            <div class="highTemp" id="highTemp3"></div>
            <div class="lowTemp" id="lowTemp3"></div>
            <div class="lowTempNum" id="lowTempNum3">9°</div>
            <div class="dayWeatherPic"></div>
            <div class="dayWeather">雷阵雨</div>
            <div class="dayDate">06/09</div>
        </a>
        <a class="oneDayWeather" href="http://www.sinohuaxia.cn" target="_blank">
            <div class="weekDay">周二</div>
            <div class="curWeather">阵雨</div>
            <div class="curWeatherPic"></div>
            <div class="highTempNum" id="highTempNum4">31°</div>
            <div class="highTemp" id="highTemp4"></div>
            <div class="lowTemp" id="lowTemp4"></div>
            <div class="lowTempNum" id="lowTempNum4">20°</div>
            <div class="dayWeatherPic"></div>
            <div class="dayWeather">雷阵雨</div>
            <div class="dayDate">06/09</div>
        </a>
        <a class="oneDayWeather" href="http://www.sinohuaxia.cn" target="_blank">
            <div class="weekDay">周二</div>
            <div class="curWeather">阵雨</div>
            <div class="curWeatherPic"></div>
            <div class="highTempNum" id="highTempNum5">28°</div>
            <div class="highTemp" id="highTemp5"></div>
            <div class="lowTemp" id="lowTemp5"></div>
            <div class="lowTempNum" id="lowTempNum5">12°</div>
            <div class="dayWeatherPic"></div>
            <div class="dayWeather">雷阵雨</div>
            <div class="dayDate">06/09</div>
        </a>
    </div>
    
    <script type="text/javascript">
    var canvas=document.getElementById('myCanvas');
    var canvasTop=canvas.offsetTop;
    var ctx=canvas.getContext('2d');
    var sixdayHighTemperature=[32,29,32,28,31,28];
    var sixdayLowTemperature=[20,24,22,9,20,12];
    var picHeight=150;
    var maxHighTemp=maxNum(sixdayHighTemperature);
    var minLowTemp=minNum(sixdayLowTemperature);
    var maxRange=maxHighTemp-minLowTemp;
    function maxNum(arr){
        var maxNum=0;
        if(arr.length){
            for(var i=0,len=arr.length;i<len;i++){
                if(i==0){
                    maxNum=arr[0];
                }else if(maxNum<arr[i]){
                    maxNum=arr[i];
                }
            }
        }
        return maxNum;
    }
    function minNum(arr){
        var minNum=0;
        if(arr.length){
            for(var i=0,len=arr.length;i<len;i++){
                if(i==0){
                    minNum=arr[0];
                }else if(minNum>arr[i]){
                    minNum=arr[i];
                }
            }
        }
        return minNum;
    }
    ctx.strokeStyle="#fff";
    ctx.lineWidth=2;
    (function drawHighTemp(temps){
            var drawX=0,drawY=0;
            var distance=Math.floor(picHeight/maxRange);
            for(var i=0,len=temps.length;i<len;i++){
                drawX=i*106+53;
                drawY=(maxHighTemp-temps[i])*distance;
                document.getElementById('highTemp'+i).style.top=(canvasTop+drawY-5)+"px";
                document.getElementById('highTempNum'+i).style.top=(canvasTop+drawY-25)+"px";
                if(i==0){
                    ctx.moveTo(drawX,drawY);
                }else{
                    ctx.lineTo(drawX,drawY);
                }
            }
            ctx.stroke();
        })(sixdayHighTemperature);
        (function drawHighTemp(temps){
            var drawX=0,drawY=0;
            var distance=Math.floor(picHeight/maxRange);
            for(var i=0,len=temps.length;i<len;i++){
                drawX=i*106+53;
                drawY=picHeight-((temps[i]-minLowTemp)*distance);
                document.getElementById('lowTemp'+i).style.top=(canvasTop+drawY-5)+"px";
                document.getElementById('lowTempNum'+i).style.top=(canvasTop+drawY+10)+"px";
                if(i==0){
                    ctx.moveTo(drawX,drawY);
                }else{
                    ctx.lineTo(drawX,drawY);
                }
            }
            ctx.stroke();
        })(sixdayLowTemperature)
    
    </script>
</body>
</html>

CSS代码如下:

body {
    background: url(img/bg_night_sunny.jpg) no-repeat;
    margin: 0;
    padding: 0;
}
.weather-container{
    width:640px;
    height:360px;
    position:relative;
}
.oneDayWeather{
    position: relative;
    overflow: hidden;
    width:105px;
    height:360px;
    float:left;
    text-align: center;
    line-height: 20px;
    color:#fff;
    border-right:1px solid rgba(255,255,255,0.25);
}
.oneDayWeather:last-child{
    border:none;
}
.oneDayWeather:active{
    background:rgba(0,0,0,0.2);
}
.pastDay{
    opacity: 0.6;
}
.weekDay{
    position: absolute;
    left:0;
    top:0;
    width:100%;
}
.curWeather{
    position: absolute;
    left:0;
    top:20px;
    width:100%;
}
.curWeatherPic{
    position: absolute;
    left:0;
    top:40px;
    width:100%;
    height:30px;
    background:url(img/w1.png) center 0 no-repeat;
    background-size:contain;
}
.highTemp,.lowTemp{
    position: absolute;
    left:0;
    top:-240px;
    width:100%;
    height:10px;
    background: url(img/splash_indicator_focused.png) center 0 no-repeat;
}
.highTempNum,.lowTempNum{
    position: absolute;
    left:0;
    top:-20em;
    width:100%;
    height:20px;
    text-indent: 15px
}
.dayWeatherPic{
    position: absolute;
    left:0;
    bottom:40px;
    width:100%;
    height:30px;
    background:url(img/w2.png) center 0 no-repeat;
    background-size:contain;
}
.dayWeather{
    position: absolute;
    left:0;
    bottom:20px;
    width:100%;
}
.dayDate{
    position: absolute;
    left:0;
    bottom:0;
    width:100%;
}
#myCanvas{
    position:absolute;
    top:105px;
    left:0;
}

 

转载于:https://www.cnblogs.com/plBlog/p/11452011.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要绘制号码走势图,可以使用HTML5的Canvas元素。Canvas是一个用于绘制图形的HTML元素,通过JavaScript代码可以在Canvas上绘制各种图形和图表。 具体实现步骤如下: 1. 在HTML文件中创建一个Canvas元素,并设置宽度和高度。 ``` <canvas id="myCanvas" width="800" height="600"></canvas> ``` 2. 在JavaScript文件中获取Canvas元素,并创建一个用于绘制的2D上下文对象。 ``` var canvas = document.getElementById("myCanvas"); var ctx = canvas.getContext("2d"); ``` 3. 绘制坐标轴和刻度线。 ``` ctx.beginPath(); ctx.moveTo(50, 550); ctx.lineTo(750, 550); ctx.stroke(); ctx.beginPath(); ctx.moveTo(50, 550); ctx.lineTo(50, 50); ctx.stroke(); ctx.font = "14px Arial"; ctx.textAlign = "center"; ctx.fillText("号码", 25, 300); ctx.fillText("期数", 400, 580); for(var i=0; i<10; i++){ var x = 100 + i*60; ctx.beginPath(); ctx.moveTo(x, 550); ctx.lineTo(x, 540); ctx.stroke(); ctx.fillText(i, x, 570); } for(var i=0; i<6; i++){ var y = 550 - i*80; ctx.beginPath(); ctx.moveTo(50, y); ctx.lineTo(60, y); ctx.stroke(); ctx.fillText((i+1)*10, 25, y); } ``` 4. 绘制号码走势图。 ``` var data = [ [1, 3, 5, 7, 9], [2, 4, 6, 8, 0], [1, 4, 7, 0, 3], [2, 5, 8, 0, 6], [3, 6, 9, 0, 2] ]; for(var i=0; i<data.length; i++){ var y = 550 - (i+1)*80; for(var j=0; j<data[i].length; j++){ var x = 100 + data[i][j]*60; ctx.beginPath(); ctx.arc(x, y, 20, 0, 2*Math.PI); ctx.stroke(); } } ``` 这样就可以绘制出一个简单的号码走势图。具体的样式和数据可以根据实际需求进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值