HTML <canvas> 学习笔记
2015-06-11 17:39
435 查看
Professional JavaScript for Web Developers P552
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:
View Code
arc(x, y, radius, startAngle, endAngle, counterclockwise)
arcTo(x1, y1, x2, y2, radius)
lineTo(x, y)
moveTo(x, y)
rect(x, y, width, height)
利用HTML <canvas>做的弹球,[点击这里],具体细节还需要更改~
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:
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <script> window.onload = function () { var drawing = document.getElementById("drawing"); // make sure <canvas> is complet supported if (drawing.getContext) { var context = drawing.getContext("2d"); // draw a red rectangle context.fillStyle = "#ff0000"; context.fillRect(10, 10, 50, 50); // draw a blue rectangle that's semi-transparent context.fillStyle = "rgba(0, 0, 255, 0.5)"; context.fillRect(30, 30, 50, 50); // clear a rectangle that overlaps both of the previous rectangles context.clearRect(40, 40, 10, 10); } } </script> </head> <body> <canvas id="drawing" width="200" height="200">A drawing of something.</canvas> </body> </html>
View Code
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)
lineTo(x, y)
moveTo(x, y)
rect(x, y, width, height)
利用HTML <canvas>做的弹球,[点击这里],具体细节还需要更改~
相关文章推荐
- innerHTML对比DOM方法,哪个性能好?有什么区别?
- html 字符串拼接
- 为什么HTML使用<!DOCTYPE HTML>
- HTML<!DOCTYPE> 宣示
- html 界面跳转传值问题
- HTML特殊字符编码对照表
- HtmlHelper.Raw,<%%>,<%:%>,<%=%>的区别及使用
- html按钮跳转
- 一般框架中有一个空的index .html,是干什么用的?
- 不被select选择框遮挡的div层 (可拖动)-- 2007-12-21的博客搬家
- HTML代码导出EXCEL数字前面的0消失解决办法
- 辛星浅析html中的name和id的区别
- HTML select回显
- HtmlHelper
- HTML常用标签之DOCTYPE
- html DIV元素左右偏移方法,偏移后默认宽度仍浏览器宽度一致
- 记一个发HTML格式邮件的问题
- HTML小技巧
- HTML特殊字符过滤器
- HTML空格占位符