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:

 var drawing = document.getElementById("drawing");

 // make sure <canvas> is complet supported
if (drawing.getContext) {
var context = drawing.getContext("2d");
}

  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:

 <!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);
}
}
</script>
</head>
<body>
<canvas id="drawing" width="200" height="200">A drawing of something.</canvas>
</body>
</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:

 <!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>

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>做的弹球,[点击这里],具体细节还需要更改~

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

  1. js学习笔记:webpack基础入门(一)

    之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...

  2. PHP-自定义模板-学习笔记

    1.  开始 这几天,看了李炎恢老师的<PHP第二季度视频>中的“章节7:创建TPL自定义模板”,做一个学习笔记,通过绘制架构图.UML类图和思维导图,来对加深理解. 2.  整体架构图 ...

  3. PHP-会员登录与注册例子解析-学习笔记

    1.开始 最近开始学习李炎恢老师的<PHP第二季度视频>中的“章节5:使用OOP注册会员”,做一个学习笔记,通过绘制基本页面流程和UML类图,来对加深理解. 2.基本页面流程 3.通过UM ...

  4. 2014年暑假c#学习笔记目录

    2014年暑假c#学习笔记 一.C#编程基础 1. c#编程基础之枚举 2. c#编程基础之函数可变参数 3. c#编程基础之字符串基础 4. c#编程基础之字符串函数 5.c#编程基础之ref.ou ...

  5. JAVA GUI编程学习笔记目录

    2014年暑假JAVA GUI编程学习笔记目录 1.JAVA之GUI编程概述 2.JAVA之GUI编程布局 3.JAVA之GUI编程Frame窗口 4.JAVA之GUI编程事件监听机制 5.JAVA之 ...

  6. seaJs学习笔记2 – seaJs组建库的使用

    原文地址:seaJs学习笔记2 – seaJs组建库的使用 我觉得学习新东西并不是会使用它就够了的,会使用仅仅代表你看懂了,理解了,二不代表你深入了,彻悟了它的精髓. 所以不断的学习将是源源不断. 最 ...

  7. CSS学习笔记

    CSS学习笔记 2016年12月15日整理 CSS基础 Chapter1 在console输入escape("宋体") ENTER 就会出现unicode编码 显示"%u ...

  8. HTML学习笔记

    HTML学习笔记 2016年12月15日整理 Chapter1 URL(scheme://host.domain:port/path/filename) scheme: 定义因特网服务的类型,常见的为 ...

  9. DirectX Graphics Infrastructure(DXGI):最佳范例 学习笔记

    今天要学习的这篇文章写的算是比较早的了,大概在DX11时代就写好了,当时龙书11版看得很潦草,并没有注意这篇文章,现在看12,觉得是跳不过去的一篇文章,地址如下: https://msdn.micro ...

  10. ucos实时操作系统学习笔记——任务间通信(消息)

    ucos另一种任务间通信的机制是消息(mbox),个人感觉是它是queue中只有一个信息的特殊情况,从代码中可以很清楚的看到,因为之前有关于queue的学习笔记,所以一并讲一下mbox.为什么有了qu ...

随机推荐

  1. 【转载】softmax的log似然代价函数(求导过程)

    全文转载自:softmax的log似然代价函数(公式求导) 在人工神经网络(ANN)中,Softmax通常被用作输出层的激活函数.这不仅是因为它的效果好,而且因为它使得ANN的输出值更易于理解.同时, ...

  2. Summer training #3

    A:给一个包含字母 加号 括号的序列 要求你删除多余的括号然后输出 (待改) #include <bits/stdc++.h> #include <cstring> #incl ...

  3. [angular2/4/8]用ng new创建项目卡住的解决办法

    官方文档 英文版:https://angular.io/guide/quickstart 中文版:https://angular.cn/guide/quickstart Installing pack ...

  4. cnblogs设置各级标题样式和目录

    向博客园申请js权限 我们需要进入博客园自定义博客模板的页面,向博客园管理团队申请页面运行js的权限. [博客园]->[设置]->[博客设置],点击页面上的js权限申请,然后填写申请的理由 ...

  5. OkHttp3-基本用法(转)

    OkHttp 一个支持Http和Http/2,可适用于Android以及Java应用的网络请求客户端. 概述 Http是现代网络应用的所常用的协议,它是一种数据传输的媒介.执行高效的Http代码可以让 ...

  6. 制作中文字符集zh_CN.utf8的centos7系统

    以下是Dockerfile文件 ===================================== FROM centos:7ENV LANG=zh_CN.UTF-8 \ LANGUAGE=z ...

  7. 【C#-文件管理】 判断文件夹或文件是否存在,并创建文件夹或文件

    1.判断文件夹是否存在 Directory.Exists(“文件夹路径”)    返回false表示不存在,true表示存在 2.判断文件是否存在 File.Exists("文件路径&quo ...

  8. flex布局 justify-content:space-between; 解决最后一排数量不够自动向两端排列问题和flex布局的元素会有默认间隙(垂直间隙)

    1 .flex 布局两端对齐当最后一排数量不够时,会出现以下布局情况 实现效果: 解决方案:使用after伪类, 解决最后一排数量不够两端分布的情况.宽度就是每张图片的宽度 .list:after { ...

  9. removeProp(name)

    removeProp(name) 概述 用来删除由.prop()方法设置的属性集 随着一些内置属性的DOM元素或window对象,如果试图将删除该属性,浏览器可能会产生错误.jQuery第一次分配un ...

  10. web上传整个文件夹

    在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现. 先说下要求: PC端全平台支持,要求支持Windows,Mac,Linux 支持所 ...