当前位置:首页 > JavaScript

js实现刻度

2026-02-01 09:28:43JavaScript

刻度实现的基本方法

使用JavaScript实现刻度可以通过Canvas或SVG进行绘制。Canvas适合动态生成的图形,SVG适合需要交互的场景。

Canvas绘制刻度

创建Canvas元素并设置宽高:

<canvas id="scaleCanvas" width="400" height="100"></canvas>

JavaScript代码实现刻度绘制:

const canvas = document.getElementById('scaleCanvas');
const ctx = canvas.getContext('2d');

function drawScale(start, end, interval, majorHeight, minorHeight) {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  ctx.beginPath();

  // 绘制主刻度
  for(let i = start; i <= end; i += interval) {
    const x = (i - start) * (canvas.width / (end - start));
    ctx.moveTo(x, canvas.height);
    ctx.lineTo(x, canvas.height - majorHeight);
    ctx.fillText(i.toString(), x - 5, canvas.height - majorHeight - 5);
  }

  // 绘制次刻度
  for(let i = start; i <= end; i += interval/2) {
    const x = (i - start) * (canvas.width / (end - start));
    ctx.moveTo(x, canvas.height);
    ctx.lineTo(x, canvas.height - minorHeight);
  }

  ctx.stroke();
}

drawScale(0, 100, 10, 20, 10);

SVG实现刻度

创建SVG元素:

<svg id="scaleSVG" width="400" height="100"></svg>

JavaScript代码实现:

const svg = document.getElementById('scaleSVG');

function createScale(start, end, interval, majorHeight, minorHeight) {
  svg.innerHTML = '';
  const scaleLength = end - start;

  // 创建主刻度
  for(let i = start; i <= end; i += interval) {
    const x = (i - start) * (svg.width.baseVal.value / scaleLength);
    const line = document.createElementNS("http://www.w3.org/2000/svg", "line");
    line.setAttribute("x1", x);
    line.setAttribute("y1", svg.height.baseVal.value);
    line.setAttribute("x2", x);
    line.setAttribute("y2", svg.height.baseVal.value - majorHeight);
    line.setAttribute("stroke", "black");
    svg.appendChild(line);

    const text = document.createElementNS("http://www.w3.org/2000/svg", "text");
    text.setAttribute("x", x - 5);
    text.setAttribute("y", svg.height.baseVal.value - majorHeight - 5);
    text.textContent = i.toString();
    svg.appendChild(text);
  }

  // 创建次刻度
  for(let i = start; i <= end; i += interval/2) {
    const x = (i - start) * (svg.width.baseVal.value / scaleLength);
    const line = document.createElementNS("http://www.w3.org/2000/svg", "line");
    line.setAttribute("x1", x);
    line.setAttribute("y1", svg.height.baseVal.value);
    line.setAttribute("x2", x);
    line.setAttribute("y2", svg.height.baseVal.value - minorHeight);
    line.setAttribute("stroke", "black");
    svg.appendChild(line);
  }
}

createScale(0, 100, 10, 20, 10);

动态刻度实现

实现可交互的动态刻度尺:

const scaleContainer = document.getElementById('scaleContainer');
let isDragging = false;
let startX = 0;
let scrollLeft = 0;

scaleContainer.addEventListener('mousedown', (e) => {
  isDragging = true;
  startX = e.pageX - scaleContainer.offsetLeft;
  scrollLeft = scaleContainer.scrollLeft;
});

window.addEventListener('mouseup', () => {
  isDragging = false;
});

window.addEventListener('mousemove', (e) => {
  if(!isDragging) return;
  e.preventDefault();
  const x = e.pageX - scaleContainer.offsetLeft;
  const walk = (x - startX) * 2;
  scaleContainer.scrollLeft = scrollLeft - walk;
});

function generateDynamicScale() {
  const scaleWidth = 2000;
  scaleContainer.style.width = '400px';
  scaleContainer.style.overflowX = 'scroll';
  scaleContainer.innerHTML = `
    <div style="width:${scaleWidth}px; height:50px; position:relative; border-top:1px solid #000">
      ${Array.from({length: scaleWidth/10}, (_, i) => 
        i % 10 === 0 ? 
          `<div style="position:absolute; left:${i*10}px; top:0; height:20px; width:1px; background:#000"></div>
           <div style="position:absolute; left:${i*10-5}px; top:25px;">${i}</div>` :
          `<div style="position:absolute; left:${i*10}px; top:10px; height:10px; width:1px; background:#ccc"></div>`
      ).join('')}
    </div>
  `;
}

generateDynamicScale();

响应式刻度尺

实现适应不同屏幕尺寸的刻度尺:

function responsiveScale() {
  const container = document.getElementById('responsiveScale');
  const width = window.innerWidth * 0.8;
  container.style.width = `${width}px`;

  const majorInterval = width > 600 ? 50 : 100;
  const minorInterval = majorInterval / 5;

  container.innerHTML = `
    <svg width="100%" height="50">
      ${Array.from({length: Math.floor(width/majorInterval)}, (_, i) => 
        `<line x1="${i*majorInterval}" y1="50" x2="${i*majorInterval}" y2="30" stroke="#000" stroke-width="1"/>
         <text x="${i*majorInterval-10}" y="25">${i*majorInterval}</text>
         ${Array.from({length: 4}, (_, j) => 
           `<line x1="${i*majorInterval + (j+1)*minorInterval}" y1="50" 
                 x2="${i*majorInterval + (j+1)*minorInterval}" y2="40" stroke="#999" stroke-width="0.5"/>`
         ).join('')}`
      ).join('')}
    </svg>
  `;
}

window.addEventListener('resize', responsiveScale);
responsiveScale();

以上方法提供了从基础到高级的刻度实现方案,可以根据具体需求选择适合的方式。Canvas方案适合需要频繁更新的动态刻度,SVG方案适合需要交互的场景,而动态和响应式方案则提供了更完善的用户体验。

js实现刻度

标签: 刻度js
分享给朋友:

相关文章

js 实现vue

js 实现vue

实现 Vue 的核心功能 在 JavaScript 中实现 Vue 的核心功能需要模拟数据绑定、虚拟 DOM 和响应式系统。以下是一个简化版的实现思路: 响应式系统 通过 Object.define…

js实现vue

js实现vue

Vue.js 简介 Vue.js 是一个渐进式 JavaScript 框架,用于构建用户界面。其核心库专注于视图层,易于与其他库或现有项目整合。 实现 Vue.js 的基本步骤 安装 Vue.j…

js实现图片轮播

js实现图片轮播

实现基础图片轮播 使用HTML、CSS和JavaScript创建一个简单的图片轮播。HTML部分定义轮播容器和图片,CSS设置样式和动画效果,JavaScript处理轮播逻辑。 <div c…

js实现打印

js实现打印

使用window.print()方法实现打印 在JavaScript中,可以通过调用window.print()方法直接触发浏览器的打印功能。这会打开系统的打印对话框,用户可以选择打印机和设置打印选项…

js实现选项卡

js实现选项卡

实现选项卡的基本思路 选项卡通常由一组标签和对应的内容面板组成。点击标签时,显示对应的内容面板,隐藏其他面板。实现这一效果需要结合HTML结构、CSS样式和JavaScript交互逻辑。 HTML…

js防抖和节流实现

js防抖和节流实现

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