强大且灵活的注释工具,添加手绘风格的注释: RoughNotation

208 阅读5分钟

RoughNotation 使用详解:从基础到进阶

RoughNotation 是一个轻量级且功能强大的 JavaScript 库,用于为 HTML 元素添加手绘风格的注释。这种手绘风格有趣且生动,能够帮助用户在网页上快速聚焦到重要的部分。RoughNotation 支持多种类型的注释效果,如下划线、框线、圆圈、删除线等,甚至可以通过动画将注释动态地展现在用户眼前。


1. 安装与引入 RoughNotation

在开始之前,我们需要将 RoughNotation 引入项目中。该库提供了几种简单的安装方式,您可以根据需求选择适合的方式。

1.1 通过 CDN 引入

如果你不想设置复杂的构建环境,可以直接通过 CDN 引入 RoughNotation。以下是使用 JSDelivr CDN 引入该库的方法:

<script src="https://cdn.jsdelivr.net/npm/rough-notation@0.5.1/lib/rough-notation.iife.min.js"></script>

将以上代码放入 HTML 文件的 <head> 部分或其他脚本文件之前,确保 RoughNotation 可在浏览器环境中正常使用。

1.2 使用 npm 安装

对于通过 npm 管理项目的开发者,可以使用以下命令来安装 RoughNotation:

npm install rough-notation

安装完成后,你可以在项目的 JavaScript 文件中导入 RoughNotation 并开始使用:

import { annotate, annotationGroup } from 'rough-notation';

2. 基础用法

RoughNotation 使用非常简单,核心的 annotate() 方法可以快速为 DOM 元素添加注释。annotate() 方法会返回一个 annotation 对象,通过它可以控制注释的外观、动画等细节。以下是最基础的使用示例。

2.1 为元素添加注释

首先,我们来看看如何为一个 HTML 元素添加简单的注释。在这个例子中,我们会为一段文本添加一个手绘风格的下划线。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>RoughNotation Example</title>
  <script src="https://cdn.jsdelivr.net/npm/rough-notation@0.5.1/lib/rough-notation.iife.min.js"></script>
</head>
<body>
  <p id="text">这是一段文本,我们要为它添加注释。</p>

  <script>
    const text = document.getElementById('text');
    const annotation = RoughNotation.annotate(text, { type: 'underline' });
    annotation.show();
  </script>
</body>
</html>

在这个示例中:

  • 我们首先通过 document.getElementById() 获取到 idtext 的元素。
  • 然后通过 RoughNotation.annotate() 为该元素创建了一个注释对象,注释的类型为 underline(下划线)。
  • 最后调用 annotation.show() 方法显示注释。
2.2 注释类型

RoughNotation 提供了多种注释类型,通过配置选项 type 可以控制注释的形式。常见的注释类型包括:

  • underline: 添加下划线,强调文本。
  • box: 在元素周围绘制一个矩形框。
  • circle: 绘制一个圆形围绕元素。
  • highlight: 为元素背景添加高亮效果。
  • strike-through: 在文本上添加删除线。
  • bracket: 在元素两侧添加大括号。

你可以在 annotate() 函数中指定 type 选项来应用不同的注释类型。例如,为元素添加一个框线注释:

const annotation = RoughNotation.annotate(text, { type: 'box' });
annotation.show();

3. 配置选项

除了选择注释类型外,RoughNotation 还提供了许多配置选项来定制注释的外观和行为。下面我们会逐一介绍这些选项。

3.1 color

color 属性用于设置注释线条的颜色。它接受任意有效的 CSS 颜色值,例如:

const annotation = RoughNotation.annotate(text, { type: 'box', color: 'red' });
annotation.show();
3.2 strokeWidth

strokeWidth 用来设置线条的粗细,默认为 1。如果你希望线条更粗或更细,可以修改该值:

const annotation = RoughNotation.annotate(text, { type: 'underline', strokeWidth: 3 });
annotation.show();
3.3 padding

padding 控制注释与元素内容之间的距离,默认值为 5。增加该值可以让注释与元素之间留出更多的空间,适用于 boxcircle 等注释类型:

const annotation = RoughNotation.annotate(text, { type: 'box', padding: 10 });
annotation.show();
3.4 iterations

iterations 控制注释线条的绘制迭代次数。默认值为 2,增加该值会让线条显得更加粗糙和手绘风格:

const annotation = RoughNotation.annotate(text, { type: 'box', iterations: 4 });
annotation.show();
3.5 animateanimationDuration

RoughNotation 默认支持动画效果。你可以通过 animate 选项控制是否启用动画,animationDuration 用来设置动画的时长(单位为毫秒)。默认动画持续时间为 800 毫秒:

const annotation = RoughNotation.annotate(text, {
  type: 'highlight',
  animate: true,
  animationDuration: 2000, // 2 秒
});
annotation.show();

4. 组合注释:使用注释组

RoughNotation 提供了 annotationGroup() 方法,用来为多个注释创建一个组。这意味着你可以将多个注释同时应用到不同的元素上,并通过组控制这些注释的显示顺序和动画效果。

4.1 创建注释组

以下示例展示了如何为多个元素创建注释,并通过注释组一次性显示:

<p id="first">第一段文字。</p>
<p id="second">第二段文字。</p>

<script>
  const first = document.getElementById('first');
  const second = document.getElementById('second');

  const annotation1 = RoughNotation.annotate(first, { type: 'underline', color: 'blue' });
  const annotation2 = RoughNotation.annotate(second, { type: 'highlight', color: 'yellow' });

  const group = RoughNotation.annotationGroup([annotation1, annotation2]);
  group.show();  // 一次性显示所有注释
</script>
4.2 分步动画展示

RoughNotation 还支持分步展示多个注释。你可以通过 show() 方法控制每个注释的显示时机。例如,先显示一个注释,延迟一段时间后再显示其他注释:

const group = RoughNotation.annotationGroup([annotation1, annotation2]);
group.show();

setTimeout(() => {
  annotation3.show();  // 2 秒后显示第三个注释
}, 2000);

5. 高级用法:在项目中的实际应用

RoughNotation 特别适合在教程、文档或产品展示中使用,通过注释引导用户关注关键内容。下面是一个结合多个注释类型和动画效果的复杂示例:

<h1 id="title">RoughNotation 使用示例</h1>
<p id="content">我们将使用 RoughNotation 为这个页面添加注释和动画效果。</p>
<button id="button">点击按钮查看效果</button>

<script>
  const title = document.getElementById('title');
  const content = document.getElementById('content');
  const button = document.getElementById('button');

  // 为标题添加绿色框线
  const annotationTitle = RoughNotation.annotate(title, { type: 'box', color: 'green', strokeWidth: 3 });
  
  // 为内容添加蓝色下划线
  const annotationContent = RoughNotation.annotate(content, { type: 'underline', color: 'blue', padding: 5 });
  
  // 为按钮添加粉色高亮背景
  const annotationButton = RoughNotation.annotate(button, { type: 'highlight', color: 'pink', animationDuration: 1000 });

  // 创建注释组
  const group = RoughNotation.annotationGroup([annotationTitle, annotationContent, annotationButton]);

  // 点击按钮时显示注释
  button.addEventListener('click', () => {
   

 group.show();
  });
</script>

在这个示例中:

  • 标题 被绿色的框线标记,强调页面的主标题。
  • 段落内容 使用蓝色下划线来加强文本的视觉吸引力。
  • 按钮 使用粉色的高亮注释,通过点击按钮触发所有注释的显示。