mirror of
https://github.com/excalidraw/excalidraw.git
synced 2025-05-03 10:00:07 -04:00
Move math and random files into their respective modules (#198)
* Move math and random files into their respective modules - Move distanceBetweenPointAndSegment to math module - Move LCG, randomSeed, and withCustomMathRandom to random module * Add everything else back
This commit is contained in:
parent
b3667000e2
commit
e3eef04e00
3 changed files with 59 additions and 58 deletions
18
src/random.ts
Normal file
18
src/random.ts
Normal file
|
@ -0,0 +1,18 @@
|
|||
// https://stackoverflow.com/questions/521295/seeding-the-random-number-generator-in-javascript/47593316#47593316
|
||||
export const LCG = (seed: number) => () =>
|
||||
((2 ** 31 - 1) & (seed = Math.imul(48271, seed))) / 2 ** 31;
|
||||
|
||||
export function randomSeed() {
|
||||
return Math.floor(Math.random() * 2 ** 31);
|
||||
}
|
||||
|
||||
// Unfortunately, roughjs doesn't support a seed attribute (https://github.com/pshihn/rough/issues/27).
|
||||
// We can achieve the same result by overriding the Math.random function with a
|
||||
// pseudo random generator that supports a random seed and swapping it back after.
|
||||
export function withCustomMathRandom<T>(seed: number, cb: () => T): T {
|
||||
const random = Math.random;
|
||||
Math.random = LCG(seed);
|
||||
const result = cb();
|
||||
Math.random = random;
|
||||
return result;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue