zoukankan      html  css  js  c++  java
  • Canvas路径、描边、填充

    <script>
        var context = document.getElementById('canvas').getContext('2d');
        context.font = '48pt Helvetica';
        context.strokeStyle = 'blue';//边框颜色
        context.fillStyle = 'red';//填充颜色
        context.lineWidth = '2';
    
        //画文字
        context.strokeText('边框', 60, 110);
        context.fillText('填充', 440, 110);
    
        context.strokeText('边框和填充', 650, 110);
        context.fillText('边框和填充', 650, 110);
        //画矩形
        context.lineWidth = '5';
        context.beginPath();//边框 矩形
        context.rect(80, 150, 150, 100);
        context.stroke();
    
        context.beginPath();//填充 矩形
        context.rect(400, 150, 150, 100);
        context.fill();
    
        context.beginPath();//边框和填充
        context.rect(750, 150, 150, 100);
        context.stroke();
        context.fill();
    
        //画弧度
        context.beginPath();
        context.arc(150, 370, 60, 0, 1.5 * Math.PI);
        context.stroke();
    
        context.beginPath();
        context.arc(475, 370, 60, 0, Math.PI * 3 / 2);
        context.fill();
    
        context.beginPath();
        context.arc(820, 370, 60, 0, Math.PI * 3 / 2);
        context.stroke();
        context.fill();
        //closePath()方法创建当前点到起始点的路径
        context.beginPath();
        context.arc(150, 550, 60, 0, Math.PI * 3 / 2);
        context.closePath();
        context.stroke();
    
        context.beginPath();
        context.arc(475, 550, 60, 0, Math.PI * 3 / 2);
        context.closePath();
        context.fill();
    
        context.beginPath();
        context.arc(820, 550, 60, 0, Math.PI * 3 / 2);
        context.closePath();
        context.stroke();
        context.fill();
        //绘制三角形
        context.beginPath();
        context.moveTo(120, 650);
        context.lineTo(120, 750);
        context.lineTo(180, 750);
        context.closePath();
        context.stroke();
    
        context.beginPath();
        context.moveTo(450, 650);
        context.lineTo(450, 750);
        context.lineTo(510, 750);
        context.closePath();
        context.fill();
    
        context.beginPath();
        context.moveTo(810, 650);
        context.lineTo(810, 750);
        context.lineTo(870, 750);
        context.closePath();
        context.fill();
        context.stroke();
    
        context.beginPath();
        context.moveTo(120, 800);
        context.lineTo(120, 900);
        context.lineTo(180, 900);
        context.stroke();
    
        context.beginPath();
        context.moveTo(450, 800);
        context.lineTo(450, 900);
        context.lineTo(510, 900);
        context.fill();
    
        context.beginPath();
        context.moveTo(810, 800);
        context.lineTo(810, 900);
        context.lineTo(870, 900);
        context.fill();
        context.stroke();
    </script>

    练习代码

  • 相关阅读:
    快速掌握Vue3部分特性
    浅析Web Worker使用技巧及实战场景
    浅析前端路由原理和实现方式
    浅析Vue.observable()实现类似vuex的状态管理功能创建响应式全局数据
    [Typescript v4] Tuple Types && Recursive types
    [Typescript] Nullish Coalescing
    [Tools] Volta
    [Spring Pattern] Builder pattern
    [Javascript] Broadcaster + Operator + Listener pattern -- 17. Building a Word Matching Game
    [Spring] Factory Pattern
  • 原文地址:https://www.cnblogs.com/yanshanshuo/p/3908589.html
Copyright © 2011-2022 走看看