前端通过不同方式画等腰梯形

1.css画

说明:css画出倒梯形

javascript 复制代码
  <div class="outer-box">
  <div class="gradient_bg"></div>
  </div>
javascript 复制代码
.outer-box{
  width: 50px;
  height: 50px;
  position: relative;
  //overflow: hidden;
}
/*使用transform属性后,梯形样式会溢出父容器*/
.gradient_bg {
  position: absolute;
  //left: 50%;
  top: 50%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, #010101 13.46%, #1B3D55 59.95%);
  transform: perspective(0.1em) rotateX(179deg);
  border-radius: 5px;
}

2.canvas

说明:画出正梯形

javascript 复制代码
    <canvas id="trapezoidCanvas" width="200" height="100"></canvas>
javascript 复制代码
<script>
export default {
  name: "index",
  mounted() {
    this.ininShape()
  },
  methods:{
    ininShape(){
      // 获取画布元素
      const canvas = document.querySelector('#trapezoidCanvas');
      const ctx = canvas.getContext('2d');

      // 设置梯形的参数
      const topWidth = 30;
      const bottomWidth = 100;
      const height = 50;
      const trapezoidColor = '#008000'; // 绿色

      // 计算梯形的顶点坐标
      const xTopLeft = (canvas.width - topWidth) / 2;
      const xTopRight = xTopLeft + topWidth;
      const xBottomLeft = (canvas.width - bottomWidth) / 2;
      const xBottomRight = xBottomLeft + bottomWidth;
      const yTop = (canvas.height - height) / 2;
      const yBottom = yTop + height;

      // 绘制梯形
      ctx.fillStyle = trapezoidColor;
      ctx.beginPath();
      ctx.moveTo(xTopLeft, yTop);
      ctx.lineTo(xTopRight, yTop);
      ctx.lineTo(xBottomRight, yBottom);
      ctx.lineTo(xBottomLeft, yBottom);
      ctx.closePath();
      ctx.fill();
    }
  }
}
</script>

如何想让它倒过来;直接沿着X旋转180度。

javascript 复制代码
transform: rotateX(180deg)
相关推荐
疯狂的沙粒21 分钟前
在web-view 加载的本地及远程HTML中调用uniapp的API及网页和vue页面是如何通讯的?
前端·uni-app·html
小妖66624 分钟前
html 滚动条滚动过快会留下边框线
前端·html
heroboyluck38 分钟前
Svelte 核心语法详解:Vue/React 开发者如何快速上手?
前端·svelte
海的诗篇_40 分钟前
前端开发面试题总结-JavaScript篇(二)
开发语言·前端·javascript·typescript
琹箐1 小时前
ant-design4.xx实现数字输入框; 某些输入法数字需要连续输入两次才显示
前端·javascript·anti-design-vue
程序员-小李1 小时前
VuePress完美整合Toast消息提示
前端·javascript·vue.js
Uyker2 小时前
从零开始制作小程序简单概述
前端·微信小程序·小程序
EndingCoder6 小时前
React从基础入门到高级实战:React 实战项目 - 项目三:实时聊天应用
前端·react.js·架构·前端框架
阿阳微客7 小时前
Steam 搬砖项目深度拆解:从抵触到真香的转型之路
前端·笔记·学习·游戏
德育处主任Pro7 小时前
『React』Fragment的用法及简写形式
前端·javascript·react.js