Skip to content

Instantly share code, notes, and snippets.

View hvianna's full-sized avatar

Henrique Vianna hvianna

View GitHub Profile
@hvianna
hvianna / svgToCanvas.js
Created September 22, 2023 13:22
Draw an SVG element into a canvas
// thanks https://levelup.gitconnected.com/draw-an-svg-to-canvas-and-download-it-as-image-in-javascript-f7f7713cf81f
const canvas = document.getElementById('canvas'),
ctx = canvas.getContext('2d'),
svgEl = document.getElementById('mysvg');
const image = new Image();
image.src = URL.createObjectURL( new Blob( [ svgEl.outerHTML ], { type: 'image/svg+xml;charset=utf-8' } ) );
image.onload = () => {
ctx.drawImage( image, 0, 0 );
@hvianna
hvianna / embed-svg.js
Last active September 21, 2023 13:31
Embed external SVG image into the DOM so attributes (fill, background) can be manipulated via JavaScript
fetch( 'image.svg' )
.then( response => response.text() )
.then( content => {
const doc = new DOMParser().parseFromString( content, 'text/xml' ),
svg = doc.querySelector('svg');
svg.setAttribute( 'fill', '#d20a11' );
document.getElementById('image-container').append( svg );
});
@hvianna
hvianna / styles.css
Last active October 5, 2020 23:13
CSS for a simple website
/*
Demo: https://henriquevianna.com/simple/
Based on https://blog.koley.in/baserock/
*/
@import url('https://fonts.googleapis.com/css?family=Fira+Sans');
body {
font-family: 'Fira Sans', sans-serif;
line-height: 1.6;
color: #222;
max-width: 40rem;
@hvianna
hvianna / parse-tsv.js
Created January 31, 2020 12:40
Ler arquivo texto linha a linha com node.js
// https://nodejs.org/api/readline.html#readline_example_read_file_stream_line_by_line
const fs = require('fs');
const readline = require('readline');
const rl = readline.createInterface({
input: fs.createReadStream('arquivo.tsv'),
crlfDelay: Infinity
});