0

i have an issue trying to load images form the assets folder to the canvas.

the path to the images are: /src/assets/1.jpg /src/assets/2.jpg /src/assets/3.jpg

my code:

<template>
  <canvas id="CanvasTest" width="800" height="600"></canvas>
</template>

<script lang="ts">
  import { defineComponent } from 'vue';
  export default defineComponent({
    name: 'CanvasTest',  
    data() {
      return { images_src: ["/assets/1.jpg","/assets/2.jpg","/assets/3.jpg"] }
    },
    mounted() {
      const canvas = document.getElementById('CanvasTest') as HTMLCanvasElement;
      const ctx = canvas.getContext("2d") as CanvasRenderingContext2D;

      const img_w = 200;
      const img_h = 100;

      for (let i = 0; i < this.images_src.length; i++) {
        const imgElement = new Image();
        imgElement.src = this.images_src[i];
        imgElement.onload = () => {
          ctx.drawImage(imgElement, i*img_w, i*img_h, img_w, img_h);
        };
      }
    },
  });
</script>

<style>
  #CanvasTest {
    background-color: #555;
  }
</style>

i get these error:

GEThttp://localhost:8080/assets/1.jpg [HTTP/1.1 404 Not Found 1ms]

GEThttp://localhost:8080/assets/2.jpg [HTTP/1.1 404 Not Found 3ms]

GEThttp://localhost:8080/assets/3.jpg [HTTP/1.1 404 Not Found 1ms]

image of the browser console with the errors

i found a solution which is to upload/move the images to the public folder, but i don't really like that. I would like to know if there are better ways to upload the images from the assets folder.

3
  • please post the error as text, not an image. Commented Sep 1, 2022 at 3:07
  • errors in text form are above the image link Commented Sep 1, 2022 at 3:22
  • 1
    try the answer shown here and turn your image links into require statements Commented Sep 2, 2022 at 20:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.