canvas2gl 1.0.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/README.md ADDED
@@ -0,0 +1,376 @@
1
+ 中文文档 [README.zh-CN.md](README.zh-CN.md)
2
+ # Canvas2GL
3
+
4
+ A high-performance 2D graphics library that maps the Canvas 2D API to WebGL rendering. Use standard Canvas 2D API calls — the library handles WebGL acceleration under the hood. No new APIs to learn.
5
+
6
+ ## Quick Start
7
+
8
+ ```html
9
+ <canvas id="c" width="800" height="600"></canvas>
10
+
11
+ <script type="module">
12
+ import Canvas2gl from 'canvas2gl';
13
+
14
+ const ctx = new Canvas2gl(document.getElementById('c'));
15
+
16
+ ctx.fillStyle = '#e94560';
17
+ ctx.fillRect(50, 50, 200, 100);
18
+
19
+ ctx.fillStyle = 'white';
20
+ ctx.font = '18px sans-serif';
21
+ ctx.fillText('Hello Canvas2GL', 80, 110);
22
+ </script>
23
+ ```
24
+
25
+ ## Properties
26
+
27
+ | Property | Type | Default | Description |
28
+ |----------|------|---------|-------------|
29
+ | `fillStyle` | `string \| Gradient` | `'#000000'` | Fill color or gradient |
30
+ | `strokeStyle` | `string \| Gradient` | `'#000000'` | Stroke color or gradient |
31
+ | `lineWidth` | `number` | `1` | Line width in pixels |
32
+ | `globalAlpha` | `number` | `1` | Global alpha transparency (0–1) |
33
+ | `globalCompositeOperation` | `string` | `'source-over'` | Compositing operation |
34
+ | `font` | `string` | `'10px sans-serif'` | Font style (same as CSS `font`) |
35
+ | `textAlign` | `string` | `'start'` | Horizontal text alignment |
36
+ | `textBaseline` | `string` | `'alphabetic'` | Text baseline alignment |
37
+ | `direction` | `string` | `'inherit'` | Text direction (`'ltr'` / `'rtl'` / `'inherit'`) |
38
+ | `letterSpacing` | `string` | `'0px'` | Letter spacing |
39
+
40
+ ### Supported `globalCompositeOperation` Values
41
+
42
+ | Value | Description |
43
+ |-------|-------------|
44
+ | `'source-over'` | Default — draws new shapes over existing content |
45
+ | `'lighter'` | Additive blending |
46
+ | `'copy'` | Only shows new shapes, clears existing content |
47
+ | `'source-in'` | Keeps new shapes only in overlapping areas |
48
+ | `'source-out'` | Keeps new shapes only in non-overlapping areas |
49
+ | `'source-atop'` | New shapes on top, only in overlapping areas |
50
+ | `'destination-in'` | Keeps existing content only in overlapping areas |
51
+ | `'destination-out'` | Keeps existing content only in non-overlapping areas |
52
+ | `'destination-atop'` | Existing content on top, only in overlapping areas |
53
+ | `'xor'` | XOR mode — overlapping area becomes transparent |
54
+ | `'multiply'` | Multiply blending |
55
+
56
+ ### Supported `textAlign` Values
57
+
58
+ | Value | Description |
59
+ |-------|-------------|
60
+ | `'left'` | Align text to the left |
61
+ | `'center'` | Center text |
62
+ | `'right'` | Align text to the right |
63
+ | `'start'` | Align based on text direction (default) |
64
+ | `'end'` | Align based on text direction |
65
+
66
+ ### Supported `textBaseline` Values
67
+
68
+ | Value | Description |
69
+ |-------|-------------|
70
+ | `'top'` | Align to top of text |
71
+ | `'hanging'` | Hanging baseline |
72
+ | `'middle'` | Middle of text |
73
+ | `'alphabetic'` | Alphabetic baseline (default) |
74
+ | `'ideographic'` | Ideographic baseline |
75
+ | `'bottom'` | Bottom of text |
76
+
77
+ ## Path Methods
78
+
79
+ ### beginPath()
80
+ Starts a new path, clearing previous path commands.
81
+
82
+ ```js
83
+ ctx.beginPath();
84
+ ```
85
+
86
+ ### moveTo(x, y)
87
+ Moves the pen to the given coordinates, starting a new sub-path.
88
+
89
+ ```js
90
+ ctx.moveTo(100, 100);
91
+ ```
92
+
93
+ ### lineTo(x, y)
94
+ Draws a straight line from the current position to the given coordinates.
95
+
96
+ ```js
97
+ ctx.lineTo(200, 100);
98
+ ```
99
+
100
+ ### arc(cx, cy, radius, startAngle, endAngle, anticlockwise?)
101
+ Draws an arc. Angles are in radians.
102
+
103
+ ```js
104
+ // Full circle
105
+ ctx.arc(150, 150, 50, 0, Math.PI * 2);
106
+
107
+ // Counter-clockwise half circle
108
+ ctx.arc(150, 150, 50, 0, Math.PI, true);
109
+ ```
110
+
111
+ ### rect(x, y, w, h)
112
+ Adds a rectangle sub-path.
113
+
114
+ ```js
115
+ ctx.rect(10, 10, 100, 50);
116
+ ```
117
+
118
+ ### bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y)
119
+ Adds a cubic Bezier curve from the current point to `(x, y)`, with control points `(cp1x, cp1y)` and `(cp2x, cp2y)`.
120
+
121
+ ```js
122
+ ctx.beginPath();
123
+ ctx.moveTo(50, 100);
124
+ ctx.bezierCurveTo(100, 20, 200, 180, 300, 100);
125
+ ctx.stroke();
126
+ ```
127
+
128
+ ### quadraticCurveTo(cpx, cpy, x, y)
129
+ Adds a quadratic Bezier curve from the current point to `(x, y)`, with control point `(cpx, cpy)`.
130
+
131
+ ```js
132
+ ctx.beginPath();
133
+ ctx.moveTo(50, 100);
134
+ ctx.quadraticCurveTo(200, 20, 350, 100);
135
+ ctx.stroke();
136
+ ```
137
+
138
+ ### closePath()
139
+ Closes the current path by connecting the end point to the start point.
140
+
141
+ ```js
142
+ ctx.closePath();
143
+ ```
144
+
145
+ ### fill()
146
+ Fills the current path. Automatically calls `beginPath()` afterwards.
147
+
148
+ ```js
149
+ ctx.beginPath();
150
+ ctx.arc(100, 100, 40, 0, Math.PI * 2);
151
+ ctx.fill();
152
+ ```
153
+
154
+ ### stroke()
155
+ Strokes the current path. Automatically calls `beginPath()` afterwards.
156
+
157
+ ```js
158
+ ctx.beginPath();
159
+ ctx.moveTo(50, 50);
160
+ ctx.lineTo(150, 100);
161
+ ctx.stroke();
162
+ ```
163
+
164
+ ## Rectangle Methods
165
+
166
+ ### fillRect(x, y, w, h)
167
+ Draws a filled rectangle.
168
+
169
+ ```js
170
+ ctx.fillStyle = '#e94560';
171
+ ctx.fillRect(50, 50, 200, 100);
172
+ ```
173
+
174
+ ### strokeRect(x, y, w, h)
175
+ Draws a stroked rectangle.
176
+
177
+ ```js
178
+ ctx.strokeStyle = '#00d4ff';
179
+ ctx.lineWidth = 3;
180
+ ctx.strokeRect(50, 50, 200, 100);
181
+ ```
182
+
183
+ ### clearRect(x, y, w, h)
184
+ Clears the specified rectangular area to transparent.
185
+
186
+ ```js
187
+ ctx.clearRect(0, 0, 400, 200);
188
+ ```
189
+
190
+ ## Gradient Methods
191
+
192
+ ### createLinearGradient(x0, y0, x1, y1)
193
+ Creates a linear gradient from `(x0, y0)` to `(x1, y1)`. Returns a `Gradient` object that can be assigned to `fillStyle` or `strokeStyle`.
194
+
195
+ ```js
196
+ const grad = ctx.createLinearGradient(0, 0, 400, 0);
197
+ grad.addColorStop(0, '#e94560');
198
+ grad.addColorStop(0.5, '#ffd700');
199
+ grad.addColorStop(1, '#00d4ff');
200
+ ctx.fillStyle = grad;
201
+ ctx.fillRect(0, 0, 400, 200);
202
+ ```
203
+
204
+ ### Gradient.addColorStop(offset, color)
205
+ Adds a color stop to the gradient. `offset` is a value between 0 and 1, `color` is any valid CSS color string.
206
+
207
+ ```js
208
+ grad.addColorStop(0, '#e94560'); // red at start
209
+ grad.addColorStop(0.5, 'rgba(0,255,100,0.5)'); // semi-transparent green in middle
210
+ grad.addColorStop(1, 'blue'); // blue at end
211
+ ```
212
+
213
+ ## Text Methods
214
+
215
+ ### fillText(text, x, y, maxWidth?)
216
+ Draws filled text.
217
+
218
+ ```js
219
+ ctx.font = '24px sans-serif';
220
+ ctx.fillStyle = 'white';
221
+ ctx.fillText('Hello World', 100, 100);
222
+
223
+ // With max width constraint
224
+ ctx.fillText('Hello World', 100, 100, 200);
225
+ ```
226
+
227
+ ### strokeText(text, x, y, maxWidth?)
228
+ Draws stroked (outlined) text.
229
+
230
+ ```js
231
+ ctx.font = 'bold 18px sans-serif';
232
+ ctx.strokeStyle = '#00d4ff';
233
+ ctx.strokeText('Stroke Text', 50, 80);
234
+ ```
235
+
236
+ ### measureText(text)
237
+ Measures text size and returns a `TextMetrics` object.
238
+
239
+ ```js
240
+ const metrics = ctx.measureText('Hello');
241
+ console.log(metrics.width); // text width
242
+ ```
243
+
244
+ ## Image Methods
245
+
246
+ ### drawImage(source, ...args)
247
+ Draws an image onto the canvas. Supports three overloads:
248
+
249
+ **Form 1: Draw at original size**
250
+ ```js
251
+ ctx.drawImage(image, dx, dy);
252
+ ```
253
+
254
+ **Form 2: Draw with scaling**
255
+ ```js
256
+ ctx.drawImage(image, dx, dy, dw, dh);
257
+ ```
258
+
259
+ **Form 3: Crop and scale**
260
+ ```js
261
+ ctx.drawImage(image, sx, sy, sw, sh, dx, dy, dw, dh);
262
+ ```
263
+
264
+ Parameters:
265
+ - `source`: `HTMLImageElement` / `HTMLCanvasElement` / `HTMLVideoElement`
266
+ - `sx`, `sy`: Source crop start coordinates
267
+ - `sw`, `sh`: Source crop size
268
+ - `dx`, `dy`: Destination canvas coordinates
269
+ - `dw`, `dh`: Destination canvas size
270
+
271
+ ```js
272
+ const img = new Image();
273
+ img.src = 'texture.png';
274
+ img.onload = () => {
275
+ // Draw at original size at (50, 50)
276
+ ctx.drawImage(img, 50, 50);
277
+
278
+ // Draw scaled to 100x100 at (200, 50)
279
+ ctx.drawImage(img, 200, 50, 100, 100);
280
+
281
+ // Crop (10,10,50,50) and draw to (300,50,80,80)
282
+ ctx.drawImage(img, 10, 10, 50, 50, 300, 50, 80, 80);
283
+ };
284
+ ```
285
+
286
+ ## Pixel Methods
287
+
288
+ ### getImageData(sx, sy, sw, sh)
289
+ Reads pixel data from the specified rectangle and returns an `ImageData` object.
290
+
291
+ ```js
292
+ const imageData = ctx.getImageData(0, 0, 400, 200);
293
+ // imageData.data is a Uint8ClampedArray of RGBA values
294
+ // imageData.width and imageData.height are the requested dimensions
295
+ const r = imageData.data[0]; // red component of first pixel
296
+ const g = imageData.data[1]; // green
297
+ const b = imageData.data[2]; // blue
298
+ const a = imageData.data[3]; // alpha
299
+ ```
300
+
301
+ ## Transform Methods
302
+
303
+ ### translate(x, y)
304
+ Moves the origin to the given coordinates.
305
+
306
+ ```js
307
+ ctx.translate(200, 100);
308
+ ```
309
+
310
+ ### rotate(angle)
311
+ Rotates by `angle` (in radians).
312
+
313
+ ```js
314
+ ctx.rotate(Math.PI / 4); // 45 degrees
315
+ ```
316
+
317
+ ### scale(x, y)
318
+ Scales by `x` horizontally and `y` vertically.
319
+
320
+ ```js
321
+ ctx.scale(2, 1); // double horizontal size
322
+ ```
323
+
324
+ ### transform(a, b, c, d, e, f)
325
+ Applies a cumulative transform matrix `[a, b, c, d, e, f]`.
326
+
327
+ ```js
328
+ ctx.transform(1, 0.5, 0, 1, 10, 0);
329
+ ```
330
+
331
+ ### setTransform(a, b, c, d, e, f)
332
+ Resets and sets the transform matrix.
333
+
334
+ ```js
335
+ ctx.setTransform(2, 0, 0, 2, 50, 50);
336
+ ```
337
+
338
+ ### resetTransform()
339
+ Resets the transform matrix to the identity matrix.
340
+
341
+ ```js
342
+ ctx.resetTransform();
343
+ ```
344
+
345
+ ## State Methods
346
+
347
+ ### save()
348
+ Saves the current drawing state onto a stack, including: transform matrix, `fillStyle`, `strokeStyle`, `lineWidth`, `globalAlpha`, `globalCompositeOperation`.
349
+
350
+ ```js
351
+ ctx.save();
352
+ ctx.translate(100, 50);
353
+ // ... draw operations ...
354
+ ctx.restore(); // restores the saved state
355
+ ```
356
+
357
+ ### restore()
358
+ Restores the most recently saved drawing state from the stack.
359
+
360
+ ## Canvas Methods
361
+
362
+ ### clear()
363
+ Clears the entire canvas to transparent.
364
+
365
+ ```js
366
+ ctx.clear();
367
+ ```
368
+
369
+ ### resize()
370
+ Resizes the WebGL viewport to match the CSS size of the canvas element. Call this when the canvas size changes.
371
+
372
+ ```js
373
+ window.addEventListener('resize', () => {
374
+ ctx.resize();
375
+ });
376
+ ```