canvasengine 2.0.0-beta.1 → 2.0.0-beta.2

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.
@@ -1,286 +0,0 @@
1
- /*
2
- * Joystick
3
- *
4
- * Inspired by https://github.com/endel/pixi-virtual-joystick
5
- */
6
-
7
- import * as PIXI from "pixi.js";
8
- import { Container, Graphics, Sprite } from "../components";
9
- import { h } from "../engine/signal";
10
- import { signal } from "@signe/reactive";
11
-
12
- export interface JoystickChangeEvent {
13
- angle: number;
14
- direction: Direction;
15
- power: number;
16
- }
17
-
18
- export enum Direction {
19
- LEFT = "left",
20
- TOP = "top",
21
- BOTTOM = "bottom",
22
- RIGHT = "right",
23
- TOP_LEFT = "top_left",
24
- TOP_RIGHT = "top_right",
25
- BOTTOM_LEFT = "bottom_left",
26
- BOTTOM_RIGHT = "bottom_right",
27
- }
28
-
29
- export interface JoystickSettings {
30
- outer?: string;
31
- inner?: string;
32
- outerScale?: { x: number; y: number };
33
- innerScale?: { x: number; y: number };
34
- onChange?: (data: JoystickChangeEvent) => void;
35
- onStart?: () => void;
36
- onEnd?: () => void;
37
- }
38
-
39
- export function Joystick(opts: JoystickSettings = {}) {
40
- const settings = Object.assign(
41
- {
42
- outerScale: { x: 1, y: 1 },
43
- innerScale: { x: 1, y: 1 },
44
- },
45
- opts
46
- );
47
-
48
- let outerRadius = 70;
49
- let innerRadius = 10;
50
- const innerAlphaStandby = 0.5;
51
-
52
- let dragging = false;
53
- let startPosition: PIXI.PointData | null = null;
54
- let power = 0;
55
-
56
- const innerPositionX = signal(0);
57
- const innerPositionY = signal(0);
58
- const innerAlpha = signal(innerAlphaStandby);
59
-
60
- function getPower(centerPoint: PIXI.Point) {
61
- const a = centerPoint.x - 0;
62
- const b = centerPoint.y - 0;
63
- return Math.min(1, Math.sqrt(a * a + b * b) / outerRadius);
64
- }
65
-
66
- function getDirection(center: PIXI.Point) {
67
- let rad = Math.atan2(center.y, center.x); // [-PI, PI]
68
- if ((rad >= -Math.PI / 8 && rad < 0) || (rad >= 0 && rad < Math.PI / 8)) {
69
- return Direction.RIGHT;
70
- } else if (rad >= Math.PI / 8 && rad < (3 * Math.PI) / 8) {
71
- return Direction.BOTTOM_RIGHT;
72
- } else if (rad >= (3 * Math.PI) / 8 && rad < (5 * Math.PI) / 8) {
73
- return Direction.BOTTOM;
74
- } else if (rad >= (5 * Math.PI) / 8 && rad < (7 * Math.PI) / 8) {
75
- return Direction.BOTTOM_LEFT;
76
- } else if (
77
- (rad >= (7 * Math.PI) / 8 && rad < Math.PI) ||
78
- (rad >= -Math.PI && rad < (-7 * Math.PI) / 8)
79
- ) {
80
- return Direction.LEFT;
81
- } else if (rad >= (-7 * Math.PI) / 8 && rad < (-5 * Math.PI) / 8) {
82
- return Direction.TOP_LEFT;
83
- } else if (rad >= (-5 * Math.PI) / 8 && rad < (-3 * Math.PI) / 8) {
84
- return Direction.TOP;
85
- } else {
86
- return Direction.TOP_RIGHT;
87
- }
88
- }
89
-
90
- function handleDragStart(event: PIXI.FederatedPointerEvent) {
91
- startPosition = event.getLocalPosition(this);
92
- dragging = true;
93
- innerAlpha.set(1);
94
- settings.onStart?.();
95
- }
96
-
97
- function handleDragEnd() {
98
- if (!dragging) return;
99
- innerPositionX.set(0);
100
- innerPositionY.set(0);
101
- dragging = false;
102
- innerAlpha.set(innerAlphaStandby);
103
- settings.onEnd?.();
104
- }
105
-
106
- function handleDragMove(event: PIXI.FederatedPointerEvent) {
107
- if (dragging == false) {
108
- return;
109
- }
110
-
111
- let newPosition = event.getLocalPosition(this);
112
-
113
- let sideX = newPosition.x - (startPosition?.x ?? 0);
114
- let sideY = newPosition.y - (startPosition?.y ?? 0);
115
-
116
- let centerPoint = new PIXI.Point(0, 0);
117
- let angle = 0;
118
-
119
- if (sideX == 0 && sideY == 0) {
120
- return;
121
- }
122
-
123
- let calRadius = 0;
124
-
125
- if (sideX * sideX + sideY * sideY >= outerRadius * outerRadius) {
126
- calRadius = outerRadius;
127
- } else {
128
- calRadius = outerRadius - innerRadius;
129
- }
130
-
131
- /**
132
- * x: -1 <-> 1
133
- * y: -1 <-> 1
134
- * Y
135
- * ^
136
- * |
137
- * 180 | 90
138
- * ------------> X
139
- * 270 | 360
140
- * |
141
- * |
142
- */
143
-
144
- let direction = Direction.LEFT;
145
-
146
- if (sideX == 0) {
147
- if (sideY > 0) {
148
- centerPoint.set(0, sideY > outerRadius ? outerRadius : sideY);
149
- angle = 270;
150
- direction = Direction.BOTTOM;
151
- } else {
152
- centerPoint.set(
153
- 0,
154
- -(Math.abs(sideY) > outerRadius ? outerRadius : Math.abs(sideY))
155
- );
156
- angle = 90;
157
- direction = Direction.TOP;
158
- }
159
- innerPositionX.set(centerPoint.x);
160
- innerPositionY.set(centerPoint.y);
161
- power = getPower(centerPoint);
162
- settings.onChange?.({ angle, direction, power });
163
- return;
164
- }
165
-
166
- if (sideY == 0) {
167
- if (sideX > 0) {
168
- centerPoint.set(
169
- Math.abs(sideX) > outerRadius ? outerRadius : Math.abs(sideX),
170
- 0
171
- );
172
- angle = 0;
173
- direction = Direction.LEFT;
174
- } else {
175
- centerPoint.set(
176
- -(Math.abs(sideX) > outerRadius ? outerRadius : Math.abs(sideX)),
177
- 0
178
- );
179
- angle = 180;
180
- direction = Direction.RIGHT;
181
- }
182
-
183
- innerPositionX.set(centerPoint.x);
184
- innerPositionY.set(centerPoint.y);
185
- power = getPower(centerPoint);
186
- settings.onChange?.({ angle, direction, power });
187
- return;
188
- }
189
-
190
- let tanVal = Math.abs(sideY / sideX);
191
- let radian = Math.atan(tanVal);
192
- angle = (radian * 180) / Math.PI;
193
-
194
- let centerX = 0;
195
- let centerY = 0;
196
-
197
- if (sideX * sideX + sideY * sideY >= outerRadius * outerRadius) {
198
- centerX = outerRadius * Math.cos(radian);
199
- centerY = outerRadius * Math.sin(radian);
200
- } else {
201
- centerX = Math.abs(sideX) > outerRadius ? outerRadius : Math.abs(sideX);
202
- centerY = Math.abs(sideY) > outerRadius ? outerRadius : Math.abs(sideY);
203
- }
204
-
205
- if (sideY < 0) {
206
- centerY = -Math.abs(centerY);
207
- }
208
- if (sideX < 0) {
209
- centerX = -Math.abs(centerX);
210
- }
211
-
212
- if (sideX > 0 && sideY < 0) {
213
- // < 90
214
- } else if (sideX < 0 && sideY < 0) {
215
- // 90 ~ 180
216
- angle = 180 - angle;
217
- } else if (sideX < 0 && sideY > 0) {
218
- // 180 ~ 270
219
- angle = angle + 180;
220
- } else if (sideX > 0 && sideY > 0) {
221
- // 270 ~ 369
222
- angle = 360 - angle;
223
- }
224
- centerPoint.set(centerX, centerY);
225
- power = getPower(centerPoint);
226
-
227
- direction = getDirection(centerPoint);
228
- innerPositionX.set(centerPoint.x);
229
- innerPositionY.set(centerPoint.y);
230
-
231
- settings.onChange?.({ angle, direction, power });
232
- }
233
-
234
- let innerElement;
235
- let outerElement;
236
-
237
- if (!settings.outer) {
238
- outerElement = h(Graphics, {
239
- draw: (g) => {
240
- g.circle(0, 0, outerRadius).fill(0x000000);
241
- },
242
- alpha: 0.5,
243
- });
244
- } else {
245
- outerElement = h(Sprite, {
246
- image: settings.outer,
247
- anchor: { x: 0.5, y: 0.5 },
248
- scale: settings.outerScale,
249
- });
250
- }
251
-
252
- const innerOptions: any = {
253
- scale: settings.innerScale,
254
- x: innerPositionX,
255
- y: innerPositionY,
256
- alpha: innerAlpha,
257
- };
258
-
259
- if (!settings.inner) {
260
- innerElement = h(Graphics, {
261
- draw: (g) => {
262
- g.circle(0, 0, innerRadius * 2.5).fill(0x000000);
263
- },
264
- ...innerOptions,
265
- });
266
- } else {
267
- innerElement = h(Sprite, {
268
- image: settings.inner,
269
- anchor: { x: 0.5, y: 0.5 },
270
- ...innerOptions,
271
- });
272
- }
273
-
274
- return h(
275
- Container,
276
- {
277
- ...opts,
278
- pointerdown: handleDragStart,
279
- pointerup: handleDragEnd,
280
- pointerupoutside: handleDragEnd,
281
- pointermove: handleDragMove,
282
- },
283
- outerElement,
284
- innerElement
285
- );
286
- }
@@ -1,122 +0,0 @@
1
- import { Container, Graphics } from "../components";
2
- import { h, mount } from "../engine/signal";
3
- import { animatedSignal } from "../engine/animation";
4
- import { RadialGradient } from "../utils/RadialGradient";
5
- import { effect, isSignal, signal } from "@signe/reactive";
6
- import { useProps } from "../hooks/useProps";
7
- import { isObservable } from "rxjs";
8
-
9
- export function LightSpot(opts) {
10
- const { radius } = useProps(opts);
11
- const scale = animatedSignal(1);
12
-
13
- const minScale = 1;
14
- const maxScale = 2; // Reduced max scale for subtler effect
15
- const scintillationSpeed = 0.001; // Significantly reduced for slower scintillation
16
-
17
- const animate = () => {
18
- // Use time-based animation for smoother, slower scintillation
19
- const time = Date.now() * scintillationSpeed;
20
-
21
- // Combine multiple sine waves for a more natural, less predictable effect
22
- const scintillationFactor =
23
- (Math.sin(time) + Math.sin(time * 1.3) + Math.sin(time * 0.7)) / 3;
24
-
25
- // Map the scintillation factor to the scale range
26
- const newScale =
27
- minScale + (maxScale - minScale) * (scintillationFactor * 0.5 + 0.5);
28
-
29
- scale.update(() => newScale);
30
-
31
- requestAnimationFrame(animate);
32
- };
33
-
34
- animate();
35
-
36
- const draw = (g) => {
37
- const size = radius() * 2;
38
- const gradient = new RadialGradient(size, size, 0, size, size, 0);
39
- gradient.addColorStop(0, "rgba(255, 255, 0, 1)");
40
- gradient.addColorStop(0.5, "rgba(255, 255, 0, 0.3)");
41
- gradient.addColorStop(0.8, "rgba(255, 255, 0, 0)");
42
-
43
- const translate = size / 2;
44
-
45
- g.rect(-translate, -translate, size, size).fill(
46
- gradient.render({ translate: { x: translate, y: translate } })
47
- );
48
- };
49
-
50
- return h(Graphics, {
51
- draw,
52
- ...opts,
53
- scale,
54
- });
55
- }
56
-
57
- export function NightAmbiant(props) {
58
- const { children } = props;
59
- let el
60
- const width = signal(0);
61
- const height = signal(0);
62
- let subscription
63
- const draw = (rectAndHole) => {
64
- const margin = 80
65
- rectAndHole.rect(-margin, -margin, width() + margin*2, height() + margin*2);
66
- rectAndHole.fill(0x000000);
67
- const applyChildren = (child) => {
68
- const x = isSignal(child.propObservables.x)
69
- ? child.propObservables.x()
70
- : child.props.x;
71
- const y = isSignal(child.propObservables.y)
72
- ? child.propObservables.y()
73
- : child.props.y;
74
- const radius = isSignal(child.propObservables.radius)
75
- ? child.propObservables.radius()
76
- : child.props.radius;
77
- rectAndHole.circle(x, y, radius);
78
- rectAndHole.cut();
79
- }
80
- for (let child of children) {
81
- if (isObservable(child)) {
82
- if (subscription) {
83
- subscription.unsubscribe()
84
- }
85
- subscription = child.subscribe((event) => {
86
- for (let child of event.fullElements) {
87
- applyChildren(child)
88
- }
89
- })
90
- return
91
- }
92
- applyChildren(child)
93
- }
94
- };
95
-
96
- mount((el) => {
97
- effect(() => {
98
- const { displayWidth, displayHeight } = el.componentInstance
99
- const w = +displayWidth()
100
- const h = +displayHeight()
101
- setTimeout(() => {
102
- width.update(() => w)
103
- height.update(() => h)
104
- }, 0) // hack
105
- });
106
- });
107
-
108
- return h(
109
- Container,
110
- {
111
- width: "100%",
112
- height: "100%",
113
- ...props,
114
- },
115
- h(Graphics, {
116
- draw,
117
- alpha: 0.8,
118
- blur: 80,
119
- }),
120
- ...children
121
- );
122
- }
@@ -1,53 +0,0 @@
1
- import * as PIXI from "pixi.js";
2
- import { FX } from "revolt-fx";
3
- import { h, mount, tick } from "../engine/signal";
4
- import { Container } from "../components";
5
- import { on } from "../engine/trigger";
6
- import { useProps } from "../hooks/useProps";
7
-
8
- export function Particle(options) {
9
- const { emit, settings = {} } = options;
10
- const { name } = useProps(options);
11
- const fx = new FX();
12
- let element;
13
-
14
- PIXI.Assets.add({ alias: "fx_settings", src: "/default-bundle.json" });
15
- PIXI.Assets.add({
16
- alias: "fx_spritesheet",
17
- src: "/revoltfx-spritesheet.json",
18
- });
19
-
20
- tick(({deltaRatio}) => {
21
- fx.update(deltaRatio);
22
- });
23
-
24
- mount(async (_element) => {
25
- element = _element;
26
-
27
- const data = await PIXI.Assets.load(["fx_settings", "fx_spritesheet"]);
28
- let fxSettings = {...data.fx_settings};
29
-
30
- if (settings.emitters) {
31
- const lastId = 10000;
32
- const emittersWithIds = settings.emitters.map((emitter, index) => ({
33
- ...emitter,
34
- id: lastId + index
35
- }));
36
-
37
- fxSettings.emitters = [
38
- ...fxSettings.emitters,
39
- ...emittersWithIds,
40
- ];
41
-
42
- }
43
-
44
- fx.initBundle(fxSettings, true);
45
- });
46
-
47
- on(emit, () => {
48
- const emitter = fx.getParticleEmitter(name());
49
- emitter.init(element.componentInstance);
50
- });
51
-
52
- return h(Container);
53
- }
Binary file
@@ -1,18 +0,0 @@
1
- <Canvas
2
- backgroundColor="#fff"
3
- width="100%"
4
- height="100%"
5
- antialias="true"
6
- >
7
- <Container
8
- width="100%"
9
- height="100%"
10
- justifyContent="center"
11
- alignItems="center">
12
- <HelloWorld text="CanvasEngine" />
13
- </Container>
14
- </Canvas>
15
-
16
- <script>
17
- import HelloWorld from "./hello.ce";
18
- </script>
@@ -1,34 +0,0 @@
1
- <Container flexDirection="column" width="500px">
2
- <Sprite
3
- image="/assets/logo.png"
4
- anchor="0.5"
5
- rotation
6
- scale
7
- @pointerenter={onEnter}
8
- @pointerleave={onLeave}
9
- />
10
- <Text text size="70" fontFamily="Helvetica" x="90" y="-30" />
11
- </Container>
12
-
13
- <script>
14
- import { signal, tick, animatedSignal, Easing } from "canvasengine";
15
-
16
- const { text } = defineProps();
17
- const rotation = signal(0);
18
- const scale = animatedSignal(1, {
19
- duration: 300,
20
- ease: Easing.easeInOut,
21
- });
22
-
23
- tick(() => {
24
- rotation.update(rotation => rotation + 0.01);
25
- });
26
-
27
- const onEnter = () => {
28
- scale.set(1.5);
29
- };
30
-
31
- const onLeave = () => {
32
- scale.set(1);
33
- };
34
- </script>
@@ -1,21 +0,0 @@
1
- <!doctype html>
2
- <html>
3
- <head>
4
- <meta charset="UTF-8" />
5
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6
- <title>Canvas Engine</title>
7
- <style>
8
- body, html, #root {
9
- margin: 0;
10
- padding: 0;
11
- height: 100vh;
12
- width: 100vw;
13
- overflow: hidden;
14
- }
15
- </style>
16
- </head>
17
- <body>
18
- <div id="root"></div>
19
- <script type="module" src="/main.ts"></script>
20
- </body>
21
- </html>
package/starter/main.ts DELETED
@@ -1,4 +0,0 @@
1
- import App from './components/app.ce'
2
- import { bootstrapCanvas } from 'canvasengine';
3
-
4
- await bootstrapCanvas(document.getElementById("root"), App);
@@ -1,16 +0,0 @@
1
- {
2
- "name": "template",
3
- "version": "1.0.0",
4
- "description": "",
5
- "main": "index.js",
6
- "scripts": {
7
- "dev": "vite"
8
- },
9
- "keywords": [],
10
- "author": "",
11
- "license": "ISC",
12
- "type": "module",
13
- "devDependencies": {
14
- "vite": "^6.0.3"
15
- }
16
- }
@@ -1,12 +0,0 @@
1
- import { defineConfig } from 'vite'
2
- import vitePluginCe from 'canvasengine/compiler'
3
-
4
- // https://vitejs.dev/config/
5
- export default defineConfig({
6
- plugins: [vitePluginCe()],
7
- resolve: {
8
- alias: {
9
- path: 'rollup-plugin-node-polyfills/polyfills/path',
10
- }
11
- }
12
- });
package/tsconfig.json DELETED
@@ -1,32 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES2020",
4
- "lib": ["dom", "esnext"],
5
- "module": "ES2020",
6
- "outDir": "./lib",
7
- "rootDir": "src",
8
- "strict": true,
9
- "sourceMap": true,
10
- "strictNullChecks": true,
11
- "strictPropertyInitialization": false,
12
- "moduleResolution": "Bundler",
13
- "esModuleInterop": true,
14
- "removeComments": false,
15
- "noUnusedParameters": false,
16
- "noUnusedLocals": false,
17
- "noImplicitThis": false,
18
- "noImplicitAny": false,
19
- "noImplicitReturns": false,
20
- "declaration": true,
21
- "experimentalDecorators": true,
22
- "emitDecoratorMetadata": false,
23
- "stripInternal": true,
24
- "skipLibCheck": true,
25
- "types": ["node"]
26
- },
27
- "include": [
28
- "src",
29
- "sample"
30
- ],
31
-
32
- }
@@ -1,10 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "composite": true,
4
- "skipLibCheck": true,
5
- "module": "ESNext",
6
- "moduleResolution": "bundler",
7
- "allowSyntheticDefaultImports": true
8
- },
9
- "include": ["vite.config.ts"]
10
- }
package/tsup.config.ts DELETED
@@ -1,28 +0,0 @@
1
- import { defineConfig } from 'tsup'
2
-
3
- export default [
4
- defineConfig({
5
- format: ['esm'],
6
- target: 'es2020',
7
- splitting: true,
8
- clean: true,
9
- shims: false,
10
- dts: true,
11
- sourcemap: true,
12
- entry: ['src/index.ts'],
13
- outDir: 'dist'
14
- }),
15
-
16
- defineConfig({
17
- format: ['esm'],
18
- target: 'node20',
19
- splitting: true,
20
- clean: false,
21
- shims: false,
22
- dts: true,
23
- sourcemap: true,
24
- entry: ['src/compiler/vite.ts'],
25
- outDir: 'dist/compiler',
26
- external: ['vite', 'acorn', 'peggy', 'typescript'],
27
- })
28
- ]
package/vitest.config.ts DELETED
@@ -1,12 +0,0 @@
1
- /// <reference types="vitest" />
2
- import { defineConfig } from 'vite'
3
-
4
- export default defineConfig(async () => {
5
- return {
6
- test: {
7
- environment: 'jsdom',
8
- pool: 'forks',
9
- setupFiles: ['./tests/setup/canvas.ts']
10
- }
11
- }
12
- })