koch-snowflake
v1.0.0
Published
Koch snowflake drawing for Canvas
Downloads
6
Readme
koch-snowflake
一个 函数式、高性能的科赫雪花生成器 (Koch Snowflake Generator) for Canvas.
可以生成分形雪花,并支持重复绘制,同时自定义位置、颜色和线宽。
Features / 特性
- 函数式编程风格 (Functional style, no classes, no global state)
- 一次生成雪花点集,可重复绘制 (Generates points once, supports repeated drawing)
- 支持自定义位置、大小、颜色和线宽 (Customizable position, size, color, line width)
- 参数验证,错误提示清晰 (Parameter validation with clear error messages)
- 可在浏览器前端框架中使用 (Works in browser and front-end frameworks)
- 轻量级,易于集成 (Lightweight and easy to integrate)
Installation / 安装
npm install koch-snowflake
<canvas id="canvas" width="600" height="600"></canvas>
<script type="module">
import { createKochSnowflake } from "koch-snowflake";
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
// 创建雪花句柄,只计算一次点集 (Create a Koch Snowflake handle)
const snowflake = createKochSnowflake(150, 4);
// 在不同位置绘制雪花 (Draw snowflake at different positions)
// 参数说明 / Parameters: ctx = canvas context, x/y = center coordinates, color = line color, width = line width
snowflake.draw(ctx, 150, 150, "cyan", 2); // 左上角小雪花 / top-left snowflake
snowflake.draw(ctx, 450, 150, "yellow", 2); // 右上角雪花 / top-right snowflake
snowflake.draw(ctx, 300, 400, "white", 2); // 底部雪花 / bottom snowflake
</script>