Vue component does not display FabricJS image

Currently, I am facing an issue where I want to manipulate images on a canvas using FabricJS inside a VueJS app. In the view component, there is a prop called background which I pass in and then use fabric.Image.fromURL() to load it onto the canvas. However, nothing seems to appear on the canvas:

<template>
  <div>
    <h1>Edit Game</h1>
    <canvas id="game-board" width="400px" height="400px"></canvas>

    <img :src="background" width="100px" height="100px">
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { fabric } from 'fabric'

export default {
  mounted() {
    let canvas = new fabric.Canvas('game-board');

    fabric.Image.fromURL(this.background, function(oImg) {
      canvas.add(oImg);
    });
  },

  computed: {
    ...mapGetters([
      'background'
    ])
  }
}
</script>

Although the background image loads properly into an img tag, it does not appear on the canvas as intended.

You can view the screenshot of the canvas and image here.

The functionality works correctly on this Code Sandbox, but for some reason, executing nearly the same code locally yields different results.

Answer №1

Show image only when it has fully loaded

  const newImage = new window.Image()
  newImage.src = this.image_link
  newImage.onload = () => {
    let imgObject = new fabric.Image(newImage, {
      left: 0,
      top: 0,
    })
    imgObject.scaleToWidth(this.canvas.getWidth())
    this.preventElementMovement(imgObject)
    this.canvas.add(imgObject)
  }

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

JavaScript animation for sequencing traffic lights

I have been working on completing a task that was assigned to me. (iii) Explain the organization of an array that could manage the traffic light sequence. (iv) Develop a script that utilizes the array outlined in part (iii) to create an animated display ...

When using JSON.Stringify in a React App, it only displays the first item and the length of the object instead of all the other items

Working on a React App, I encountered an issue while trying to convert an array to JSON and send it to the server. My approach was like this: console.log(JSON.stringify(mainArray)) I anticipated seeing output like this during testing: "breakfast": { ...

On startup of the chrome app, read and load a JSON file into a variable

As I develop a chrome app, my goal is to store all configuration defaults in json file(s) alongside other assets. I am currently using AJAX requests to load them, but I'm wondering if there is a more efficient way to handle this. Is there perhaps an o ...

Learn how to default export React with withRouter, all while taking advantage of Material UI's makeStyles

I have been working on integrating Material UI makeStyles with class components, passing useStyles as props while default exporting it in an arrow function. export default () => { const classes = useStyles(); return ( <LoginClass classes={cl ...

Tips for displaying an arrow icon that points up or down when clicking on a table header in Vue.js

I am currently working on a listing page where data is fetched using Laravel. The issue I am facing is related to sorting functionality when clicking on the table headings (<th>). The desired behavior is for arrow marks to indicate ascending or desce ...

Dynamically adjust the gage value

Currently, I am working on a fitness application that involves showcasing BMI data using a gauge. However, I am struggling to figure out how to dynamically change the value of the gauge. In addition, when trying to implement the gauge script on button cl ...

Getting state values from a custom component to another parent component can be achieved by lifting the state up

In my project, I have two classes both extending React.Component. One of these classes is a custom component that is built upon another custom component called React Places Autocomplete. If you want to see how it looks, check out this picture. Here is the ...

Using the Mousetrap binding on a nuxt.js page may not be effective

Hey there! I'm trying to achieve a certain functionality where I want to redirect to a different page once a specific sequence is typed. Although I can see the message "It works" in my console, the redirection is not happening and instead, I am gettin ...

Create a search preview in JavaScript that emphasizes key information and provides a condensed overview

I am currently working on incorporating a feature that generates a highlighted and condensed preview of a provided text. Below is the code snippet I have so far: <div> Search: <input value='vestibulum' disabled/> </div> < ...

Using Chrome's tabs.executeScript() method with an included script file

I'm in the process of creating a basic Chrome extension, where I need to actively monitor the AJAX calls being made on the page. Here is the code snippet I have implemented to listen to AJAX calls: var isAjaxWorking = false; //timeout var timeout; ...

Using an arbitrary object as an argument in a CoffeeScript anonymous function

When I execute the below Coffeescript code: @total = (a, b) -> a + b The resulting compiled Javascript is: (function() { this.total = function(a, b) { return a + b; }; }).call(this); Is there a method in Coffeescript to substitute ...

Is it possible to utilize the Node.JS PassThrough stream as a straightforward input buffer?

In my Node.js application, I am utilizing a subprocess referred to as the "generator" to produce data. The challenge arises when the generator occasionally outputs a large chunk of data at a speed of around 50MB/s, while generally operating at a much slowe ...

Issues with Laravel 6 user authentication functionality

Today I set up a new project with Laravel version 6.x. Having PHP 7.3 on my system, the installation of laravel 6 went smoothly. Next, I executed the following command to enable Auth UI for VueJS: artisan ui vue —auth and also ran this command: comp ...

Is it possible for me to determine whether a javascript file has been executed?

I am currently working with an express framework on Node.js and I have a requirement to dynamically change the value (increase or decrease) of a variable in my testing module every time the module is executed. Is there a way to determine if the file has ...

Discovering hospitals in the vicinity with the help of Google Maps API and JavaScript

var MapApiApplication = { myCurrentPosition : "", mapOptions : "", marker : "", initialize : function(){ MapApiApplication.myCurrentPosition = new google.maps.LatLng(10.112293000000000000, 76.352684500000010000); M ...

Is it wise to question the validity of req.body in express.js?

https://expressjs.com/en/4x/api.html mentions It is crucial to validate all properties and values in the req.body object as they are derived from user input. Any operation performed on this object should be validated to prevent security risks. For instan ...

Reviving jQuery Smooth Zoom Pan viewer following container resize

I am in the process of developing a responsive image viewer and have decided to incorporate the Smooth Zoom Pan plugin for enhanced pan/zoom functionality. Within my layout, I have two primary panels: the viewer container and a controls container that are ...

What is preventing me from loading Google Maps within my Angular 2 component?

Below is the TypeScript code for my component: import {Component, OnInit, Output, EventEmitter} from '@angular/core'; declare var google: any; @Component({ selector: 'app-root', templateUrl: './app.component.html', st ...

Using AJAX to retrieve version information via jQuery

I have a straightforward script that extracts the value of a textarea and sends it via AJAX. When I input "??" as the text, the output seems to be corrupted with strange values. However, when I log out the retrieved value before sending it, everything appe ...

HTML5: Enhancing Video Playback on the iPad with Custom Zoom Controls

I've customized a smaller UIWebView specifically for the iPad, and I've created my own HTML5 controls for the video playback. However, when I try to maximize the video, all I see is a black screen instead of the actual content. The audio still pl ...