画布功能

The <canvas> element is used to draw graphics, on the fly, on a web page.

 

一、概述

 

What is Canvas?

 

The HTML5 <canvas> element is used to draw graphics, on the fly, via scripting (usually JavaScript).

 

The <canvas> element is only a container for graphics, you must use a script to actually draw the graphics.

 

A canvas is a drawable region defined in HTML code with height and width attributes.

 

Canvas has several methods for drawing paths, boxes, circles, characters, and adding images.

 

二、步骤

 

(1)

Create a Canvas

 

A canvas is specified with the <canvas> element.

 

Specify the id, width, and height of the <canvas> element:

 

<canvas id="myCanvas" width="200" height="100"></canvas>

 

(2)

Draw With JavaScript

 

The <canvas> element has no drawing abilities of its own.

 

All drawing must be done inside a JavaScript:

 

<script type="text/javascript">

var c=document.getElementById("myCanvas");

var ctx=c.getContext("2d");

ctx.fillStyle="#FF0000";

ctx.fillRect(0,0,150,75);

</script>

 

JavaScript uses the id to find the <canvas> element:

 

var c=document.getElementById("myCanvas");

Then, create a context object:

 

var ctx=c.getContext("2d");

The getContext("2d") object is a built-in HTML5 object, with many methods to draw paths, boxes, circles, characters, images and more.

 

The next two lines draws a red rectangle:

 

ctx.fillStyle="#FF0000";

ctx.fillRect(0,0,150,75);

The fillStyle attribute makes it red, and the fillRect attribute specifies the shape, position, and size.

 

Understanding Coordinates

 

The fillRect property above had the parameters (0,0,150,75).

 

This means: Draw a 150x75 rectangle on the canvas, starting at the top left corner (0,0).

 

The canvas' X and Y coordinates are used to position drawings on the canvas.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值