Skip to content

Instantly share code, notes, and snippets.

@freakdesign
freakdesign / select-from-hash
Last active October 29, 2018 08:00
Javascript snippet to help auto select a Shopify variant from a hash.
<script type="text/javascript">
/*
re: http://ecommerce.shopify.com/c/ecommerce-design/t/land-on-specific-variant-when-loading-product-page-147793
Assumes:
--------
* that you are calling the javascript once the select element is available
* you have a select element on the page like:
@homam
homam / AWS_S3_File_Upload.js
Created January 27, 2014 10:08
How to upload files to AWS S3 with NodeJS SDK
var AWS = require('aws-sdk'),
fs = require('fs');
// For dev purposes only
AWS.config.update({ accessKeyId: '...', secretAccessKey: '...' });
// Read in the file, convert it to base64, store to S3
fs.readFile('del.txt', function (err, data) {
if (err) { throw err; }
@gustavohenke
gustavohenke / svg2png.js
Created February 18, 2014 15:27
SVG to PNG
var svg = document.querySelector( "svg" );
var svgData = new XMLSerializer().serializeToString( svg );
var canvas = document.createElement( "canvas" );
var ctx = canvas.getContext( "2d" );
var img = document.createElement( "img" );
img.setAttribute( "src", "data:image/svg+xml;base64," + btoa( svgData ) );
img.onload = function() {
@timo22345
timo22345 / flatten.js
Last active March 12, 2025 09:42
Flatten.js, general SVG flattener. Flattens transformations of SVG shapes and paths. All shapes and path commands are supported.
<!doctype html>
<html>
<title>Flatten.js, General SVG Flattener</title>
<head>
<script>
/*
Random path and shape generator, flattener test base: https://jsfiddle.net/fjm9423q/embedded/result/
Basic usage example: https://jsfiddle.net/nrjvmqur/embedded/result/
@riston
riston / svg2png.js
Created April 3, 2014 14:25
Convert currently active svg to png, copy this to your web console and execute "svg2png(document.querySelector('svg'))", click the url to open.
// Takes an SVG element as target
function svg2png(target) {
// Flatten CSS styles into the SVG
for (i = 0; i < target.childNodes.length; i++) {
child = target.childNodes[i];
child.style.cssText = window.getComputedStyle(child).cssText;
}
var box = target.viewBox.baseVal;
@davelandry
davelandry / README.md
Last active October 17, 2024 06:40
SVG Text Wrapping

Using d3plus.textwrap, SVG <text> elements can be broken into separate <tspan> lines, as HTML does with <div> elements. In this example, the first column shows normal wrapped text, the second column shows text that is resized to fill the available space, and the third column shows the default SVG behavior.

D3plus automatically detects if there is a <rect> or <circle> element placed directly before the <text> container element in DOM, and uses that element's shape and dimensions to wrap the text. If it can't find one, or that behavior needs to be overridden, they can manually be specified using .shape( ), .width( ), and .height( ).

Featured on D3plus.org

@hackerzhut
hackerzhut / imageResize.js
Created July 1, 2014 02:30
Resize image using Graphicsmagick and upload using S3
gm(request(url))
.resize("80^", "80^")
.stream(function (err, stdout, stderr) {
var chunks = [];
stdout.on('data', function (chunk) {
chunks.push(chunk);
});
stdout.on('end', function () {
var image = Buffer.concat(chunks);
var options = {
@vicapow
vicapow / README.md
Last active December 3, 2024 13:11
An example of creating a PNG from an SVG in D3.

This is an example of creating a PNG from an SVG. (You should notice that you're able to right click on the last image and save it as a PNG image.) It has been Tested in Firefox and Chrome but doesn't work in Safari as of 2014-07-20.

Normally, you'll create your SVG in D3 but to make the example a bit more readable, the SVG is already placed in the document. There are a few important points. Namely:

  1. All the styles of the SVG need to be self contained in side of the <defs> tags. (These styles should be escaped using the <![[CDATA[ ... ]] tag.)
  2. The SVG needs to have the proper namespaces and document types.
  3. The SVG needs to be saved to an image, then read from an canvas element, then saved to an image again.

Note: Portions of this demo where taken from The New York Times' excellent Crowbar SVG extractor.

@cameronbarker
cameronbarker / gist:f8e48dca8f497177b5f4
Created July 16, 2014 16:33
Lyst image background removal
#http://developers.lyst.com/data/images/2014/02/13/background-removal/
import pgmagick as pg
def trans_mask_sobel(img):
""" Generate a transparency mask for a given image """
image = pg.Image(img)
# Find object