当前位置:首页 > JavaScript

js实现笔画

2026-02-01 11:39:40JavaScript

实现笔画效果的JavaScript方法

在JavaScript中实现笔画效果通常涉及Canvas API或SVG技术。以下是几种常见的方法:

使用Canvas绘制笔画

Canvas提供路径绘制功能,可以模拟手写笔画效果:

const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
let isDrawing = false;

canvas.addEventListener('mousedown', startDrawing);
canvas.addEventListener('mousemove', draw);
canvas.addEventListener('mouseup', stopDrawing);
canvas.addEventListener('mouseout', stopDrawing);

function startDrawing(e) {
    isDrawing = true;
    ctx.beginPath();
    ctx.moveTo(e.offsetX, e.offsetY);
}

function draw(e) {
    if (!isDrawing) return;
    ctx.lineTo(e.offsetX, e.offsetY);
    ctx.strokeStyle = '#000000';
    ctx.lineWidth = 3;
    ctx.lineCap = 'round';
    ctx.lineJoin = 'round';
    ctx.stroke();
}

function stopDrawing() {
    isDrawing = false;
}

SVG路径动画

通过SVG的path元素可以实现更复杂的笔画动画:

const svg = document.getElementById('svg');
const path = document.createElementNS('http://www.w3.org/2000/svg', 'path');
svg.appendChild(path);

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

let points = [];
svg.addEventListener('mousedown', (e) => {
    points = [];
    const pt = svg.createSVGPoint();
    pt.x = e.clientX;
    pt.y = e.clientY;
    points.push(pt);
});

svg.addEventListener('mousemove', (e) => {
    if (points.length === 0) return;
    const pt = svg.createSVGPoint();
    pt.x = e.clientX;
    pt.y = e.clientY;
    points.push(pt);

    let d = 'M' + points[0].x + ',' + points[0].y;
    for (let i = 1; i < points.length; i++) {
        d += ' L' + points[i].x + ',' + points[i].y;
    }
    path.setAttribute('d', d);
});

笔画压力敏感效果

对于支持压感的设备,可以添加压力敏感效果:

canvas.addEventListener('pointermove', (e) => {
    if (!isDrawing) return;
    ctx.lineWidth = e.pressure * 10;
    ctx.lineTo(e.offsetX, e.offsetY);
    ctx.stroke();
});

笔画平滑处理

使用贝塞尔曲线算法平滑笔画:

function smoothPoints(points) {
    if (points.length < 3) return points;

    const smoothed = [points[0]];
    for (let i = 1; i < points.length - 1; i++) {
        const prev = points[i-1];
        const curr = points[i];
        const next = points[i+1];

        smoothed.push({
            x: (prev.x + curr.x + next.x) / 3,
            y: (prev.y + curr.y + next.y) / 3
        });
    }
    smoothed.push(points[points.length-1]);
    return smoothed;
}

笔画回放功能

记录笔画坐标并实现回放:

const strokeHistory = [];
let isRecording = false;

function recordStroke(x, y) {
    if (!isRecording) return;
    strokeHistory.push({x, y});
}

function replayStrokes() {
    let i = 0;
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    ctx.beginPath();

    const interval = setInterval(() => {
        if (i >= strokeHistory.length) {
            clearInterval(interval);
            return;
        }
        const point = strokeHistory[i];
        if (i === 0) {
            ctx.moveTo(point.x, point.y);
        } else {
            ctx.lineTo(point.x, point.y);
            ctx.stroke();
        }
        i++;
    }, 16);
}

这些方法可以根据具体需求组合使用,创建更丰富的笔画效果。对于更高级的笔画模拟,可以考虑使用专门的绘图库如Paper.js或Rough.js。

js实现笔画

标签: 笔画js
分享给朋友:

相关文章

js实现全屏

js实现全屏

实现全屏的基本方法 使用JavaScript实现全屏功能主要依赖Element.requestFullscreen()方法。现代浏览器均支持此API,但不同浏览器可能需要添加前缀。 // 触发全屏…

js 进度条的实现

js 进度条的实现

使用 HTML 和 CSS 创建基础进度条 HTML 结构可以简单使用一个 div 元素作为容器,内部嵌套另一个 div 表示进度: <div class="progress-contain…

js实现选项卡

js实现选项卡

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

js实现复制到剪贴板

js实现复制到剪贴板

使用document.execCommand方法(传统方式,已废弃但部分浏览器仍支持) 该方法适用于旧版浏览器,但已被标记为废弃。执行前需确保文本已被选中: function copyToClip…

js实现pdf在线预览

js实现pdf在线预览

使用PDF.js实现PDF在线预览 PDF.js是由Mozilla开发的一个开源JavaScript库,可以在网页中直接渲染PDF文件。以下是实现PDF在线预览的步骤: 引入PDF.js库 在HT…

js实现瀑布流

js实现瀑布流

实现瀑布流布局 瀑布流布局是一种常见的网页布局方式,常用于图片展示、商品列表等场景。以下是使用 JavaScript 实现瀑布流布局的几种方法。 纯 JavaScript 实现 通过计算元素的位置…