使用Canvas绘制图形

html文件

<!DOCTYPE html>
<head>
  <meta charset="UTF-8">
  <title>canvas</title>
  <script type="text/javascript" src="script.js" charset="gb2312"></script>
</head>
<body "draw('canvas');">
  <h1>canvas</h1>
  <canvas id="canvas" width="400" height="300"></canvas>
</body>
</html>

script.js脚本文件

/*
// 绘制矩形
function draw(id) {

  // 取得canvas元素,并获取上下文
  var canvas = document.getElementById(id)
  if (canvas == null)
    return false;
  var context = canvas.getContext('2d');

  // 填充
  context.fillStyle = '#EEEEFF';
  context.fillRect(0,0,400,300);

  // 填充
  context.fillStyle = 'red';
  context.fillRect(50,50,100,100)

  // 绘制边框
  context.strokeStyle='blue'
  context.lineWidth=1;
  context.strokeRect(50,50,100,100)

  // 擦除
  context.clearRect(50,50,100,100)
  
}
*/

// 绘制圆形
function draw(id) {

  var canvas = document.getElementById(id)
  if (canvas == null)
    return false
  var context = canvas.getContext('2d');

  context.fillStyle = '#EEEEFF'
  context.fillRect(0,0,400,300)

  var n = 0;
  for(var i = 0; i<10; i++) {
    // 开始创建路径
    context.beginPath();
    // 创建圆形路径
    context.arc(i*25,i*25,i*10,0,Math.PI*2,true);
    // 关闭路径
    context.closePath();
    // 设定绘制样式,进行图形绘制
    context.fillStyle='rgba(255,0,0,0.25)';
    context.fill();
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值