js语法笔记4---canvas

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body style="background: rgba(199,237,204,1)">

<div style="display:flex; flex-direction: row">

    <!--通过style方式为canvas设置宽高在火狐浏览器上导致绘制内容纵向拉伸。。。-->
    <canvas id="drawing" width="400px" height="400px">canvas to draw</canvas>

    <pre id="container" style="margin: 10px"/>

</div>
</body>
<script type="text/javascript">


    var drawing = document.getElementById('drawing');

    if (drawing.getContext) {
        print('support')
        var context = drawing.getContext('2d');
        context.fillStyle = '#fff';
        context.fillRect(0, 0, 400, 400);

        context.fillStyle = 'black'
        for (let i in [0, 1, 2, 3, 4, 5, 6, 7]) {
            context.fillText(i * 50, i * 50, 10)
            context.fillText(i * 50, 0, i * 50)

        }
        context.font = 'bold 15px Arial';
        context.textAlign = 'left';
        context.textBaseline = 'bottom'
        draw(context);

    } else {
        print('not ')
    }
    function draw(context) {
        context.fillStyle = 'red';
        context.fillRect(10, 10, 150, 150);

        context.fillStyle = 'rgba(0,255,0,0.5)';
        context.fillRect(100, 100, 150, 150);

        context.lineWidth = 0.5;
        context.strokeStyle = 'blue';
        context.strokeRect(50, 50, 150, 150);
        context.fillStyle = 'black'
        context.fillText('hello canvas', 200, 400)


        context.clearRect(125, 125, 25, 25)
        context.clearRect(20, 20, 25, 25)
//        context.lineCap
//context.lineJoin

        context.strokeStyle = '#0f08'
        context.moveTo(50, 50);
        context.lineTo(100, 100);
        context.lineTo(50, 100);
        context.closePath();
        context.stroke();

        context.fillStyle = '#e1e1e188'
        context.rect(200, 200, 150, 150)
        context.fill();

        drawClock(context);

        context.fillText('hello', 0, 15)
        drawLine(context);


        // toImage();
    }

    function drawLine(context) {
        context.save();
        context.lineCap = 'round'
        context.lineJoin = 'bevel' //miter  round
        context.strokeStyle = '#00f'
//        context.lineWidth=3
        context.translate(300, 300)
        context.moveTo(0, 0);
        context.lineTo(80, 80);
        context.stroke()

        context.moveTo(80, 0);
        context.lineTo(0, 80)
        context.stroke()

        context.restore();

    }
    function drawClock(context) {
        context.save();
        context.strokeStyle = 'black';
//        context.lineJoin='miter'
        context.lineWidth = 3
        context.lineCap = 'round' //round square butt
        context.beginPath();
        context.arc(100, 100, 99, 0, 2 * Math.PI, false);
        context.arc(100, 100, 94, 0, 2 * Math.PI, false);

        context.moveTo(100, 100);
        context.lineTo(100, 15);

        context.moveTo(100, 100);
        context.lineTo(35, 100);


        context.stroke();

        context.fillStyle = '#000'

        context.translate(100, 100);
        for (i of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) {
            context.rotate(Math.PI / 6);
            context.fillText(i + "", 0, -80)
        }

        context.restore();

    }
    function toImage() {
        var imageUri = drawing.toDataURL('image/png');
        var imageTag = document.createElement('img');
        imageTag.style = 'margin:10px;width:200px;height:200px'
        imageTag.src = imageUri;
        document.body.appendChild(imageTag)
    }

    function print(txt) {
        document.getElementById("container").innerHTML += ('\n') + txt;
    }

    document.body.onclick = function () {
        window.location.reload()
    }
    console.log = print;


</script>


</html>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值