Skip to content

Instantly share code, notes, and snippets.

View cecilemuller's full-sized avatar

Cecile Muller cecilemuller

View GitHub Profile
@cecilemuller
cecilemuller / example.mjs
Created May 28, 2023 11:30
Download to file (Node 18)
import {createWriteStream} from "fs";
import {pipeline} from "stream/promises";
try {
const response = await fetch("https://github.githubassets.com/images/modules/logos_page/Octocat.png");
if (!response.ok) {
throw new Error(response.statusText);
} else {
await pipeline(response.body, createWriteStream("Octocat.png"));
}
@cecilemuller
cecilemuller / default_server.conf
Created May 13, 2023 21:42
Nginx: default_server fallback
# /etc/nginx/conf.d/default_server.conf
server {
server_name _;
listen 80 default_server;
listen [::]:80 default_server;
log_not_found off;
return 410;
}
server {
server_name _;
@cecilemuller
cecilemuller / UIView+distort.swift
Created April 9, 2023 07:43
Swift: distort a UIView to fit arbitrary corners
import UIKit
extension UIView {
/// Updates `anchorPoint`, `frame`, and `layer.transform` to fit arbitrary corners.
/// Based on https://stackoverflow.com/questions/9470493/transforming-a-rectangle-image-into-a-quadrilateral-using-a-catransform3d/39981054#39981054
func distort(topLeft tl: CGPoint, topRight tr: CGPoint, bottomLeft bl: CGPoint, bottomRight br: CGPoint) {
// Frame ----------------------------------------------------------- /
let xmin = min(tr.x, tl.x, bl.x, br.x)
let ymin = min(tr.y, tl.y, bl.y, br.y)
let xmax = max(tr.x, tl.x, bl.x, br.x)
@cecilemuller
cecilemuller / OrientationDamper.swift
Last active February 16, 2023 13:33
Swift Dampers
import simd
// Interpolate a Quaternion without specifying duration, in the style of Herbert Stocker's X3D OrientationDamper:
// https://www.web3d.org/specifications/java/javadoc/org/web3d/x3d/sai/Followers/OrientationDamper.html
class OrientationDamper {
private(set) var isActive: Bool = false
private(set) var value: simd_quatf
private(set) var destination: simd_quatf
private(set) var tau: Float
private(set) var tolerance: Float
@cecilemuller
cecilemuller / Dockerfile
Last active December 12, 2022 10:55
doctl in Ubuntu Docker
FROM ubuntu:22.04
RUN apt update \
&& apt install -y wget \
&& wget https://github.com/digitalocean/doctl/releases/download/v1.88.0/doctl-1.88.0-linux-amd64.tar.gz -O doctl.tar.gz \
&& tar xf doctl.tar.gz \
&& rm doctl.tar.gz \
&& mv doctl /usr/local/bin
@cecilemuller
cecilemuller / Example.swift
Last active November 6, 2022 19:56
CSS HSL "linear-gradient" to Swift "CAGradientLayer"
// CSS gradient generated by https://www.joshwcomeau.com/gradient-generator/
// hsl(199deg 95% 55%) 0%
// hsl(140deg 63% 65%) 50%
// hsl(99deg 100% 50%) 100%
let gradient = CAGradientLayer()
// ...
gradient.type = .axial
gradient.colors = [
UIColor.hsl(degrees: 199, saturation: 0.95, lightness: 0.55).cgColor,
@cecilemuller
cecilemuller / JSON.swift
Created October 26, 2022 07:41
Swift: JSON helpers
import Foundation
class JSON {
static func encodeData<T>(_ from: T, format: JSONEncoder.OutputFormatting = .sortedKeys) -> Data? where T: Encodable {
let encoder = JSONEncoder()
encoder.outputFormatting = format
do {
return try encoder.encode(from)
} catch {
print(error)
@cecilemuller
cecilemuller / FileManager+documentDirectory.swift
Last active June 29, 2022 23:43
Swift: get document directory
import Foundation
extension FileManager {
public static var documentDirectory: URL {
return FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
}
}
@cecilemuller
cecilemuller / TextureResource+generate.swift
Created June 24, 2022 12:31
Swift: generate a TextureResource from UIImage
import UIKit
import RealityKit
extension TextureResource {
static func generate(from: UIImage, withName: String?) -> TextureResource? {
guard let cgImage = from.cgImage else { return nil }
let options = TextureResource.CreateOptions(
semantic: .color,
mipmapsMode: .allocateAndGenerateAll
)
@cecilemuller
cecilemuller / example.scss
Created January 18, 2022 22:52
Animated CSS Custom Property
// Static value by default:
// https://caniuse.com/css-variables
:root {
--example: 0deg;
}
.myclass {
background-image: linear-gradient( var(--example), red, green 50%, blue );
}
// Animated value where supported: