Canvas 梯形水池

**HTML的 canvas 水位图 **
*

利用正弦函数做出波浪

function init(height,width,xOffset,speed,rangeValue,nowRange) {
    this.canvas=document.querySelector('#wave');
    this.canvas.height = height;
    this.canvas.width = width;
    this.ctx = this.canvas.getContext('2d');
    this.xOffset = xOffset;  // 初始偏移
    this.speed = speed;  // 偏移速度
    this.isDrawCircle=false;//画圆
    this.rangeValue = rangeValue;
    // 初始水位
    this.nowRange = nowRange;
    this.drawSin=drawSin;
    this.drawCircle=drawCircle;
    this.draw=draw;
    requestAnimationFrame(this.draw.bind(this));
  }
  function drawSin() {
    const points = [];
    const canvasWidth = this.canvas.width;
    const canvasHeight = this.canvas.height;
    const startX = 0;
    const waveWidth = 0.1; // 波浪宽度,数越小越宽
    const waveHeight = 10; // 波浪高度,数越大越高
    // const xOffset = 0; // 水平位移
    this.ctx.beg
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是建立梯形画布的步骤: 1. 首先,我们需要在HTML文件中创建一个canvas元素,并设置它的宽度和高度。例如,我们可以设置宽度为500像素,高度为400像素。 ```html <canvas id="myCanvas" width="500" height="400"></canvas> ``` 2. 接下来,我们需要获取canvas元素的上下文对象,以便我们可以使用它来绘制形。我们可以使用getContext()方法来获取上下文对象。在这个例子中,我们将使用2D上下文。 ```javascript var canvas = document.getElementById("myCanvas"); var ctx = canvas.getContext("2d"); ``` 3. 现在,我们可以开始绘制梯形。我们可以使用以下步骤来绘制梯形: a. 首先,我们需要定义梯形的四个顶点的坐标。在这个例子中,我们将使用以下坐标: ```javascript var x1 = 100; var y1 = 100; var x2 = 400; var y2 = 100; var x3 = 300; var y3 = 300; var x4 = 200; var y4 = 300; ``` b. 接下来,我们需要使用beginPath()方法开始绘制路径。 ```javascript ctx.beginPath(); ``` c. 然后,我们需要使用moveTo()方法将绘游标移动到第一个顶点的位置。 ```javascript ctx.moveTo(x1, y1); ``` d. 接下来,我们需要使用lineTo()方法绘制从第一个顶点到第二个顶点的线段。 ```javascript ctx.lineTo(x2, y2); ``` e. 然后,我们需要使用lineTo()方法绘制从第二个顶点到第三个顶点的线段。 ```javascript ctx.lineTo(x3, y3); ``` f. 接下来,我们需要使用lineTo()方法绘制从第三个顶点到第四个顶点的线段。 ```javascript ctx.lineTo(x4, y4); ``` g. 最后,我们需要使用closePath()方法将路径闭合,并使用fill()方法填充梯形。 ```javascript ctx.closePath(); ctx.fill(); ``` 4. 完整的代码如下: ```html <canvas id="myCanvas" width="500" height="400"></canvas> <script> var canvas = document.getElementById("myCanvas"); var ctx = canvas.getContext("2d"); var x1 = 100; var y1 = 100; var x2 = 400; var y2 = 100; var x3 = 300; var y3 = 300; var x4 = 200; var y4 = 300; ctx.beginPath(); ctx.moveTo(x1, y1); ctx.lineTo(x2, y2); ctx.lineTo(x3, y3); ctx.lineTo(x4, y4); ctx.closePath(); ctx.fill(); </script> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值