用canvas写了一个移动端走势图

<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
    <title></title>
    <style type="text/css">
        body,p{
            padding: 0;
            margin: 0;
            font-family: "Comic Sans MS","幼圆","黑体",sans-serif;
        }
        #container{
            margin-top: 40px;
            position: relative;
            height: 100%;
            /*    background: #aaa;
            */ }
        .text{
            position: absolute;
            height: 100%;
            top:0;
        }
        div.text p{
            position: absolute;
            color: #666;
            font-size: .8em;
        }
        div.date{
            position: absolute;
            color: #666;
            left: 25;
        }
        div.date p{
            position: absolute;
            font-size: .6em;
        }
        #canvas{
            position: relative;
            z-index: 9;
            background: #fff;
            margin-left: 50px;
        }
    </style>
</head>
<body>
<div id="container">
    <canvas id="canvas"></canvas>
    <div class="text">
        <p>4.14%</p>
        <p>4.24%</p>
        <p>4.34%</p>
        <p>4.44%</p>
        <p>4.54%</p>
    </div>
    <div class="date">
        <p>03.01</p>
        <p>03.02</p>
        <p>03.03</p>
        <p>03.04</p>
        <p>03.05</p>
        <p>03.06</p>
        <p>03.07</p>
    </div>
</div>
</body>
</html>
<script>
    document.getElementById('container').style.width=window.screen.wdith+'px';
    var width=window.screen.width-50-20;
    var height=120;
    var height1=150;

    var pointX=[0,width/6,2*width/6,3*width/6,4*width/6,5*width/6,width];/*X*/
    var poin=[0,height-3*height/4,height-2*height/4,height-1*height/4,height];/*Y*/

    var pointY=[0,30,height1-3*height/4,height1-2*height/4,height1-1*height/4,height1];

    var pointZ=[height1-27,height1-30,height1-80,height1-90,height1-50,height1-70,height1-40];
    var p=document.querySelector('.text').getElementsByTagName('p');
    var date=document.querySelector('.date').getElementsByTagName('p');
    var canvas=document.getElementById('canvas').getContext('2d');
    document.getElementById('canvas').setAttribute('width',width);
    document.getElementById('canvas').setAttribute('height',height1);

        /*Y*/
    for(var i=0;i< p.length;i++){
        p[i].style.bottom=poin[i]+'px';
    }

    /*X*/
    for(var j=0;j<date.length;j++){
        if(window.screen.width<=320){
            date[j].style.left=pointX[j]+25+'px';
            // date[j].style.transform='rotate(-40deg)';
        }else{
            date[j].style.left=pointX[j]+25+'px';
        }
    }


    function drawX(){
        for(var i=1;i<pointY.length;i++){
            canvas.beginPath();
            if(i==5) {
                canvas.strokeStyle = 'red';
                canvas.moveTo(pointX[0], height1);
                canvas.lineTo(pointX[6], height1);
                canvas.moveTo(pointX[0], height1);
                canvas.lineTo(pointX[0], 0);

            }else{
                canvas.strokeStyle='yellow';
                canvas.moveTo(pointX[0],pointY[i]);
                canvas.lineTo(pointX[6],pointY[i]);
            }
            canvas.closePath();
            canvas.lineWidth=1;
            canvas.stroke();
        }

    }
    drawX();

    function drawY(){
        for(var i=1;i<pointX.length;i++){
            canvas.beginPath();
            canvas.strokeStyle='green';
            for(var i=1;i<pointX.length;i++){
                canvas.moveTo(pointX[i],height1);
                canvas.lineTo(pointX[i],0);
            }
            canvas.closePath();
            canvas.lineWidth=1;
            canvas.stroke();
        }
    }
    drawY();

    function drawZ(){
        canvas.beginPath();
        canvas.strokeStyle='#000';
        for(var i=0;i<pointZ.length;i++){
            canvas.arc(pointX[i],pointZ[i],2,360,false);
            canvas.moveTo(pointX[i],pointZ[i]);
            canvas.lineTo(pointX[i+1],pointZ[i+1]);
        }
        canvas.closePath();
        canvas.stroke()
        canvas.lineWidth=1;
        canvas.stroke();
    };
    drawZ();
</script>














当您提到"包装分解图"时,我理解为您希望创建一个Canvas图形,展示一个物体被包装和解包的过程。以下是一个简单的示例代码,可以实现这个效果: ```html <!DOCTYPE html> <html> <head> <title>Canvas包装分解图</title> <style> canvas { border: 1px solid #000; } </style> </head> <body> <canvas id="myCanvas" width="400" height="400"></canvas> <script> // 获取Canvas元素 const canvas = document.getElementById('myCanvas'); const ctx = canvas.getContext('2d'); // 定义物体的初始位置和尺寸 let x = 100; let y = 100; let width = 200; let height = 200; // 定义包装分解的动画函数 function animate() { ctx.clearRect(0, 0, canvas.width, canvas.height); // 清空画布 // 绘制包装的矩形 ctx.strokeStyle = '#000'; ctx.strokeRect(x, y, width, height); // 更新尺寸和位置 width -= 2; // 每帧减小2个单位 height -= 2; // 每帧减小2个单位 x += 1; // 每帧向右移动1个单位 y += 1; // 每帧向下移动1个单位 // 如果物体未完全被分解,则继续动画 if (width > 0 && height > 0) { requestAnimationFrame(animate); } } // 开始动画 animate(); </script> </body> </html> ``` 在上述代码中,我们使用了Canvas的getContext方法获取了绘图上下文对象ctx,然后利用该上下文对象进行绘制。通过不断更新物体的尺寸和位置,实现了一个简单的包装分解动画效果。您可以将代码复制粘贴到HTML文件中运行,观察效果。请注意,这只是一个简单的示例,您可以根据需要进行更复杂的定制和改进。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值