1 | # MatrixPointer
|
2 | This is a library that assists in focusing on any position of a matrix and moving relative or absolute to that position.
|
3 |
|
4 | ## Features
|
5 | - Written in Typescript
|
6 | - Has no dependencies (e.g. jquery)
|
7 | - Lightweight, 1KB
|
8 | - Supports ES6
|
9 |
|
10 | ## Install
|
11 | ### git
|
12 | ```bash
|
13 | git clone https://github.com/arayutw/matrix-pointer.git
|
14 | cd matrix-pointer
|
15 | npm install
|
16 | npm run build
|
17 | ```
|
18 |
|
19 | ### npm
|
20 | ```bash
|
21 | npm install @arayutw/matrix-pointer
|
22 | ```
|
23 |
|
24 | ### CDN
|
25 | Please find various build files (esm, cjs, umd).
|
26 | - [unpkg](https://unpkg.com/browse/@arayutw/matrix-pointer/dist/scripts/)
|
27 | - [jsdelivr](https://cdn.jsdelivr.net/npm/@arayutw/matrix-pointer@latest/dist/scripts/)
|
28 |
|
29 |
|
30 | ## Package
|
31 | If you plan to use it as a package, it is recommended to use [@arayutw/matrix-pointer-import](https://www.npmjs.com/package/@arayutw/matrix-pointer-import?activeTab=code). It is a minimal project with only TypeScript files that has the same contents as this repository.
|
32 |
|
33 | ```bash
|
34 | npm install @arayutw/matrix-pointer-import
|
35 | ```
|
36 |
|
37 | ```ts
|
38 | import MatrixPointer from "@arayutw/matrix-pointer-import";
|
39 | ```
|
40 |
|
41 | ## Demo
|
42 | Check out the demo to see the possibilities.
|
43 | [https://arayutw.github.io/matrix-pointer/demo/](https://arayutw.github.io/matrix-pointer/demo/)
|
44 |
|
45 | ## Usage
|
46 | ### load
|
47 | #### ESM
|
48 | ```js
|
49 | import MatrixPointer from "<pathto>/matrix-pointer.esm"
|
50 | ```
|
51 |
|
52 | #### UMD
|
53 | ```html
|
54 | <script src="https://unpkg.com/@arayutw/matrix-pointer@latest/dist/scripts/matrix-pointer.js"></script>
|
55 | ```
|
56 |
|
57 | ### initialize
|
58 | The `matrix` parameter is required.
|
59 | ```js
|
60 | const mp = new MatrixPointer({
|
61 | jump?: Jump
|
62 | loop?: Loop
|
63 | loose?: Loose
|
64 | matrix: [
|
65 | [1, 2, 3],
|
66 | [4, 5, 6],
|
67 | [7, 8, 9]
|
68 | ]
|
69 | });
|
70 | ```
|
71 |
|
72 | ### Matrix
|
73 | Please represent the matrix as a collection of arrays of rows.
|
74 | ```ts
|
75 | [
|
76 | [1, 2, 3], // row
|
77 | [4, 5, 6], // row
|
78 | ]
|
79 | ```
|
80 |
|
81 | You do not need to align the number of elements in each row.
|
82 | ```ts
|
83 | [
|
84 | [1, 2, 3],
|
85 | [4],
|
86 | [5, 6]
|
87 | ]
|
88 | ```
|
89 |
|
90 | Please specify a unique ID for each element in the row. The type of the ID is arbitrary, but it needs to be comparable with the `===` operator.
|
91 | ```ts
|
92 | [
|
93 | [1, 2, 3],
|
94 | ["A", "B", "C"],
|
95 | [HTMLElement, Node, Symbol("a")],
|
96 | ]
|
97 | ```
|
98 |
|
99 | Duplicate values will cause calculation errors, so please be careful.
|
100 | ```ts
|
101 | [
|
102 | [1, 2, 3],
|
103 | [1, 2, 3],
|
104 | ]
|
105 | ```
|
106 |
|
107 | `null` is special and treated as a blank. Anything other than `null` (such as `undefined`) is not treated as a blank.
|
108 | ```ts
|
109 | [
|
110 | [1, 2, 3],
|
111 | [4, null, null],
|
112 | ]
|
113 | ```
|
114 |
|
115 | A matrix can be specified using a function when it is dynamic.
|
116 | The function is called at an appropriate timing.
|
117 | ```ts
|
118 | matrix: () => {
|
119 | return [
|
120 | [1, 2, 3],
|
121 | [4, 5, 6],
|
122 | [7, 8, 9]
|
123 | ];
|
124 | }
|
125 | ```
|
126 |
|
127 | ### Loop, Jump, Loose
|
128 | Options can be defined as boolean values or x and y objects representing horizontal and vertical directions respectively.
|
129 | ```ts
|
130 | loop: true
|
131 | loop: false
|
132 | loop: {
|
133 | x: true,
|
134 | y: false,
|
135 | }
|
136 | ```
|
137 |
|
138 | You can also make changes later.
|
139 | ```
|
140 | mp.loop = true
|
141 | ```
|
142 | | name | default | description |
|
143 | | --- | --- | --- |
|
144 | | `loop` | `true` | Wrap-around movement is allowed. |
|
145 | | `jump` | `true` | When moving beyond the edge, move to the next/prev row or column. |
|
146 | | `loose` | `false` | When the destination is blank, shift the row or column to move. **Depending on the matrix structure, unreachable positions may exist, so please set accordingly.** |
|
147 |
|
148 | ### `moveTo()`
|
149 | You can move by specifying the x and y array indices or the ID directly. If the movement fails, null will be returned, otherwise a Position object representing the new position information will be returned.
|
150 | ```ts
|
151 | moveTo(x: number, y: number) => Position | null
|
152 | moveTo(id: unknown) => Position | null
|
153 | ```
|
154 | ```ts
|
155 | /*
|
156 | [
|
157 | [1, 2, 3],
|
158 | [4, 5, 6],
|
159 | [7, 8, 9]
|
160 | ]
|
161 | */
|
162 |
|
163 | mp.moveTo(1, 2); // id=8(x=1,y=2)
|
164 | const newPosition = mp.moveTo(5); // id=5(x=1,y=1)
|
165 | /*
|
166 | {
|
167 | id: unknown
|
168 | x: number
|
169 | y: number
|
170 | }
|
171 | */
|
172 | ```
|
173 |
|
174 | ### `moveBy()`
|
175 | Move relative to the current position. See moveTo() for return value description.
|
176 | ```ts
|
177 | moveBy(direction: "x"|"y", next: boolean) => Position | null
|
178 | ```
|
179 | ```ts
|
180 | mp.moveBy("x", false); // left
|
181 | mp.moveBy("x", true); // right
|
182 | mp.moveBy("y", false); // top
|
183 | const newPosition = mp.moveBy("y", true); // bottom
|
184 | ```
|
185 |
|
186 | ### `on()`
|
187 | ```js
|
188 | const handler = (event) => {
|
189 | //{
|
190 | // target: MatrixPointer
|
191 | // type: "blur" | "focus"
|
192 | // id: unknown
|
193 | // x: number
|
194 | // y: number
|
195 | //}
|
196 | console.log(event);
|
197 | }
|
198 |
|
199 | a.on("blur", handler, {
|
200 | once: false,
|
201 | });
|
202 |
|
203 | a.on("focus", handler, {
|
204 | once: false,
|
205 | });
|
206 | ```
|
207 |
|
208 | ### `off()`
|
209 | ```js
|
210 | mp.off("focus", handler);
|
211 | ```
|
212 |
|
213 | ### `emit()`
|
214 | ```js
|
215 | mp.emit("click", {
|
216 | x: 12,
|
217 | y: 34,
|
218 | });
|
219 | ```
|
220 |
|
221 | ### `destroy`
|
222 | ```js
|
223 | mp.destroy();
|
224 | ``` |
\ | No newline at end of file |