3

I'm trying to init canvas in vuejs component. Here is an example: https://jsfiddle.net/eywraw8t/55338/

I init canvas in mounted hook, which means the DOM is available. Also I can see that fabric stars doing its job by making lower-canvas div, but then it throws:

this.lowerCanvasEl.getContext is not a function

Couldn't figure out what's the problem. I already use fabric in my project, and never got such error.

1 Answer 1

4

That's not a problem of Vue. If you take all Vue code away, the problem persists.

fabric needs to be mounted on a <canvas> element, not a <div>.

Updated fiddle here.

Demo:

new Vue({
  el: "#app",

  mounted: function() {
    new fabric.Canvas('canvas', {
      width: 500,
      height: 500
    })
  }
})
#canvas {
  width: 200px;
  height: 200px;
  background-color: green;
}
<script type="text/javascript" src="//unpkg.com/vue"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.2.3/fabric.js"></script>

<div id="app">
  <canvas id="canvas"></canvas>
</div>

Sign up to request clarification or add additional context in comments.

6 Comments

Why the hell I couldn't notice it myself. I owe you two beers :)
Hahaha! 🍻 always glad to be of help! :D
The fun is that I already used fabric a lot in my project, but just didn't notice I used wrong tag... Maybe you also know why canvas can't catch drag&drop events, jsfiddle.net/eywraw8t/56370 ?
Those listeners on that fiddle are bound to the canvas, not to the #draggable div, is that it?
Yeah, I want to listen when I drop the red rectangle on canvas, and I didn't manage to do it both in jsfiddle and in my project
|

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.