-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a2062ed
commit 0663d03
Showing
14 changed files
with
158 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export default { | ||
plugins: { | ||
tailwindcss: {}, | ||
autoprefixer: {}, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
@tailwind base; | ||
@tailwind components; | ||
@tailwind utilities; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { lerp } from '$lib/utils'; | ||
import type { Action } from 'svelte/action'; | ||
|
||
export type Transform = { x: number; y: number; k: number }; | ||
|
||
function drawLines({ | ||
canvas, | ||
transform, | ||
spacing: baseSpacing = 80 | ||
}: { | ||
canvas: HTMLCanvasElement; | ||
|
||
transform: Transform; | ||
spacing?: number; | ||
}) { | ||
if (transform.k < 0.01) return; | ||
const ctx = canvas.getContext('2d'); | ||
if (!ctx) { | ||
throw new Error('Canvas is not a 2d canvas.'); | ||
} | ||
ctx.save(); | ||
ctx.beginPath(); | ||
const scalingFactor = Math.floor(1 + -Math.log(transform.k) / Math.log(3)); | ||
const spacing = scalingFactor > 0 ? baseSpacing * scalingFactor : baseSpacing; | ||
console.log('spacing\t', spacing); | ||
const step = spacing * transform.k; | ||
console.log('zoom\t', transform.k); | ||
// Draw vertical lines | ||
for (let x = (transform.x % step) - step; x <= canvas.width; x += step) { | ||
ctx.moveTo(x, 0); | ||
ctx.lineTo(x, canvas.height); | ||
} | ||
|
||
// Draw horizontal lines | ||
for (let y = (transform.y % step) - step; y <= canvas.height; y += step) { | ||
ctx.moveTo(0, y); | ||
ctx.lineTo(canvas.width, y); | ||
} | ||
// Stroke the lines | ||
ctx.strokeStyle = '#ddd'; | ||
ctx.lineWidth = transform.k > 0.1 ? 0.08 : lerp(0.08, 0, (transform.k - 0.01) / 0.09); | ||
ctx.stroke(); | ||
ctx.restore(); | ||
} | ||
|
||
export const gridLines: Action<HTMLCanvasElement, { transform: Transform; spacing?: number }> = ( | ||
canvas, | ||
params | ||
) => { | ||
const ctx = canvas.getContext('2d'); | ||
if (!ctx) { | ||
throw new Error('Canvas is not a 2d canvas.'); | ||
} | ||
|
||
drawLines({ ...params, canvas }); | ||
|
||
return { | ||
destroy() {}, | ||
update(params) { | ||
ctx.clearRect(0, 0, canvas.width, canvas.height); | ||
drawLines({ canvas, ...params }); | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function lerp(start: number, end: number, alpha: number) { | ||
return start + (end - start) * alpha; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<script lang="ts"> | ||
import '../app.css' | ||
</script> | ||
<main> | ||
<slot/> | ||
</main> | ||
|
||
<style> | ||
main { | ||
height: 100%; | ||
width: 100%; | ||
display: grid; | ||
gap: 1rem; | ||
justify-items: center; | ||
align-items: center; | ||
align-content: center; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<script> | ||
import { gridLines } from '$lib/actions/canvas'; | ||
let x = $state(0); | ||
let y = $state(0); | ||
</script> | ||
|
||
<label> | ||
<span>x</span> | ||
<input type="range" bind:value={x} min="-100" max="100" /> | ||
<span class="number">{x}</span> | ||
</label> | ||
<label> | ||
<span>y</span> | ||
<input type="range" bind:value={y} min="-100" max="100" /> | ||
<span class="number">{y}</span> | ||
</label> | ||
<div class="container"> | ||
<canvas use:gridLines style="transform: translate({x}px, {y}px);"> </canvas> | ||
</div> | ||
|
||
<style> | ||
.number { | ||
width: 2rem; | ||
text-align: end; | ||
} | ||
label { | ||
display: flex; | ||
align-items: center; | ||
gap: 1rem; | ||
} | ||
.container { | ||
width: 20rem; | ||
height: 20rem; | ||
overflow: hidden; | ||
} | ||
canvas { | ||
/* width: 100%; | ||
height: 100%; */ | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** @type {import('tailwindcss').Config} */ | ||
export default { | ||
content: ['./src/**/*.{html,js,ts,svelte}'], | ||
theme: { | ||
extend: {} | ||
}, | ||
plugins: [] | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters