rough-emoji-draw 0.2.1 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.en.md CHANGED
@@ -7,11 +7,10 @@ Rough Emoji Draw is a lightweight frontend library that renders Unicode flag emo
7
7
  ## Features
8
8
 
9
9
  - Draw Unicode flag emojis onto a given `canvas`.
10
- - Hand-drawn templates for common flags such as China, Japan, the United States, Australia, Thailand, France, Italy, Spain, and Vatican City.
11
- - A unified fallback pipeline via emoji rasterization and pixel sampling for flags without dedicated templates.
10
+ - About 100 hand-drawn flag templates (including China, Japan, the United States, Australia, Thailand, France, Italy, Spain, Vatican City, and more); remaining regions use a unified emoji rasterization and pixel-sampling fallback.
12
11
  - Three integration paths: npm `import` / Node `require`, and browser `<script>` with the global `window.RoughEmoji`.
13
12
  - TypeScript declaration files for API autocomplete in TS projects.
14
- - An `index.html` batch QA page to visually inspect 240+ country/region flags.
13
+ - An `index.html` batch QA page to preview 240+ country/region flags, with search by region code or localized name, and one-click copy of transparent-background SVG.
15
14
 
16
15
  ## Tech Stack
17
16
 
@@ -73,6 +72,12 @@ const canvas = document.querySelector<HTMLCanvasElement>("#flag-canvas")!;
73
72
  RoughEmoji.draw(canvas, "🇨🇳");
74
73
  ```
75
74
 
75
+ You can also import utility functions directly:
76
+
77
+ ```ts
78
+ import { isFlagEmoji, resolveFlag } from "rough-emoji-draw";
79
+ ```
80
+
76
81
  ### npm package (CommonJS / Node)
77
82
 
78
83
  ```js
@@ -96,11 +101,17 @@ Load the IIFE bundle only; it automatically attaches `window.RoughEmoji`:
96
101
  </script>
97
102
  ```
98
103
 
99
- If the page includes the demo form DOM (`#rough-canvas`, `#emoji-form`, etc.), load the root-level `rough-emoji-app.js` to wire up interaction and download handlers:
104
+ You can also reference the same IIFE file via the package export subpath:
105
+
106
+ ```html
107
+ <script src="./node_modules/rough-emoji-draw/dist/rough-emoji.js"></script>
108
+ ```
109
+
110
+ If the page includes the demo form DOM (`#rough-canvas`, `#emoji-form`, etc.), load `demo/rough-emoji-app.js` to wire up interaction and PNG download handlers:
100
111
 
101
112
  ```html
102
113
  <script src="./dist/rough-emoji.js"></script>
103
- <script src="./rough-emoji-app.js"></script>
114
+ <script type="module" src="./demo/rough-emoji-app.js"></script>
104
115
  ```
105
116
 
106
117
  ## API
@@ -111,11 +122,11 @@ Normalizes the input to a flag emoji and draws it on the given `HTMLCanvasElemen
111
122
 
112
123
  ### `RoughEmoji.isFlagEmoji(value)`
113
124
 
114
- Returns whether the string is a flag emoji composed of two regional indicator symbols.
125
+ Returns whether the string is a flag emoji composed of two regional indicator symbols. Also available as the named export `isFlagEmoji`.
115
126
 
116
127
  ### `RoughEmoji.resolveFlag(value)`
117
128
 
118
- Converts arbitrary input into a drawable flag emoji. Only valid flag emojis are accepted; invalid values fall back to `🇨🇳`.
129
+ Converts arbitrary input into a drawable flag emoji. Only valid flag emojis are accepted; invalid values fall back to `🇨🇳`. Also available as the named export `resolveFlag`.
119
130
 
120
131
  ### Type exports
121
132
 
@@ -132,12 +143,16 @@ You can also import `RoughOptions`, `Point`, and `RoughCanvasLike` as needed.
132
143
  ├── src/
133
144
  │ ├── index.ts # npm library entry; exports API and types
134
145
  │ ├── browser.ts # browser IIFE entry; mounts window.RoughEmoji
135
- │ ├── rough-emoji.ts # core drawing logic
146
+ │ ├── rough-emoji.ts # drawing facade and render dispatch
147
+ │ ├── flag-drawers.ts # per-flag hand-drawn templates and fallback drawing
136
148
  │ ├── flag-utils.ts # flag emoji parsing and validation
137
149
  │ ├── render-context.ts # canvas context switching
138
150
  │ ├── constant.ts # static constants and template map
139
151
  │ └── types.ts # public type declarations
140
- ├── rough-emoji-app.js # index.html demo DOM bindings (not in npm artifacts)
152
+ ├── demo/
153
+ │ ├── index.js # index.html batch QA script (search, render, copy SVG)
154
+ │ ├── index-svg-export.js # canvas flag crop and transparent SVG export
155
+ │ └── rough-emoji-app.js # single-page demo form bindings and PNG download (not in npm artifacts)
141
156
  ├── index.html # batch visual QA page
142
157
  ├── rslib.config.ts # Rslib multi-format build config
143
158
  ├── tsconfig.json # TypeScript config
@@ -146,7 +161,7 @@ You can also import `RoughOptions`, `Point`, and `RoughCanvasLike` as needed.
146
161
 
147
162
  ## Development Notes
148
163
 
149
- - Add new flags in `src/rough-emoji.ts` with dedicated draw functions and register them in the template dispatch table.
164
+ - Add new flags in `src/flag-drawers.ts` with dedicated draw functions and register them in the `templateDrawers` dispatch table; `src/rough-emoji.ts` handles orchestration and does not contain per-flag drawing logic.
150
165
  - Draw functions use a square canvas coordinate system; derive new coordinates from `size` or existing flag layout variables.
151
- - After changing drawing behavior, run `pnpm run typecheck`, then `pnpm run build`, and inspect results in `index.html`.
152
- - `dist/` is build output and is usually not maintained as source; run `pnpm run build` before publishing or local preview.
166
+ - After changing drawing behavior, run `pnpm run typecheck`, then `pnpm run build`, and inspect results via `pnpm run dev` on `index.html`.
167
+ - Scripts under `demo/` are for local demo and QA only and are not published with the npm package; `dist/` is build output and is usually not maintained as source.
package/README.md CHANGED
@@ -7,11 +7,10 @@ Rough Emoji Draw 是一个把 Unicode 国旗 emoji 绘制成 Rough.js 手写风
7
7
  ## 功能特性
8
8
 
9
9
  - 将 Unicode 国旗 emoji 绘制到指定 `canvas`。
10
- - 内置中国、日本、美国、澳大利亚、泰国、法国、意大利、西班牙、梵蒂冈等常见旗帜的手写模板。
11
- - 对未单独适配的国旗使用 emoji 栅格化与像素采样流程生成统一风格的 fallback 图像。
10
+ - 内置约 100 面国旗的手写模板(含中国、日本、美国、澳大利亚、泰国、法国、意大利、西班牙、梵蒂冈等),其余地区走统一的 emoji 栅格化与像素采样 fallback。
12
11
  - 支持三种接入方式:npm `import` / Node `require`、浏览器 `<script>` 全局变量 `window.RoughEmoji`。
13
12
  - 提供 TypeScript 类型声明,可在 TS 项目中直接获得 API 提示。
14
- - 提供 `index.html` 批量 QA 页面,用于检查 240+ 国家/地区旗帜绘制效果。
13
+ - 提供 `index.html` 批量 QA 页面,预览 240+ 国家/地区旗帜,支持按地区码或中文名搜索,并可一键复制透明背景 SVG。
15
14
 
16
15
  ## 技术栈
17
16
 
@@ -73,6 +72,12 @@ const canvas = document.querySelector<HTMLCanvasElement>("#flag-canvas")!;
73
72
  RoughEmoji.draw(canvas, "🇨🇳");
74
73
  ```
75
74
 
75
+ 也可按需导入独立工具函数:
76
+
77
+ ```ts
78
+ import { isFlagEmoji, resolveFlag } from "rough-emoji-draw";
79
+ ```
80
+
76
81
  ### npm 包(CommonJS / Node)
77
82
 
78
83
  ```js
@@ -96,11 +101,17 @@ console.log(typeof RoughEmoji.draw); // "function"
96
101
  </script>
97
102
  ```
98
103
 
99
- 若页面包含演示表单 DOM(`#rough-canvas`、`#emoji-form` 等),可额外加载根目录下的 `rough-emoji-app.js` 绑定交互与下载逻辑:
104
+ 也可通过包导出子路径引用同一 IIFE 文件:
105
+
106
+ ```html
107
+ <script src="./node_modules/rough-emoji-draw/dist/rough-emoji.js"></script>
108
+ ```
109
+
110
+ 若页面包含演示表单 DOM(`#rough-canvas`、`#emoji-form` 等),可额外加载 `demo/rough-emoji-app.js` 绑定交互与 PNG 下载逻辑:
100
111
 
101
112
  ```html
102
113
  <script src="./dist/rough-emoji.js"></script>
103
- <script src="./rough-emoji-app.js"></script>
114
+ <script type="module" src="./demo/rough-emoji-app.js"></script>
104
115
  ```
105
116
 
106
117
  ## API
@@ -111,11 +122,11 @@ console.log(typeof RoughEmoji.draw); // "function"
111
122
 
112
123
  ### `RoughEmoji.isFlagEmoji(value)`
113
124
 
114
- 判断字符串是否为由两个区域指示符组成的国旗 emoji
125
+ 判断字符串是否为由两个区域指示符组成的国旗 emoji。也可作为命名导出 `isFlagEmoji` 直接使用。
115
126
 
116
127
  ### `RoughEmoji.resolveFlag(value)`
117
128
 
118
- 把任意输入转换为可绘制的国旗 emoji;当前只接受合法国旗 emoji,非法值回退为 `🇨🇳`。
129
+ 把任意输入转换为可绘制的国旗 emoji;当前只接受合法国旗 emoji,非法值回退为 `🇨🇳`。也可作为命名导出 `resolveFlag` 直接使用。
119
130
 
120
131
  ### 类型导出
121
132
 
@@ -132,12 +143,16 @@ import type { RoughEmojiApi } from "rough-emoji-draw";
132
143
  ├── src/
133
144
  │ ├── index.ts # npm 库入口,导出 API 与类型
134
145
  │ ├── browser.ts # 浏览器 IIFE 入口,挂载 window.RoughEmoji
135
- │ ├── rough-emoji.ts # 核心绘制逻辑
146
+ │ ├── rough-emoji.ts # 绘制门面与渲染调度
147
+ │ ├── flag-drawers.ts # 各国旗手写模板与 fallback 绘制
136
148
  │ ├── flag-utils.ts # 国旗 emoji 解析与校验
137
149
  │ ├── render-context.ts # Canvas 上下文切换
138
150
  │ ├── constant.ts # 静态常量与模板映射
139
151
  │ └── types.ts # 对外类型声明
140
- ├── rough-emoji-app.js # index.html 演示页 DOM 绑定(不进入 npm 产物)
152
+ ├── demo/
153
+ │ ├── index.js # index.html 批量 QA 页脚本(搜索、渲染、复制 SVG)
154
+ │ ├── index-svg-export.js # Canvas 旗面裁剪与透明 SVG 导出
155
+ │ └── rough-emoji-app.js # 单页演示表单绑定与 PNG 下载(不进入 npm 产物)
141
156
  ├── index.html # 批量视觉 QA 页面
142
157
  ├── rslib.config.ts # Rslib 多格式构建配置
143
158
  ├── tsconfig.json # TypeScript 配置
@@ -146,7 +161,7 @@ import type { RoughEmojiApi } from "rough-emoji-draw";
146
161
 
147
162
  ## 开发说明
148
163
 
149
- - 新增旗帜优先在 `src/rough-emoji.ts` 中补充独立绘制函数,并在模板分发表中接入。
164
+ - 新增旗帜优先在 `src/flag-drawers.ts` 中补充独立绘制函数,并在 `templateDrawers` 分发表中接入;`src/rough-emoji.ts` 负责调度,不直接承载具体旗面绘制。
150
165
  - 绘制函数按正方形画布坐标系工作,新增坐标建议基于当前 `size` 或已有旗帜区域变量推导。
151
- - 修改绘制效果后,先运行 `pnpm run typecheck`,再运行 `pnpm run build` 并打开 `index.html` 进行视觉检查。
152
- - `dist/` 为构建产物,通常不作为源码维护对象;发布或本地预览前执行 `pnpm run build` 即可。
166
+ - 修改绘制效果后,先运行 `pnpm run typecheck`,再运行 `pnpm run build`,并通过 `pnpm run dev` 打开 `index.html` 进行视觉检查。
167
+ - `demo/` 下的页面脚本仅用于本地演示与 QA,不随 npm 包发布;`dist/` 为构建产物,通常不作为源码维护对象。
@@ -0,0 +1,9 @@
1
+ import { TEMPLATE_FLAGS } from "./constant";
2
+ /** 专门模板绘制函数签名。 */
3
+ export type FlagDrawer = () => void;
4
+ /** 专门模板覆盖的国旗 emoji 联合类型。 */
5
+ export type TemplateFlag = (typeof TEMPLATE_FLAGS)[keyof typeof TEMPLATE_FLAGS];
6
+ /** 未覆盖输入的兜底绘制流程,保留较克制的细节数量以控制性能。 */
7
+ export declare function drawGenericFlag(flagEmoji: string): void;
8
+ /** 专门模板分发表:每个标准国旗都有独立绘制入口,避免大量 if 分支。 */
9
+ export declare const templateDrawers: Record<TemplateFlag, FlagDrawer>;