rendx-grid-plugin 0.1.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025-present wei.liang (https://github.com/weiliang0121)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/main.cjs ADDED
@@ -0,0 +1,146 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __typeError = (msg) => {
7
+ throw TypeError(msg);
8
+ };
9
+ var __export = (target, all) => {
10
+ for (var name in all)
11
+ __defProp(target, name, { get: all[name], enumerable: true });
12
+ };
13
+ var __copyProps = (to, from, except, desc) => {
14
+ if (from && typeof from === "object" || typeof from === "function") {
15
+ for (let key of __getOwnPropNames(from))
16
+ if (!__hasOwnProp.call(to, key) && key !== except)
17
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
18
+ }
19
+ return to;
20
+ };
21
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
22
+ var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
23
+ var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
24
+ var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
25
+ var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
26
+ var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
27
+
28
+ // src/main.ts
29
+ var main_exports = {};
30
+ __export(main_exports, {
31
+ gridPlugin: () => gridPlugin
32
+ });
33
+ module.exports = __toCommonJS(main_exports);
34
+
35
+ // src/grid.ts
36
+ var DEFAULTS = {
37
+ spacing: 20,
38
+ dotRadius: 1,
39
+ color: "#d0d0d0",
40
+ zIndex: -1
41
+ };
42
+ var _app, _canvas, _ctx, _options, _GridPlugin_instances, createCanvas_fn;
43
+ var GridPlugin = class {
44
+ constructor(options = {}) {
45
+ __privateAdd(this, _GridPlugin_instances);
46
+ this.name = "grid";
47
+ __privateAdd(this, _app, null);
48
+ __privateAdd(this, _canvas, null);
49
+ __privateAdd(this, _ctx, null);
50
+ __privateAdd(this, _options);
51
+ __privateSet(this, _options, { ...DEFAULTS, ...options });
52
+ }
53
+ install(app) {
54
+ __privateSet(this, _app, app);
55
+ const container = app.container;
56
+ if (!container) {
57
+ throw new Error(
58
+ "[rendx-grid-plugin] App must be mounted before calling use(). Call app.mount(el) first, then app.use(gridPlugin(...))."
59
+ );
60
+ }
61
+ __privateMethod(this, _GridPlugin_instances, createCanvas_fn).call(this, container);
62
+ this.draw();
63
+ }
64
+ /** 绘制点阵网格 */
65
+ draw() {
66
+ const ctx = __privateGet(this, _ctx);
67
+ const canvas = __privateGet(this, _canvas);
68
+ if (!ctx || !canvas) return;
69
+ const { width, height } = __privateGet(this, _app).cfg;
70
+ const w = width ?? 800;
71
+ const h = height ?? 600;
72
+ const { spacing, dotRadius, color } = __privateGet(this, _options);
73
+ ctx.clearRect(0, 0, w, h);
74
+ ctx.fillStyle = color;
75
+ for (let x = spacing; x < w; x += spacing) {
76
+ for (let y = spacing; y < h; y += spacing) {
77
+ ctx.beginPath();
78
+ ctx.arc(x, y, dotRadius, 0, Math.PI * 2);
79
+ ctx.fill();
80
+ }
81
+ }
82
+ }
83
+ /** 调整网格 Canvas 尺寸并重绘(由 App.resize 或外部触发) */
84
+ resize(width, height) {
85
+ const canvas = __privateGet(this, _canvas);
86
+ const ctx = __privateGet(this, _ctx);
87
+ if (!canvas || !ctx) return;
88
+ const ratio = window.devicePixelRatio || 1;
89
+ canvas.width = width * ratio;
90
+ canvas.height = height * ratio;
91
+ canvas.style.width = `${width}px`;
92
+ canvas.style.height = `${height}px`;
93
+ ctx.scale(ratio, ratio);
94
+ this.draw();
95
+ }
96
+ /** 更新配置并重绘 */
97
+ update(options) {
98
+ Object.assign(__privateGet(this, _options), options);
99
+ if (options.zIndex !== void 0 && __privateGet(this, _canvas)) {
100
+ __privateGet(this, _canvas).style.zIndex = String(options.zIndex);
101
+ }
102
+ this.draw();
103
+ }
104
+ dispose() {
105
+ if (__privateGet(this, _canvas)) {
106
+ __privateGet(this, _canvas).remove();
107
+ __privateSet(this, _canvas, null);
108
+ __privateSet(this, _ctx, null);
109
+ }
110
+ __privateSet(this, _app, null);
111
+ }
112
+ };
113
+ _app = new WeakMap();
114
+ _canvas = new WeakMap();
115
+ _ctx = new WeakMap();
116
+ _options = new WeakMap();
117
+ _GridPlugin_instances = new WeakSet();
118
+ createCanvas_fn = function(container) {
119
+ const { width, height } = __privateGet(this, _app).cfg;
120
+ const w = width ?? 800;
121
+ const h = height ?? 600;
122
+ const canvas = document.createElement("canvas");
123
+ const ratio = window.devicePixelRatio || 1;
124
+ canvas.width = w * ratio;
125
+ canvas.height = h * ratio;
126
+ canvas.style.position = "absolute";
127
+ canvas.style.left = "0";
128
+ canvas.style.top = "0";
129
+ canvas.style.width = `${w}px`;
130
+ canvas.style.height = `${h}px`;
131
+ canvas.style.zIndex = String(__privateGet(this, _options).zIndex);
132
+ canvas.style.pointerEvents = "none";
133
+ container.insertBefore(canvas, container.firstChild);
134
+ const ctx = canvas.getContext("2d");
135
+ if (!ctx) throw new Error("[rendx-grid-plugin] Could not get 2d context");
136
+ ctx.scale(ratio, ratio);
137
+ __privateSet(this, _canvas, canvas);
138
+ __privateSet(this, _ctx, ctx);
139
+ };
140
+ function gridPlugin(options) {
141
+ return new GridPlugin(options);
142
+ }
143
+ // Annotate the CommonJS export names for ESM import in node:
144
+ 0 && (module.exports = {
145
+ gridPlugin
146
+ });
@@ -0,0 +1,40 @@
1
+ import { Plugin, App } from 'rendx-engine';
2
+
3
+ interface GridPluginOptions {
4
+ /** 点阵间距(px),默认 20 */
5
+ spacing?: number;
6
+ /** 点半径(px),默认 1 */
7
+ dotRadius?: number;
8
+ /** 点颜色,默认 '#d0d0d0' */
9
+ color?: string;
10
+ /** 层级 z-index,默认 -1(在所有渲染层下方) */
11
+ zIndex?: number;
12
+ }
13
+ declare class GridPlugin implements Plugin {
14
+ #private;
15
+ name: string;
16
+ constructor(options?: GridPluginOptions);
17
+ install(app: App): void;
18
+ /** 绘制点阵网格 */
19
+ draw(): void;
20
+ /** 调整网格 Canvas 尺寸并重绘(由 App.resize 或外部触发) */
21
+ resize(width: number, height: number): void;
22
+ /** 更新配置并重绘 */
23
+ update(options: Partial<GridPluginOptions>): void;
24
+ dispose(): void;
25
+ }
26
+ /**
27
+ * 创建点阵网格背景插件
28
+ *
29
+ * @example
30
+ * ```ts
31
+ * import {gridPlugin} from 'rendx-grid-plugin';
32
+ *
33
+ * const app = new App({width: 800, height: 600});
34
+ * app.mount(container);
35
+ * app.use(gridPlugin({spacing: 20, color: '#ccc'}));
36
+ * ```
37
+ */
38
+ declare function gridPlugin(options?: GridPluginOptions): GridPlugin;
39
+
40
+ export { type GridPluginOptions, gridPlugin };
package/dist/main.d.ts ADDED
@@ -0,0 +1,40 @@
1
+ import { Plugin, App } from 'rendx-engine';
2
+
3
+ interface GridPluginOptions {
4
+ /** 点阵间距(px),默认 20 */
5
+ spacing?: number;
6
+ /** 点半径(px),默认 1 */
7
+ dotRadius?: number;
8
+ /** 点颜色,默认 '#d0d0d0' */
9
+ color?: string;
10
+ /** 层级 z-index,默认 -1(在所有渲染层下方) */
11
+ zIndex?: number;
12
+ }
13
+ declare class GridPlugin implements Plugin {
14
+ #private;
15
+ name: string;
16
+ constructor(options?: GridPluginOptions);
17
+ install(app: App): void;
18
+ /** 绘制点阵网格 */
19
+ draw(): void;
20
+ /** 调整网格 Canvas 尺寸并重绘(由 App.resize 或外部触发) */
21
+ resize(width: number, height: number): void;
22
+ /** 更新配置并重绘 */
23
+ update(options: Partial<GridPluginOptions>): void;
24
+ dispose(): void;
25
+ }
26
+ /**
27
+ * 创建点阵网格背景插件
28
+ *
29
+ * @example
30
+ * ```ts
31
+ * import {gridPlugin} from 'rendx-grid-plugin';
32
+ *
33
+ * const app = new App({width: 800, height: 600});
34
+ * app.mount(container);
35
+ * app.use(gridPlugin({spacing: 20, color: '#ccc'}));
36
+ * ```
37
+ */
38
+ declare function gridPlugin(options?: GridPluginOptions): GridPlugin;
39
+
40
+ export { type GridPluginOptions, gridPlugin };
package/dist/main.js ADDED
@@ -0,0 +1,120 @@
1
+ var __typeError = (msg) => {
2
+ throw TypeError(msg);
3
+ };
4
+ var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
5
+ var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
6
+ var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
7
+ var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
8
+ var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
9
+
10
+ // src/grid.ts
11
+ var DEFAULTS = {
12
+ spacing: 20,
13
+ dotRadius: 1,
14
+ color: "#d0d0d0",
15
+ zIndex: -1
16
+ };
17
+ var _app, _canvas, _ctx, _options, _GridPlugin_instances, createCanvas_fn;
18
+ var GridPlugin = class {
19
+ constructor(options = {}) {
20
+ __privateAdd(this, _GridPlugin_instances);
21
+ this.name = "grid";
22
+ __privateAdd(this, _app, null);
23
+ __privateAdd(this, _canvas, null);
24
+ __privateAdd(this, _ctx, null);
25
+ __privateAdd(this, _options);
26
+ __privateSet(this, _options, { ...DEFAULTS, ...options });
27
+ }
28
+ install(app) {
29
+ __privateSet(this, _app, app);
30
+ const container = app.container;
31
+ if (!container) {
32
+ throw new Error(
33
+ "[rendx-grid-plugin] App must be mounted before calling use(). Call app.mount(el) first, then app.use(gridPlugin(...))."
34
+ );
35
+ }
36
+ __privateMethod(this, _GridPlugin_instances, createCanvas_fn).call(this, container);
37
+ this.draw();
38
+ }
39
+ /** 绘制点阵网格 */
40
+ draw() {
41
+ const ctx = __privateGet(this, _ctx);
42
+ const canvas = __privateGet(this, _canvas);
43
+ if (!ctx || !canvas) return;
44
+ const { width, height } = __privateGet(this, _app).cfg;
45
+ const w = width ?? 800;
46
+ const h = height ?? 600;
47
+ const { spacing, dotRadius, color } = __privateGet(this, _options);
48
+ ctx.clearRect(0, 0, w, h);
49
+ ctx.fillStyle = color;
50
+ for (let x = spacing; x < w; x += spacing) {
51
+ for (let y = spacing; y < h; y += spacing) {
52
+ ctx.beginPath();
53
+ ctx.arc(x, y, dotRadius, 0, Math.PI * 2);
54
+ ctx.fill();
55
+ }
56
+ }
57
+ }
58
+ /** 调整网格 Canvas 尺寸并重绘(由 App.resize 或外部触发) */
59
+ resize(width, height) {
60
+ const canvas = __privateGet(this, _canvas);
61
+ const ctx = __privateGet(this, _ctx);
62
+ if (!canvas || !ctx) return;
63
+ const ratio = window.devicePixelRatio || 1;
64
+ canvas.width = width * ratio;
65
+ canvas.height = height * ratio;
66
+ canvas.style.width = `${width}px`;
67
+ canvas.style.height = `${height}px`;
68
+ ctx.scale(ratio, ratio);
69
+ this.draw();
70
+ }
71
+ /** 更新配置并重绘 */
72
+ update(options) {
73
+ Object.assign(__privateGet(this, _options), options);
74
+ if (options.zIndex !== void 0 && __privateGet(this, _canvas)) {
75
+ __privateGet(this, _canvas).style.zIndex = String(options.zIndex);
76
+ }
77
+ this.draw();
78
+ }
79
+ dispose() {
80
+ if (__privateGet(this, _canvas)) {
81
+ __privateGet(this, _canvas).remove();
82
+ __privateSet(this, _canvas, null);
83
+ __privateSet(this, _ctx, null);
84
+ }
85
+ __privateSet(this, _app, null);
86
+ }
87
+ };
88
+ _app = new WeakMap();
89
+ _canvas = new WeakMap();
90
+ _ctx = new WeakMap();
91
+ _options = new WeakMap();
92
+ _GridPlugin_instances = new WeakSet();
93
+ createCanvas_fn = function(container) {
94
+ const { width, height } = __privateGet(this, _app).cfg;
95
+ const w = width ?? 800;
96
+ const h = height ?? 600;
97
+ const canvas = document.createElement("canvas");
98
+ const ratio = window.devicePixelRatio || 1;
99
+ canvas.width = w * ratio;
100
+ canvas.height = h * ratio;
101
+ canvas.style.position = "absolute";
102
+ canvas.style.left = "0";
103
+ canvas.style.top = "0";
104
+ canvas.style.width = `${w}px`;
105
+ canvas.style.height = `${h}px`;
106
+ canvas.style.zIndex = String(__privateGet(this, _options).zIndex);
107
+ canvas.style.pointerEvents = "none";
108
+ container.insertBefore(canvas, container.firstChild);
109
+ const ctx = canvas.getContext("2d");
110
+ if (!ctx) throw new Error("[rendx-grid-plugin] Could not get 2d context");
111
+ ctx.scale(ratio, ratio);
112
+ __privateSet(this, _canvas, canvas);
113
+ __privateSet(this, _ctx, ctx);
114
+ };
115
+ function gridPlugin(options) {
116
+ return new GridPlugin(options);
117
+ }
118
+ export {
119
+ gridPlugin
120
+ };
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "rendx-grid-plugin",
3
+ "version": "0.1.0",
4
+ "description": "Grid overlay plugin for Rendx engine",
5
+ "license": "MIT",
6
+ "author": "wei.liang (https://github.com/weiliang0121)",
7
+ "type": "module",
8
+ "keywords": [
9
+ "rendx",
10
+ "2d",
11
+ "canvas",
12
+ "rendering",
13
+ "visualization",
14
+ "scene-graph"
15
+ ],
16
+ "homepage": "https://weiliang0121.github.io/rendx/",
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "https://github.com/weiliang0121/rendx.git",
20
+ "directory": "packages/grid-plugin"
21
+ },
22
+ "main": "dist/main.cjs",
23
+ "module": "dist/main.js",
24
+ "types": "dist/main.d.ts",
25
+ "exports": {
26
+ ".": {
27
+ "types": "./dist/main.d.ts",
28
+ "import": "./dist/main.js",
29
+ "require": "./dist/main.cjs"
30
+ }
31
+ },
32
+ "dependencies": {
33
+ "rendx-engine": "^0.2.0"
34
+ },
35
+ "sideEffects": false,
36
+ "files": [
37
+ "dist"
38
+ ],
39
+ "publishConfig": {
40
+ "access": "public"
41
+ },
42
+ "scripts": {
43
+ "build": "tsup",
44
+ "dev": "tsup --watch"
45
+ }
46
+ }