Skip to content

Instantly share code, notes, and snippets.

@j2is
j2is / canvasSize.js
Created March 5, 2023 23:18
detect max possible canvas capabilities
import canvasSize from "canvas-size";
const getMaxTextureSize = () => {
const canvas = document.createElement("canvas");
const gl = canvas.getContext("experimental-webgl");
if (!gl) return 2048;
return gl.getParameter(gl.MAX_TEXTURE_SIZE) || 2048;
};
const maxTextureSize = getMaxTextureSize();
@j2is
j2is / Video.js
Last active November 9, 2021 21:41
Better Sanity Player Barebones
import React, { useRef, useState, useEffect } from "react";
import Hls from "hls.js";
function getPosterSrc(playbackId, options = {}) {
const width = options.width || 640;
const height = options.height || "";
const time = options.time || 0;
const fitMode =
typeof options.fitMode === "undefined" ? "smartcrop" : options.fitMode;
let url = `https://image.mux.com/${playbackId}/thumbnail.png?width=${width}&fit_mode=${fitMode}&time=${time}`;
@j2is
j2is / config.js
Created July 18, 2021 18:37
Next Sanity Config
export const config = {
dataset: process.env.NEXT_PUBLIC_SANITY_DATASET || "production",
projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID,
apiVersion: "2021-03-25",
/**
* Set useCdn to `false` if your application require the freshest possible
* data always (potentially slightly slower and a bit more expensive).
* Authenticated request (like preview) will always bypass the CDN
**/
useCdn: process.env.NODE_ENV === "production",
@j2is
j2is / sanity.js
Created July 18, 2021 18:37
Next Sanity Client
// lib/sanity.js
import {
createImageUrlBuilder,
createPortableTextComponent,
createPreviewSubscriptionHook,
createCurrentUserHook,
} from "next-sanity";
import { config } from "./config";
@j2is
j2is / sanity.server.js
Created July 18, 2021 18:36
Next Sanity Server
// lib/sanity.server.js
import { createClient } from "next-sanity";
import { config } from "./config";
// Set up the client for fetching data in the getProps page functions
export const sanityClient = createClient(config);
// Set up a preview client with serverless authentication for drafts
export const previewClient = createClient({
...config,
@j2is
j2is / index.js
Created July 18, 2021 18:34
Next Sanity Index
// pages/index.js
import React from "react";
import { groq } from "next-sanity";
import { usePreviewSubscription, urlFor, PortableText } from "../lib/sanity";
import { getClient } from "../lib/sanity.server";
import { useRouter } from "next/router";
import ErrorPage from './ErrorPage';
import HomeComponent from '../components/HomeComponent';
@j2is
j2is / Immer
Created June 30, 2021 10:59
Immer state management
import React, { useState, useMemo, useContext } from "react";
import immer from "immer";
export default function makeStore() {
// This is basically a factory for a new store instance
const loggerActive = false;
// Create a new context for this store instance
const context = React.createContext();
@j2is
j2is / Craft Add Items To Cart
Created June 29, 2021 09:10
How to add items to the cart, this example is headless but can be modified for a regular setup by also posting a CSRF token.
import axios from "axios";
async function request(request) {
if (!request) {
return { data: undefined, error: "no request" };
}
const data = typeof FormData !== "undefined" ? new FormData() : {};
if (request.data) {
Object.entries(request.data).forEach(([key, value]) => {
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "chrome",
"request": "launch",
"name": "Next: Chrome",
import {
useRef, useCallback, useEffect, useState
} from 'react';
import smoothscroll from 'smoothscroll-polyfill';
smoothscroll.polyfill();
function useSmoothScroll() {
const scrollRafPending = useRef(false);
const scrollRafInstance = useRef(false);