zoukankan      html  css  js  c++  java
  • 学习canvas过程中的小菜鸟

    canvas浅谈

    canvas是一个可以使用脚本(通常为JavaScript)在其中绘制图形的 HTML 元素

    基本用法:我们用代码来讲解

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <script type="application/javascript">
            /*画图方法*/
            function draw() {
                /*获取ID*/
                var canvas = document.getElementById("canvas");
                if (canvas.getContext) {
                    /*创建画布*/
                    var ctx = canvas.getContext("2d");
                    /*填充颜色*/
                    ctx.fillStyle = "rgb(200,0,0)";   //可以是颜色值 颜色名称
                    /*画出矩形*/
                    ctx.fillRect (10, 10, 55, 50);   //起始位置  矩形的宽和高 
    
                    /*填充颜色*/
                    ctx.fillStyle = "rgba(0, 0, 200, 0.5)";
                    /*画出矩形*/
                    ctx.fillRect (30, 30, 55, 50);
                }
            }
        </script>
    </head>
    <!--加载之后执行-->
    <body onload="draw();">
    <!--使用canvas标签-->
    <canvas id="canvas" width="150" height="150"></canvas>
    </body>
    </html>
    

    canvas元素有一个做 getContext() 的方法,这个方法是用来获得渲染上下文和它的绘画功能。getContext()只有一个参数,上下文的格式。

    基本常用方法:

    1. fillRect(x, y, width, height)

    绘制一个填充的矩形

    2. strokeRect(x, y, width, height)

    绘制一个矩形的边框

    3. clearRect(x, y, width, height)

    清除指定矩形区域,让清除部分完全透明

    4. beginPath()

    新建一条路径,生成之后,图形绘制命令被指向到路径上生成路径

    5. closePath()

    闭合路径之后图形绘制命令又重新指向到上下文中

    6. stroke()

    通过线条来绘制图形轮廓

    7. fill()

    通过填充路径的内容区域生成实心的图形

    8. lineTo(x, y)

    绘制一条从当前位置到指定x以及y位置的直线

    9. arc(x, y, radius, startAngle, endAngle, anticlockwise)

    画一个以(x,y)为圆心的以radius为半径的圆弧(圆),从startAngle开始到endAngle结束,按照anticlockwise给定的方向(默认为顺时针)来生成

    10. arcTo(x1, y1, x2, y2, radius)

    根据给定的控制点和半径画一段圆弧,再以直线连接两个控制点

    11. fillStyle = color

    设置图形的填充颜色

    12. strokeStyle = color

    设置图形轮廓的颜色

    13. lineWidth = value

    设置线条宽度

    14. lineCap = type

    设置线条末端样式

    15. lineJoin = type

    设定线条与线条间接合处的样式

    16. miterLimit = value

    限制当两条线相交时交接处最大长度;所谓交接处长度(斜接长度)是指线条交接处内角顶点到外角顶点的长度

    17. getLineDash()

    返回一个包含当前虚线样式,长度为非负偶数的数组

    18. setLineDash(segments)

    设置当前虚线样式

    19. lineDashOffset = value

    设置虚线样式的起始偏移量

    20. fillText(text, x, y [, maxWidth])

    在指定的(x,y)位置填充指定的文本,绘制的最大宽度是可选的

    21. strokeText(text, x, y [, maxWidth])

    在指定的(x,y)位置绘制文本边框,绘制的最大宽度是可选的

    22. font = value

    当前我们用来绘制文本的样式. 这个字符串使用和 CSS font 属性相同的语法. 默认的字体是 10px sans-serif

    23. textAlign = value

    文本对齐选项. 可选的值包括:start, end, left, right or center. 默认值是 start

    24. textBaseline = value

    基线对齐选项. 可选的值包括:top, hanging, middle, alphabetic, ideographic, bottom。默认值是 alphabetic。

    25. direction = value

    文本方向。可能的值包括:ltr, rtl, inherit。默认值是 inherit。

    26. HTMLImageElement

    这些图片是由Image()函数构造出来的,或者任何的元素

    27. HTMLVideoElement

    用一个HTML的

    28. HTMLCanvasElement

    可以使用另一个 元素作为你的图片源

    29. ImageBitmap

    这是一个高性能的位图,可以低延迟地绘制,它可以从上述的所有源以及其它几种源中生成

    30. drawImage(image, x, y, width, height)

    这个方法多了2个参数:width 和 height,这两个参数用来控制 当像canvas画入时应该缩放的大小

    31. drawImage(image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight)

    第一个参数和其它的是相同的,都是一个图像或者另一个 canvas 的引用。其它8个参数最好是参照右边的图解,前4个是定义图像源的切片位置和大小,后4个则是定义切片的目标显示位置和大小

    32. save()restore()

    save 和 restore 方法是用来保存和恢复 canvas 状态的,都没有参数。Canvas 的状态就是当前画面应用的所有样式和变形的一个快照

    33. translate(x, y)

    translate 方法接受两个参数。x 是左右偏移量,y 是上下偏移量

    34. rotate(angle)

    这个方法只接受一个参数:旋转的角度(angle),它是顺时针方向的,以弧度为单位的值

    35. scale(x, y)

    scale 方法接受两个参数。x,y 分别是横轴和纵轴的缩放因子,它们都必须是正值。值比 1.0 小表示缩小,比 1.0 大则表示放大,值为 1.0 时什么效果都没有

    实战:使用路径画一个笑脸 练习路径

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <script type="application/javascript">
            function draw() {
                var canvas = document.getElementById('canvas');
                if (canvas.getContext){
                    var ctx = canvas.getContext('2d');
    
                    ctx.beginPath();
                    ctx.arc(75,75,50,0,Math.PI*2,true);
                    ctx.moveTo(110,75);
                    ctx.arc(75,75,35,0,Math.PI,false);
                    ctx.moveTo(65,65);
                    ctx.arc(60,65,5,0,Math.PI*2, true);
                    ctx.moveTo(95,65);
                    ctx.arc(90,65,5,0,Math.PI*2,false);
                    ctx.stroke();
                }
            }
        </script>
    </head>
    <body onload="draw();">
    <canvas id="canvas" width="150" height="150"></canvas>
    </body>
    </html>
    

    实战:画两三角形 练习线条

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <script type="application/javascript">
            function draw() {
                var canvas = document.getElementById('canvas');
                if (canvas.getContext){
                    var ctx = canvas.getContext('2d');
    
                    ctx.beginPath();
                    ctx.moveTo(25,25);
                    ctx.lineTo(105,25);
                    ctx.lineTo(25,105);
                    ctx.fill();
    
                    ctx.beginPath();
                    ctx.moveTo(125,125);
                    ctx.lineTo(125,45);
                    ctx.lineTo(45,125);
                    ctx.closePath();
                    ctx.stroke();
                }
            }
        </script>
    </head>
    <body onload="draw();">
    <canvas id="canvas" width="150" height="150"></canvas>
    </body>
    </html>
    

    实战:各种圆

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <script type="application/javascript">
            function draw() {
                var canvas = document.getElementById('canvas');
                if (canvas.getContext){
                    var ctx = canvas.getContext('2d');
    
                    for(var i=0;i<4;i++){
                        for(var j=0;j<3;j++){
                            ctx.beginPath();
                            var x              = 25+j*50;               // x 坐标值
                            var y              = 25+i*50;               // y 坐标值
                            var radius         = 20;                    // 圆弧半径
                            var startAngle     = 0;                     // 开始点
                            var endAngle       = Math.PI+(Math.PI*j)/2; // 结束点
                            var anticlockwise  = i%2==0 ? false : true; // 顺时针或逆时针
    
                            ctx.arc(x, y, radius, startAngle, endAngle, anticlockwise);
    
                            if (i>1){
                                ctx.fill();
                            } else {
                                ctx.stroke();
                            }
                        }
                    }
                }
            }
        </script>
    </head>
    <body onload="draw();">
    <canvas id="canvas" width="150" height="150"></canvas>
    </body>
    </html>
    

    arc()函数中的角度单位是弧度,不是度数。角度与弧度的js表达式:radians=(Math.PI/180)*degrees。

    实战:综合使用

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <script type="application/javascript">
            function draw() {
                var canvas = document.getElementById('canvas');
                if (canvas.getContext){
                    var ctx = canvas.getContext('2d');
                    roundedRect(ctx,12,12,150,150,15);
                    roundedRect(ctx,19,19,150,150,9);
                    roundedRect(ctx,53,53,49,33,10);
                    roundedRect(ctx,53,119,49,16,6);
                    roundedRect(ctx,135,53,49,33,10);
                    roundedRect(ctx,135,119,25,49,10);
    
                    ctx.beginPath();
                    ctx.arc(37,37,13,Math.PI/7,-Math.PI/7,false);
                    ctx.lineTo(31,37);
                    ctx.fill();
    
                    for(var i=0;i<8;i++){
                        ctx.fillRect(51+i*16,35,4,4);
                    }
    
                    for(i=0;i<6;i++){
                        ctx.fillRect(115,51+i*16,4,4);
                    }
    
                    for(i=0;i<8;i++){
                        ctx.fillRect(51+i*16,99,4,4);
                    }
    
                    ctx.beginPath();
                    ctx.moveTo(83,116);
                    ctx.lineTo(83,102);
                    ctx.bezierCurveTo(83,94,89,88,97,88);
                    ctx.bezierCurveTo(105,88,111,94,111,102);
                    ctx.lineTo(111,116);
                    ctx.lineTo(106.333,111.333);
                    ctx.lineTo(101.666,116);
                    ctx.lineTo(97,111.333);
                    ctx.lineTo(92.333,116);
                    ctx.lineTo(87.666,111.333);
                    ctx.lineTo(83,116);
                    ctx.fill();
    
                    ctx.fillStyle = "white";
                    ctx.beginPath();
                    ctx.moveTo(91,96);
                    ctx.bezierCurveTo(88,96,87,99,87,101);
                    ctx.bezierCurveTo(87,103,88,106,91,106);
                    ctx.bezierCurveTo(94,106,95,103,95,101);
                    ctx.bezierCurveTo(95,99,94,96,91,96);
                    ctx.moveTo(103,96);
                    ctx.bezierCurveTo(100,96,99,99,99,101);
                    ctx.bezierCurveTo(99,103,100,106,103,106);
                    ctx.bezierCurveTo(106,106,107,103,107,101);
                    ctx.bezierCurveTo(107,99,106,96,103,96);
                    ctx.fill();
    
                    ctx.fillStyle = "black";
                    ctx.beginPath();
                    ctx.arc(101,102,2,0,Math.PI*2,true);
                    ctx.fill();
    
                    ctx.beginPath();
                    ctx.arc(89,102,2,0,Math.PI*2,true);
                    ctx.fill();
                }
            }
    
            function roundedRect(ctx,x,y,width,height,radius){
                ctx.beginPath();
                ctx.moveTo(x,y+radius);
                ctx.lineTo(x,y+height-radius);
                ctx.quadraticCurveTo(x,y+height,x+radius,y+height);
                ctx.lineTo(x+width-radius,y+height);
                ctx.quadraticCurveTo(x+width,y+height,x+width,y+height-radius);
                ctx.lineTo(x+width,y+radius);
                ctx.quadraticCurveTo(x+width,y,x+width-radius,y);
                ctx.lineTo(x+radius,y);
                ctx.quadraticCurveTo(x,y,x,y+radius);
                ctx.stroke();
            }
    
    
    
        </script>
    </head>
    <body onload="draw();">
    <canvas id="canvas" width="150" height="150"></canvas>
    </body>
    </html>
    

    实战:颜色练习

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <script type="application/javascript">
            function draw() {
                var ctx = document.getElementById('canvas').getContext('2d');
                for (var i=0;i<6;i++){
                    for (var j=0;j<6;j++){
                        ctx.fillStyle = 'rgb(' + Math.floor(255-42.5*i) + ',' + Math.floor(255-42.5*j) + ',0)';
                        ctx.fillRect(j*25,i*25,25,25);
                    }
                }
            }
        </script>
    </head>
    <body onload="draw();">
    <canvas id="canvas" width="150" height="150"></canvas>
    </body>
    </html>
    

    实战:文字练习

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <script type="application/javascript">
    
            function draw() {
                var ctx = document.getElementById('canvas').getContext('2d');
                ctx.font = "48px serif";
                /*无边框*/
                ctx.fillText("Hello world", 10, 50);
                /*有边框*/
                ctx.strokeText("Hello world", 10, 120);
            }
        </script>
    </head>
    <body onload="draw();">
    <canvas id="canvas" width="150" height="150"></canvas>
    </body>
    </html>
    

    后面有的属性不太常用,其实我我有点蒙圈了!嘻嘻 ,大家如果需要继续深入学习,可到下面的网站###

    mdn>web>技术文档web>api>接口>canvas>canvas教程

  • 相关阅读:
    一个顽猴沿着一座小山的n级台阶向上跳,猴子上山一步可跳1级或3级,试求上山的n级台阶有多少种不同的爬法。
    postgres的initdb解析——从一次插件升级失败说起
    跟我一起读postgresql源码(十四)——Executor(查询执行模块之——Join节点(下))
    跟我一起读postgresql源码(十三)——Executor(查询执行模块之——Join节点(上))
    跟我一起读postgresql源码(十二)——Executor(查询执行模块之——Materialization节点(下))
    跟我一起读postgresql源码(十一)——Executor(查询执行模块之——Materialization节点(上))
    跟我一起读postgresql源码(十)——Executor(查询执行模块之——Scan节点(下))
    跟我一起读postgresql源码(九)——Executor(查询执行模块之——Scan节点(上))
    跟我一起读postgresql源码(八)——Executor(查询执行模块之——可优化语句的执行)
    跟我一起读postgresql源码(七)——Executor(查询执行模块之——数据定义语句的执行)
  • 原文地址:https://www.cnblogs.com/zhnaglei/p/6606140.html
Copyright © 2011-2022 走看看