Skip to content

Instantly share code, notes, and snippets.

View kfitfk's full-sized avatar
😇

Hao Ye kfitfk

😇
View GitHub Profile
@kfitfk
kfitfk / json_alphabetic.js
Created December 20, 2023 07:50
parse JSON string and keep the props in alphabetic order
function sortObjectAlphabetically(obj) {
if (typeof obj !== 'object' || obj === null) {
// Base case: obj is not an object, or is null
return obj;
}
if (Array.isArray(obj)) {
// If obj is an array, recursively sort its elements
return obj.map((element) => sortObjectAlphabetically(element));
}
@kfitfk
kfitfk / canvas_to_img.js
Last active August 30, 2023 06:15
Save canvas drawing to local image
function saveCanvas(canvas) {
var image = canvas.toDataURL('image/png').replace('image/png', 'image/octet-stream'); // here is the most important part because if you dont replace you will get a DOM 18 exception.
window.location.href = image; // it will save locally
}
function openInNewWindow(canvas) {
// works in Firefox
window.open(canvas.toDataURL("image/png"), '_blank');
}
@kfitfk
kfitfk / ps_save_jpeg.jsx
Created July 23, 2015 06:49
A Photoshop script to automate save for web as JPEG. Save the file as using jsx extenstion. Then in Photoshop go to "File-Scripts-Browse..." to load the file. This script will overwrite the existing output file if it already exists.
// Utils
function trace() {
$.writeln.apply($, arguments);
}
// Construct the dialog layout
var layoutInfo = "dialog{\
text: 'Save for Web in JPEG',\
info: Panel {\
orientation: 'column',\
@kfitfk
kfitfk / customized_gb2312_charset
Last active March 16, 2023 07:15
Use pyftsubset to subset a font file and convert to web font format
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~・ˉˇ¨―∥±÷∶∧∨∑∏∪∩∈∷√⊥∥∠⌒⊙∫∮≡≌≈∽∝≠≮≯≤≥∞∵∴♂♀°′″℃¤¢£‰§№☆★○●◎◇◆□■△▲※→←↑↓⒈⒉⒊⒋⒌⒍⒎⒏⒐⒑⒒⒓⒔⒕⒖⒗⒘⒙⒚⒛⑴⑵⑶⑷⑸⑹⑺⑻⑼⑽⑾⑿⒀⒁⒂⒃⒄⒅⒆⒇①②③④⑤⑥⑦⑧⑨⑩ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫ ̄ぁあぃいぅうぇえぉおかがきぎくぐけげこごさざしじすずせぜそぞただちぢっつづてでとどなにぬねのはばぱひびぴふぶぷへべぺほぼぽまみむめもゃやゅゆょよらりるれろゎわゐゑをんァアィイゥウェエォオカガキギクグケゲコゴサザシジスズセゼソゾタダチヂッツヅテデトドナニヌネノハバパヒビピフブプヘベペホボポマミムメモャヤュユョヨラリルレロヮワヰヱヲンヴヵヶΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩαβγδεζηθικλμνξοπρστυφχψωАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюяāáǎàēéěèīíǐìōóǒòūúǔùǖǘǚǜüêㄅㄆㄇㄈㄉㄊㄋㄌㄍㄎㄏㄐㄑㄒㄓㄔㄕㄖㄗㄘㄙㄚㄛㄜㄝㄞㄟㄠㄡㄢㄣㄤㄥㄦㄧㄨㄩ─━│┃┄┅┆┇┈┉┊┋┌┍┎┏┐┑┒┓└┕┖┗┘┙┚┛├┝┞┟┠┡┢┣┤┥┦┧┨┩┪┫┬┭┮┯┰┱┲┳┴┵┶┷┸┹┺┻┼┽┾┿╀╁╂╃╄╅╆╇╈╉╊╋一丁七万丈三上下丌不与丐丑专且丕世丘丙业丛东丝丞丢两严丧丨个丫丬中丰串临丶丸丹为主丽举丿乃久乇么义之乌乍乎乏乐乒乓乔乖乘乙乜九乞也习乡书乩买乱乳乾了予争事二亍于亏云互亓五井亘亚些亟亠亡亢交亥亦产亨亩享京亭亮亲亳亵人亻亿什仁仂仃仄仅仆仇仉今介仍从仑仓仔仕他仗付仙仝仞仟仡代令以仨仪仫们仰仲仳仵件价任份仿企伉伊伍伎伏伐休众优伙会伛伞伟传伢伤伥伦伧伪伫伯估伲伴伶伸伺似伽佃但位低住佐佑体何佗佘余佚佛作佝佞佟你佣佤佥佧佩佬佯佰佳佴佶佻佼佾使侃侄侈侉例侍侏侑侔侗供依侠侣侥侦侧侨侩侪侬侮侯侵便促俄俅俊俎俏俐俑俗俘俚俜保俞俟信俣俦俨俩俪俭修俯俱俳俸俺俾倌倍倏倒倔倘候倚倜借倡倥倦倨倩倪倬倭倮债值倾偃假偈偌偎偏偕做停健偬偶偷偻偾偿傀傅傈傍傣傥傧储傩催傲傺傻像僖僚僦僧僬僭僮僳僵僻儆儇儋儒儡儿兀允元兄充兆先光克免兑兔兕兖党兜兢入全八公六兮兰共关兴兵其具典兹养兼兽冀冁冂内
@kfitfk
kfitfk / base64.js
Created November 28, 2013 03:15
JS Base 64 encode/decode
/**
*
* Base64 encode / decode
* http://www.webtoolkit.info/
*
**/
var Base64 = {
// private property
@kfitfk
kfitfk / webm2hevc
Created November 3, 2022 06:04
Convert from webm to hevc using ffmpeg with hevc_videotoolbox
ffmpeg -c:v libvpx-vp9 -i [input_file.webm] -c:v hevc_videotoolbox -q:v 60 -allow_sw 1 -alpha_quality 0.7 -vtag hvc1 -movflags +faststart [output_file.mp4]
# use -q:v to set quality, 60 should suffice
# use -alpha_quality to adjust alpha channel quality
# for more options, check
# ffmpeg -h encoder=hevc_videotoolbox
# ffmpeg version 4.4.1 Copyright (c) 2000-2021 the FFmpeg developers
# built with Apple clang version 13.0.0 (clang-1300.0.29.3)
@kfitfk
kfitfk / m1_accent_colors.md
Last active August 9, 2022 06:32
Use iMac M1 accent colours on any Mac

First, enable NSColorSimulateHardwareAccent

defaults write -g NSColorSimulateHardwareAccent -bool YES

Then choose a color with NSColorSimulatedHardwareEnclosureNumber with a value between 3 and 8 inclusive.

defaults write -g NSColorSimulatedHardwareEnclosureNumber -int 4

  • 3: yellow
  • 4: green
@kfitfk
kfitfk / git_search_history_content.md
Last active May 9, 2022 09:37
Search modified files to see if they include a term; 在 git 文件修改历史内容中查询某个关键词
git log -Sword
git log -Gword
  • -G by default accepts a regex, while -S accepts a string, but it can be modified to accept regexes using the --pickaxe-regex.
  • -S finds commits where the number of occurrences of "word" changed, while -G finds commits where "word" appears in the diff.
  • This means that -S<regex> --pickaxe-regex and -G<regex> do not do exactly the same thing.
@kfitfk
kfitfk / nth_index.js
Created March 22, 2022 02:19
Get nth or last nth index of a pattern from a string
function nthIndex(str, pat, n){
let i= -1;
while (n-- && i++ < str.length) {
i = str.indexOf(pat, i);
if (i < 0) break;
}
return i;
}
function lastNthIndex(str, pat, n) {
@kfitfk
kfitfk / ffmpeg_extract_frame_with_transparency.sh
Last active September 22, 2021 02:59
extract a frame with transparency from a webm file using ffmpeg
# Replace 34 from select=eq(n\,34) with any frame number. Frame starts from 0.
ffmpeg -vcodec libvpx-vp9 -i input_file.webm -pix_fmt rgba -vf "select=eq(n\,34)" -vframes 1 output_file.png
# To generate a png file for each frame, use something like "frames/%04d.png" as the output file name