Skip to content

Instantly share code, notes, and snippets.

@stephanbogner
stephanbogner / joint.html
Last active July 23, 2017 11:47
Joint between two particles (verlet integration)
<canvas id="myCanvas" width="600" height="400"></canvas>
<style type="text/css">
canvas {
background: #F9F9F9;
margin: 3rem auto;
display: block;
}
</style>
<script>
var point1, point2, canvas, context, fps, interval, cursorX, cursorY;
@stephanbogner
stephanbogner / magnetism.html
Last active July 23, 2017 13:52
Magnet-like repulsion of two points
<canvas id="myCanvas" width="600" height="400"></canvas>
<style type="text/css">
canvas {
background: #F9F9F9;
margin: 3rem auto;
display: block;
}
</style>
<script>
var point1, point2, canvas, context, fps, interval, cursorX, cursorY;
@stephanbogner
stephanbogner / draw.js
Created July 31, 2017 12:31
Small helpers to convert svg points to arrays
// Example
// Input: getCoordinatesFromSvgCoordinatesString('M0.5,0.5 L0.5,89.5 L141.5,89.5 L141.5,0.5 L0.5,0.5 Z')
// Output: array like example above
function getCoordinatesFromSvgCoordinatesString(svgString){
var coordinates = [];
var svgParts = svgString.split(' ');
var cursor = {
"x": 0,
"y": 0
@stephanbogner
stephanbogner / bearingOnLine.js
Created August 14, 2017 11:41
TurfJS Calculate bearing on a certain point on a line (with a buffer)
// import turf
function bearingOnLine(feature, distance, buffer, units){
var lineDistance = turf.lineDistance(feature, units);
buffer = buffer || lineDistance * 0.07;
var measureFromDistance = Math.max(0, distance - buffer);
var measureToDistance = Math.min(lineDistance, distance + buffer);
var measureFrom = turf.along(feature, measureFromDistance, units);
var measureTo = turf.along(feature, measureToDistance, units);
@stephanbogner
stephanbogner / test.js
Created August 19, 2017 18:47
[TurfTest] Check how 'line-distance' measures for routes across the antimeridian
var turf = require('turf');
var lineString = {
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
@stephanbogner
stephanbogner / timeHelper.js
Last active September 5, 2017 14:16
Some snippets for working with days/months/years
console.log('In which month is day #368: ', 'Month ' + (getMonthIdFromDay(368).index + 1) + ' of year ' + (getMonthIdFromDay(368).cycle + 1));
// -> In which month is day #368: Month 1 of year 2
console.log('Days in the last quarter of a year: ', getDaysPerQuarter()[3] );
// -> Days in the last quarter of a year: 92
function getMonthIdFromDay(day) {
var dayRange = valuesToRange(getDaysPer('month'));
return getPositionInLoopingRange(day, dayRange);
}
@stephanbogner
stephanbogner / gist:5e005aed1db59e45b32b7a6b6966bdcd
Created October 18, 2017 18:53
Launch mongod with more possible open files
sudo launchctl limit maxfiles 16384 16384 && ulimit -n 16384
@stephanbogner
stephanbogner / index.html
Last active October 24, 2022 12:33
Save svg from dom as file (great when working with d3)
<a id="downloadLink" href="" download="diagram.svg">Download ↓</a>
<svg id="svg" width="120" height="120">
<rect fill="#000000" x="10" y="10" width="100" height="100"></rect>
</svg>
<script type="text/javascript">
setDownloader('downloadLink', 'svg')
function setDownloader(linkId, svgId) {
// Get svg element
@stephanbogner
stephanbogner / index.html
Created November 8, 2017 14:49
Simple example of using javascript to take a photo from a website
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Camera</title>
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<meta name="viewport" content="width=device-width,initial-scale=1">
<style type="text/css">
.cameraRoll {
@stephanbogner
stephanbogner / index.html
Created November 10, 2017 14:05
Simple example to check if a user leaves a browser window and show some information
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Demo</title>
<style type="text/css">
body {
background-color: #ECECEC;
font-family: sans-serif;