Canvas 绘制矩形,圆形,不规则图形(线条),渐变等图像效果

绘制矩形:

getContext("2d") 对象是内建的 HTML5 对象,拥有多种绘制路径、矩形、圆形、字符以及添加图像的方法。

fillStyle 方法将其染成红色,fillRect 方法规定了形状、位置和尺寸。

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <style type="text/css">
         #myCanvas{ border: 1px dotted #aaaaaa; padding:10px; }
        </style>
    </head>

    <body>
        <canvas id="myCanvas" width="300" height="200"> </canvas>
        <script>
        var c = document.getElementById("myCanvas");
        var cxt=c.getContext("2d");
        cxt.fillStyle="#FF0000";
        cxt.fillRect(10,10,150,100);
        </script>
    </body>

</html>

绘制圆形:

创建圆形路径时需要用到对象的arc方法,方法定义如:XXX.arc(x,y,radius,startAngle,endAngle,anticlockwise)

x为圆形起点的横坐标,y为圆形起点的纵坐标,radius为圆形半径,startAngle为开始角度,endAngle为结束角度,anticlockwise是否按顺时针方向进行绘制。

XXX.beginPath() 创建路径,XXX.closePath() 结束路径,然后XXX.fill() 渲染即可。

<script>
        var c = document.getElementById("myCanvas");
        var cxt=c.getContext("2d");
        cxt.fillStyle="#ff0000";
        cxt.beginPath();
        cxt.arc(50,50,30,0,Math.PI*2,true);
        cxt.closePath();
        cxt.fill();
</script>

绘制线条形:

moveTox,y)指定从何处(x,y)开始,lineTo指定在何处结束,来绘制一条线:

然后 调用stroke()实现绘制。

<script>
        var c = document.getElementById("myCanvas");
        var cxt=c.getContext("2d");
        cxt.moveTo(20,20);
        cxt.lineTo(40,90);
        cxt.lineTo(80,70);
        cxt.lineTo(130,90);
        cxt.stroke();
</script>

绘制渐变效果:

使用您指定的颜色来绘制渐变背景,跟绘制普通矩形差不多,只是fillStyle的值为渐变色了而已。

线性渐变:

方法 createLinearGradient(0,0,150,100); 指定了创建线性渐变色范围

方法 grd.addColorStop(0,"#ff0000");
  grd.addColorStop(1,"#00ff00"); 为渐变色指定渐变“开始与结束”

假如你想实现一个上下的线性渐变的效果,那么你应该在调用createLinearGradient(x0,y0,x1,y1) 创建渐变的时候保证点(x0,y0),点(x1,y1)连成的直线为竖直直线,也就是x0=x1。

当然你也可以实现对角线渐变的效果,只要(x0,y0) (x1,y1)组成一条对角线就可以了。

创建上下渐变和对角线渐变可以这样来:

createLinearGradient(0,0,0,300);    // 创建上下渐变

createLinearGradient(10,10,300,300);    // 创建对角线渐变

<script>
        var c = document.getElementById("myCanvas");
        var cxt=c.getContext("2d");
        var grd = cxt.createLinearGradient(0,0,150,100);
        grd.addColorStop(0,"#ff0000");
        grd.addColorStop(1,"#00ff00");
        cxt.fillStyle = grd;
        cxt.fillRect(0,0,150,70);
</script>

径向渐变:

createRadialGradient(x0,y0,r0,x1,y1,r1)

创建一个沿两个圆之间的锥面绘制渐变。前三个参数代表一个圆心为(x0,y0)半径为r0的开始圆,后三个参数代表圆心为(x1,y1)半径为r1的结束圆。

比如 var grd = cxt.createRadialGradient(100,100,30,100,100,70)

就创建了一个由圆心(100,100)半径为30的圆 ---》 圆心(100,100)半径为70的圆  的渐变效果。

时间: 2024-08-25 14:03:14

Canvas 绘制矩形,圆形,不规则图形(线条),渐变等图像效果的相关文章

canvas 绘制 矩形 圆形

<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head> <title></title></head><body> <!-- canvas的width/height默认都是300 --> <canvas id="fillRect" width="100" height=&qu

canvas绘制矩形

canvas绘制矩形 方法 fillRect(x, y, width, height) 画一个实心的矩形 clearRect(x, y, width, height) 清除一块儿矩形区域 strokeRect(x, y, width, height) 画一个带边框的矩形 rect(x, y, width, height) 直接画一个矩形 画一个矩形 const canvas = document.getElementById('canvas'); const ctx = canvas.getCo

html5 canvas绘制矩形和圆形

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body onload="draw(),drawarc()"> <!--绘制的步骤:获取canvas元素->取得上下文->填充与绘制边框->设定绘图样式--> <!--绘制其他复杂图

HTML5—canvas绘制图形(1)

1.canvas基础知识 canvas元素是HTML5中新增的一个重要的元素,专门用来绘制图形,不过canvas本身不具备画图的能力,在页面中放置了canvas元素,就相当于在页面中放置了一块矩形的“画布”,我们可以利用js脚本在“画布”上绘制图形. 1.1canvas元素 在利用canvas绘制图形之前,我们首先需要在页面中放置一个canvas元素,如下代码: <canvas id="mycanvas" width="400" height="40

使用html5 canvas绘制圆形或弧线

注意:本文属于<html5 Canvas绘制图形入门详解>系列文章中的一部分.如果你是html5初学者,仅仅阅读本文,可能无法较深入的理解canvas,甚至无法顺畅地通读本文.请点击上述链接以了解使用html5 canvas绘制图形的完整内容. 在html5中,CanvasRenderingContext2D对象也提供了专门用于绘制圆形或弧线的方法,请参考以下属性和方法介绍: arc(x, y, radius, startRad, endRad, anticlockwise) 在canvas画

canva基本使用和绘制矩形

1:canvas元素及基本定义与使用: <canvas id="test" width="400" height="400"></canvas> if(test.getContext) //判断是否有画笔 { var cdx = test.getContext("2d");//代表2d绘图 } 2:###canvas绘制矩形 HTML中的元素canvas只支持一种原生的图形绘制:矩形.所有其他的图形的绘

HTML5 Canvas 学习笔记(canvas绘制线条、矩形、多边形、圆形、圆环、组合图形、文字、自定义图像)

学习资源:http://www.w3school.com.cn/html5/html_5_canvas.asp   http://blog.csdn.net/clh604/article/details/8536059   http://www.jb51.net/html5/70307.html 一.对 canvas 的理解 <canvas>标签是 HTML5 中的新标签,像所有的 dom 对象一样它有自己本身的属性.方法和事件. canvas 是用来在网页上绘制图形的(我们通常称之为画布),

Canvas入门(1):绘制矩形、圆、直线、曲线等基本图形

来源:http://www.ido321.com/968.html 一.Canvas的基础知识 Canvas是HTML 5中新增的元素,专门用于绘制图形.canvas元素就相当于一块"画布",一块无色的透明区域.须要利用JavaScript编写在当中进行绘画的脚本. 在页面放置canvas元素非常easy.利用<canvas>标签.同一时候指定几个主要的属性:id,width和height.接下来通过几个小案例,跟我入门canvas吧~~~^_^~~~ 二.Canvas小案

微信小程序开发—(八)canvas绘制图形

一.小知识 (1).API接口 (2).context 对象的方法列表 二.步骤 wxml中: <canvas canvas-id="myCanvas" class="myCanvas" ></canvas> 在js文件onLoad: function() {}的方法中开始编写代码 1.创建一个 Canvas 绘图上下文 CanvasContext const ctx = wx.createCanvasContext('myCanvas')