简体   繁体   English

如何使用fabric js在画布上的每个对象使用ifisType()?

[英]How do I use if isType() for each object on a canvas with fabric js?

I am trying to show certain attributes for each object on a canvas, but I am having a difficult time writing the code to do so. 我试图显示画布上每个对象的某些属性,但是我很难编写代码来做到这一点。 EG I want the code below to identify the difference between a shape, text, and image objects and show attributes based off the type. 例如,我希望下面的代码识别形状,文本和图像对象之间的差异,并根据类型显示属性。

Attributes to console.log: console.log的属性:

for each object on canavas check to see if - 对于canavas上的每个对象,请检查是否-

  • Image then URL (source) and scale <= I am having a difficult time showing the source and name of the image. 图片,然后是URL(源)和比例尺<=,我很难显示图片的来源和名称。
  • Text then font family, font size, color, scale 文字,然后是字体系列,字体大小,颜色,比例
  • Shape then scale and color 形状然后缩放和颜色

What do I need to add/remove/change to make it work? 要使其正常运行,我需要添加/删除/更改什么?

Here is a JS fiddle example: https://jsbin.com/vevihikefo/1/edit?html,js,console,output 这是一个JS小提琴示例: https : //jsbin.com/vevihikefo/1/edit? html,js,console,output

canvas.getObjects().forEach(object=>{
    if(object.isType('xyz')) { // object is a shape
      console.log(object.scaleX, object.fill);
    } else if(object.isType('text')) { // object is a text
      console.log(object.text, object.fontFamily, object.fontSize*3, object.scaleX, object.fill);
    } else { // object is an image
      console.log(object.name, object.scaleX, object.fill);
    }
})

There are two things that need to be added / changed, to make it work ... 为了使其正常工作,需要添加/更改两件事...

  • change object.isType('xyz') to !object.isType('text') && !object.isType('image') ( as there isn't any type called 'xyz' ) object.isType('xyz')更改为!object.isType('text') && !object.isType('image') (因为没有任何类型称为'xyz')
  • wrap the entire code for getting the objects attributes in a function and call that function on image load, to get the image object attributes properly. 将用于获取对象属性的整个代码包装在函数中,并在图像加载时调用该函数 ,以正确获取图像对象属性。

 fabric.Object.prototype.objectCaching = false; var canvas = new fabric.Canvas('canvas'); canvas.add(new fabric.Text('foo', { fontFamily: 'Roboto', left: 100, top: 100, fontSize: 25 })); canvas.add(new fabric.Text('help', { fontFamily: 'Roboto', left: 100, top: 200, fontSize: 25 })); canvas.add(new fabric.Triangle({ fill: 'green', left: 200, top: 200 })); fabric.Image.fromURL('http://fabricjs.com/assets/pug_small.jpg', function(myImg) { //i create an extra var for to change some image properties var img1 = myImg.set({ left: 200, top: 0, width: 150, height: 150 }); canvas.add(img1); getAttributes(); }); canvas.renderAll(); function getAttributes() { canvas.getObjects().forEach(object => { if (!object.isType('text') && !object.isType('image')) { // object is a shape console.log(object.scaleX, object.fill); } else if (object.isType('text')) { // object is a text console.log(object.text, object.fontFamily, object.fontSize * 3, object.scaleX, object.fill); } else if (object.isType('image')) { // object is an image console.log(object.name, object.scaleX, object.fill); } }); } 
 <script src="https://rawgit.com/kangax/fabric.js/master/dist/fabric.js"></script> <canvas id="canvas" width="800" height="600" style="border: 1px solid #d3d3d3; position: absolute;"> Your browser does not support the canvas element. </canvas> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM