Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Update image loading code to clear Error #13

Merged
merged 1 commit into from
Nov 23, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 27 additions & 19 deletions assets/js/clock.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,35 @@
// http://www.script-tutorials.com/html5-clocks/
// https://www.w3schools.com/graphics/canvas_clock.asp

// Get the javascript DOM reference to the canvas tag
const canvas = document.getElementById('clockCanvas');
const canvas = document.getElementById('clockCanvas'); // Get the javascript DOM reference to the canvas tag
const context = canvas.getContext('2d'); // The context method contains all the properties needed to draw on the canvas
const clockFaceImg = new Image();
clockFaceImg.src = 'assets/img/clock-face.png';




// Code Snippet adapted from https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Async_await
// Fetch the background image and wait for it to load
async function getClockImage() {
const response = await fetch('assets/img/clock-face.png');
if (!response.ok) {
throw new Error(`Error! status: ${response.status}`);
} else {
const blob = await response.blob(); // returns the results of the fetch function as a blob
const url = URL.createObjectURL(blob); // creates a DOMString containing the object URL that can be used to reference the contents of the specified source object.
clockFaceImg.src = url;
/*async function getClockImage() {
try {
const response = await fetch('assets/img/clock-face.png');
if (!response.ok) {
throw new Error(`Error! status: ${response.status}`);
} else {
const blob = await response.blob(); // returns the results of the fulfilled fetch promise as a blob
const url = URL.createObjectURL(blob); // creates a DOMString containing the object URL that can be used to reference the contents of the specified source object.
clockFaceImg.src = url;
}
} catch(error) {
console.log('There has been a problem with your fetch operation: ' + error.message);
}
}

getClockImage().catch(error => {
console.log('There has been a problem with your fetch operation: ' + error.message);
});
getClockImage();


/*function getImage(url) {
function getImage(url) {
return new Promise(function(resolve, reject) {
let clockFaceImg = new Image();
clockFaceImg.onload = function() {
Expand All @@ -46,9 +52,10 @@ getImage('assets/img/clock-face.png').then(function(response) {
})*/

// Add the clock face
function loadBackgroundImage() {
context.drawImage(getClockImage, 0, 0, canvas.clientWidth, canvas.height);
}
clockFaceImg.addEventListener('Load', e => {
context.drawImage(clockFaceImg, 0, 0, canvas.width, canvas.height);
});


// Draw the clock hour hand

Expand All @@ -64,12 +71,13 @@ function loadBackgroundImage() {

// Create the whole clock
function generateClock() {
loadBackgroundImage();

}

// Make the clock run
function clock() {

context.translate(canvas.width/2, canvas.height/2);
generateClock();
}

clock(); // start the application