html5 canvas

  1. WHAT: canvas 是什么?
    canvas 是画布,也就是一块矩形区域。
  2. WHY: canvas 作用是什么?
    用它画图像。

    测试内容:

    1. 画 矩形
    2. 画三角形
    3. 画圆
    4. 画线性渐变矩形
    5. 画图片

示例代码:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>test html5 canvas</title>
</head>
<body>
<canvas id="myCanvas" width="500" height="500" style="border:1px red solid"></canvas>
<div style="display:none">
<img id="img" src = '../images/psuCA0V6ZG5.jpg'>
</div>


<script>
function createCanvas(){
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
// stroke rectangle
// strokeRect(context);

// stroke line
// strokeLine(ctx);

// stroke arc
// strokeArc(ctx);

// stroke linear gradient
// strokeLinearGradient(ctx);
drawImage(ctx);

}


function strokeRect(ctx){
context.fillStyle="red";
context.fillRect(0,0,280,90);
}


function strokeLine(ctx){
ctx.moveTo(100, 100);
ctx.lineTo(500,400);
ctx.lineTo(100,400);
ctx.lineTo(100, 100);
ctx.stroke();
} 


function strokeArc(ctx){
ctx.fillStyle="red";
ctx.beginPath();
ctx.arc(200,200,100,0, 2*Math.PI, false);
ctx.closePath();
ctx.fill();
}


function strokeLinearGradient(ctx){
var clg = ctx.createLinearGradient(100,100, 300,100);
clg.addColorStop(0, "red");
clg.addColorStop(1, "cyan");
ctx.fillStyle = clg;
ctx.fillRect(100,100, 300,200);

}


function drawImage(ctx){
// var img = new Image();
//  img.src = "../images/psuCA0V6ZG5.jpg";
//  ctx.drawImage(img,0,0);
var img2 = document.getElementById("img");
    ctx.drawImage(img2,0,0);
} 


window.onload = createCanvas;
</script>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值