当前位置:首页 > JavaScript

js实现超大的大括号

2026-01-31 08:31:43JavaScript

使用 CSS 和 SVG 实现超大括号

在 JavaScript 中可以通过动态生成 SVG 或 CSS 样式来创建超大括号效果。以下是两种实现方式:

CSS 实现方案 通过伪元素和边框旋转实现大括号效果:

js实现超大的大括号

.big-brace {
  position: relative;
  height: 200px;
  width: 50px;
}

.big-brace::before, .big-brace::after {
  content: '';
  position: absolute;
  width: 25px;
  height: 100px;
  border: 5px solid #000;
  border-radius: 50%;
}

.big-brace::before {
  top: 0;
  border-right: none;
  border-bottom: none;
  transform: rotate(-45deg);
}

.big-brace::after {
  bottom: 0;
  border-left: none;
  border-top: none;
  transform: rotate(-45deg);
}

SVG 实现方案 使用 JavaScript 动态创建 SVG 路径:

js实现超大的大括号

function createBigBrace(width, height) {
  const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
  svg.setAttribute('width', width);
  svg.setAttribute('height', height);

  const path = document.createElementNS("http://www.w3.org/2000/svg", "path");
  const curveHeight = height * 0.8;
  const curveWidth = width * 0.4;

  path.setAttribute('d', 
    `M ${width/2} 0 
     Q ${width/2 - curveWidth} ${curveHeight/4}, ${width/2 - curveWidth} ${curveHeight/2}
     Q ${width/2 - curveWidth} ${3*curveHeight/4}, ${width/2} ${curveHeight}
     M ${width/2} 0
     Q ${width/2 + curveWidth} ${curveHeight/4}, ${width/2 + curveWidth} ${curveHeight/2}
     Q ${width/2 + curveWidth} ${3*curveHeight/4}, ${width/2} ${curveHeight}`
  );

  path.setAttribute('stroke', 'black');
  path.setAttribute('fill', 'none');
  path.setAttribute('stroke-width', '5');

  svg.appendChild(path);
  return svg;
}

// 使用示例
document.body.appendChild(createBigBrace(100, 500));

使用 Canvas 绘制大括号

通过 Canvas API 可以更灵活地控制大括号的形状和大小:

function drawBigBrace(ctx, x, y, width, height) {
  const halfWidth = width / 2;
  const halfHeight = height / 2;

  ctx.beginPath();
  ctx.moveTo(x + halfWidth, y);

  // 上半部分曲线
  ctx.bezierCurveTo(
    x + halfWidth - width*0.3, y + height*0.2,
    x + halfWidth - width*0.3, y + halfHeight,
    x + halfWidth, y + halfHeight
  );

  // 下半部分曲线
  ctx.bezierCurveTo(
    x + halfWidth - width*0.3, y + halfHeight,
    x + halfWidth - width*0.3, y + height*0.8,
    x + halfWidth, y + height
  );

  ctx.moveTo(x + halfWidth, y);

  // 上半部分镜像曲线
  ctx.bezierCurveTo(
    x + halfWidth + width*0.3, y + height*0.2,
    x + halfWidth + width*0.3, y + halfHeight,
    x + halfWidth, y + halfHeight
  );

  // 下半部分镜像曲线
  ctx.bezierCurveTo(
    x + halfWidth + width*0.3, y + halfHeight,
    x + halfWidth + width*0.3, y + height*0.8,
    x + halfWidth, y + height
  );

  ctx.lineWidth = 5;
  ctx.stroke();
}

// 使用示例
const canvas = document.createElement('canvas');
canvas.width = 200;
canvas.height = 600;
document.body.appendChild(canvas);
const ctx = canvas.getContext('2d');
drawBigBrace(ctx, 50, 50, 100, 500);

使用 Unicode 字符

对于简单的需求,可以直接使用 Unicode 中的大括号字符:

const bigBrace = document.createElement('div');
bigBrace.style.fontSize = '200px';
bigBrace.style.lineHeight = '1';
bigBrace.textContent = '{'; // 或 '}' 
document.body.appendChild(bigBrace);

这种方法简单但可定制性较差,适合快速实现基本效果。

标签: 括号js
分享给朋友:

相关文章

js实现点击显示和隐藏

js实现点击显示和隐藏

实现点击显示和隐藏的JavaScript方法 使用classList.toggle切换类名 通过添加/移除CSS类控制元素的显示与隐藏,需提前在CSS中定义隐藏样式(如display: none)。…

js实现防洪

js实现防洪

防抖(Debounce)实现 防抖的核心思想是在事件触发后延迟执行回调函数,若在延迟时间内再次触发事件,则重新计时。适用于输入框搜索、窗口调整等场景。 function debounce(func,…

js实现正交

js实现正交

正交的概念 正交在数学和计算机科学中通常指两个向量垂直或线性无关。在编程中,正交性常被用于设计模块化、低耦合的系统。 向量正交判断 判断两个向量是否正交可以通过点积是否为0来实现: fun…

js实现下拉菜单

js实现下拉菜单

使用HTML和CSS创建基础结构 HTML部分需要包含一个触发下拉的按钮和隐藏的下拉菜单内容: <div class="dropdown"> <button class="dr…

实现继承js

实现继承js

实现继承的方式 在JavaScript中,实现继承可以通过多种方式完成,每种方式都有其优缺点。以下是几种常见的实现继承的方法: 原型链继承 原型链继承是最基本的继承方式,通过将子类的原型指向父类的实…

vue js 实现排序

vue js 实现排序

Vue.js 实现排序的方法 在 Vue.js 中实现排序可以通过多种方式完成,包括使用计算属性、方法或直接操作数据。以下是几种常见的实现方式: 使用计算属性排序 计算属性可以自动响应数据变化并重新…