Skip to content

Instantly share code, notes, and snippets.

@redblobgames
redblobgames / vue-canvas.js
Last active April 11, 2023 07:31
Vue component for canvas with automatic dependency tracking
/** Canvas component
A generic canvas component that calls a draw function to draw the
contents, and automatically calls it again when anything the draw
function depends on changes. Blog entry:
http://simblob.blogspot.com/2018/03/using-vue-with-canvas.html
Example:
<a-canvas width="500" height="200"
@Daniel-Hug
Daniel-Hug / collision-detection.js
Last active March 3, 2024 04:02
JS functions: check if 2 rectangles intersect, are touching, or if one contains the other
// Check if rectangle a contains rectangle b
// Each object (a and b) should have 2 properties to represent the
// top-left corner (x1, y1) and 2 for the bottom-right corner (x2, y2).
function contains(a, b) {
return !(
b.x1 < a.x1 ||
b.y1 < a.y1 ||
b.x2 > a.x2 ||
b.y2 > a.y2
);
@LuisSevillano
LuisSevillano / README.md
Created October 25, 2016 14:11
polylabel.js
@kbuhrer
kbuhrer / D3 Symbol Extra Demo
Last active March 29, 2017 11:46
Demo of d3-symbol-extra
A simple demonstration of the d3-symbol-extra library.
@bycoffe
bycoffe / README.md
Last active March 29, 2017 13:03
Element rotation with point-along-path interpolation
@schnogz
schnogz / isArray-implementation-attemps.js
Last active December 27, 2023 08:13
JavaScript .isArray Implementations
// Attempt #1: using typeof
// fails in all cases since typeof [] returns "object"
Array.prototype.isArray = function(obj) {
return (typeof obj === "array");
}
// Attempt #2: using instanceof
// fails when obj = Array.prototype
// and when array is defined in another window or frame
Array.prototype.isArray = function(obj) {
@KoGor
KoGor / README.md
Last active August 20, 2023 20:26
Marker animation along SVG <path> element with D3.js III

Marker animation along SVG "path" element with D3.js: animating "path" and marker movement synchronously, marker rotate according to tangent line to path.

@jimhigson
jimhigson / gist:7985923
Last active January 19, 2024 11:27
round svg path to one decimal place
/* round("M -0.09375,-3 A 1.001098,1.001098 0 1 0 0,-1 C 0.56412939,-1 1,-0.56412939 1,0 1,0.27245181 0.8799664,0.4950336 0.6875,0.6875 A 1.016466,1.016466 0 1 0 2.125,2.125 C 2.6563912,1.5936088 3,0.83211769 3,0 3,-1.6450096 1.6450096,-3 0,-3 a 1.0001,1.0001 0 0 0 -0.09375,0 z")
=> "M -0.1,-3 A 1,1 0 1 0 0,-1 C 0.6,-1 1,-0.6 1,0 1,0.3 0.9,0.5 0.7,0.7 A 1,1 0 1 0 2.1,2.1 C 2.7,1.6 3,0.8 3,0 3,-1.6 1.6,-3 0,-3 a 1,1 0 0 0 -0.1,0 z"
*/
function round (path) {
return path.replace(/[\d\.-][\d\.e-]*/g, function(n){return Math.round(n*10)/10})
}