Skip to content

Instantly share code, notes, and snippets.

View aolo2's full-sized avatar

Алексей Олохтонов aolo2

View GitHub Profile
@aolo2
aolo2 / zoom.js
Last active January 17, 2024 10:48
zoom around the mouse
// What we want is the mouse position to stay the same relative to the zoomable object (it's a map in this example)
// To do that we solve the equation "oldMouseRelativePos = newMouseRelativePos" to find the value of newMapOffset.
// We know the values of oldZoom, newZoom, oldMapOffset and mousePos
//
// It is assumed that canvas->screen (something you would use in OpenGL, for example) transform is "(p - offset) / zoom",
// meaning screen->canvas (something you would use in Canvas2D) transform is "p / zoom - offset".
// mousePos is assumed to be in some kind of screen coordinates
// mapZoom is assumed to be zooming relative to the top left corner of the map
//
// "map relative mouse position" is:
@mauskin
mauskin / bare-drag-no-drop.js
Last active March 3, 2023 22:16
Minimal cross-browser drag with no drop function
// The most bare cross-browser drag with no drop function I could do.
// Example:
//
// make_draggable(
// thing,
// () => console.log("let’s go"),
// (event) => {
// console.log(event.clientX, event.clientY);
// element.style.transform = `translate(${event.clientX}px, ${event.clientY}px)`;
@mmozeiko
mmozeiko / pcg32.h
Last active February 5, 2024 10:46
simple standalone C headers for PCG random number generator
#pragma once
#include <stdint.h>
#define PCG_DEFAULT_MULTIPLIER_64 6364136223846793005ULL
#define PCG_DEFAULT_INCREMENT_64 1442695040888963407ULL
typedef struct {
uint64_t state;
} pcg32;
@rygorous
rygorous / magic_ring.cpp
Created July 22, 2012 03:55
The magic ring buffer.
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <string.h>
#include <Windows.h>
// This allocates a "magic ring buffer" that is mapped twice, with the two
// copies being contiguous in (virtual) memory. The advantage of this is
// that this allows any function that expects data to be contiguous in
// memory to read from (or write to) such a buffer. It also means that