Converting ordinary shapes into a hand-drawn style: Comparing line fluctuations with Rough.js

プログラミング・Web開発カテゴリを表すパンダのイラスト Programming / Web Development

About this article
This article was created using an automated generation workflow powered by generative AI. After reviewing the official Rough.js README, API documentation, and package definitions, we prepared educational material to compare line fluctuations for the same shape.

Verification Status: 📘 Official documentation verified, rendering function mock-tested, actual browser verification pending.

Drawing shapes neatly and making them look approachable serve different purposes. With Rough.js, you can make only the outlines look hand-drawn without altering the coordinates of rectangles or circles. This can also be used to visually distinguish early-stage design explanatory diagrams from finalized specification diagrams.

Let's try running it first

ItemContent
TargetModern SVG-compatible browsers
LibraryRough.js 4.6.6
NetworkFetch JavaScript from an external CDN
DataFictional squares and circles only
PermissionsLogin, location data, camera, and other permissions are not required.

The initial value is 1. Move the slider to 0 and compare the standard SVG at the top with the shape at the bottom. Next, move it to 3. You should see the shape change from a well-defined square outline to one with noticeable hand-drawn wobbles.

The same settings are applied to the circle. However, because rectangles and circles generate outlines differently, the visual effect of the same setting value is not identical. The shapes in the standard SVG do not respond to the slider. Keeping the comparison baseline fixed makes it easier to spot changes.

Clicking "Reset to Default" returns the value to 1. Since the value is displayed on the screen, you can check the current settings even on a smartphone. You can also select and adjust the slider using the keyboard.

If you cannot open it in Pages, save the static versions ofindex.htmlandapp.jsin the same folder and open the HTML file. Communication with the CDN is required. There is no need to bypass organizational network restrictions.

What does Rough.js change?

SVG is a system for drawing shapes by specifying coordinates and dimensions. For a rectangle, you can specify the top-left coordinates, width, and height. It also scales easily with screen magnification.

Rough.js is a library that renders specified shapes with hand-drawn style lines. It supports both Canvas and SVG. SVG was chosen for this tutorial because generated elements can be easily replaced to update the screen, and it simplifies comparison with standard SVG.

roughnessis a numeric value that specifies the strength of the waviness. According to the official API documentation, the default value is 1, and a rectangle with a value of 0 results in a clean shape. In this tutorial, the value is restricted from 0 to 3 to allow comparison of small changes. This value does not directly specify the pixel offset of the lines.

flowchart LR
    A["スライダーの値"] --> B["数値へ変換"]
    B --> C["Rough.jsで四角・円を生成"]
    C --> D["SVGの子要素を置換"]
    D --> E["輪郭を比較"]

Only the SVG at the bottom is subject to rendering updates. The upper one remains fixed.

Key parts of the code

function drawSketch(api, svg, value) {
  // 乱数の種を固定し、線の揺れだけを比較する。
  const options = {
    roughness: value, seed: 42,
    stroke: "#245487", strokeWidth: 2,
    fill: "#dceafb", fillStyle: "solid"
  };
  const rc = api.svg(svg);
  svg.replaceChildren(
    rc.rectangle(20, 20, 130, 100, options),
    rc.circle(245, 70, 100, options)
  );
}

This is an excerpt with line breaks added to make the tutorial's rendering function easier to read. Running it standalone requires Rough.js and the SVG elements loaded in the completed version.

api.svg(svg)creates a renderer for SVG. The arguments for rectangle are the top-left coordinates, width, and height. The arguments for circle are the center coordinates and diameter. This differs from the radius specified in standard SVG circles. For example, a diameter of 100 corresponds to a radius of 50.

replaceChildrenreplaces old shapes in the SVG with new ones. This process prevents previous renderings from remaining when the slider is moved repeatedly. Simply continuing to add elements causes the outlines to overlap, making it impossible to tell what has changed.

Colors and coordinates are fixed. The random number seed is also fixed at 42. This makes it easier to reproduce shapes when regenerating with the same parameters. However, identical rendering is not guaranteed if the library version is changed.

Change one parameter at a time and compare

SettingsExpected observation result
roughness = 0The outline of the rectangle becomes clean
roughness = 1Baseline hand-drawn style line
roughness = 3The fluctuation becomes more noticeable

When comparing, do not change other settings simultaneously. Since only the fluctuation is changed while keeping the coordinates, color, and seed identical, it is easy to determine the cause of the difference.

As a next experiment, if you change the fillStyle of the completed version from solid to hachure, you can observe the difference between solid filling and hatching. Keeping a separate copy of the pre-change file makes it easy to revert. This additional experiment has not been verified in actual browsers.

Troubleshooting

SymptomThings to check
Only standard SVG is visibleCheck if fetching the CDN failed
Nothing is rendered even when values are changedJavaScript errors in the console
Local HTML does not workVerify if app.js is in the same folder
The shape is different than expectedCheck if any settings other than roughness were changed

Display an error message on the screen if the library fails to load. The display of standard SVG is not proof that the external library was also loaded successfully.

Standard connection information is transmitted to the CDN. This educational material does not contain processing to transmit shape setting values or personal information. When implementing in actual operations, check the dependency file retrieval method, the organization's external communication policy, and the licenses.

Application to work and study

Diagrams drawn during meetings and formal blueprints require different roles in terms of visual appearance. Hand-drawn style diagrams can be used for explaining tentative proposals. For blueprints where finalized dimensions need to be read accurately, it is better to use precise representations such as standard SVG. Do not distinguish states by appearance alone; also include text such as under consideration or finalized.

By separating the processing that changes the visual appearance of shapes from business data, multiple representations can be generated from the same data. This educational material serves as an introduction to verifying that separation using a small example.

Official information and GitHub samples

Verification date: 2026-12-02. The Rough.js license is MIT. A mock API was passed to the educational material's drawing function to inspect values 0 and 3, and the fixed seed. Rendering using the actual library, CDN retrieval, public Pages, physical smartphones, and WordPress display have not been verified.

Document information

Article title
Converting ordinary shapes into a hand-drawn style: Comparing line fluctuations with Rough.js
Published
Updated
Source
https://papanda925.com/?p=19173&lang=en

License: Text and original figures for which this site holds the relevant rights are available under CC BY 4.0 , unless otherwise noted. This article may include content created or edited with generative AI. If code has a separate license notice or a linked GitHub repository license, that license takes precedence for the code. Quotations, third-party materials, images, and trademarks are excluded from this license. Usage policy

Copied title and URL