透明色rgba
fillStyle,strokeStyle
drawRect2()
fillRect()
strokeRect()
drawRect()改进
cxt.rect
rgba和hsla的最后一位是不透明度。css中可用的方式,此处都可以用字符串传进去
cxt.rect(x, y, width, height); 画矩形的API
cxt.fillRect(x, y, width, height);
cxt.strokeRect(x, y, width, height);
rect函数可以绘制矩形,传入左上角坐标和宽高
fillStyle strokeStyle 颜色值的写法:
#fff;
#643;
rgb(255,128,0)
rgba(100,100,100,0.8);
hsl(20,62%,28%)
hsla(40,82%,33%,0.6)
red
context.fillRect(x,y,width,height);将直接用当前定义的填充色绘制一个矩形;
context.strokeRect(x,y,width,height);将直接用当前定义的描边色绘制一个矩形;
context.rect(x,y,width,height);快速绘制矩形
笔记记录 - 4
笔记记录 - 3
绘制矩形api:
ctx.beginPath(); ctx.rect(x, y, width, height); ctx.closePath(); ctx.stroke(); ctx.fillRect(x, y, width, height); ctx.strokeRect(x, y, width, height);
fillStyle 和 strokeStyle 支持所有CSS支持的颜色表示。
context.fillRect(x,y,width,height)
context.strokeRect(x,y,width,height)矩形左上角坐标和长宽
表示颜色的方式
![]()
绘制矩形:cxt.rect(x,y,width,height)左上角的横纵坐标、矩形的宽和高
cxt.fillRect(x,y,width,height)
cxt.strokeRect(x,y,width,height)
fillStyle,strokeStyle 只要是css支持的颜色值;就可以 以字符串的形式赋值
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>矩形</title>
</head>
<body>
<canvas id="canvas" style="border: 1px solid black;display: block;margin:10px auto;"></canvas>
<script type="text/javascript">
window.onload=function(){
var c=document.getElementById("canvas");
c.width=1024;
c.height=500;
var context=c.getContext("2d");
drawRect(context,100,100,200,200,10,"#058","red");
drawRect2(context,200,200,200,200,10,"#058","rgba(0,256,0,0.5)");
}
function drawRect(ctx,x,y,width,height,borderWidth,borderColor,fillColor){
ctx.beginPath();//开始绘制新路径
ctx.rect(x,y,width,height);//矩形绘制,省去了路径绘制
ctx.closePath();//结束路径,具有自动补全的作用
ctx.lineWidth=borderWidth;//描边的粗细
ctx.fillStyle=fillColor;//所要填充的颜色
ctx.strokeStyle=borderColor;//描边的颜色
ctx.fill();//填充颜色,要先填充再描边,这样描边的颜色就不会被填充的颜色所覆盖;
ctx.stroke();//描边
}
function drawRect2(ctx,x,y,width,height,borderWidth,borderColor,fillColor){
ctx.beginPath();//开始绘制新路径
ctx.rect(x,y,width,height);
ctx.closePath();//结束路径,具有自动补全的作用
ctx.lineWidth=borderWidth;//描边的粗细
ctx.fillStyle=fillColor;//所要填充的颜色
ctx.strokeStyle=borderColor;//描边的颜色
ctx.fillRect(x,y,width,height);//
ctx.strokeRect(x,y,width,height);
}
</script>
</body>
</html>