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

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)
相关推荐
浩男孩3 分钟前
🍀终于向常量组件下手了,使用TypeScript 基于 TDesign二次封装常量组件 🚀🚀
前端·vue.js
玲小珑5 分钟前
LangChain.js 完全开发手册(十三)AI Agent 生态系统与工具集成
前端·langchain·ai编程
布列瑟农的星空16 分钟前
什么?sessionStorage可以跨页签?
前端
苏打水com16 分钟前
网易前端业务:内容生态与游戏场景下的「沉浸式体验」与「性能优化」实践
前端·游戏·性能优化
恋猫de小郭19 分钟前
React 和 React Native 不再直接归属 Meta,React 基金会成立
android·前端·ios
掘金安东尼23 分钟前
前端周刊434期(2025年9月29日–10月5日)
前端·javascript·面试
brzhang28 分钟前
当我第一次看到 snapDOM,我想:这玩意儿终于能解决网页「截图」这破事了?
前端·后端·架构
掘金安东尼33 分钟前
前端周刊433期(2025年9月22日–9月28日)
前端·javascript·github
万少44 分钟前
我的HarmonyOS百宝箱
前端
江城开朗的豌豆1 小时前
uni-app弹层遮罩难题?看我如何见招拆招!
前端·javascript·微信小程序