canvas拖拽矩形四个点调整矩形大小_canvas基本使用

一、绘制矩形

fillRect(x,y,width,height) 填充矩形

fillStyle = "rbg(x,x,x)" 填充矩形颜色

strokeRect(x,y.width,height) 绘制矩形边框

strokeRect = "rgb(x,x,x)" 绘制矩形边框颜色

clearRect(x,y,width,height) 清除矩形区域

使用案例:

<html lang="en"><head>    <meta charset="UTF-8">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>Documenttitle>head><body>    <canvas id="canvas" width="500px" height="500px">canvas>body><script>    const canvas = document.querySelector("#canvas");    const ctx = canvas.getContext("2d");    //填充矩形    ctx.fillStyle = "rgb(100,0,50)";    ctx.fillRect(0,0,200,100);    //绘制矩形边框    ctx.strokeStyle = "rgb(0,150,210)";    ctx.strokeRect(100,100,200,100);    //清除矩形区域    ctx.clearRect(20,20,50,50);script>html>

二、绘制路径

  1. beginPath() 创建路径

  2. moveTo(x, y) 起始坐标点

  3. lineTo(x,y) 绘制坐标点

  4. closePath() 闭合路径

  5. stroke() 绘制边线

  6. fill() 填充图形

使用案例:

<html lang="en"><head>    <meta charset="UTF-8">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>Documenttitle>head><body>    <canvas id="canvas" width="500px" height="500px">canvas>body><script>    const canvas = document.querySelector("#canvas");    const ctx = canvas.getContext("2d");    ctx.beginPath();    ctx.moveTo(100,50);    ctx.lineTo(150,100);    ctx.lineTo(100,150);    ctx.closePath();    ctx.stroke();    // ctx.fill();script>html>

三、绘制圆弧

1、arc(x, y, r, start, end, direction)  x,y是圆心坐标;r是半径;start,end是开始和结束弧度;direction是布尔值 顺时针(false)或逆时针(true)。

radians=(Math.PI/180)*angle  //角度转换成弧度

案例使用:

<html lang="en"><head>    <meta charset="UTF-8">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>Documenttitle>head><body>    <canvas id="canvas" width="500px" height="500px">canvas>body><script>    const canvas = document.querySelector("#canvas");    const ctx = canvas.getContext("2d");    ctx.beginPath();    ctx.arc(100,100,50,0,Math.PI,true);    // ctx.closePath(); //闭合    ctx.stroke();script>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值