js 固定四点绘制梯形

可以通过 Canvas API 来实现固定四点绘制梯形。具体可以按照如下步骤:

  1. 创建一个 Canvas 元素。
<canvas id="myCanvas"></canvas>

  1. 获取 Canvas 上下文。
const canvas = document.getElementById('myCanvas');
const ctx = canvas.getContext('2d');

  1. 定义四个坐标点。
const x1 = 50;
const y1 = 50;
const x2 = 100;
const y2 = 100;
const x3 = 150;
const y3 = 100;
const x4 = 200;
const y4 = 50;

  1. 绘制路径。
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.lineTo(x3, y3);
ctx.lineTo(x4, y4);
ctx.closePath();

  1. 填充颜色。
ctx.fillStyle = '#f00';
ctx.fill();

完整代码如下:

<canvas id="myCanvas"></canvas>

<script>
  const canvas = document.getElementById('myCanvas');
  const ctx = canvas.getContext('2d');
  
  const x1 = 50;
  const y1 = 50;
  const x2 = 100;
  const y2 = 100;
  const x3 = 150;
  const y3 = 100;
  const x4 = 200;
  const y4 = 50;
  
  ctx.beginPath();
  ctx.moveTo(x1, y1);
  ctx.lineTo(x2, y2);
  ctx.lineTo(x3, y3);
  ctx.lineTo(x4, y4);
  ctx.closePath();
  
  ctx.fillStyle = '#f00';
  ctx.fill();
</script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值