javascript - 在 FabricJS Canvas 内显示图像时出现 polymer 元素错误

标签 javascript html canvas polymer fabricjs

我正在尝试在 polymer 元素内的 FabricJS Canvas 上显示图像。通过这样做,会出现此错误:

Uncaught TypeError: Cannot read property '_set' of undefined
at klass._onObjectAdded (fabric.js:6964)
at klass.add (fabric.js:260)
at HTMLElement.ready (convert-section.html:97)
at HTMLElement._enableProperties (property-accessors.html:531)
at HTMLElement.connectedCallback (element-mixin.html:630)
at HTMLElement._attachDom (element-mixin.html:690)
at HTMLElement._readyClients (element-mixin.html:663)
at HTMLElement._flushClients (property-effects.html:1518)
at HTMLElement._propertiesChanged (property-effects.html:1644)
at HTMLElement._flushProperties (property-accessors.html:549)

是否可以在我的 polymer 元素内的 FabricJs Canvas 上显示图像?或者我犯了一个错误?我是 Polymer 的新手,没有太多经验。

我可以显示三 Angular 形等形状。

这是我的元素:

<link rel="import" href="../../bower_components/polymer/polymer-element.html">
<link rel="import" href="my-paper-element-styles.html">

<dom-module id="convert-section">
     <template>
    <style include="my-paper-element-styles">
    </style>

  <canvas  id="convertCanvas" ></canvas>

  </template>
     <script>
          class ConvertSection extends Polymer.Element {
               static get is() {
                    return 'convert-section';
               }
               static get properties() {
                    return {
                         imageLocationPath: {
                              type: String,
                              notify: true
                         }
                    };
               }
               disconnectedCallback() {
                 super.disconnectedCallback();
                 this.imageLocationPath = "";
               }
               ready() {
                    super.ready();
                    this.canvas = this.__canvas = new fabric.Canvas(this.$.convertCanvas);
                    var height = window.innerHeight - 48;
                    var width = window.innerWidth - 300;
                    this.canvas.setHeight(height);
                    this.canvas.setWidth(width);
                    var image = fabric.Image.fromURL('img/viper-board.png');
                    this.canvas.add(image);
               }

          }
          window.customElements.define(ConvertSection.is, ConvertSection);
     </script>
     <script src="../../node_modules/fabric/dist/fabric.js"></script>
</dom-module>

最佳答案

ready() {
  super.ready();
  var self = this;
  self.canvas = self.__canvas = new fabric.Canvas(self.$.convertCanvas);
  var height = window.innerHeight - 48;
  var width = window.innerWidth - 300;
  self.canvas.setHeight(height);
  self.canvas.setWidth(width);
  fabric.Image.fromURL('img/viper-board.png',function(oImg){
   oImg.set({
    left:10,
    top:10
   });
   self.canvas.add(oImg);
  });
}

如您所见fromURL不返回任何内容。您需要传递源 url 和回调函数,并将该图像添加到回调函数中。

关于javascript - 在 FabricJS Canvas 内显示图像时出现 polymer 元素错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46653161/

相关文章:

javascript - 如何使用 Canvas 绘制图像 Sprite ?

javascript - 如何在 ASP.NET 母版页中正确引用 javascript 文件?

javascript - 从 WordPress 循环中单击链接时添加类

html - 响应式设计和隐藏 div 元素

javascript - jquery 可拖动和可调整大小不能在动态生成的元素中一起工作

javascript - 在 html 5 Canvas 上添加背景图像

javascript - 如何删除 HTML 表单中提交按钮的默认焦点?

javascript - 浏览器如何根据css样式生成CSSOM的细节?

javascript - 为什么可以用new Image 而不能用new Div 或new Span?

javascript - 如何让webGL扭曲它下面的HTML?