@buncanvas/core 0.1.2 → 0.1.4
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 +137 -11
- package/index.js +3350 -2
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -1,23 +1,149 @@
|
|
|
1
|
-
#
|
|
1
|
+
# BunCanvas 🎨
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A fast, lightweight 2D graphics canvas binding for the **Bun** JavaScript runtime, powered by native Google Skia rendering.
|
|
4
|
+
|
|
5
|
+
BunCanvas brings hardware-accelerated 2D drawing capabilities directly to desktop applications—without needing a browser, DOM environment, or heavy webview dependencies.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## ⚙️ Installation
|
|
10
|
+
|
|
11
|
+
Add `BunCanvas` to your project via Bun:
|
|
4
12
|
|
|
5
13
|
```bash
|
|
6
|
-
bun
|
|
14
|
+
bun add @buncanvas/core
|
|
15
|
+
Note: BunCanvas automatically resolves and installs the matching native binary (.so, .dll, or .dylib) for your platform via optionalDependencies.
|
|
7
16
|
```
|
|
8
17
|
|
|
9
|
-
|
|
18
|
+
## 🚀 Quick Start
|
|
19
|
+
Here is a minimal example that opens a window and renders a simple rectangle.
|
|
10
20
|
|
|
11
|
-
```
|
|
12
|
-
|
|
21
|
+
```JavaScript
|
|
22
|
+
import { Window, Canvas } from "@buncanvas/core";
|
|
23
|
+
|
|
24
|
+
// 1. Create a native window
|
|
25
|
+
const win = new Window(800, 600, {
|
|
26
|
+
title: "BunCanvas Quickstart",
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
// 2. Create a 2D drawing canvas matching the window size
|
|
30
|
+
const canvas = new Canvas(800, 600);
|
|
31
|
+
const ctx = canvas.getContext("2d");
|
|
32
|
+
|
|
33
|
+
// 3. Draw onto the canvas context
|
|
34
|
+
ctx.fillStyle = "#3498db";
|
|
35
|
+
ctx.fillRect(50, 50, 200, 150);
|
|
36
|
+
|
|
37
|
+
// 4. Attach the canvas to the window frame
|
|
38
|
+
win.append(canvas);
|
|
13
39
|
```
|
|
14
40
|
|
|
15
|
-
|
|
41
|
+
## 🎨 Code Examples
|
|
42
|
+
1. Animated Bouncing Ball (Render Loop)
|
|
43
|
+
Use requestAnimationFrame to manage a smooth render loop for real-time graphics and physics.
|
|
16
44
|
|
|
17
|
-
```
|
|
18
|
-
|
|
45
|
+
```JavaScript
|
|
46
|
+
import { Window, Canvas } from "@buncanvas/core";
|
|
47
|
+
|
|
48
|
+
const WIDTH = 800;
|
|
49
|
+
const HEIGHT = 600;
|
|
50
|
+
|
|
51
|
+
const win = new Window(WIDTH, HEIGHT, { title: "Animation Example" });
|
|
52
|
+
const canvas = new Canvas(WIDTH, HEIGHT);
|
|
53
|
+
const ctx = canvas.getContext("2d");
|
|
54
|
+
|
|
55
|
+
win.append(canvas);
|
|
56
|
+
|
|
57
|
+
// Ball state
|
|
58
|
+
let x = 100;
|
|
59
|
+
let y = 100;
|
|
60
|
+
let vx = 4;
|
|
61
|
+
let vy = 3;
|
|
62
|
+
const radius = 25;
|
|
63
|
+
|
|
64
|
+
function render(timestamp) {
|
|
65
|
+
// Clear screen
|
|
66
|
+
ctx.fillStyle = "#1e1e2e";
|
|
67
|
+
ctx.fillRect(0, 0, WIDTH, HEIGHT);
|
|
68
|
+
|
|
69
|
+
// Update position
|
|
70
|
+
x += vx;
|
|
71
|
+
y += vy;
|
|
72
|
+
|
|
73
|
+
// Bounce off edges
|
|
74
|
+
if (x - radius < 0 || x + radius > WIDTH) vx *= -1;
|
|
75
|
+
if (y - radius < 0 || y + radius > HEIGHT) vy *= -1;
|
|
76
|
+
|
|
77
|
+
// Draw circle
|
|
78
|
+
ctx.beginPath();
|
|
79
|
+
ctx.arc(x, y, radius, 0, Math.PI * 2);
|
|
80
|
+
ctx.fillStyle = "#ff5555";
|
|
81
|
+
ctx.fill();
|
|
82
|
+
ctx.lineWidth = 3;
|
|
83
|
+
ctx.strokeStyle = "#ffffff";
|
|
84
|
+
ctx.stroke();
|
|
85
|
+
|
|
86
|
+
// Schedule next frame
|
|
87
|
+
requestAnimationFrame(render);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Start loop
|
|
91
|
+
requestAnimationFrame(render);
|
|
19
92
|
```
|
|
20
93
|
|
|
21
|
-
|
|
94
|
+
## 2. Gradient & Path Drawing
|
|
95
|
+
BunCanvas supports complex paths, transformations, and gradient shaders.
|
|
96
|
+
|
|
97
|
+
```JavaScript
|
|
98
|
+
import { Window, Canvas } from "@buncanvas/core";
|
|
99
|
+
|
|
100
|
+
const win = new Window(600, 600, { title: "Gradient & Paths" });
|
|
101
|
+
const canvas = new Canvas(600, 600);
|
|
102
|
+
const ctx = canvas.getContext("2d");
|
|
103
|
+
|
|
104
|
+
// Create linear gradient
|
|
105
|
+
const gradient = ctx.createLinearGradient(0, 0, 600, 600);
|
|
106
|
+
gradient.addColorStop(0, "#8e44ad");
|
|
107
|
+
gradient.addColorStop(1, "#f39c12");
|
|
108
|
+
|
|
109
|
+
ctx.fillStyle = gradient;
|
|
110
|
+
ctx.fillRect(0, 0, 600, 600);
|
|
111
|
+
|
|
112
|
+
// Draw a star shape
|
|
113
|
+
ctx.save();
|
|
114
|
+
ctx.translate(300, 300);
|
|
115
|
+
ctx.fillStyle = "#ffffff";
|
|
116
|
+
ctx.beginPath();
|
|
117
|
+
|
|
118
|
+
for (let i = 0; i < 5; i++) {
|
|
119
|
+
ctx.lineTo(Math.cos(((18 + i * 72) * Math.PI) / 180) * 120, -Math.sin(((18 + i * 72) * Math.PI) / 180) * 120);
|
|
120
|
+
ctx.lineTo(Math.cos(((54 + i * 72) * Math.PI) / 180) * 50, -Math.sin(((54 + i * 72) * Math.PI) / 180) * 50);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
ctx.closePath();
|
|
124
|
+
ctx.fill();
|
|
125
|
+
ctx.restore();
|
|
126
|
+
|
|
127
|
+
win.append(canvas);
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## 🛠️ API Reference Summary
|
|
131
|
+
`new Window(width, height, options)` Creates a desktop window frame.
|
|
132
|
+
|
|
133
|
+
`width`: number - Initial window width in pixels.
|
|
134
|
+
|
|
135
|
+
`height`: number - Initial window height in pixels.
|
|
136
|
+
|
|
137
|
+
`options`: object (Optional)
|
|
138
|
+
|
|
139
|
+
`title`: string - Title bar text.
|
|
140
|
+
|
|
141
|
+
`window.append(canvas)`: Binds a Canvas instance to render inside the window viewport.
|
|
142
|
+
|
|
143
|
+
`new Canvas(width, height)` Creates an off-screen 2D drawing surface.
|
|
144
|
+
|
|
145
|
+
`canvas.getContext('2d')`: Returns the 2D rendering context supporting standard canvas operations (fillRect, arc, save, restore, etc.).
|
|
146
|
+
|
|
147
|
+
`requestAnimationFrame(callback)` Schedules a frame update for continuous rendering.
|
|
22
148
|
|
|
23
|
-
|
|
149
|
+
Refer to <a href="https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D">CanvasRenderingContext2D</a> for other drawing methods.
|