@edadma/logo 0.0.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 +176 -0
- package/dist/logo.js +44428 -0
- package/dist/react/LogoCanvas.js +74 -0
- package/dist/react/index.js +2 -0
- package/dist/types/LogoCanvas.d.ts +67 -0
- package/dist/types/index.d.ts +2 -0
- package/package.json +59 -0
- package/react/LogoCanvas.tsx +174 -0
- package/react/index.ts +2 -0
package/README.md
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
# @edadma/logo
|
|
2
|
+
|
|
3
|
+
A Logo programming language interpreter for JavaScript/TypeScript with React component support.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @edadma/logo
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Basic Usage
|
|
12
|
+
|
|
13
|
+
```javascript
|
|
14
|
+
import { Logo } from '@edadma/logo';
|
|
15
|
+
|
|
16
|
+
const canvas = document.getElementById('myCanvas');
|
|
17
|
+
const logo = new Logo(canvas);
|
|
18
|
+
|
|
19
|
+
// Run a Logo program
|
|
20
|
+
logo.run(`
|
|
21
|
+
repeat 4 [
|
|
22
|
+
forward 100
|
|
23
|
+
right 90
|
|
24
|
+
]
|
|
25
|
+
`);
|
|
26
|
+
|
|
27
|
+
// Execute single commands
|
|
28
|
+
logo.execute('forward 50');
|
|
29
|
+
logo.execute('right 90');
|
|
30
|
+
|
|
31
|
+
// Clear and reset
|
|
32
|
+
logo.clear();
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## React Component
|
|
36
|
+
|
|
37
|
+
```jsx
|
|
38
|
+
import { LogoCanvas } from '@edadma/logo/react';
|
|
39
|
+
import { useRef } from 'react';
|
|
40
|
+
|
|
41
|
+
function App() {
|
|
42
|
+
const logoRef = useRef(null);
|
|
43
|
+
const [code, setCode] = useState('repeat 4 [fd 100 rt 90]');
|
|
44
|
+
|
|
45
|
+
return (
|
|
46
|
+
<div>
|
|
47
|
+
<textarea value={code} onChange={e => setCode(e.target.value)} />
|
|
48
|
+
<button onClick={() => logoRef.current?.run(code)}>Run</button>
|
|
49
|
+
<button onClick={() => logoRef.current?.clear()}>Clear</button>
|
|
50
|
+
|
|
51
|
+
<LogoCanvas
|
|
52
|
+
ref={logoRef}
|
|
53
|
+
width={600}
|
|
54
|
+
height={400}
|
|
55
|
+
pathRendering={true}
|
|
56
|
+
onError={(e) => console.error(e)}
|
|
57
|
+
/>
|
|
58
|
+
</div>
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### LogoCanvas Props
|
|
64
|
+
|
|
65
|
+
| Prop | Type | Default | Description |
|
|
66
|
+
|------|------|---------|-------------|
|
|
67
|
+
| `width` | number | 600 | Canvas width in pixels |
|
|
68
|
+
| `height` | number | 400 | Canvas height in pixels |
|
|
69
|
+
| `program` | string | - | Logo program to run (auto-runs on change) |
|
|
70
|
+
| `pathRendering` | boolean | true | Use smooth path rendering |
|
|
71
|
+
| `onError` | function | - | Error callback |
|
|
72
|
+
| `onComplete` | function | - | Completion callback |
|
|
73
|
+
| `className` | string | - | CSS class for canvas |
|
|
74
|
+
| `style` | object | - | Inline styles for canvas |
|
|
75
|
+
|
|
76
|
+
### LogoCanvas Ref Methods
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
interface LogoCanvasRef {
|
|
80
|
+
execute(command: string): void;
|
|
81
|
+
run(program: string): void;
|
|
82
|
+
clear(): void;
|
|
83
|
+
getDrawing(): LogoDrawing;
|
|
84
|
+
getTurtle(): TurtleState;
|
|
85
|
+
getLogo(): Logo | null;
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## API Reference
|
|
90
|
+
|
|
91
|
+
### Logo Class
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
class Logo {
|
|
95
|
+
constructor(canvas: HTMLCanvasElement);
|
|
96
|
+
|
|
97
|
+
run(program: string): void;
|
|
98
|
+
execute(command: string): void;
|
|
99
|
+
clear(): void;
|
|
100
|
+
render(): void;
|
|
101
|
+
setPathRendering(enabled: boolean): void;
|
|
102
|
+
setAutoRender(enabled: boolean): void;
|
|
103
|
+
getDrawing(): LogoDrawing;
|
|
104
|
+
getTurtle(): TurtleState;
|
|
105
|
+
}
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### Drawing Data
|
|
109
|
+
|
|
110
|
+
If you want to implement custom rendering:
|
|
111
|
+
|
|
112
|
+
```typescript
|
|
113
|
+
interface LogoDrawing {
|
|
114
|
+
lines: Array<{
|
|
115
|
+
x1: number; y1: number;
|
|
116
|
+
x2: number; y2: number;
|
|
117
|
+
color: string; // e.g., "rgb(0,0,0)"
|
|
118
|
+
width: number;
|
|
119
|
+
}>;
|
|
120
|
+
labels: Array<{
|
|
121
|
+
x: number; y: number;
|
|
122
|
+
heading: number; // radians
|
|
123
|
+
text: string;
|
|
124
|
+
}>;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
interface TurtleState {
|
|
128
|
+
x: number;
|
|
129
|
+
y: number;
|
|
130
|
+
heading: number; // radians, π/2 = north
|
|
131
|
+
visible: boolean;
|
|
132
|
+
}
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
## Supported Logo Commands
|
|
136
|
+
|
|
137
|
+
### Movement
|
|
138
|
+
- `forward <dist>` / `fd` - Move forward
|
|
139
|
+
- `back <dist>` / `bk` - Move backward
|
|
140
|
+
- `right <angle>` / `rt` - Turn right (degrees)
|
|
141
|
+
- `left <angle>` / `lt` - Turn left (degrees)
|
|
142
|
+
- `setxy <x> <y>` - Move to position
|
|
143
|
+
- `home` - Return to center
|
|
144
|
+
|
|
145
|
+
### Pen Control
|
|
146
|
+
- `penup` / `pu` - Lift pen
|
|
147
|
+
- `pendown` / `pd` - Lower pen
|
|
148
|
+
- `setpensize <size>` - Set line width
|
|
149
|
+
- `setcolor <color>` - Set pen color (name, number, or [r g b])
|
|
150
|
+
|
|
151
|
+
### Turtle
|
|
152
|
+
- `hideturtle` / `ht` - Hide turtle
|
|
153
|
+
- `showturtle` / `st` - Show turtle
|
|
154
|
+
|
|
155
|
+
### Control Flow
|
|
156
|
+
- `repeat <n> [commands]` - Repeat commands
|
|
157
|
+
- `if <cond> [commands]` - Conditional
|
|
158
|
+
- `ifelse <cond> [yes] [no]` - If-else
|
|
159
|
+
|
|
160
|
+
### Procedures
|
|
161
|
+
- `to <name> <:params> ... end` - Define procedure
|
|
162
|
+
- `output <value>` / `op` - Return value
|
|
163
|
+
- `stop` - Stop procedure
|
|
164
|
+
|
|
165
|
+
### Math
|
|
166
|
+
- `sum`, `difference`, `product`, `quotient`
|
|
167
|
+
- `sin`, `cos`, `tan`, `sqrt`, `pow`
|
|
168
|
+
- Infix operators: `+`, `-`, `*`, `/`, `^`, `=`, `<`, `>`
|
|
169
|
+
|
|
170
|
+
### Variables
|
|
171
|
+
- `make "name <value>` - Set variable
|
|
172
|
+
- `:name` - Get variable value
|
|
173
|
+
|
|
174
|
+
## License
|
|
175
|
+
|
|
176
|
+
ISC
|