Skip to content

Instantly share code, notes, and snippets.

@kejun
Last active April 10, 2022 12:37
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save kejun/57c2ef14cd097edbf1ed5ef6f7460d63 to your computer and use it in GitHub Desktop.
function getUUID() {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
ctx.fillStyle = '#ff0000';
ctx.fillRect(0,0,8,10);
const b64 = canvas.toDataURL().replace('data:image/png;base64,', '');
const bin = window.atob(b64);
return bin2hex(bin.slice(-16, -12));
}
function bin2hex(s){
// Converts the binary representation of data to hex
//
// version: 812.316
// discuss at: http://phpjs.org/functions/bin2hex
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + bugfixed by: Onno Marsman
// + bugfixed by: Linuxworld
// * example 1: bin2hex('Kev');
// * returns 1: '4b6576'
// * example 2: bin2hex(String.fromCharCode(0x00));
// * returns 2: '00'
var v,i, f = 0, a = [];
s += '';
f = s.length;
for (i = 0; i<f; i++) {
a[i] = s.charCodeAt(i).toString(16).replace(/^([\da-f])$/,"0$1");
}
return a.join('');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment