tscratch 0.3.0 → 0.3.1
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 +85 -24
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -7,11 +7,11 @@ Type-safe, lightweight, and fun — bring the simplicity of Scratch into real co
|
|
|
7
7
|
|
|
8
8
|
## ✨ Features
|
|
9
9
|
|
|
10
|
-
-
|
|
11
|
-
-
|
|
12
|
-
-
|
|
13
|
-
-
|
|
14
|
-
-
|
|
10
|
+
- **Scratch-style API** — `goTo`, `setX`, `move`, etc.
|
|
11
|
+
- **TypeScript first** — full type safety & IntelliSense.
|
|
12
|
+
- **Canvas rendering** — built on the HTML5 canvas.
|
|
13
|
+
- **Sprites system** — extend `Sprite` to create your own shapes.
|
|
14
|
+
- **Lightweight** — no heavy dependencies, works in any TypeScript project.
|
|
15
15
|
|
|
16
16
|
---
|
|
17
17
|
|
|
@@ -30,49 +30,88 @@ import { Engine, Rectangle } from 'tscratch';
|
|
|
30
30
|
const engine = Engine.init();
|
|
31
31
|
|
|
32
32
|
// Create a rectangle sprite
|
|
33
|
-
const rect = new Rectangle();
|
|
34
|
-
|
|
33
|
+
const rect = new Rectangle({ color: 'red' });
|
|
35
34
|
// Move it to the center
|
|
36
|
-
rect.goTo(
|
|
35
|
+
rect.goTo(100, 50);
|
|
37
36
|
|
|
38
37
|
// Animate in the game loop
|
|
39
|
-
engine.
|
|
40
|
-
rect.
|
|
41
|
-
|
|
38
|
+
engine.setLoop('main', () => {
|
|
39
|
+
rect.move(1);
|
|
40
|
+
rect.turn(-2);
|
|
41
|
+
});
|
|
42
42
|
```
|
|
43
43
|
## 🎨 Example: Multiple Sprites
|
|
44
44
|
|
|
45
45
|
```ts
|
|
46
46
|
import { Engine, Rectangle } from 'tscratch';
|
|
47
47
|
|
|
48
|
+
// Setup
|
|
48
49
|
const engine = Engine.init();
|
|
49
50
|
|
|
50
|
-
const redBox = new Rectangle();
|
|
51
|
+
const redBox = new Rectangle({ scene: 'primary' });
|
|
51
52
|
|
|
52
53
|
redBox.setColor('red');
|
|
53
54
|
redBox.goTo(-100, 0);
|
|
54
55
|
|
|
55
|
-
const blueBox = new Rectangle();
|
|
56
|
+
const blueBox = new Rectangle({ scene: 'secondary' });
|
|
56
57
|
|
|
57
58
|
blueBox.setColor('blue');
|
|
58
59
|
blueBox.goTo(100, 0);
|
|
59
60
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
61
|
+
// Scenes & loops
|
|
62
|
+
engine.changeScene('primary');
|
|
63
|
+
|
|
64
|
+
engine.setLoop('primary', () => redBox.changeX(1));
|
|
65
|
+
engine.setLoop('secondary', () => blueBox.changeX(-1));
|
|
64
66
|
```
|
|
65
67
|
|
|
68
|
+
The recommended way of managing multiple scenes is to seperate your code into
|
|
69
|
+
1 file per scene, export the loop function and set the loops inside index.ts
|
|
70
|
+
(your entry file).
|
|
71
|
+
|
|
72
|
+
```ts
|
|
73
|
+
// scenes/main.ts
|
|
74
|
+
|
|
75
|
+
const rect = new Rectangle();
|
|
76
|
+
|
|
77
|
+
// Export the loop
|
|
78
|
+
export default () => rect.move(1);
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
```ts
|
|
82
|
+
// index.ts
|
|
83
|
+
|
|
84
|
+
import { Engine } from 'tscratch';
|
|
85
|
+
import main from './scenes/main.ts';
|
|
86
|
+
|
|
87
|
+
const engine = Engine.init();
|
|
88
|
+
|
|
89
|
+
// No need for changeScene() here, because it's 'main'
|
|
90
|
+
engine.setLoop('main', main);
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## Scenes
|
|
94
|
+
|
|
95
|
+
TScratch supports scenes with the `scene` property. In every sprite you create,
|
|
96
|
+
you can specify, in which scene you want it to render. TScratch will default it
|
|
97
|
+
to 'main' if not specified.
|
|
98
|
+
|
|
99
|
+
After that, you can switch between scenes by using `engine.changeScene(scene)`.
|
|
100
|
+
TScratch will then render only the sprites, that belong in that specific scene.
|
|
101
|
+
|
|
102
|
+
You can also specify 1 loop per scene using `engine.setLoop(scene, callback)`.
|
|
103
|
+
Keep in mind that there is only 1 loop running at a time, which is the one
|
|
104
|
+
in the current scene.
|
|
105
|
+
|
|
66
106
|
## 🛠️ API Overview
|
|
67
107
|
|
|
68
108
|
### Engine
|
|
69
109
|
|
|
70
110
|
- `Engine.init()` → get the singleton instance
|
|
71
|
-
- `engine.removeSprites(...sprites)` → removes sprites from the stage
|
|
72
111
|
- `engine.setMaxFramesPerSecond(FPS)`→ sets the maximum FPS
|
|
73
|
-
- `engine.
|
|
74
|
-
- `engine.
|
|
75
|
-
- `await engine.wait(ms)` → Wait some time (
|
|
112
|
+
- `engine.setLoop(scene, callback)` → game loop logic
|
|
113
|
+
- `engine.changeScene(scene)` → changes the scene, renders only the targeted sprites
|
|
114
|
+
- `await engine.wait(ms)` → Wait some time in milliseconds (experimental)
|
|
76
115
|
- `engine.toRadians(rad)` → converts degrees to radians
|
|
77
116
|
- `engine.toDegrees(deg)` → converts radians to degrees
|
|
78
117
|
|
|
@@ -84,16 +123,38 @@ engine.loop = () => {
|
|
|
84
123
|
- `turn(deg)` / `point(deg)` → change / set direction
|
|
85
124
|
- `changeX(dX)` / `changeY(dY)` → relative movement
|
|
86
125
|
|
|
87
|
-
### Rectangle
|
|
126
|
+
### Rectangle
|
|
127
|
+
|
|
128
|
+
- Has `width`, `height`, and `color` properties.
|
|
129
|
+
- Draws a rectangle centered on `(x, y)`.
|
|
130
|
+
|
|
131
|
+
### RegularPolygon
|
|
132
|
+
|
|
133
|
+
- Has `radius`, `sides`, and `color` properties.
|
|
134
|
+
- Draws a polygon centered on `(x, y)`.
|
|
135
|
+
|
|
136
|
+
### Oval
|
|
137
|
+
|
|
138
|
+
- Has `radA`, `radB`, and `color` properties.
|
|
139
|
+
- Draws an oval centered on `(x, y)`.
|
|
140
|
+
|
|
141
|
+
### Text
|
|
142
|
+
|
|
143
|
+
- Has `content`, `color`, and other properties specifying the style.
|
|
144
|
+
- Draws a label aligned to your preference.
|
|
145
|
+
|
|
146
|
+
### ImageSprite
|
|
88
147
|
|
|
89
|
-
- Has `
|
|
90
|
-
- Draws
|
|
148
|
+
- Has `src`, `width`, and `height` properties.
|
|
149
|
+
- Draws an image centered on `(x, y)`.
|
|
91
150
|
|
|
92
151
|
### Pen
|
|
93
152
|
|
|
153
|
+
- Has `color`, `size`, and `drawing` properties.
|
|
94
154
|
- `down()` → starts drawing
|
|
95
155
|
- `up()` → stops drawing
|
|
96
156
|
- `dot()` → draws a single dot
|
|
157
|
+
- Movement methods, such as `move()`, can draw a line based on if `drawing` is true.
|
|
97
158
|
|
|
98
159
|
### Canvas
|
|
99
160
|
|