HTML <canvas> 学习笔记

Professional JavaScript for Web Developers    P552

Basic Usage

  The <canvas> element requires at least its width and height attributes to be set in order to indicate the size of the drawing to be created. Any content appearing  between the opening and closing tags is fallback data that is displayed only if the <canvas> element isn‘t supported.

  To begin with drawing on a canvas, you need to retrieve a drawing context. A reference to a drawing context is retrieved using the getContext() method and passing in the name of the context. For example, passing "2d" retrieves a 2D context object:

1 var drawing = document.getElementById("drawing");
2
3 // make sure <canvas> is complet supported
4 if (drawing.getContext) {
5     var context = drawing.getContext("2d");
6 }

  The coordinates in a 2D context begin at the upper-left of the <canvas> element, which is considered point(0, 0). All coordinate values are calculated in relation to that point. By default, the width and height indicate how many pixels are available in each direction.

Fills and Strokes

  Fill automatically fills in the shape with a specific style (color, gradient, or image) while stroke colors only the edges. Most of the 2D context operations have both fill and stroke variant, and how they are displayed is based on a couple of properties: fillStyle and strokeStyle.

  Both properties can be set to a string, a gradient object, or a pattern object. A string value indicates a color defined using one of the various CSS color formats: name, hex code, rgb, rgba, hsl, or hsla.

Drawing Rectangles

  There are three methods for working with rectangles: fillRect(), strokeRect(), and clearRect(). Each of these methods accepts four arguments: the x-coordinate of the rectangle, the y-coordinate of the rectangle, the width of the rectangle, and the height of the rectangle. Each of these arguments is considered to be in pixels.

  The fillRect() method is used to draw a rectangle that is filled with a specific color onto the canvas. The fill color is specified using the fillStyle property, for example:

 1 <!DOCTYPE html>
 2 <html>
 3 <head lang="en">
 4     <meta charset="UTF-8">
 5     <title></title>
 6     <script>
 7         window.onload = function () {
 8             var drawing = document.getElementById("drawing");
 9
10             // make sure <canvas> is complet supported
11             if (drawing.getContext) {
12                 var context = drawing.getContext("2d");
13
14                 // draw a red rectangle
15                 context.fillStyle = "#ff0000";
16                 context.fillRect(10, 10, 50, 50);
17
18                 // draw a blue rectangle that‘s semi-transparent
19                 context.fillStyle = "rgba(0, 0, 255, 0.5)";
20                 context.fillRect(30, 30, 50, 50);
21             }
22         }
23     </script>
24 </head>
25 <body>
26     <canvas id="drawing" width="200" height="200">A drawing of something.</canvas>
27 </body>
28 </html>

  You can earse an area of the canvas by using the clearRect() method. This method is used to make an area of the drawing context transparent. Here is an example:

 1 <!DOCTYPE html>
 2 <html>
 3 <head lang="en">
 4     <meta charset="UTF-8">
 5     <title></title>
 6     <script>
 7         window.onload = function () {
 8             var drawing = document.getElementById("drawing");
 9
10             // make sure <canvas> is complet supported
11             if (drawing.getContext) {
12                 var context = drawing.getContext("2d");
13
14                 // draw a red rectangle
15                 context.fillStyle = "#ff0000";
16                 context.fillRect(10, 10, 50, 50);
17
18                 // draw a blue rectangle that‘s semi-transparent
19                 context.fillStyle = "rgba(0, 0, 255, 0.5)";
20                 context.fillRect(30, 30, 50, 50);
21
22                 // clear a rectangle that overlaps both of the previous rectangles
23                 context.clearRect(40, 40, 10, 10);
24             }
25         }
26     </script>
27 </head>
28 <body>
29     <canvas id="drawing" width="200" height="200">A drawing of something.</canvas>
30 </body>
31 </html>

Drawing Paths

  Paths allow you to create complex shapes and lines. To start creating a path, you must first call beginPath() to indicate that a new path has begun. After that, the following methods can be called to create the path:

  • arc(x, y, radius, startAngle, endAngle, counterclockwise)
  • arcTo(x1, y1, x2, y2, radius)
  • be
时间: 2024-10-27 06:53:12

HTML <canvas> 学习笔记的相关文章

html5 canvas 学习笔记(一)

一.canvas元素API canvas元素并未提供很多API,实际上他只提供了两个属性与三个方法: 1.canvas元素属性 width 属性:与 height 属性: canvas元素绘图表面的宽度,在默认状况下,浏览器会将canvas元素大小设定成与绘图表面大小一致,然而如果在css中覆写了元素大小,那么浏览器则会将绘图表面进行缩放,使之符合元素尺寸.其值为非负整数,默认值为300. 2.canvas元素方法 getContext()方法: 返回与该canvas元素相关的绘图环境对象,每个

[转载]Android Bitmap和Canvas学习笔记

http://blog.chinaunix.net/uid-20771867-id-3053339.html [转载]Android Bitmap和Canvas学习笔记,布布扣,bubuko.com

Canvas 学习笔记1

#Canvas 学习笔记1 @[Canvas,Nunn,HTML5,javascript] ##前言 相信大家多多少少都有了解过`Canvas`,这里我就不多做解释了,网上也充斥了这方面的知识,很多人看了之后,其实发现作用非常小,因为似乎这些东西什么也做不了,本着学习提升自己,造福大家,我打算把我学习`Canvas`的历程记录在这里. 首先推荐大家先看看阮一峰大大写的这个[Canvas API](http://javascript.ruanyifeng.com/htmlapi/canvas.ht

【HTML】【学习】 Canvas学习笔记【上】

Canvas是HTML新增的开发跨平台动画和游戏的标准解决方案,能够实现对图像和视频进行像素级操作. 一.在页面中添加canvas元素 就像添加一个普通div一样,canvas的添加方式为: <canvas id = "myCanvas" width = "200px" height = "100px">不支持时显示的部分</canvas> 可以用对div添加样式的方法使用CSS对Canvas添加样式 二.使用Canvas

【HTML】【学习】 2、Canvas学习笔记【上】

Canvas是HTML新增的开发跨平台动画和游戏的标准解决方案,能够实现对图像和视频进行像素级操作. 一.在页面中添加canvas元素 就像添加一个普通div一样,canvas的添加方式为: <canvas id = "myCanvas" width = "200px" height = "100px">不支持时显示的部分</canvas> 可以用对div添加样式的方法使用CSS对Canvas添加样式 二.使用Canvas

canvas学习笔记

学习网址:https://developer.mozilla.org/zh-CN/docs/Web/API/Canvas_API <canvas> 是 HTML5 新增的元素,可使用JavaScript脚本来绘制图形. canvas可以用来做下面的事:

【canvas学习笔记一】基本认识

<canvas>标签定义了一块画布,画布可以在网页中绘制2D和3D图象,现在先学习如何绘制2D图象,绘制3D图象属于WebGL的内容(也就是网页版的OpenGL,3D图形接口). 属性 <canvas>只有width和height两个属性.如果没有设置width和height属性,canvas的默认初始大小是宽300px,高150px.如果通过CSS来设置canvas的宽高,而设置的宽高不是默认大小的比例,则canvas呈现的图象会失真变形.所以,建议用JavaScript来设置c

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学习笔记一

为了研究pixi库,就顺带从头到位学习下canvas吧 判断支持力度 var webgl = (function() { try { var canvas = document.createElement('canvas'); return !!window.WebGLRenderingContext && (canvas.getContext('webgl') || canvas.getContext('experimental-webgl')); } catch (e) { retur

Canvas学习笔记——动画中的三角学

示例1,跟随鼠标的键头: 需要掌握一个重要的公式,这个方法返回从 x 轴到点 (x,y) 之间的角度 Math.atan2(dy,dx); 关键代码: 1 function Arrow() { 2 this.x = 0; 3 this.y = 0; 4 this.rotate = 0; 5 this.color = '#ffff00'; 6 } 7 8 Arrow.prototype.draw = function(context) { 9 context.save(); 10 context.