canvas画时钟案例


<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>钟表</title>
    <style>
        #canvas {
            border: 1px solid #000;
            margin: 0 auto;
            display: block;
        }
    </style>
</head>

<body>
    <canvas id="canvas" width="400" height="400"></canvas>
    <script>
        var canvas = document.getElementById('canvas'),
            context = canvas.getContext('2d'),
            deg = Math.PI / 180;
        context.translate(200, 200);

        function dialPlate() { //创建表盘
            context.clearRect(-150, -150, 400, 400); //清除画布
            context.beginPath();
            context.arc(0, 0, 150, 0, 360 * deg);
            context.lineWidth = 3;
            context.stroke();
            context.closePath();
            //创建刻度
            for (var i = 1; i <= 60; i++) {
                context.beginPath();
                context.save();
                context.rotate(6 * i * deg);
                context.moveTo(0, -150);
                if (i % 15 == 0) {
                    context.strokeStyle = 'red';
                    context.lineWidth = 3;
                    context.lineTo(0, -135);
                    context.stroke();
                } else if (i % 5 == 0) {
                    context.strokeStyle = 'orange';
                    context.lineWidth = 2;
                    context.lineTo(0, -140);
                    context.stroke();
                } else {
                    context.strokeStyle = '#000';
                    context.lineWidth = 1;
                    context.lineTo(0, -145);
                    context.stroke();
                }
                context.restore();
                context.closePath();
            }
            //创建数字
            for (var i = 1; i <= 12; i++) {
                context.beginPath();
                context.save();
                context.rotate(30 * i * deg);
                context.textAlign = 'center';
                if (i % 3 == 0) {
                    context.fillStyle = 'red';
                    context.font = "normal 28px arial";
                    context.fillText(i, 0, -110);
                } else {
                    context.font = "normal 20px arial";
                    context.fillText(i, 0, -120);
                }
                context.restore();
                context.closePath();
            }
            //中心点
            context.beginPath();
            context.arc(0, 0, 5, 0, 360 * deg);
            context.fill();
            context.closePath();
        }

        function Pointer() { //创建指针
            var nowdate = new Date(),
                hour = nowdate.getHours() % 12,
                minu = nowdate.getMinutes(),
                second = nowdate.getSeconds();
            var ms = nowdate.getMilliseconds(); //毫秒
            //秒针
            context.beginPath();
            context.save();
            context.lineWidth = 1;
            context.strokeStyle = 'red';
            //context.rotate(6*second*deg);
            context.rotate((ms / 1000 + second) * 6 * deg);
            context.moveTo(0, 20);
            context.lineTo(0, -130);
            context.stroke();
            context.restore();
            context.closePath();
            //分针
            context.beginPath();
            context.save();
            context.lineWidth = 2;
            context.strokeStyle = 'orange';
            //context.rotate((second/60+minu)*6*deg);
            context.rotate((ms / 1000 / 60 + second / 60 + minu) * 6 * deg);
            context.moveTo(0, 10);
            context.lineTo(0, -120);
            context.stroke();
            context.restore();
            context.closePath();
            //时针
            context.beginPath();
            context.save();
            context.lineWidth = 3;
            context.strokeStyle = '#000';
            //context.rotate((second/3600+minu/60+hour)*30*deg);
            context.rotate((ms / 1000 / 60 / 60 + second / 60 / 60 + minu / 60 + hour) * 30 * deg);
            context.moveTo(0, 0);
            context.lineTo(0, -110);
            context.stroke();
            context.restore();
            context.closePath();
        }

        dialPlate();
        Pointer();
        //生硬
        //			setInterval(function(){
        //				dialPlate();
        //				Pointer();
        //			},1000)
        //比较圆滑
        setInterval(function () {
            dialPlate();
            Pointer();
        }, 1000 / 60)
    </script>
</body>

</html>

图片加水印


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<canvas id="myCanvas" width="1000" height="500" >
Your browser does not support the HTML5 canvas tag.
</canvas>
<script>
    //准备img对象
    var img = new Image();  
    img.src = 'https://www.vcg.com/creative/collection/?phrase=%E5%9B%BE%E5%83%8F&type=1';
  
    // 加载完成开始绘制
    img.onload=function(){
       //准备canvas环境
       var canvas=document.getElementById("myCanvas");
       var ctx=canvas.getContext("2d");
       // 绘制图片
       ctx.drawImage(img,0,0);
       // 绘制水印
       ctx.font="20px microsoft yahei";
       ctx.fillStyle = "rgba(255,255,255,0.5)";
       ctx.fillText("my images",100,100);
    }
</script>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

web修理工

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值