美文网首页
canvas简述

canvas简述

作者: bo_bo_bo_la | 来源:发表于2017-12-25 22:36 被阅读91次

1. 绘制三角形

fill是填充面(实心),stroke是画线(空心)
    <script type="text/javascript">
        //首先,找到 <canvas> 元素:
        var canvas = document.getElementById('myCanvas');
        //然后,创建 context 对象:
        //getContext("2d") 对象是内建的 HTML5 对象,
        //拥有多种绘制路径、矩形、圆形、字符以及添加图像的方法。
        var context = canvas.getContext('2d');
        
        //canvas 是一个二维网格。
        //canvas 的左上角坐标为 (0,0)
        context.beginPath();
        //moveTo(x,y) 定义线条开始坐标
        context.moveTo(0,0);
        //lineTo(x,y) 定义线条结束坐标
        //绘制三角形
        context.lineTo(0,100);
        context.lineTo(100,100);
        context.lineTo(0,0)
        context.strokeStyle = "#0000FF";
        context.stroke();
//      context.lineWidth = 10;
//      context.fillStyle = "#008000";
//      context.fill();

2. 画圆

lineCap:线帽 有三个参数
  • butt 默认。向线条的每个末端添加平直的边缘。
  • round 向线条的每个末端添加圆形线帽。
  • square 向线条的每个末端添加正方形线帽。
context.arc(参数1,参数2,参数3,参数4,参数5,参数6);
  • 参数1 参数2 圆形原点的坐标
  • 参数3 半径
  • 参数4 绘制圆形的起始角度
  • 参数5 绘制圆形的结束角度
  • 参数6 顺时针 | 逆时针 参数是布尔:true 逆时针,false 顺时针
    context.arc(200,100,100,0,Math.PI,false);
        //画圆 
        context.beginPath(); // 重新开启一个新的图层
        context.arc(200,100,100,0,Math.PI,false);
        context.lineCap = "round";
        context.lineWidth = 15;
        context.strokeStyle = "aquamarine";
        context.stroke();

3. 矩形

context.rect(参数1,参数2,参数3,参数4);
  • 参数1 ,参数2 矩形绘制的起点位置(参数1为X轴的坐标,参数2为Y轴的坐标)
  • 参数3,矩形的宽
  • 参数4矩形的高
创建线条渐变 context.createLinearGradient(参数1,参数2,参数3,参数4);
  • 参数1 ,参数2 渐变颜色开始的位置(参数1为X轴的坐标,参数2为Y轴的坐标)
  • 参数3,参数4 渐变颜色结束的位置(参数3为X轴的坐标,参数4为Y轴的坐标)
grd.addColorStop(参数1,参数2);
  • 参数1 ,0~1之间的数
  • 参数3,参数4 颜色
        //矩形
        context.beginPath();
        context.rect(0,200,200,100);
        var grd = context.createLinearGradient(0,200,200,200);
        grd.addColorStop(0,"red");
        grd.addColorStop(0.5,"green");
        grd.addColorStop(1,"blue");
        context.fillStyle = grd;
        context.fill();
创建一个径向/圆渐变 context.createRadialGradient(xStart, yStart,radiusStart,xEnd, yEnd,radiusEnd);

-xStart, yStart 开始圆的圆心的坐标。
-radiusStart 开始圆的半径。
-xEnd, yEnd 结束圆的圆心的坐标。
-radiusEnd 结束圆的半径。

        context.beginPath();
        context.rect(0,200,200,100);
        var grd = context.createRadialGradient(200,200,50,200,200,200);
        grd.addColorStop(0,"red");
        grd.addColorStop(0.5,"green");
        grd.addColorStop(1,"blue");
        context.fillStyle = grd;
        context.fill();

4. 文本

  • font - 定义字体
  • fillText(text,x,y) - 在 canvas 上绘制实心的文本
  • strokeText(text,x,y) - 在 canvas 上绘制空心的文本
  • context.textBaseline 垂直对齐方式
  • context.textAlign 水平对齐方式
        // bold 可以省略   Arial 不可以省略
        context.font = "bold 50px Arial";
        context.textBaseline = "middle";
        context.textAlign = "center";
        var grd = context.createLinearGradient(0,0,200,0);
        grd.addColorStop(0,"blue");
        grd.addColorStop(0.5,"yellow");
        grd.addColorStop(1,"red");
        context.fillStyle = grd; 
        context.fillText("hello",100,100);

相关文章

  • canvas简述

    1. 绘制三角形 fill是填充面(实心),stroke是画线(空心) 2. 画圆 lineCap:线帽 有三个参...

  • 实现球体碰撞,使用这个库就够了

    一、简述 大家在用Canvas实现球体碰撞的动画场景时是否感觉无从下手,或者是知道怎么去实现,但是要进行大量的计算...

  • android随笔之自定义View的Canvas用法

    对Canvas进行操作: 1,Canvas平移 2,Canvas缩放 3,Canvas旋转 Canvas操作例子 ...

  • HTML5中canvas使用

    1、Canvas基础 2、Canvas画直线 3、Canvas画矩形 4、Canvas画文字 5、Canvas画圆...

  • 【微信小程序】canvas is empty

    问题:使用canvas时,报错:canvas is empty原因:因为创建canvas对象时,canvas还未加...

  • canvas

    @(HTML5)[canvas与SVG] [TOC] 十 、canvas canvas的基本用法 canvas是H...

  • canvas

    canvas canvas绘制 获取元素var canvas = document.getelementbyid(...

  • 2D学习之Bitmap

    Canvas canvas =new Canvas(); Paint paint =new Paint(); ca...

  • UICamera 世界转UI坐标

    Vector2 pos; Canvas canvas = GameObject.Find("Canvas"...

  • 明源云

    1.写出输入结果 2.输出结果 3.运用canvas绘制一个扇形4.列举8css3的新特性及简述其用法5.如何做到...

网友评论

      本文标题:canvas简述

      本文链接:https://www.haomeiwen.com/subject/rqyrgxtx.html