HTML5(二)basic drawing

HTML5(二)basic drawing

1.coordinate
(0,0) is at the top and left corner of canvas.

2. Stroke and Fill
Stroke
line, the picture are base on the lines.

Fill
area filled with something.

We can see the difference in StrokeRect and FillRect. My page test2.html is:

<canvas id="test2" width="200" height="200" style="background-color: grey">
your browser does not support this.
</canvas>
<input type="button" value="strokeRect" οnclick="strokeRect();"/>
<input type="button" value="fillRect" οnclick="fillRect();"/>

<script type="text/javascript">
function strokeRect(){
var canvas = document.getElementById('test2');
var ctx=canvas.getContext("2d");

ctx.clearRect(0,0,200,200);
ctx.strokeStyle="blue";
ctx.strokeRect(10,10,180,180);
}

function fillRect(){
var canvas = document.getElementById('test2');
var ctx=canvas.getContext("2d");

ctx.clearRect(0,0,200,200);
ctx.fillStyle="blue";
ctx.fillRect(10,10,180,180);
}
</script>

3.Color
stokeStyle is the css of line.
fillStyle is the css of fill area.

4.Basic drawing method
moveTo(x,y): move the pen to the position (x,y)

lineTo(x,y): draw a straight line from current position to (x,y)

arc(x,y,radius,startAngle,endAngle,anticlockwise): draw a arc.

quadraticCurveTo(cp1x,cp1y,x,y)
bezierCurveTo(cp1x,cp1y,cp2x,cp2y,x,y): read doc.

rect(x,y,width,height): rectangle

5.Drawing Path
beginPath(): start to draw to context(in memory, do not display in the screen)
stroke(): all the lines will displayed in the screen
closePath(): This method is not nessary and it will give the picture a occlusion line.
fill(): give the picture a occlusion line and the fill the picture

all the fill style picture have not drawing path, they display in the screen immediately.

my example test2-2.html is :
<canvas id="test2" width="200" height="200" style="border:1px solid #c3c3c3;">
update your browser, please.
</canvas>
<input type="button" value="draw" οnclick="drawTri();"/>
<input type="button" value="clean" οnclick="clearTri();"/>

<script type="text/javascript">
function drawTri(){
var canvas = document.getElementById('test2');
var ctx=canvas.getContext("2d");

ctx.beginPath();
ctx.moveTo(75,50);
ctx.lineTo(100,75);
ctx.lineTo(100,25);
ctx.fill();
}
function clearTri(){
var canvas = document.getElementById('test2');
var ctx=canvas.getContext("2d");
ctx.clearRect(0,0,200,200);
}
</script>

6.draw the grid
my example test2-3.html is like this:
<canvas id="test3" width="500" height="375" style="border:1px solid #c3c3c3;">
old browser.update right now!
</canvas>
<input type="button" value="draw grid" οnclick="drawMap();"/>
<input type="button" value="clean" οnclick="clearMap();"/>

<script type="text/javascript">
function drawMap(){
var canvas = document.getElementById('test3');
var ctx=canvas.getContext("2d");
ctx.beginPath();
for (var x = 0.5; x < 500; x += 10) {
ctx.moveTo(x, 0);
ctx.lineTo(x, 375);
}
for (var y = 0.5; y < 375; y += 10) {
ctx.moveTo(0, y);
ctx.lineTo(500, y);
}
ctx.strokeStyle = "#eee";
ctx.stroke();
}
function clearMap(){
var canvas = document.getElementById('test3');
var ctx=canvas.getContext("2d");
ctx.clearRect(0,0,500,375);
}
</script>

7. clean the canvas
clearRect(x,y,width,height)

references:
http://www.blogjava.net/myqiao/category/46360.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值