Skip to content

Instantly share code, notes, and snippets.

View steveruizok's full-sized avatar
🏠

Steve Ruiz steveruizok

🏠
View GitHub Profile
import merge from 'deepmerge'
import * as idb from 'idb-keyval'
import createReact, { UseStore } from 'zustand'
import createVanilla, { StoreApi } from 'zustand/vanilla'
// Types
export type Patch<T> = Partial<{ [P in keyof T]: Patch<T[P]> }>
export type Command<T> = {
@steveruizok
steveruizok / getRelativeTransformedBoundingBox.ts
Last active July 29, 2021 07:14
getTransformedBoundingBox
/**
* Get the relative bounds (usually a child) within a transformed bounding box.
*/
function getRelativeTransformedBoundingBox(
bounds: TLBounds,
initialBounds: TLBounds,
initialShapeBounds: TLBounds,
isFlippedX: boolean,
isFlippedY: boolean,
): TLBounds {
@steveruizok
steveruizok / [...nextauth].ts
Created July 8, 2021 13:20
Sponsorware with next-auth.
// pages/api/auth/[...nextAuth].ts
import { NextApiHandler, NextApiRequest, NextApiResponse } from 'next'
import NextAuth from 'next-auth'
import Providers from 'next-auth/providers'
export default function Auth(
req: NextApiRequest,
res: NextApiResponse
): ReturnType<NextApiHandler> {
@steveruizok
steveruizok / Vec.ts
Created June 28, 2021 07:01
A big collection of vector utilities.
// A big collection of vector utilities. Collected into a class to improve logging / packaging.
/* ----------------- Start Copy Here ---------------- */
export default class Vec {
/**
* Clamp a value into a range.
* @param n
* @param min
*/
@steveruizok
steveruizok / CodeMirror.tsx
Created June 28, 2021 05:49 — forked from alinnert/CodeMirror.tsx
CodeMirror 6 React Component
import { FC } from 'react'
import { useCodeMirror } from './useCodeMirror'
interface Props {
content: string
classNames?: string
onContentChange: (content: string) => void
}
export const CodeMirror: FC<Props> = ({
@steveruizok
steveruizok / cacheflowelike.ts
Created June 27, 2021 21:05
A beautiful shape for tldraw.
// Run this code on tldraw.com
// By @steveruizok
// Based on an awesome image by @cacheflowe: https://twitter.com/cacheflowe/status/1408902719130288130
new NumberControl({
label: 'radius',
value: 200,
min: 50,
max: 500,
})
@steveruizok
steveruizok / fillShapeWithLines.ts
Last active June 24, 2021 21:35
Fill a tldraw shape with lines.
function fillRectangleWithLines(
shape: Rectangle,
gap: number,
messiness = 0.5
) {
const { size } = shape
const rng = Utils.rng(shape.shape.id)
const gapX = size[0] / (Math.floor((size[0] - gap) / gap) / 2)
@steveruizok
steveruizok / getRaysIntersection.ts
Last active October 10, 2021 11:46
Get the intersection of two rays.
/**
* Get the intersection of two rays, with origin points p0 and p1, and direction vectors n0 and n1.
* @param p0 The origin point of the first ray
* @param n0 The direction vector of the first ray
* @param p1 The origin point of the second ray
* @param n1 The direction vector of the second ray
* @returns
*/
export function getRaysIntersection(
p0: number[],
@steveruizok
steveruizok / getClosestPointOnSVGPath.ts
Created June 24, 2021 09:15
Find the closest point on a SVG path to an off-path point.
/**
* Find the closest point on a SVG path to an off-path point.
* @param pathNode
* @param point
* @returns
*/
export function getClosestPointOnSVGPath(
pathNode: SVGPathElement,
point: number[]
): {
@steveruizok
steveruizok / copyStringToClipboard.ts
Created June 24, 2021 08:53
Copy string to clipboard.
/**
* Copy a string to the clipboard.
* @param string
*/
export function copyToClipboard(string: string): boolean {
let textarea: HTMLTextAreaElement
let result: boolean
try {