Skip to content

Commit

Permalink
added jump, save cue
Browse files Browse the repository at this point in the history
  • Loading branch information
leomcelroy committed Oct 20, 2023
1 parent 96b06cb commit 9d4e114
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 16 deletions.
4 changes: 2 additions & 2 deletions astro/src/components/Toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import GitHubIcon from '../ui/GitHubIcon.tsx'
import { saveFile } from "../lib/saveFile.ts";

export default function Toolbar() {
const { connected } = getStore()
const { connected, needsSaving } = getStore()

const [hidden, setHidden] = useState(true)

Expand All @@ -39,7 +39,7 @@ export default function Toolbar() {
<div
class="relative cursor-pointer w-max h-full flex items-center p-1 hover:bg-white hover:bg-opacity-10"
onClick={() => saveFile(getCode())}>
save (ctrl/cmd+s)
{ needsSaving ? "save* (ctrl/cmd+s)" : "save (ctrl/cmd+s)" }
</div>
<NewButton />
<OpenButton />
Expand Down
29 changes: 20 additions & 9 deletions astro/src/lib/drawingToolkit/turtle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ export class Turtle {
return this
}

jump(pt: Point) {
this.up();
this.goTo(pt);
this.down();
return this;
}

forward(distance: number) {
const last = this.position
const a = (this.angle / 180) * Math.PI
Expand Down Expand Up @@ -217,9 +224,9 @@ export class Turtle {
return getAngle(this.path, t)
}

getNormal(t: number) {
return getNormal(this.path, t)
}
// getNormal(t: number) {
// return getNormal(this.path, t)
// }

trim(t0: number, t1: number) {
trimPolylines(this.path, t0, t1)
Expand Down Expand Up @@ -466,15 +473,19 @@ function extrema(pts: Point[]) {
function tValuesForPoints(polylines: Polyline[]) {
let totalLength = 0
let lengths = []
let tValues = [0]
let tValues = []

let segmentLength = 0;
for (let i = 0; i < polylines.length; i++) {
let polyline = polylines[i]
for (let j = 1; j < polyline.length; j++) {
let dx = polyline[j][0] - polyline[j - 1][0]
let dy = polyline[j][1] - polyline[j - 1][1]
let segmentLength = Math.sqrt(dx * dx + dy * dy)
totalLength += segmentLength
for (let j = 0; j < polyline.length; j++) {
if (j > 0) {
let dx = polyline[j][0] - polyline[j - 1][0]
let dy = polyline[j][1] - polyline[j - 1][1]
segmentLength = Math.sqrt(dx * dx + dy * dy)
totalLength += segmentLength
}

lengths.push(segmentLength)
}
}
Expand Down
6 changes: 6 additions & 0 deletions astro/src/lib/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { addMachineControl } from './events/addMachineControl.js'
import { addLoadBackup } from './events/addLoadBackup.js'
import { addSrcURLParam } from './events/addSrcURLParam.js'
import { saveFile } from "./saveFile.ts";
import { useOnEditorChange } from "./events.ts";


export function init() {
console.log('init')
Expand Down Expand Up @@ -39,6 +41,10 @@ export function init() {
}
})

useOnEditorChange(() => {
patchStore({ needsSaving: true });
})

// get settings from localStorage

const theme = localStorage.getItem('colorTheme') ?? 'light'
Expand Down
4 changes: 2 additions & 2 deletions astro/src/lib/saveFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ export async function saveFile(content, { filename, fileHandle } = {} ) {
suggestedName: filename ?? "anon",
});

patchStore({ fileHandle });

// Create a FileSystemWritableFileStream to write to.
const writableStream = await fileHandle.createWritable();

Expand All @@ -35,6 +33,8 @@ export async function saveFile(content, { filename, fileHandle } = {} ) {

// Here, you can only print the file name, not the full path
console.log(`File saved: ${fileHandle.name}`);

patchStore({ fileHandle, needsSaving: false });
} catch (err) {
console.error("File save failed", err);
}
Expand Down
6 changes: 4 additions & 2 deletions astro/src/lib/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ export type GlobalState = {
view: EditorView | null
theme: 'light' | 'dark'
vimMode: boolean,
fileHandle: any
fileHandle: any,
needsSaving: boolean
}

// setting/initializing state
Expand All @@ -58,7 +59,8 @@ const newState: Omit<GlobalState, 'code'> = {
view: null,
theme: 'light',
vimMode: false,
fileHandle: null
fileHandle: null,
needsSaving: false
}

export const makeNewState = (): GlobalState => {
Expand Down
23 changes: 22 additions & 1 deletion docs/TOOLKIT.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ t.goTo([ x: number, y: number ])
t.forward(distance: number)
t.arc(angle: number, radius: number)

// to move to pt without drawing and end up in drawing position
t.jump([ x: number, y: number ])

// change angle
t.setAngle(theta: number)
t.right(theta: number)
Expand Down Expand Up @@ -66,14 +69,32 @@ t.height
t.join(anotherTurtle)

// to apply a function to all pts in a turtle
// this will replace the old point values with the new ones
// fn takes (pt, tValue) => { ... }
// return [ x, y ] to replace the old point value with the new one
// return "BREAK" to split path at that point
// return "REMOVE" to filter out that point
t.iteratePath(fn)

t.resample(resolution)

// takes value 0 - 1 and returns point that far along paths
t.interpolate(tValue)

// takes value 0 - 1 and returns angle that far along paths
t.getAngle(tValue)
```

<!--
displace
warp
bezierEasing
trim
merge
getNormal
extrema
copy
-->

To render a turtle use `drawTurtles`, it takes a list of turtles to draw.

```js
Expand Down

1 comment on commit 9d4e114

@vercel
Copy link

@vercel vercel bot commented on 9d4e114 Oct 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

blot – ./astro

blot.hackclub.dev
blot-git-main.hackclub.dev

Please sign in to comment.