Skip to content

Instantly share code, notes, and snippets.

View shamansir's full-sized avatar

Ulric Wilfred shamansir

View GitHub Profile
@shamansir
shamansir / draw_svg.js
Last active February 20, 2023 16:11
draw SVG path on canvas
// take SVG commands and draw this path to HTML5 canvas
// commandList should look like that: [ { marker: "M", values: [ 10, 10 ] },
// { marker: "l", values: [ 5, 7 ] },
// { marker: "C", values: [ -5, 7.2, .3, -16, 24, 10 ] },
// . . .
// { marker: "z", values: [ ] } ]
// there's another gist which has the code to parse SVG paths:
// https://gist.github.com/shamansir/0ba30dc262d54d04cd7f79e03b281505
@shamansir
shamansir / _.js
Last active May 14, 2024 19:35
Parse and convert any SVG path to the list of commands with JavaScript + Regular Expressions
// svgPathToCommands('M10,10 l 5,7 C-5,7.2,.3-16,24,10 z');
//
// produces:
//
// [ { marker: "M", values: [ 10, 10 ] },
// { marker: "l", values: [ 5, 7 ] },
// { marker: "C", values: [ -5, 7.2, 0.3, -16, 24, 10 ] },
// { marker: "z", values: [ ] } ]
//
// commandsToSvgPath(svgPathToCommands('M10,10 l 5,7 C-5,7.2,.3-16,24,10 z'))
@shamansir
shamansir / rrect.js
Last active August 29, 2015 14:28
SVG generate partially rounded rectangle path
// based on http://bl.ocks.org/mbostock/3468167
function roundedRect(x, y, width, height, rtl, rtr, rbr, rbl) {
return "M" + x + "," + y
+ (rtl ? ("v" + rtl
+ "a" + rtl + "," + rtl + " 0 0 1 " + rtl + "," + -rtl) : "")
+ "h" + (width - (rtl ? rtl : 0) - (rtr ? rtr : 0))
+ (rtr ? ("a" + rtr + "," + rtr + " 0 0 1 " + rtr + "," + rtr) : "")
+ "v" + (height - (rtr ? rtr : 0) - (rbr ? rbr : 0))
+ (rbr ? ("a" + rbr + "," + rbr + " 0 0 1 " + -rbr + "," + rbr) : "")
// allows to check if the spy was called with specified arguments in exact order,
// like:
// ```
// var spy = createSpy('foo');
// spy('a', { prop: 'foo' });
// spy('b', { prop: 'bar' });
// spy('c', { prop: 'baz' });
// spy('d', { prop: 'fuz' });
// expect(spy).toHaveBeenCalledInOrder() {[
@shamansir
shamansir / _.md
Last active August 29, 2015 14:18
Tributary inlet
@shamansir
shamansir / spread.js
Last active August 29, 2015 14:15
Spread-Repeat
function numIter(max) {
return function(signal) {
if (signal) {
var value = 0;
return signal.takeWhile(function() { return value < max; })
.map(function() { console.log('iter('+max+')', value); return value++; })
} else {
return Kefir.fromBinder(function(emitter) {
var value = 0;
@shamansir
shamansir / progress.js
Created February 10, 2015 09:47
monitor html5 audio loading progress
// source: http://jspro.brothercake.com/media-events/progress.html
// article: http://www.sitepoint.com/essential-audio-and-video-events-for-html5/
(function()
{
//create a new video element with "auto" preload and native "controls"
var media = document.body.appendChild(document.createElement('video'));
media.setAttribute('preload', 'auto');
@shamansir
shamansir / _toolkit.js
Last active August 29, 2015 14:14
RPD Toolkit example
Rpd.channeltype('pd/t-num', {
show: function(t_num) { return t_num ? t_num.value : '?'; }
});
Rpd.channeltype('pd/spinner', {
adapt: function(val) {
return { value: val, time: Date.now() };
}
});
@shamansir
shamansir / chaining.rs
Created November 9, 2014 02:18
Rust chaining methods w/o mutable self
// inspired by rust-jlens: https://github.com/bkoropoff/rust-jlens/blob/master/src/lib.rs
pub trait Step {
fn perform(&self, &mut|&str|);
fn step1(self) -> Step1<Self> { Step1 { prev: self } }
fn step2(self) -> Step2<Self> { Step2 { prev: self } }
@shamansir
shamansir / arithmetics-parser.pegjs-0-6-2.js
Created September 6, 2014 22:39
Comparison between parser generated with `pegjs` and parser generated with `pegjs-fn` – 2
module.exports = (function(){
/* Generated by PEG.js 0.6.2 (http://pegjs.majda.cz/). */
var result = {
/*
* Parses the input with a generated parser. If the parsing is successfull,
* returns a value explicitly or implicitly specified by the grammar from
* which the parser was generated (see |PEG.buildParser|). If the parsing is
* unsuccessful, throws |PEG.parser.SyntaxError| describing the error.
*/