Skip to content

Commit

Permalink
Add gridlines and lerp
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaitanLyss committed Jul 15, 2024
1 parent a2062ed commit 0663d03
Show file tree
Hide file tree
Showing 14 changed files with 158 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"trailingComma": "none",
"printWidth": 100,
"plugins": ["prettier-plugin-svelte"],
"overrides": [{ "files": "*.svelte", "options": { "parser": "svelte" } }]
"overrides": [{ "files": "*.svelte", "options": { "parser": "svelte" } }],
"bracketSameLine": true
}
Binary file modified bun.lockb
Binary file not shown.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@selenite/commons",
"version": "0.1.4",
"version": "0.2.1",
"scripts": {
"dev": "vite dev",
"build": "vite build && npm run package",
Expand Down Expand Up @@ -36,16 +36,19 @@
"@sveltejs/vite-plugin-svelte": "^3.0.0",
"@types/eslint": "^8.56.7",
"@types/lodash-es": "^4.17.12",
"autoprefixer": "^10.4.19",
"eslint": "^9.0.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-svelte": "^2.36.0",
"globals": "^15.0.0",
"lodash-es": "^4.17.21",
"postcss": "^8.4.39",
"prettier": "^3.1.1",
"prettier-plugin-svelte": "^3.1.2",
"publint": "^0.1.9",
"svelte": "^5.0.0-next.1",
"svelte-check": "^3.6.0",
"tailwindcss": "^3.4.4",
"tslib": "^2.4.1",
"typescript": "^5.0.0",
"typescript-eslint": "^8.0.0-alpha.20",
Expand Down
6 changes: 6 additions & 0 deletions postcss.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
}
3 changes: 3 additions & 0 deletions src/app.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@tailwind base;
@tailwind components;
@tailwind utilities;
4 changes: 2 additions & 2 deletions src/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1" />
%sveltekit.head%
</head>
<body data-sveltekit-preload-data="hover">
<div>%sveltekit.body%</div>
<body data-sveltekit-preload-data="hover" style="color: white; background-color: hsl(0, 0%, 10%); margin: 0;">
<div style="height: 100vh;width: 100vw;">%sveltekit.body%</div>
</body>
</html>
64 changes: 64 additions & 0 deletions src/lib/actions/canvas.ts
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 });
}
};
};
1 change: 1 addition & 0 deletions src/lib/actions/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Action } from 'svelte/action';
export * from './focusTrap.js'
export * from './shortcut.js'
export * from './canvas.js'
let handleFocusLeaveRefCount = 0;
let handleFocusLeaveCallbacks: ((isKeyboard: boolean) => void)[] = [];
function handleKeydown(e: KeyboardEvent) {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
export * from './string.js';
export * from './eventListeners.js';
export * from './array/index.js'

export * from './math.js'
export { v4 as uuidv4 } from 'uuid';
import {v4 as uuid} from 'uuid'
let idCount = 0;
Expand Down
3 changes: 3 additions & 0 deletions src/lib/utils/math.ts
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;
}
18 changes: 18 additions & 0 deletions src/routes/+layout.svelte
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>
43 changes: 43 additions & 0 deletions src/routes/canvas/+page.svelte
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>
9 changes: 9 additions & 0 deletions tailwind.config.ts
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: []
};

4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"skipLibCheck": true,
"sourceMap": true,
"strict": true,
"module": "NodeNext",
"moduleResolution": "NodeNext"
"module": "ES2022",
"moduleResolution": "Bundler"
}
}

0 comments on commit 0663d03

Please sign in to comment.