Skip to content

Instantly share code, notes, and snippets.

View rowan-m's full-sized avatar
🐢
I'll get to it eventually.

Rowan Merewood rowan-m

🐢
I'll get to it eventually.
View GitHub Profile
@rowan-m
rowan-m / reduce-ua.js
Last active June 8, 2022 20:30
This code might reduce Chrome user-agents
// Only match Chrome user-agents we know we can reduce
// https://www.chromium.org/updates/ua-reduction#TOC-Reduced-User-Agent-String-Reference
const chromeUAs = /^Mozilla\/5\.0 \(((?<platform>Lin|Win|Mac|X11; C|X11; L)+[^\)]+)\) AppleWebKit\/537.36 \(KHTML, like Gecko\) Chrome\/(?<major>\d+)[\d\.]+(?<mobile>[ Mobile]*) Safari\/537\.36$/;
const matched = chromeUAs.exec(navigator.userAgent);
if (matched) {
// Map detected platform to reduced value
const unifiedPlatform = {
'Lin': 'Linux; Android 10; K',
'Win': 'Windows NT 10.0; Win64; x64',
@rowan-m
rowan-m / server.js
Created May 14, 2020 11:39
ExpressJS always redirect to HTTPS with HSTS
const express = require('express');
const app = express();
app.use(function (req, res, next) {
if (req.secure) {
res.set('Strict-Transport-Security', 'max-age=63072000; includeSubdomains; preload');
return next();
}
res.redirect(301, 'https://' + req.headers.host + req.url);
const template = document.createElement('template');
template.innerHTML = `...✂️`;
// 1. Prepare template!
window.ShadyCSS && ShadyCSS.prepareTemplate(template, 'input-knob');
class InputKnob extends HTMLElement {
constructor() {
super();
#container {
--angle: 0rad;
transform: rotate(var(--angle));
will-change: transform;
}
_onTouchstart(e) {
e.preventDefault();
this._touchX = e.changedTouches[0].clientX;
this._touchY = e.changedTouches[0].clientY;
this._rotationStart();
this.addEventListener('touchmove', this._onTouchmove);
this.addEventListener('touchend', this._onTouchend);
this.addEventListener('touchcancel', this._onTouchend);
}
_onTouchmove(e) {
_onMousedown(e) {
this._touchX = e.clientX;
this._touchY = e.clientY;
this._rotationStart();
document.addEventListener('mousemove', this._onMousemove);
document.addEventListener('mouseup', this._onMouseup);
}
_onMousemove(e) {
e.preventDefault();
this._touchX = e.clientX;
// disconnectedCallback
if ('PointerEvent' in window) {
this.removeEventListener('pointerdown', this._onPointerdown);
} else {
this.removeEventListener('touchstart', this._onTouchstart);
this.removeEventListener('mousedown', this._onMousedown);
}
// connectedCallback()
if ('PointerEvent' in window) {
this.addEventListener('pointerdown', this._onPointerdown);
} else {
this.addEventListener('touchstart', this._onTouchstart);
this.addEventListener('mousedown', this._onMousedown);
}
_onPointerdown(e) {
e.preventDefault();
this._touchX = e.clientX;
this._touchY = e.clientY;
// ✂️ Chop out existing code, extract into new method
this._rotationStart();
this.setPointerCapture(e.pointerId);
this.addEventListener('pointermove', this._onPointermove);
this.addEventListener('pointerup', this._onPointerup);
this.addEventListener('pointercancel', this._onPointerup);
// _onPointermove(e)
this._touchX = e.clientX;
this._touchY = e.clientY;
this._angle =
// initial rotation of the element
this._initialAngle
// subtract the starting touch angle
- this._initialTouchAngle
// add the current touch angle
+ Math.atan2(this._touchY - this._centerY, this._touchX - this._centerX);