Skip to content

Instantly share code, notes, and snippets.

@samme
samme / hex.js
Last active April 26, 2024 15:47
Hexadecimal number/string conversions
// 0x00000f (15) to '#00000f'
'#' + (0x00000f).toString(16).padStart(6, '0')
// '#00000f' to 0x00000f (15)
parseInt('0x' + '#00000f'.slice(1))
import Phaser from 'phaser';
const playerPositions = {
1: { x: 112, y: 112 },
2: { x: 1376, y: 160 },
3: { x: 2000, y: 48 },
4: { x: 2176, y: 144 },
};
export class GameScene extends Phaser.Scene {
@samme
samme / random.js
Last active April 26, 2025 03:30
Weighted random number generators <https://codepen.io/samme/pen/dPPRKjO>
const { asin, cos, random, PI } = Math;
// Average 0.333
const Low = () => random() ** 2;
// Average 0.666
const High = () => 1 - random() ** 2;
// Average 0.5, middle-biased
const Mid = () => 0.5 + asin(2 * random() - 1) / PI;