Skip to content

Instantly share code, notes, and snippets.

function deepClone(target) {
if (!target || typeof target !== 'object')
return target
const clone = Array.isArray(target) ? [] : {}
for (const [key, value] of Object.entries(target)) {
clone[key] = deepClone(value)
function ScreenScalar(props = { p1:1, p2:1 }) {
const compute = () => this.value = 1 - Object.values(props).reduce((r, v) => r * (1 - v), 1)
compute()
for (const key of Object.keys(props)) {
Object.defineProperty(this, key, {
get: () => props[key],
set: value => {
@jniac
jniac / prune.js
Last active September 25, 2020 09:35
// https://gist.github.com/jniac/39eabea6b06a9be5be348c11447c249c
/**
* 'prune' will simplify any tree (target) by removing any 'autoKey'
* found by its inner value. eg:
*
* `prune({ text:{ en:'foo', fr:'toto' }}, 'en') // -> { text:'foo' }`
* `prune({ text:{ en:'foo', fr:'toto' }}, 'fr') // -> { text:'toto' }`
* @param {object} target
* @param {...string} autoKeys
*/
@jniac
jniac / anyDicesComparison.js
Last active June 21, 2020 20:38
roll some dices
D = n => Math.ceil(n * Math.random())
run = ({ name = 'run', d1, d2, n = 100000 } = {}) => {
result = {
win: 0,
loose: 0,
equals: 0,
}
@jniac
jniac / getVertigoCamera.js
Created June 23, 2020 13:17
get a "Vertigo" Camera for THREE
let VertigoCamera
const init = THREE => {
const {
Camera,
PerspectiveCamera,
OrthographicCamera,
@jniac
jniac / html.js
Last active November 18, 2022 09:25
html generator based on template literals
// https://gist.github.com/jniac/1f1b8e57dbb320138af00680c090f94e
const parser = new DOMParser()
const html = (strings, ...inserts) => {
if (typeof strings === 'string') {
// html is used as a regular function, eg:
// html(`<div>${foo}</div>`)
const removeHeadingWhitespaces = str => {
const lines = str.split('\n')
while (lines.length > 0 && /^\s*$/.test(lines[0])) {
lines.shift()
}
while (lines.length > 0 && /^\s*$/.test(lines[lines.length - 1])) {
lines.pop()
@jniac
jniac / index.mjs
Last active November 22, 2020 21:42
download google fonts (with node)
import fetch from 'node-fetch'
import fs from 'fs-extra'
const safeName = str => str.replace(/\W/g, '-')
const parseFontFace = fontFace => {
const [, url] = fontFace.match(/url\((.*?)\)/)
const [, family] = fontFace.match(/font-family: '(.*?)';/)
const [, style] = fontFace.match(/font-style: (.*?);/)
const [, weight] = fontFace.match(/font-weight: (.*?);/)
@jniac
jniac / iterateDimensionsFrom.js
Last active November 25, 2020 23:22
get a n-dimensions array, iterate through indexes from a specified dimension
/**
* returns [1, 4, 12] from [4, 3, 2]
* @param {number[]} array
*/
function getArrayScale (array) {
let scale = 1
return array.map(dim => {
const x = scale
scale *= dim
return x
@jniac
jniac / three-stage.js
Created January 12, 2021 12:32
quick "stage" script for prototyping w threejs
import { WebGLRenderer, PerspectiveCamera, Scene } from 'https://threejs.org/build/three.module.js'
import { OrbitControls } from 'https://threejs.org/examples/jsm/controls/OrbitControls.js'
const renderer = new WebGLRenderer({ antialias: true })
renderer.setPixelRatio(window.devicePixelRatio)
renderer.setSize(window.innerWidth, window.innerHeight)
document.body.append(renderer.domElement)
const camera = new PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 100)
camera.position.z = 5