Skip to content

Instantly share code, notes, and snippets.

View rikschennink's full-sized avatar

Rik rikschennink

View GitHub Profile
@rikschennink
rikschennink / drop-simulation.js
Created June 13, 2024 07:48
A quick script to simulate dropping files with a Drag & Drop operation
// drop simulation function
async function runDropSimulation(sources, path, options) {
const { wait = 1000, shouldRelease = true } = options || {};
// create the data transfer
const dataTransfer = new DataTransfer();
for (const src of sources) {
const blob = await fetch(src).then((res) => res.blob());
const file = new File([blob], src.split('/').pop(), { type: blob.type });
dataTransfer.items.add(file);
@rikschennink
rikschennink / drag-simulation.js
Created May 23, 2024 08:18
A quick script to simulate dragging an element with PointerEvent
// drag simulation function
async function runDragSimulation(path, options) {
const { wait = 1000, shouldRelease = true } = options;
// helper to wait for x milliseconds
const sleep = (time) =>
new Promise((resolve) => {
setTimeout(() => {
resolve();
}, time);
@rikschennink
rikschennink / main.rs
Created December 13, 2022 14:10
Tauri MacOS tray app
#![cfg_attr(
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
)]
// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
#[tauri::command]
fn sync(name: &str) -> String {
format!("Hello, {}! You've been greeted from Rust!", name)
@rikschennink
rikschennink / worker.js
Created September 28, 2022 11:33
Cloudflare purge cache worker
// Based on https://gist.github.com/vdbelt/20f116236d2ebffa92f131e679c0551a
addEventListener('fetch', event => {
event.respondWith(purgeCache(event.request))
})
async function purgeCache(request) {
// get zone querystring value
const zone = new URL(request.url).searchParams.get('zone');
@rikschennink
rikschennink / scramble.ts
Created October 11, 2021 09:42
Scramble filter for use in worker
export default (
options: { imageData: ImageData; amount: number },
done: (err: string, imageData: ImageData) => void
): void => {
const { imageData, amount = 1 } = options;
const intensity = Math.round(Math.max(1, amount) * 2);
const range = Math.round(intensity * 0.5);
const inputWidth = imageData.width;
@rikschennink
rikschennink / convolution.ts
Created October 11, 2021 09:42
Convolution filter for use in worker
export default (
options: { imageData: ImageData; matrix: number[] },
done: (err: string, imageData: ImageData) => void
): void => {
const { imageData, matrix } = options;
if (!matrix) return done(null, imageData);
// calculate kernel weight
let kernelWeight = matrix.reduce((prev, curr) => prev + curr);
@rikschennink
rikschennink / simulate-pointers.js
Created July 9, 2021 12:20
A script to simulate pointer events, very specific to Pintura project so might need some customisation to work with yours
{
const DEBUG = false;
const style = document.createElement('style');
style.textContent = `
.sim-pointer {
margin-top: -1px;
position: absolute;
z-index: 9999999999999;
left: -24px;
@rikschennink
rikschennink / code-macro.njk
Created January 14, 2021 13:40
A macro that generates faux code SVG path elements
{% macro codeline(x, y, totalStatements, spaceWidth) %}
{% set offset = x %}
{% for i in range(0, totalStatements) -%}
{% set width = [2, 4, 8, 12, 24] | random %}
{% set opacity = [0.5, 0.75, 1] | random %}
<path d="M{{ offset }} {{ y }} h{{width}}" opacity="{{ opacity }}"/>
{% set offset = offset + width + spaceWidth %}
{% endfor %}
@rikschennink
rikschennink / nudgeable.ts
Last active October 22, 2023 05:53
⌨️ A Svelte action to add arrow key interaction to an element
export default (element: HTMLElement, options: any = {}) => {
// if added as action on non focusable element you should add tabindex=0 attribute
const {
direction = undefined,
shiftMultiplier = 10,
bubbles = false,
stopKeydownPropagation = true,
} = options;
@rikschennink
rikschennink / cloudinary.js
Last active February 9, 2024 16:15
FilePond Cloudinary
const createCloudinary = (cloudName, unsignedUploadPreset) => ({
process: (fieldName, file, metadata, load, error, progress, abort) => {
// `fieldName` and `meta` are not used for now
const url = `https://api.cloudinary.com/v1_1/${cloudName}/upload`;
const xhr = new XMLHttpRequest();
const formData = new FormData();