var tCtx = document.getElementById('textCanvas').getContext('2d'),
c = document.getElementById('textCanvas'),
imageElem = document.getElementById('image'),
resultTextArea = document.getElementById('result'),
resultTextAreaFormatted = document.getElementById('resultFormatted');
document.getElementById('download').addEventListener('click', () => {
var a = document.createElement("a");
a.href = tCtx.canvas.toDataURL();
a.download = "message.png";
document.body.appendChild(a);
document.body.removeChild(a);
document.getElementById('text').addEventListener('keyup', function() {
var lines = this.value.split('\n');
if (tCtx.measureText(line).width > tCtx.measureText(maxLine).width) {
tCtx.canvas.width = tCtx.measureText(maxLine).width;
tCtx.canvas.height = lines.length * 25;
drawNoise(tCtx, 0, 0, tCtx.canvas.width, tCtx.canvas.height);
tCtx.font = "20px Helvetica Neue,Helvetica,Arial,Microsoft JhengHei,sans-serif";
tCtx.fillStyle = '#777777';
lines.forEach((line, i) => tCtx.fillText(line, 0, 20 + (i * 20)));
for (var i = 0; i < lines.length; i++)
tCtx.fillText(lines[i], 0, 20 + (i * 20));
imageElem.src = tCtx.canvas.toDataURL();
function drawNoise(ctx, x, y, width, height) {
const imageData = ctx.createImageData(width, height)
const buffer32 = new Uint32Array(imageData.data.buffer)
const LE = isLittleEndian();
const black = LE ? 0xFF000000 : 0x000000FF;
const grey = LE ? 0xFF777777 : 0x777777FF;
const blue = LE ? 0xFFFF0000 : 0x0000FFFF;
const red = LE ? 0xFF00FF00 : 0x00FF00FF;
const green = LE ? 0xFF0000FF : 0xFF0000FF;
const alpha = LE ? 0x00000000 : 0x00000000;
for (let i = 0, len = buffer32.length; i < len; i++) {
buffer32[i] = Math.random() < 0.8
Math.random() < 0.9 ? grey :
Math.random() < 0.5 ? blue :
Math.random() < 0.5 ? green : red
ctx.putImageData(imageData, x, y)
function isLittleEndian() {
const uint8 = new Uint8Array(8);
const uint32 = new Uint32Array(uint8.buffer);
return uint32[0] === 0XFF;
function DataURIToBlob(dataURI) {
const splitDataURI = dataURI.split(',')
const byteString = splitDataURI[0].indexOf('base64') >= 0 ? atob(splitDataURI[1]) : decodeURI(splitDataURI[1])
const mimeString = splitDataURI[0].split(':')[1].split(';')[0]
const ia = new Uint8Array(byteString.length)
for (let i = 0; i < byteString.length; i++)
ia[i] = byteString.charCodeAt(i)
document.getElementById('upload').addEventListener('click', () => {
const file = DataURIToBlob(imageElem.src)
let formData = new FormData();
formData.append('file', file, 'image.png');
fetch('https://img.eservice-hk.net/api.php?version=2', {
}).then(response => response.json())
resultTextArea.value = result.url
resultTextAreaFormatted.value = '[img]'+result.url+'[/img]'
document.getElementById('copyURL').addEventListener('click', () => {
var copyText = document.getElementById("result");
copyText.setSelectionRange(0, 99999);
document.execCommand("copy");
alert("Copied the text: " + copyText.value);
document.getElementById('copyFormattedUrl').addEventListener('click', () => {
var copyText = document.getElementById("resultFormatted");
copyText.setSelectionRange(0, 99999);
document.execCommand("copy");
alert("Copied the text: " + copyText.value);