Skip to content

Instantly share code, notes, and snippets.

View tomsoderlund's full-sized avatar
🤔
Learning AI/ML!

Tom Söderlund tomsoderlund

🤔
Learning AI/ML!
View GitHub Profile
@tomsoderlund
tomsoderlund / App.tsx
Created November 10, 2023 17:24
React Native 2D animation and touch handling with expo-2d-context and expo-gl
import React, { useRef, useCallback, useEffect } from 'react'
import { GestureResponderEvent, PixelRatio } from 'react-native'
import { GLView } from 'expo-gl'
import Expo2DContext, { Expo2dContextOptions } from 'expo-2d-context'
const App = (): React.ReactElement => {
const ctxRef = useRef<Expo2DContext | null>(null)
const pixelRatio = PixelRatio.get()
let originPos = [0, 0]
@tomsoderlund
tomsoderlund / useRouterQueryState.js
Last active August 15, 2023 21:03
Like useState hook, but saves current state in browser search bar (Next.js router query)
// import useRouterQueryState from '../hooks/useRouterQueryState'
// const [myState, setMyState] = useRouterQueryState(propertyName, defaultValue)
import { useState, useEffect } from 'react'
import { useRouter } from 'next/router'
function useRouterQueryState (key, defaultValue) {
const router = useRouter()
// Get initial state from router query or default value
@tomsoderlund
tomsoderlund / useItem.js
Created February 9, 2022 11:23
React Context and State rolled into one
/*
Usage:
import { ItemContextProvider } from 'hooks/useItem'
<ItemContextProvider
item={item}
>
<ComponentThatUsesItem />
@tomsoderlund
tomsoderlund / ScrollingNavigation.js
Created February 11, 2021 12:26
Scrolling 3D navigation
import React, { createContext, useContext, useState, useEffect, useRef } from 'react'
const ScrollingNavigation = ({ children, maxX, maxY = 2000, startX = 0, startY = 0, scrollSpeed = 2.0 }) => {
const scrollingRef = useRef()
return (
<ScrollContextProvider
startX={startX}
startY={startY}
scrollSpeed={scrollSpeed}
scrollingRef={scrollingRef}
@tomsoderlund
tomsoderlund / Map.js
Created June 22, 2020 09:50
Using fitBounds in ReactMapGL to center points on map
import React, { useState } from 'react'
import ReactMapGL, { Marker, WebMercatorViewport } from 'react-map-gl'
const applyToArray = (func, array) => func.apply(Math, array)
const getBoundsForPoints = (points) => {
// Calculate corner values of bounds
const pointsLong = points.map(point => point.coordinates._long)
const pointsLat = points.map(point => point.coordinates._lat)
const cornersLongLat = [
@tomsoderlund
tomsoderlund / ExampleComponent.js
Last active May 13, 2021 01:50
Shared state using React Hooks and Context, to allow sharing of state between multiple components or hooks
import React, { useContext } from 'react'
import { UserContext, UserContextProvider } from './UserContext'
export default (props) => {
const [user, setUser] = useContext(UserContext)
return (
<UserContextProvider user={null}>
<div>User name: {user && user.name}</div>
</UserContextProvider>
@tomsoderlund
tomsoderlund / parallelPool.js
Created November 26, 2019 14:43
A resource pool e.g. for projects. Inspired by https://www.npmjs.com/package/generic-pool
/**
* parallelPool module
* @description A resource pool e.g. for projects. Inspired by https://www.npmjs.com/package/generic-pool
* @module parallelPool
* @author Tom Söderlund
*/
// Private functions
/* const projectPool = new ParallelPool({}) */
@tomsoderlund
tomsoderlund / namedColors.json
Created November 3, 2019 17:08
All named HTML colors with RGB and HSL values
[
{
"name": "indianred",
"hexValue": "#cd5c5c",
"rgbValues": [205, 92, 92],
"hslValues": [0, 0.531, 0.582]
},
{
"name": "lightcoral",
"hexValue": "#f08080",
@tomsoderlund
tomsoderlund / killapps.sh
Last active January 24, 2021 17:09
macOS bash script to close all non-essential apps – like “Close all browser tabs” but for apps
#!/bin/sh
# Example: kill all non-development apps:
# . killapps.sh dev
# Example: kill all non-office apps in a soft way:
# . killapps.sh office soft
# Strip leading and trailing white space (new line inclusive).
trim () {
[[ "$1" =~ ^[[:space:]]*(.*[^[:space:]])[[:space:]]*$ ]]
@tomsoderlund
tomsoderlund / synaptic_predict.js
Last active September 21, 2023 08:27
Basic prediction using neural networks (Synaptic.js)
/*
Adapted from Lian Li’s (@chimney42) example:
https://slidr.io/Chimney42/machine-learning-with-synaptic
https://www.youtube.com/watch?v=M5glN6XjDv8
You need to include synaptic.js - visit https://caza.la/synaptic/
See my own tests at https://codepen.io/tomsoderlund/pen/MvLZLW
*/