I am interested in combining fabricjs with vue.js

I am currently exploring the integration of fabricjs with vue.js, but I have encountered some issues along the way. Below is a snippet of my code:

var app = new Vue({

  el: '#content_vue',
  data: {

  },
  methods: {
    add_image: function() {
      var vm = this;
      fabric.loadSVGFromURL('/images/sample.svg', function(objects, options) {
        for (var i = 0; i < objects.length; i++) {
          if (objects[i].get('type') == 'text') { //text condition

            var iText = textChangeToIText(fabric.cssRules[options.svgUid], objects[i], options);
            objects[i] = iText;
          }

          objects[i].setCoords();

          vm.canvas.add(objects[i]);
        }
        vm.canvas.renderAll();
      }, reviver);
    }
  },
  created: function() {
    var canvas = new fabric.Canvas('my_canvas', {
      backgroundColor: "white"
    });
    this.canvas = canvas;
    canvas.selection = false; // disable group selection
    var iText4 = new fabric.IText('abcdefg\nhijklmnop', {
      left: 200,
      top: 50,
      fontFamily: 'Lobster',
      caching: false
    });
    canvas.add(iText4);
    canvas.renderAll();

  }
});

function reviver(element, object) {
  if (object.get('type') == 'text') {
    var childrens = [].slice.call(element.childNodes);
    object.temp = childrens;
    var tmpArr = element.getAttribute('y');
    //var arr = tmpArr[tmpArr.length-1].split(')');
    //console.log(element);
    //console.log(object.top);

    //object.top = parseFloat(arr[0]);

  }
  object.id = element.getAttribute('id');
};

Despite following these steps to initialize fabricjs, I am still facing issues when integrating it with vue.js. Any suggestions on how to resolve this?

Answer №1

It's best practice to initialize the canvas within the mounted function of Vue.js and not the created function.

Here is an example:

mounted: function() {
 var canvas = new fabric.Canvas('my_canvas', {
  backgroundColor: "white"
 });
 this.canvas = canvas;
 canvas.selection = false; // disable group selection
 var iText4 = new fabric.IText('abcdefg\nhijklmnop', {
  left: 200,
  top: 50,
  fontFamily: 'Lobster',
  caching: false
 });
canvas.add(iText4);
canvas.renderAll();
}

No code is needed for the created function in this case.

created: function () {
 // No need for any fabric.js canvas code here
}

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

Troubleshoot: How to Fix the socket.io net::ERR_CONNECTION_TIMED_OUT

I am facing an issue with my website's real-time chat functionality. It works perfectly on localhost without any errors. However, when I try to run it on the server, I encounter the following error: http://my.domain:52398/socket.io/?EIO=3&transpo ...

Activate a jQuery plugin on elements that are dynamically generated

Here is the code snippet I am using: $(".address-geocomplete").geocomplete({ country: "US", details: ".details", detailsAttribute: "data-geo" }); When these elements are loaded with the document, everything works smoothly. However, there seem ...

What is the most effective method for relocating a DIV element across the webpage?

Struggling to find a solution for my problem. I have functions that fetch data from an API and display it in a div when a user right-clicks on a context menu. The div is currently fixed to the bottom right of the page, but I want it to be draggable so user ...

Is there a way in Jquery to retrieve the id of the clicked element and modify its name?

I am using a drag-and-drop website builder that utilizes HTML blocks, each with a unique ID. After dropping the blocks onto the canvas, I want to create a navigation menu that links to the IDs of each block. How can I retrieve the current ID of the block I ...

Ways to link a Javascript or Jquery function to an HTML form

I am looking to develop a form that can extract email addresses from input text. While I know there are many scripts available that can do this quite easily, my goal is to create one on my own. After reading, researching, and experimenting with differ ...

Encountering issues in parsing JSON for PhoneGap Application

I've been struggling with parsing JSON data for a unique PhoneGap application that is dynamically generated by a localStorage variable. The PHP script is functioning properly, but the JavaScript code seems to be encountering issues when trying to pars ...

Is there a way to use jQuery to animate scrolling on a mobile web browser?

I'm trying to make the page scroll to a specific position when a button is clicked. The code I currently have works fine on browsers like Chrome and IE, but doesn't seem to work on any mobile browser. Below is the code snippet I am using: $("#p ...

What is the best way to prevent a draggable div from moving past the edge of the viewport?

My situation involves a draggable div that functions as a popup window when a specific button is clicked. However, I noticed that dragging the div to the end of the viewport allows me to drag it out of the visible area, causing the body of the page to expa ...

Tips for effectively managing loading state within redux toolkit crud operations

Seeking guidance on efficiently managing the loading state in redux-toolkit. Within my slice, I have functionalities to create a post, delete a post, and fetch all posts. It appears that each operation requires handling a loading state. For instance, disp ...

Can componentDidMount() capture errors thrown by promises or event handlers linked to a component?

Consider a scenario in React 16 where there is a component as shown below: class Profile extends React.Component { onClick = () => { throw new Error("Oh nooo!"); }); }; componentDidCatch(error, info) { alert(error) } render() ...

How can I use JavaScript to create a drop-down menu that only appears after selecting the previous one?

<div class="row"> <div class="col-md-4"> <label for="selectCustomers">Customer Select</label> <select class="form-control pointer" name="tableCustomers" id=" ...

Conceal a div when the user is browsing within a Facebook page tab

I am trying to create a webpage that can be accessed directly or through a Facebook page tab. My goal is to only display a Facebook logo if the user is accessing the page outside of Facebook. If they are already viewing the page within Facebook, I don&ap ...

Tips on utilizing the translate feature of vue-i18n within the script section of a component

For text translation in my laravel project, I am utilizing the laravel-vue-i18n-generator package to handle it in vuejs components. Below is how I have set up app.js: import VueInternationalization from 'vue-i18n'; import Locale from './vue ...

Tips for preserving the integrity of square brackets while extracting data from JSON

Everyone: We've decided to utilize Newtonsoft JSON.NET for serializing some C# POCOs, and here's what we have: { "RouteID": "123321213312", "DriverName": "JohnDoe", "Shift": "Night", "ItineraryCoordinates": [ [ 9393, 44 ...

Issue: Undefined onClick function for HTML when using Node.js, NPM, and Parcel

I am currently working on a weather application using Node.js, NPM, and Parcel for development. To ensure OpenLayers functions properly, I need to incorporate these technologies, even though I'm not very familiar with them. One key aspect of my proje ...

How to adjust the size of the text on a button in jQuery mobile using custom

I am facing an issue with my jQuery mobile buttons that are placed inside a fieldset. Specifically, I need to adjust the font-size of one of the buttons. Despite attempting to add an inline style, it did not have the desired effect. Furthermore, I tried u ...

searchByTextContentUnderListItemAnchorTag

I would like to utilize the getByRole function for writing my test. However, I am encountering issues when using linkitem or 'link' as the role. It seems that I cannot find the desired element. // encountered error TestingLibraryElementError: The ...

Issues with retrieving the subsequent anchor element using Jquery Slider

(Apologies for the lengthy post. I like to provide detailed explanations.) Just starting out with Jquery, trying my hand at creating a custom image slider from scratch. Got the slider working with fade effects using this code: Javascript: $(document).re ...

Parsing the CSV file contents according to the specified columns

Currently, I'm involved in a project using AngularJS where I need to extract data from a CSV file column by column using JavaScript. So far, I've successfully retrieved the CSV data and displayed it in the console. While I've managed to sepa ...

Scale transformation - I am aiming for it to exceed the limits, yet it remains contained within

Currently, I am working on enhancing my carousel by implementing a zoom effect when hovering over the images. However, I have encountered an issue where the image gets hidden within the div container and doesn't overflow as expected. I tried adjusting ...