Pictures failing to load within the canvas

I am currently in the process of creating a very basic canvas element. The idea is to allow users to choose between two images and have their selection appear on the canvas when clicked. However, I am facing some challenges with making it work.

I have verified that the image names are correct.

Is there anyone who can offer assistance?

<script>
var canvas = document.getElementById("myCanvas");
var context = canvas.getContext("2d");
var selectedUtensil = document.getElementById("photos");

var tableclothColor="#ff0000"; 
function drawEverything(){
            context.beginPath();
            context.rect(0,0,canvas.width,canvas.height);

            //plate
            context.drawImage(plateImage,0,25);

            //utensil
            if(selectedUtensil == "forkknife") {
                context.drawImage(forkKnifeImage,0,25);
            }
            else if (selectedUtensil == "chopsticks") {
                context.drawImage(chopsticksImage,0,25);
            }
}

window.onload = function() {
drawEverything();
}

function change() {
    tableclothColor = document.getElementById("tablecloth").value;
    drawEverything();
}

var plateImage = new Image(); 
plateImage.src='plate.png';

var forkKnifeImage = new Image(); 
forkKnifeImage.src='img/image1.jpg';

var chopsticksImage = new Image(); 
chopsticksImage.src='img/image2.jpg';
</script>
</div>
<div class="footer">
</div>

Answer №1

According to the code snippet you provided, the script is searching for an image named bord.jpg in the same directory as the HTML file.

var bordImage = new Image(); 
bordImage.src='bord.png';

However, the images for mesvorkPlaatje and stokjesPlaatje are referenced in a subfolder.

var forkImage = new Image(); 
forkImage.src='img/image1.jpg';

var chopsticksImage = new Image(); 
chopsticksImage.src='img/image2.jpg';

Do you have a subfolder named img/? Are the images located there?

p.s: It's advisable to use English in your code to avoid confusion for other developers who may work on your code.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Shifting text upwards within a document

I'm struggling with CSS and bootstrap as I have limited knowledge in this area. I have multiple divs on a page that I want to align based on our old website design. The problem I'm facing is that one div is larger on the right, causing a lot of ...

Learn how to automatically set the checked state of a radio button by toggling another radio button

I have two sets of radio buttons, each with four options. The first set has no default selection (unchecked) using jQuery, while the second set has the first option checked by default. What I'm looking to achieve is that when a radio button in the f ...

Leveraging summernote in meteor with Discover Meteor tutorial

Recently, I've been delving into the world of Discover Meteor and encountering some challenges along the way. My current hurdle involves integrating content entered in summernote into mongo, but it seems to be more complicated than expected. The fil ...

Instructions on how to invoke a function defined in a JavaScript module (type=module) from an HTML document

I've been facing difficulties with using JavaScript modules... I have an HTML file and a JS module. In the javascript file, I have a function defined that I want to call from my HTML page. Here is my code: index.html <html> <head> < ...

Utilizing JavaScript for enhancing the appearance of code within a pre element

Is there a way to dynamically highlight the code inside a pre element using vanilla JavaScript instead of JQuery? I'm looking for a solution that colors each tag-open and tag-close differently, displays tag values in another color, and attributes with ...

Leverage JQuery Mobile for smooth sliding and effortless deletion of list elements

Currently, I am testing a JQuery Mobile webpage that features a simple list setup: Upon clicking the list items, they get highlighted and their ids are saved in a local array. Is there an easy (or not so easy) way to transition the selected elements slidi ...

Capture video frames from a webcam using HTML and implement them in OPENCV with Python

While many may find it simple, I am facing a challenge in extracting video frames from HTML or JavaScript and utilizing them in my Python OPENCV project. I have a strong background in Python OPENCV and Deeplearning, but lack knowledge in HTML and JavaScr ...

The issue of the Dropdown Menu Not Remaining Fixed While Scrolling

There is a challenge with a Datetime Picker Dropdown Menu not staying in place when the user scrolls. Previous attempts to use .daterangepicker {position: fixed !important;} have been unsuccessful, causing the Datetime Picker to not stay fixed in its posit ...

Create animations for SVG paths and polygons that move along specified paths

I am trying to animate both a path and a polygon in an SVG arrow along the same path. Currently, I have successfully animated the path using CSS, but I am struggling to figure out how to move the polygon along the path at the same pace. <svg id="La ...

By default, Nuxt 2.15.7 is automatically installed when you create a new Nuxt app

I am looking to develop a Nuxt app (version 3) using Vue. However, when I run the command npm create nuxt-app@version mousa, it automatically installs Nuxt2. How can I install Nuxt3 instead with this command? ...

Ajax immediately going to the error section

I'm having trouble with my ajax as it always goes straight to the error part. Below is my code along with comments on the lines I'm unsure about: <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script&g ...

Is it possible to achieve a seamless change using show/hide jQuery functions when hovering over an

How can I achieve smooth transitions when using an image map to show a div on hover, similar to the functionality seen on this website: ? Below is my current JavaScript code: var mapObject = { hover : function(area, event) { var id = area.attr ...

"Looking to add some flair to your website with a

I am trying to figure out how to place a 30x30px no-repeat background image in the top left corner of a 200px by 200px div, positioned 120px from the left and 50px from the top. However, I also want to ensure that the text inside the div is displayed on to ...

Stop the WebGL Three.js container from capturing scroll events

I am working on a full-screen three.js application which serves as the background and I am trying to add overlaid text that can be scrolled. However, my issue arises from the fact that the three.js WebGL container intercepts all scroll events and prevents ...

Tips for keeping images stationary while expanding on hover

Currently, I am in the process of developing my first website. My main goal is to create an image gallery that displays thumbnails of images and expands them when you hover over each one. Initially, I adjusted the sizes of the images to 100px*100px using H ...

Designing a webpage - patterns in the background and images layered on top

I've been tasked with creating a webpage that resembles a theatrical stage. The design calls for the following layout: [curtain repeat][left curtain][stage][right curtain][curtain repeat] The left curtain, stage, and right curtain should maintain a ...

Can a single link have a customized background color and different properties set for when it is clicked on?

I need assistance with setting the background-color of this <ul> to none while also adjusting the link's background-color to black. Here is the CSS code I have used: a:link { background-color: black } And here is the HTML code snippet: <di ...

Ways to prevent image toggling when the mouse moves out of it or when the hover effect stops

I'm looking to toggle between two images on mouseover/hover and stop the toggling when hovering stops. Here is the HTML code I have: <div class="fader"> <img src="image1" /> <img src="image2" /> </div> This is the JQuery ...

The reliability of jQuery posting varies

My current setup involves using jQuery to send data to an external source with the code snippet provided below. As part of the process, I'm also ensuring that all input fields are filled out correctly. However, there has been a strange issue where the ...

What is the best way to align text at the center of a circular animation?

I stumbled upon this fascinating animation and I'm looking to incorporate it into my project. However, I'm facing a challenge in centering text inside the circle without resorting to using position absolute. My goal is to have the text with the ...