Skip to content

Instantly share code, notes, and snippets.

View mikesurowiec's full-sized avatar
🌀

Mike Surowiec mikesurowiec

🌀
View GitHub Profile
# pip install opencv-python numpy
import cv2
import numpy as np
import sys
def color_percentage(image_path, target_color):
# Read the image
image = cv2.imread(image_path, cv2.IMREAD_UNCHANGED) # Use cv2.IMREAD_UNCHANGED to keep the alpha channel
// This is inside of our React.Component
componentDidMount() {
// ... the clicking code was up here
const zoom = d3.zoom()
.scaleExtent([1, 5])
.translateExtent([[-100, -100], [svgViewportWidth-500, svgViewportHeight-500]])
.on('zoom', zoomed);
function zoomed() {
// The React.Component from before.
// This is just highlighting the differences.
import * as d3 from 'd3';
class DCMetroMap extends React.Component {
componentDidMount() {
// For every group inside of #Stations...
d3.selectAll('#Stations g')
import React from 'react';
import DCMetroMapHTML from './DCMetroMapHTML.js';
import Dimensions from 'react-dimensions';
const svgViewportWidth = 3266;
const svgViewportHeight = 3012;
class DCMetroMap extends React.Component {
render() {
const { containerHeight, containerWidth } = this.props;
const DCMetroMapHTML = `
<!-- Generator: Sketch 3.4.2 (15855)-->
<desc>Created with Sketch.</desc>
<defs></defs>
<g id="main" stroke="none" stroke-width="1" fill-rule="evenodd" sketch:type="MSPage">
<!-- 2,100 lines of SVG, excluded for the sake of brevity -->
</g>
`;
export default DCMetroMapHTML;
@mikesurowiec
mikesurowiec / getRealtimeDataInstantaneous.js
Last active July 23, 2017 14:03
Shows how socket acts like a more standard REST api, with request / response
// Allows the client to get the data without having to wait for a new REQUEST_INTERVAL.
socket.on('getRealtimeDataForStations', (stations) => {
if (datastore.realtime && Array.isArray(stations)) {
stations.forEach((locationCode) => {
socket.emit(`realtime/${locationCode}`, datastore.realtime[locationCode]);
});
}
});
@mikesurowiec
mikesurowiec / realtime.js
Created July 23, 2017 13:47
DC Metro Pro realtime data fetch
import _ from 'lodash';
import datastore from '../datastore';
import MetroApi from '../metroApi';
import MetroResponseTimes from '../metroResponseTimes';
module.exports = () => {
const startTime = process.hrtime();
return MetroApi.getRealtime().then((realtimeData) => {
const endTime = process.hrtime(startTime)[1] / 1000000; // [0]=seconds, [1]/1000000=ms
MetroResponseTimes.push(endTime);
@mikesurowiec
mikesurowiec / syncerIndex.js
Created July 23, 2017 13:45
DC Metro Pro synchronization logic
import { EventEmitter } from 'events';
import realtimeAction from './realtime';
import incidentsAction from './incidents';
import advisoriesAction from './advisories';
const eventEmitter = new EventEmitter();
const syncers = [
{ name: 'realtime', action: realtimeAction, interval: 2.5 * 1000 },
{ name: 'incidcents', action: incidentsAction, interval: 60 * 1000 },
// AudioComponent.js
import {
NativeModules,
} from 'react-native';
const { AudioManager } = NativeModules;
async function setPlaying (shouldPlay: boolean) => {
const isPlayingNativeResult = await AudioManager.setPlaying(shouldPlay);
// AudioManager.swift
import Foundation
@objc(AudioManager)
class AudioManager: RCTEventEmitter {
// ...
@objc func setPlaying(_ shouldPlay: Bool,
resolver resolve: RCTPromiseResolveBlock,
rejecter reject: RCTPromiseRejectBlock) -> Void {