Skip to content

Instantly share code, notes, and snippets.

View steveruizok's full-sized avatar
🏠

Steve Ruiz steveruizok

🏠
View GitHub Profile
@steveruizok
steveruizok / multiclick.test.ts
Created January 27, 2022 15:12
Tests for multi-clicks.
import { DOUBLE_CLICK } from '~constants'
import { TLTestApp } from './TLTestApp'
jest.useFakeTimers()
describe('When detecting double/triple/quadruple clicks...', () => {
it('Detects a click', () => {
const app = new TLTestApp()
app.onClick = jest.fn()
app.onDoubleClick = jest.fn()
@steveruizok
steveruizok / useCursor.ts
Last active July 15, 2025 04:33
Generate a rotated cursor based on a cursor and rotation.
import { GeomUtils, TLCursor } from '@tldraw/core'
import * as React from 'react'
function getCursorCss(svg: string, r: number, f = false) {
return (
`url("data:image/svg+xml,<svg height='32' width='32' viewBox='0 0 35 35' xmlns='http://www.w3.org/2000/svg'><g fill='none' style='transform-origin:center center' transform='rotate(${r})${
f ? ` scale(-1,-1) translate(0, -32)` : ''
}'>` +
svg.replaceAll(`"`, `'`) +
'</g></svg>") 16 16, pointer'
@steveruizok
steveruizok / getLineLineIntersection.ts
Last active December 5, 2021 12:35
Get the intersection point, if any, between two infinite lines.
export function intersectLineLine(AB: number[][], PQ: number[][]): number[] | undefined {
const slopeAB = AB[0][0] === AB[1][0] ? NaN : (AB[0][1] - AB[1][1]) / (AB[0][0] - AB[1][0])
const slopePQ = AB[0][0] === PQ[1][0] ? NaN : (PQ[0][1] - PQ[1][1]) / (PQ[0][0] - PQ[1][0])
if (slopeAB === slopePQ) return undefined
if (Number.isNaN(slopeAB) && !Number.isNaN(slopePQ)) {
return [AB[0][0], (AB[0][0] - PQ[0][0]) * slopePQ + PQ[0][1]]
}
@steveruizok
steveruizok / sponsors.ts
Created November 19, 2021 13:38
A Next.js API route that will generate an image of your most recent 100 Github sponsors.
// pages/api/sponsors.ts
import { NextApiRequest, NextApiResponse } from 'next'
const AV_SIZE = 32
const PADDING = 4
const COLS = 16
type SponsorResult = { avatarUrl: string; login: string }
@steveruizok
steveruizok / pusher-auth.ts
Created October 26, 2021 13:53
Pusher auth for a Next.js app.
// pages/pusher/pusher-auth.ts
import { pusher } from "backend/pusher"
import { NextApiHandler, NextApiRequest } from "next"
interface PushAuthRequest extends NextApiRequest {
body: {
socket_id: string
channel_name: string
}
@steveruizok
steveruizok / findSnapPoints.ts
Last active June 8, 2022 01:59
A second iteration on finding snap points. Includes lines for rendering snap points.
interface BBox {
minX: number
midX: number
maxX: number
minY: number
midY: number
maxY: number
width: number
height: number
}
@steveruizok
steveruizok / findSnapPoints.ts
Last active January 23, 2025 01:54
Find the snap points between a bounding box and several other bounding boxes.
interface TLBoundsWithCenter {
minX: number
midX: number
maxX: number
minY: number
midY: number
maxY: number
width: number
height: number
}
@steveruizok
steveruizok / usePublishCallback.ts
Created September 23, 2021 16:34
Croquet.io React Bindings (requires @croquet/react and @croquet/croquet)
import * as React from "react"
import { Model } from "@croquet/croquet"
import { CroquetContext } from "@croquet/react"
/**
* A callback that publishes the returned data to the current view's model.
* @param eventName The name of the event to be published.
* @param fn A function that returns the data to be published.
* @param deps (optional) An array of dependencies for the callback.
*/
@steveruizok
steveruizok / perfect.js
Created September 21, 2021 11:56
Perfect freehand in Baku's pen tool.
/*
{
"id": "bga2899d28",
"label": "Perfect",
"icon": "✍︎",
"parameters": [
{
"name": "size",
"type": "float",
"default": "16"
@steveruizok
steveruizok / easings.ts
Created September 17, 2021 10:13
Easing functions in TypeScript.
const EASINGS: Record<string, (t: number) => number> = {
linear: (t) => t,
easeInQuad: (t) => t * t,
easeOutQuad: (t) => t * (2 - t),
easeInOutQuad: (t) => (t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t),
easeInCubic: (t) => t * t * t,
easeOutCubic: (t) => --t * t * t + 1,
easeInOutCubic: (t) =>
t < 0.5 ? 4 * t * t * t : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1,