Skip to content

Instantly share code, notes, and snippets.

@KoxSosen
Created March 14, 2024 12:47
Show Gist options
  • Save KoxSosen/87e0a23d38df0b47e765fc821a69fd67 to your computer and use it in GitHub Desktop.
Save KoxSosen/87e0a23d38df0b47e765fc821a69fd67 to your computer and use it in GitHub Desktop.
async-image-loading
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="src/style.css">
</head>
<body>
<h1 id="header"></h1>
<img id="myImage" src="src/images/Tw8t.png">
<img id="myInvalidImage" src="src/images/Tw8t.png">
<img id="myImageWithInvalidUUID" src="src/images/Tw8t.png">
<img id="myImage1" src="src/images/Tw8t.png">
<img id="myImage2" src="src/images/Tw8t.png">
<img id="myImage3" src="src/images/Tw8t.png">
<img id="myImage4" src="src/images/Tw8t.png">
<script src="src/script.js"></script>
</body>
</html>
function getImageAsync(url, uuid, element) {
let request = new XMLHttpRequest()
let blob;
request.open('GET', `${url}${uuid}`, true) // set true for asynchronous
request.responseType = 'blob';
request.onreadystatechange = function () {
if (this.readyState === 4 && this.status === 200) {
blob = this.response;
document.getElementById(`${element}`).src = window.URL.createObjectURL(blob);
}
};
// send back the response object
request.send(blob);
}
getImageAsync("https://visage.surgeplay.com/face/55/", "apple", "myImage");
getImageAsync("https://visage.surgeplay.com/face/55/", "37163hdhwd8131d1d1d1ds1", "myInvalidImage")
getImageAsync("https://visage.surgeplay.com/face/55/", "00000.00000.0000.000", "myImage")
getImageAsync("https://visage.surgeplay.com/face/55/", "notch", "myImage1")
getImageAsync("https://visage.surgeplay.com/face/55/", "peter", "myImage2")
getImageAsync("https://visage.surgeplay.com/face/55/", "null", "myImage3")
body {
background: transparent; /* Make it white if you need */
color: #fcbe24;
padding: 0 24px;
margin: 0;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment