Skip to content

Instantly share code, notes, and snippets.

View steveruizok's full-sized avatar
🏠

Steve Ruiz steveruizok

🏠
View GitHub Profile
@steveruizok
steveruizok / usePreloadAssets.ts
Created April 16, 2024 08:13
tldraw preload fonts
import { useEffect, useState } from 'react'
export type TLTypeFace = {
url: string
display?: any // FontDisplay
featureSettings?: string
stretch?: string
style?: string
unicodeRange?: string
variant?: string
/**
* Get a point along an arc.
*
* @param center - The arc's center.
* @param radius - The arc's radius.
* @param startAngle - The start point of the arc.
* @param size - The size of the arc.
* @param t - The point along the arc to get.
*/
@steveruizok
steveruizok / getTextBoundingBox.ts
Created November 12, 2023 10:02
get the bounding box of a text shape?
function getTextBoundingBox(text: SVGTextElement) {
const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg')
svg.appendChild(text)
document.body.appendChild(svg)
const bbox = text.getBoundingClientRect()
document.body.removeChild(svg)
return bbox
}
@steveruizok
steveruizok / isGifAnimated.ts
Created August 26, 2023 08:19
is gif animated
// =========================
// Modified code originally from <https://github.com/qzb/is-animated>
//
// # [MIT License](https://spdx.org/licenses/MIT)
//
// Copyright (c) 2016 Józef Sokołowski <j.k.sokolowski@gmail.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
@steveruizok
steveruizok / App.tsx
Created August 17, 2023 17:24
history scrubbing in tldraw
import '@tldraw/tldraw/tldraw.css'
import { RecordsDiff, TLRecord, Tldraw, useEditor } from '@tldraw/tldraw'
import { useEffect, useRef } from 'react'
export default function App() {
return (
<div className="tldraw__editor">
<Tldraw autoFocus>
<Slider />
</Tldraw>
@steveruizok
steveruizok / isSerializable.ts
Created July 16, 2023 11:00
is serializable
const serializableTypes = new Set(['string', 'number', 'boolean', 'undefined'])
/**
* Get whether a value is serializable.
*
* @example
*
* ```ts
* const A = isSerializable(1) // true
@steveruizok
steveruizok / tldraw-tiptap.ts
Created June 19, 2023 14:26
tldraw x tiptap
import { Node, mergeAttributes } from "@tiptap/core";
import { NodeViewWrapper, ReactNodeViewRenderer } from "@tiptap/react";
import { Tldraw } from "@tldraw/tldraw"; // use @tldraw/tldraw@canary
import "@tldraw/tldraw/tldraw.css";
function Component() {
return (
<NodeViewWrapper className="react-component">
<div style={{ width: "100%", height: 500 }}>
<Tldraw />
@steveruizok
steveruizok / writeV1ContentsToIdb.ts
Created June 2, 2023 08:42
Write tldraw v1 contents into the database.
export async function writeV1ContentsToIdb() {
// openDB is a wrapper around indexedDB.open that adds a version migration hook, but we've had to drop it as a dependency. Here it is quicky re-implemented:
function openDB(name: string, version: number) {
const request = indexedDB.open(name, version)
return new Promise<IDBDatabase>((resolve, reject) => {
request.onerror = () => reject(request.error)
request.onsuccess = () => resolve(request.result)
request.onupgradeneeded = () => {
const db = request.result
@steveruizok
steveruizok / en.json
Created April 2, 2023 19:05
tldraw keys
{
"action.convert-to-bookmark": "Convert to Bookmark",
"action.convert-to-embed": "Convert to Embed",
"action.open-embed-link": "Open link",
"action.align-bottom": "Align bottom",
"action.align-center-horizontal": "Align horizontally",
"action.align-center-vertical": "Align vertically",
"action.align-center-horizontal.short": "Align H",
"action.align-center-vertical.short": "Align V",
"action.align-left": "Align left",
@steveruizok
steveruizok / embeds.ts
Last active March 6, 2023 11:04
tldraw beta embeds
type EmbedDefinition = {
type: string
title: string
hostnames: string[]
minWidth?: number
minHeight?: number
width: number
height: number
doesResize: boolean
isAspectRatioLocked?: boolean